home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / sockopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 KB  |  226 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "nspr.h"
  20. #include "prio.h"
  21. #include "prinit.h"
  22. #include "prprf.h"
  23. #ifdef XP_MAC
  24. #include "probslet.h"
  25. #else
  26. #include "obsolete/probslet.h"
  27. #endif
  28.  
  29. #include "plerror.h"
  30.  
  31. static PRFileDesc *err = NULL;
  32. static PRBool failed = PR_FALSE;
  33.  
  34. #ifndef XP_MAC
  35. static void Failed(const char *msg1, const char *msg2)
  36. {
  37.     if (NULL != msg1) PR_fprintf(err, "%s ", msg1);
  38.     PL_FPrintError(err, msg2);
  39.     failed = PR_TRUE;
  40. }  /* Failed */
  41.  
  42. #else
  43. #include "prlog.h"
  44. #define printf PR_LogPrint
  45. extern void SetupMacPrintfLog(char *logFile);
  46. static void Failed(const char *msg1, const char *msg2)
  47. {
  48.     if (NULL != msg1) printf("%s ", msg1);
  49.     printf (msg2);
  50.     failed |= PR_TRUE;
  51. }  /* Failed */
  52.  
  53. #endif
  54.  
  55. static PRSockOption Incr(PRSockOption *option)
  56. {
  57.     PRIntn val = ((PRIntn)*option) + 1;
  58.     *option = (PRSockOption)val;
  59.     return (PRSockOption)val;
  60. }  /* Incr */
  61.  
  62. PRIntn main(PRIntn argc, char *argv)
  63. {
  64.     PRStatus rv;
  65.     PRFileDesc *udp = PR_NewUDPSocket();
  66.     PRFileDesc *tcp = PR_NewTCPSocket();
  67.     const char *tag[] =
  68.     {
  69.         "PR_SockOpt_Nonblocking",     /* nonblocking io */
  70.         "PR_SockOpt_Linger",          /* linger on close if data present */
  71.         "PR_SockOpt_Reuseaddr",       /* allow local address reuse */
  72.         "PR_SockOpt_Keepalive",       /* keep connections alive */
  73.         "PR_SockOpt_RecvBufferSize",  /* send buffer size */
  74.         "PR_SockOpt_SendBufferSize",  /* receive buffer size */
  75.  
  76.         "PR_SockOpt_IpTimeToLive",    /* time to live */
  77.         "PR_SockOpt_IpTypeOfService", /* type of service and precedence */
  78.  
  79.         "PR_SockOpt_AddMember",       /* add an IP group membership */
  80.         "PR_SockOpt_DropMember",      /* drop an IP group membership */
  81.         "PR_SockOpt_McastInterface",  /* multicast interface address */
  82.         "PR_SockOpt_McastTimeToLive", /* multicast timetolive */
  83.         "PR_SockOpt_McastLoopback",   /* multicast loopback */
  84.  
  85.         "PR_SockOpt_NoDelay",         /* don't delay send to coalesce packets */
  86.         "PR_SockOpt_MaxSegment",      /* maximum segment size */
  87.         "PR_SockOpt_Last"
  88.     };
  89.  
  90.     err = PR_GetSpecialFD(PR_StandardError);
  91.     PR_STDIO_INIT();
  92.  
  93. #ifdef XP_MAC
  94.     SetupMacPrintfLog("sockopt.log");
  95. #endif
  96.  
  97.     if (NULL == udp) Failed("PR_NewUDPSocket()", NULL);
  98.     else if (NULL == tcp) Failed("PR_NewTCPSocket()", NULL);
  99.     else
  100.     {
  101.         PRSockOption option;
  102.         PRLinger linger;
  103.         PRUint32 ttl = 20;
  104.         PRBool boolean = PR_TRUE;
  105.         PRUint32 segment = 1024;
  106.         for(option = PR_SockOpt_Linger; option < PR_SockOpt_Last; Incr(&option))
  107.         {
  108.             void *value;
  109.             PRInt32 *size;
  110.             PRInt32 ttlsize = sizeof(ttl);
  111.             PRInt32 segmentsize = sizeof(segment);
  112.             PRInt32 booleansize = sizeof(boolean);
  113.             PRInt32 lingersize = sizeof(linger);
  114.  
  115.             PRFileDesc *socket = udp;
  116.             switch (option)
  117.             {
  118.             case PR_SockOpt_Linger:          /* linger on close if data present */
  119.                 socket = tcp;
  120.                 linger.polarity = PR_TRUE;
  121.                 linger.linger = PR_SecondsToInterval(1);
  122.                 value = &linger;
  123.                 size = &lingersize;
  124.                 break;
  125.             case PR_SockOpt_NoDelay:         /* don't delay send to coalesce packets */
  126.             case PR_SockOpt_Keepalive:       /* keep connections alive */
  127.                 socket = tcp;
  128.             case PR_SockOpt_Reuseaddr:       /* allow local address reuse */
  129.                 value = &boolean;
  130.                 size = &booleansize;
  131.                 break;
  132.             case PR_SockOpt_MaxSegment:      /* maximum segment size */
  133.                 socket = tcp;
  134.             case PR_SockOpt_RecvBufferSize:  /* send buffer size */
  135.             case PR_SockOpt_SendBufferSize:  /* receive buffer size */
  136.                 value = &segment;
  137.                 size = &segmentsize;
  138.                 break;
  139.  
  140.             case PR_SockOpt_IpTimeToLive:    /* time to live */
  141.                 value = &ttl;
  142.                 size = &ttlsize;
  143.                 break;
  144.             case PR_SockOpt_IpTypeOfService: /* type of service and precedence */
  145.             case PR_SockOpt_AddMember:       /* add an IP group membership */
  146.             case PR_SockOpt_DropMember:      /* drop an IP group membership */
  147.             case PR_SockOpt_McastInterface:  /* multicast interface address */
  148.             case PR_SockOpt_McastTimeToLive: /* multicast timetolive */
  149.             case PR_SockOpt_McastLoopback:   /* multicast loopback */
  150.             default:
  151.                 continue;
  152.             }
  153.  
  154.             rv = PR_SetSockOpt(socket, option, value, *size);
  155.             if (PR_FAILURE == rv) Failed("PR_SetSockOpt()", tag[option]);
  156.             
  157.             rv = PR_GetSockOpt(socket, option, &value, size);
  158.             if (PR_FAILURE == rv) Failed("PR_GetSockOpt()", tag[option]);
  159.         }
  160.         for(option = PR_SockOpt_Linger; option < PR_SockOpt_Last; Incr(&option))
  161.         {
  162.             PRSocketOptionData data;
  163.             PRFileDesc *fd = tcp;
  164.             data.option = option;
  165.             switch (option)
  166.             {
  167.                 case PR_SockOpt_Nonblocking:
  168.                     data.value.non_blocking = PR_TRUE;
  169.                     break;    
  170.                 case PR_SockOpt_Linger:
  171.                     data.value.linger.polarity = PR_TRUE;
  172.                     data.value.linger.linger = PR_SecondsToInterval(2);          
  173.                     break;    
  174.                 case PR_SockOpt_Reuseaddr:
  175.                     data.value.reuse_addr = PR_TRUE;      
  176.                     break;    
  177.                 case PR_SockOpt_Keepalive:       
  178.                     data.value.keep_alive = PR_TRUE;      
  179.                     break;    
  180.                 case PR_SockOpt_RecvBufferSize:
  181.                     data.value.recv_buffer_size = segment;  
  182.                     break;    
  183.                 case PR_SockOpt_SendBufferSize:  
  184.                     data.value.send_buffer_size = segment;  
  185.                     break;    
  186.                 case PR_SockOpt_IpTimeToLive:
  187.                     data.value.ip_ttl = 64;  
  188.                     break;    
  189.                 case PR_SockOpt_IpTypeOfService:
  190.                     data.value.tos = 0; 
  191.                     break;    
  192.                 case PR_SockOpt_McastTimeToLive:
  193.                     fd = udp; 
  194.                     data.value.mcast_ttl = 4; 
  195.                     break;    
  196.                 case PR_SockOpt_McastLoopback:
  197.                     fd = udp; 
  198.                     data.value.mcast_loopback = PR_TRUE; 
  199.                     break;    
  200.                 case PR_SockOpt_NoDelay:
  201.                     data.value.no_delay = PR_TRUE;         
  202.                     break;    
  203.                 case PR_SockOpt_MaxSegment:
  204.                     data.value.max_segment = segment;      
  205.                     break;    
  206.                 default: continue;
  207.             }
  208.  
  209.             rv = PR_SetSocketOption(fd, &data);
  210.             if (PR_FAILURE == rv) Failed("PR_SetSocketOption()", tag[option]);
  211.  
  212.             rv = PR_GetSocketOption(fd, &data);
  213.             if (PR_FAILURE == rv) Failed("PR_GetSocketOption()", tag[option]);
  214.         }
  215.     }
  216. #ifndef XP_MAC
  217.     PR_fprintf(err, "%s\n", (failed) ? "FAILED" : "PASSED");
  218. #else
  219.    printf("%s\n", (failed) ? "FAILED" : "PASSED");
  220. #endif
  221.     return (failed) ? 1 : 0;
  222. }  /* main */
  223.  
  224. /* sockopt.c */
  225.  
  226.