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