Back to index

lightning-sunbird  0.9+nobinonly
sockopt.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nspr.h"
00039 #include "prio.h"
00040 #include "prinit.h"
00041 #include "prprf.h"
00042 #ifdef XP_MAC
00043 #include "probslet.h"
00044 #else
00045 #include "obsolete/probslet.h"
00046 #endif
00047 
00048 #include "plerror.h"
00049 
00050 static PRFileDesc *err = NULL;
00051 static PRBool failed = PR_FALSE;
00052 
00053 #ifndef XP_MAC
00054 static void Failed(const char *msg1, const char *msg2)
00055 {
00056     if (NULL != msg1) PR_fprintf(err, "%s ", msg1);
00057     PL_FPrintError(err, msg2);
00058     failed = PR_TRUE;
00059 }  /* Failed */
00060 
00061 #else
00062 #include "prlog.h"
00063 #define printf PR_LogPrint
00064 extern void SetupMacPrintfLog(char *logFile);
00065 static void Failed(const char *msg1, const char *msg2)
00066 {
00067     if (NULL != msg1) printf("%s ", msg1);
00068     printf (msg2);
00069     failed |= PR_TRUE;
00070 }  /* Failed */
00071 
00072 #endif
00073 
00074 static PRSockOption Incr(PRSockOption *option)
00075 {
00076     PRIntn val = ((PRIntn)*option) + 1;
00077     *option = (PRSockOption)val;
00078     return (PRSockOption)val;
00079 }  /* Incr */
00080 
00081 PRIntn main(PRIntn argc, char *argv)
00082 {
00083     PRStatus rv;
00084     PRFileDesc *udp = PR_NewUDPSocket();
00085     PRFileDesc *tcp = PR_NewTCPSocket();
00086     const char *tag[] =
00087     {
00088         "PR_SockOpt_Nonblocking",     /* nonblocking io */
00089         "PR_SockOpt_Linger",          /* linger on close if data present */
00090         "PR_SockOpt_Reuseaddr",       /* allow local address reuse */
00091         "PR_SockOpt_Keepalive",       /* keep connections alive */
00092         "PR_SockOpt_RecvBufferSize",  /* send buffer size */
00093         "PR_SockOpt_SendBufferSize",  /* receive buffer size */
00094 
00095         "PR_SockOpt_IpTimeToLive",    /* time to live */
00096         "PR_SockOpt_IpTypeOfService", /* type of service and precedence */
00097 
00098         "PR_SockOpt_AddMember",       /* add an IP group membership */
00099         "PR_SockOpt_DropMember",      /* drop an IP group membership */
00100         "PR_SockOpt_McastInterface",  /* multicast interface address */
00101         "PR_SockOpt_McastTimeToLive", /* multicast timetolive */
00102         "PR_SockOpt_McastLoopback",   /* multicast loopback */
00103 
00104         "PR_SockOpt_NoDelay",         /* don't delay send to coalesce packets */
00105         "PR_SockOpt_MaxSegment",      /* maximum segment size */
00106         "PR_SockOpt_Broadcast",       /* Enable broadcast */
00107         "PR_SockOpt_Last"
00108     };
00109 
00110     err = PR_GetSpecialFD(PR_StandardError);
00111     PR_STDIO_INIT();
00112 
00113 #ifdef XP_MAC
00114        SetupMacPrintfLog("sockopt.log");
00115 #endif
00116 
00117     if (NULL == udp) Failed("PR_NewUDPSocket()", NULL);
00118     else if (NULL == tcp) Failed("PR_NewTCPSocket()", NULL);
00119     else
00120     {
00121         PRSockOption option;
00122         PRUint32 segment = 1024;
00123         PRNetAddr addr;
00124 
00125         rv = PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr);
00126         if (PR_FAILURE == rv) Failed("PR_InitializeNetAddr()", NULL);
00127         rv = PR_Bind(udp, &addr);
00128         if (PR_FAILURE == rv) Failed("PR_Bind()", NULL);
00129         for(option = PR_SockOpt_Linger; option < PR_SockOpt_Last; Incr(&option))
00130         {
00131             PRSocketOptionData data;
00132             PRFileDesc *fd = tcp;
00133             data.option = option;
00134             switch (option)
00135             {
00136                 case PR_SockOpt_Nonblocking:
00137                     data.value.non_blocking = PR_TRUE;
00138                     break;    
00139                 case PR_SockOpt_Linger:
00140                     data.value.linger.polarity = PR_TRUE;
00141                     data.value.linger.linger = PR_SecondsToInterval(2);          
00142                     break;    
00143                 case PR_SockOpt_Reuseaddr:
00144                     data.value.reuse_addr = PR_TRUE;      
00145                     break;    
00146                 case PR_SockOpt_Keepalive:       
00147                     data.value.keep_alive = PR_TRUE;      
00148                     break;    
00149                 case PR_SockOpt_RecvBufferSize:
00150                     data.value.recv_buffer_size = segment;  
00151                     break;    
00152                 case PR_SockOpt_SendBufferSize:  
00153                     data.value.send_buffer_size = segment;  
00154                     break;    
00155                 case PR_SockOpt_IpTimeToLive:
00156                     data.value.ip_ttl = 64;  
00157                     break;    
00158                 case PR_SockOpt_IpTypeOfService:
00159                     data.value.tos = 0; 
00160                     break;    
00161                 case PR_SockOpt_McastTimeToLive:
00162                     fd = udp; 
00163                     data.value.mcast_ttl = 4; 
00164                     break;    
00165                 case PR_SockOpt_McastLoopback:
00166                     fd = udp; 
00167                     data.value.mcast_loopback = PR_TRUE; 
00168                     break;    
00169                 case PR_SockOpt_NoDelay:
00170                     data.value.no_delay = PR_TRUE;         
00171                     break;    
00172 #ifndef WIN32
00173                 case PR_SockOpt_MaxSegment:
00174                     data.value.max_segment = segment;      
00175                     break;    
00176 #endif
00177                 case PR_SockOpt_Broadcast:
00178                     fd = udp; 
00179                     data.value.broadcast = PR_TRUE;         
00180                     break;    
00181                 default: continue;
00182             }
00183 
00184                      /*
00185                       * TCP_MAXSEG can only be read, not set
00186                       */
00187             if (option != PR_SockOpt_MaxSegment) {
00188 #ifdef WIN32
00189               if (option != PR_SockOpt_McastLoopback)
00190 #endif
00191                             {
00192                      rv = PR_SetSocketOption(fd, &data);
00193                      if (PR_FAILURE == rv)
00194                                                  Failed("PR_SetSocketOption()", tag[option]);
00195                             }
00196                      }
00197 
00198             rv = PR_GetSocketOption(fd, &data);
00199             if (PR_FAILURE == rv) Failed("PR_GetSocketOption()", tag[option]);
00200         }
00201         PR_Close(udp);
00202         PR_Close(tcp);
00203     }
00204 #ifndef XP_MAC
00205     PR_fprintf(err, "%s\n", (failed) ? "FAILED" : "PASSED");
00206 #else
00207    printf("%s\n", (failed) ? "FAILED" : "PASSED");
00208 #endif
00209     return (failed) ? 1 : 0;
00210 }  /* main */
00211 
00212 /* sockopt.c */
00213