nsprpub/pr/tests/sockopt.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nspr.h"
michael@0 7 #include "prio.h"
michael@0 8 #include "prinit.h"
michael@0 9 #include "prprf.h"
michael@0 10 #include "obsolete/probslet.h"
michael@0 11
michael@0 12 #include "plerror.h"
michael@0 13
michael@0 14 #ifdef XP_UNIX
michael@0 15 #include <sys/socket.h> /* SO_REUSEPORT */
michael@0 16 #endif
michael@0 17
michael@0 18 static PRFileDesc *err = NULL;
michael@0 19 static PRBool failed = PR_FALSE;
michael@0 20
michael@0 21 static void Failed(const char *msg1, const char *msg2)
michael@0 22 {
michael@0 23 if (NULL != msg1) PR_fprintf(err, "%s ", msg1);
michael@0 24 PL_FPrintError(err, msg2);
michael@0 25 failed = PR_TRUE;
michael@0 26 } /* Failed */
michael@0 27
michael@0 28 static PRSockOption Incr(PRSockOption *option)
michael@0 29 {
michael@0 30 PRIntn val = ((PRIntn)*option) + 1;
michael@0 31 *option = (PRSockOption)val;
michael@0 32 return (PRSockOption)val;
michael@0 33 } /* Incr */
michael@0 34
michael@0 35 int main(int argc, char **argv)
michael@0 36 {
michael@0 37 PRStatus rv;
michael@0 38 PRFileDesc *udp = PR_NewUDPSocket();
michael@0 39 PRFileDesc *tcp = PR_NewTCPSocket();
michael@0 40 const char *tag[] =
michael@0 41 {
michael@0 42 "PR_SockOpt_Nonblocking", /* nonblocking io */
michael@0 43 "PR_SockOpt_Linger", /* linger on close if data present */
michael@0 44 "PR_SockOpt_Reuseaddr", /* allow local address reuse */
michael@0 45 "PR_SockOpt_Keepalive", /* keep connections alive */
michael@0 46 "PR_SockOpt_RecvBufferSize", /* send buffer size */
michael@0 47 "PR_SockOpt_SendBufferSize", /* receive buffer size */
michael@0 48
michael@0 49 "PR_SockOpt_IpTimeToLive", /* time to live */
michael@0 50 "PR_SockOpt_IpTypeOfService", /* type of service and precedence */
michael@0 51
michael@0 52 "PR_SockOpt_AddMember", /* add an IP group membership */
michael@0 53 "PR_SockOpt_DropMember", /* drop an IP group membership */
michael@0 54 "PR_SockOpt_McastInterface", /* multicast interface address */
michael@0 55 "PR_SockOpt_McastTimeToLive", /* multicast timetolive */
michael@0 56 "PR_SockOpt_McastLoopback", /* multicast loopback */
michael@0 57
michael@0 58 "PR_SockOpt_NoDelay", /* don't delay send to coalesce packets */
michael@0 59 "PR_SockOpt_MaxSegment", /* maximum segment size */
michael@0 60 "PR_SockOpt_Broadcast", /* Enable broadcast */
michael@0 61 "PR_SockOpt_Reuseport", /* allow local address & port reuse */
michael@0 62 "PR_SockOpt_Last"
michael@0 63 };
michael@0 64
michael@0 65 err = PR_GetSpecialFD(PR_StandardError);
michael@0 66 PR_STDIO_INIT();
michael@0 67
michael@0 68 if (NULL == udp) Failed("PR_NewUDPSocket()", NULL);
michael@0 69 else if (NULL == tcp) Failed("PR_NewTCPSocket()", NULL);
michael@0 70 else
michael@0 71 {
michael@0 72 PRSockOption option;
michael@0 73 PRUint32 segment = 1024;
michael@0 74 PRNetAddr addr;
michael@0 75
michael@0 76 rv = PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr);
michael@0 77 if (PR_FAILURE == rv) Failed("PR_InitializeNetAddr()", NULL);
michael@0 78 rv = PR_Bind(udp, &addr);
michael@0 79 if (PR_FAILURE == rv) Failed("PR_Bind()", NULL);
michael@0 80 for(option = PR_SockOpt_Linger; option < PR_SockOpt_Last; Incr(&option))
michael@0 81 {
michael@0 82 PRSocketOptionData data;
michael@0 83 PRFileDesc *fd = tcp;
michael@0 84 data.option = option;
michael@0 85 switch (option)
michael@0 86 {
michael@0 87 case PR_SockOpt_Nonblocking:
michael@0 88 data.value.non_blocking = PR_TRUE;
michael@0 89 break;
michael@0 90 #ifndef SYMBIAN
michael@0 91 case PR_SockOpt_Linger:
michael@0 92 data.value.linger.polarity = PR_TRUE;
michael@0 93 data.value.linger.linger = PR_SecondsToInterval(2);
michael@0 94 break;
michael@0 95 #endif
michael@0 96 case PR_SockOpt_Reuseaddr:
michael@0 97 data.value.reuse_addr = PR_TRUE;
michael@0 98 break;
michael@0 99 case PR_SockOpt_Keepalive:
michael@0 100 data.value.keep_alive = PR_TRUE;
michael@0 101 break;
michael@0 102 case PR_SockOpt_RecvBufferSize:
michael@0 103 data.value.recv_buffer_size = segment;
michael@0 104 break;
michael@0 105 case PR_SockOpt_SendBufferSize:
michael@0 106 data.value.send_buffer_size = segment;
michael@0 107 break;
michael@0 108 #ifndef SYMBIAN
michael@0 109 case PR_SockOpt_IpTimeToLive:
michael@0 110 data.value.ip_ttl = 64;
michael@0 111 break;
michael@0 112 case PR_SockOpt_IpTypeOfService:
michael@0 113 data.value.tos = 0;
michael@0 114 break;
michael@0 115 case PR_SockOpt_McastTimeToLive:
michael@0 116 fd = udp;
michael@0 117 data.value.mcast_ttl = 4;
michael@0 118 break;
michael@0 119 case PR_SockOpt_McastLoopback:
michael@0 120 fd = udp;
michael@0 121 data.value.mcast_loopback = PR_TRUE;
michael@0 122 break;
michael@0 123 #endif
michael@0 124 case PR_SockOpt_NoDelay:
michael@0 125 data.value.no_delay = PR_TRUE;
michael@0 126 break;
michael@0 127 #ifndef WIN32
michael@0 128 case PR_SockOpt_MaxSegment:
michael@0 129 data.value.max_segment = segment;
michael@0 130 break;
michael@0 131 #endif
michael@0 132 #ifndef SYMBIAN
michael@0 133 case PR_SockOpt_Broadcast:
michael@0 134 fd = udp;
michael@0 135 data.value.broadcast = PR_TRUE;
michael@0 136 break;
michael@0 137 #endif
michael@0 138 #ifdef SO_REUSEPORT
michael@0 139 case PR_SockOpt_Reuseport:
michael@0 140 data.value.reuse_port = PR_TRUE;
michael@0 141 break;
michael@0 142 #endif
michael@0 143 default: continue;
michael@0 144 }
michael@0 145
michael@0 146 /*
michael@0 147 * TCP_MAXSEG can only be read, not set
michael@0 148 */
michael@0 149 if (option != PR_SockOpt_MaxSegment) {
michael@0 150 #ifdef WIN32
michael@0 151 if (option != PR_SockOpt_McastLoopback)
michael@0 152 #endif
michael@0 153 {
michael@0 154 rv = PR_SetSocketOption(fd, &data);
michael@0 155 if (PR_FAILURE == rv)
michael@0 156 Failed("PR_SetSocketOption()", tag[option]);
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 rv = PR_GetSocketOption(fd, &data);
michael@0 161 if (PR_FAILURE == rv) Failed("PR_GetSocketOption()", tag[option]);
michael@0 162 }
michael@0 163 PR_Close(udp);
michael@0 164 PR_Close(tcp);
michael@0 165 }
michael@0 166 PR_fprintf(err, "%s\n", (failed) ? "FAILED" : "PASSED");
michael@0 167 return (failed) ? 1 : 0;
michael@0 168 } /* main */
michael@0 169
michael@0 170 /* sockopt.c */
michael@0 171

mercurial