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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 /*******************************************************************
39 ** udpsrc.c -- Test basic function of UDP server
41 ** udpsrv operates on the same machine with program udpclt.
42 ** udpsrv is the server side of a udp sockets application.
43 ** udpclt is the client side of a udp sockets application.
45 ** The test is designed to assist developers in porting/debugging
46 ** the UDP socket functions of NSPR.
48 ** This test is not a stress test.
50 ** main() starts two threads: UDP_Server() and UDP_Client();
51 ** main() uses PR_JoinThread() to wait for the threads to complete.
53 ** UDP_Server() does repeated recvfrom()s from a socket.
54 ** He detects an EOF condition set by UDP_Client(). For each
55 ** packet received by UDP_Server(), he checks its content for
56 ** expected content, then sends the packet back to UDP_Client().
58 ** UDP_Client() sends packets to UDP_Server() using sendto()
59 ** he recieves packets back from the server via recvfrom().
60 ** After he sends enough packets containing UDP_AMOUNT_TO_WRITE
61 ** bytes of data, he sends an EOF message.
63 ** The test issues a pass/fail message at end.
66 ** The variable "_debug_on" can be set to 1 to cause diagnostic
67 ** messages related to client/server synchronization. Useful when
70 ** Error messages are written to stdout.
72 ********************************************************************
74 /* --- include files --- */
90 #define UDP_BUF_SIZE 4096
91 #define UDP_DGRAM_SIZE 128
92 #define UDP_AMOUNT_TO_WRITE (PRInt32)((UDP_DGRAM_SIZE * 1000l) +1)
93 #define NUM_UDP_CLIENTS 1
94 #define NUM_UDP_DATAGRAMS_PER_CLIENT 5
95 #define UDP_SERVER_PORT 9050
96 #define UDP_CLIENT_PORT 9053
97 #define MY_INADDR PR_INADDR_ANY
98 #define PEER_INADDR PR_INADDR_LOOPBACK
100 #define UDP_TIMEOUT 400000
101 /* #define UDP_TIMEOUT PR_INTERVAL_NO_TIMEOUT */
103 /* --- static data --- */
104 static PRIntn _debug_on = 0;
105 static PRBool passed = PR_TRUE;
106 static PRUint32 cltBytesRead = 0;
107 static PRUint32 srvBytesRead = 0;
108 static PRFileDesc *output = NULL;
110 /* --- static function declarations --- */
111 #define DPRINTF(arg) if (_debug_on) PR_fprintf(output, arg)
115 /*******************************************************************
116 ** ListNetAddr() -- Display the Net Address on stdout
118 ** Description: displays the component parts of a PRNetAddr struct
120 ** Arguments: address of PRNetAddr structure to display
126 ********************************************************************
128 void ListNetAddr( char *msg, PRNetAddr *na )
132 sprintf( mbuf, "ListNetAddr: %s family: %d, port: %d, ip: %8.8X\n",
133 msg, na->inet.family, PR_ntohs( na->inet.port), PR_ntohl(na->inet.ip) );
137 } /* --- end ListNetAddr() --- */
139 /********************************************************************
140 ** UDP_Server() -- Test a UDP server application
142 ** Description: The Server side of a UDP Client/Server application.
151 ********************************************************************
153 static void PR_CALLBACK UDP_Server( void *arg )
155 static char svrBuf[UDP_BUF_SIZE];
159 PRBool bound = PR_FALSE;
160 PRBool endOfInput = PR_FALSE;
161 PRInt32 numBytes = UDP_DGRAM_SIZE;
163 DPRINTF("udpsrv: UDP_Server(): starting\n" );
165 /* --- Create the socket --- */
166 DPRINTF("udpsrv: UDP_Server(): Creating UDP Socket\n" );
167 svrSock = PR_NewUDPSocket();
168 if ( svrSock == NULL )
173 "udpsrv: UDP_Server(): PR_NewUDPSocket() returned NULL\n" );
177 /* --- Initialize the sockaddr_in structure --- */
178 memset( &netaddr, 0, sizeof( netaddr ));
179 netaddr.inet.family = PR_AF_INET;
180 netaddr.inet.port = PR_htons( UDP_SERVER_PORT );
181 netaddr.inet.ip = PR_htonl( MY_INADDR );
183 /* --- Bind the socket --- */
186 DPRINTF("udpsrv: UDP_Server(): Binding socket\n" );
187 rv = PR_Bind( svrSock, &netaddr );
190 if ( PR_GetError() == PR_ADDRESS_IN_USE_ERROR )
192 if (debug_mode) PR_fprintf(output, "udpsrv: UDP_Server(): \
193 PR_Bind(): reports: PR_ADDRESS_IN_USE_ERROR\n");
194 PR_Sleep( PR_MillisecondsToInterval( 2000 ));
200 if (debug_mode) PR_fprintf(output, "udpsrv: UDP_Server(): \
201 PR_Bind(): failed: %ld with error: %ld\n",
210 ListNetAddr( "UDP_Server: after bind", &netaddr );
212 /* --- Recv the socket --- */
215 DPRINTF("udpsrv: UDP_Server(): RecvFrom() socket\n" );
216 rv = PR_RecvFrom( svrSock, svrBuf, numBytes, 0, &netaddr, UDP_TIMEOUT );
222 "udpsrv: UDP_Server(): PR_RecvFrom(): failed with error: %ld\n",
227 ListNetAddr( "UDP_Server after RecvFrom", &netaddr );
231 if ( svrBuf[0] == 'E' )
233 DPRINTF("udpsrv: UDP_Server(): EOF on input detected\n" );
234 endOfInput = PR_TRUE;
237 /* --- Send the socket --- */
238 DPRINTF("udpsrv: UDP_Server(): SendTo(): socket\n" );
239 rv = PR_SendTo( svrSock, svrBuf, rv, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT );
245 "udpsrv: UDP_Server(): PR_SendTo(): failed with error: %ld\n",
250 ListNetAddr( "UDP_Server after SendTo", &netaddr );
253 /* --- Close the socket --- */
254 DPRINTF("udpsrv: UDP_Server(): Closing socket\n" );
255 rv = PR_Close( svrSock );
256 if ( rv != PR_SUCCESS )
261 "udpsrv: UDP_Server(): PR_Close(): failed to close socket\n" );
265 DPRINTF("udpsrv: UDP_Server(): Normal end\n" );
266 } /* --- end UDP_Server() --- */
269 static char cltBuf[UDP_BUF_SIZE];
270 static char cltBufin[UDP_BUF_SIZE];
271 /********************************************************************
272 ** UDP_Client() -- Test a UDP client application
280 ** 0 -- Successful execution
286 ********************************************************************
288 static void PR_CALLBACK UDP_Client( void *arg )
292 PRBool bound = PR_FALSE;
295 PRBool endOfInput = PR_FALSE;
296 PRInt32 numBytes = UDP_DGRAM_SIZE;
297 PRInt32 writeThisMany = UDP_AMOUNT_TO_WRITE;
301 DPRINTF("udpsrv: UDP_Client(): starting\n" );
303 /* --- Create the socket --- */
304 cltSock = PR_NewUDPSocket();
305 if ( cltSock == NULL )
310 "udpsrv: UDP_Client(): PR_NewUDPSocket() returned NULL\n" );
314 /* --- Initialize the sockaddr_in structure --- */
315 memset( &netaddr, 0, sizeof( netaddr ));
316 netaddr.inet.family = PR_AF_INET;
317 netaddr.inet.ip = PR_htonl( MY_INADDR );
318 netaddr.inet.port = PR_htons( UDP_CLIENT_PORT );
320 /* --- Initialize the write buffer --- */
321 for ( i = 0; i < UDP_BUF_SIZE ; i++ )
324 /* --- Bind the socket --- */
327 DPRINTF("udpsrv: UDP_Client(): Binding socket\n" );
328 rv = PR_Bind( cltSock, &netaddr );
331 if ( PR_GetError() == PR_ADDRESS_IN_USE_ERROR )
335 "udpsrv: UDP_Client(): PR_Bind(): reports: PR_ADDRESS_IN_USE_ERROR\n");
336 PR_Sleep( PR_MillisecondsToInterval( 2000 ));
344 "udpsrv: UDP_Client(): PR_Bind(): failed: %ld with error: %ld\n",
353 ListNetAddr( "UDP_Client after Bind", &netaddr );
355 /* --- Initialize the sockaddr_in structure --- */
356 memset( &netaddr, 0, sizeof( netaddr ));
357 netaddr.inet.family = PR_AF_INET;
358 netaddr.inet.ip = PR_htonl( PEER_INADDR );
359 netaddr.inet.port = PR_htons( UDP_SERVER_PORT );
361 /* --- send and receive packets until no more data left */
365 ** Signal EOF in the data stream on the last packet
367 if ( writeThisMany <= UDP_DGRAM_SIZE )
369 DPRINTF("udpsrv: UDP_Client(): Send EOF packet\n" );
371 endOfInput = PR_TRUE;
374 /* --- SendTo the socket --- */
375 if ( writeThisMany > UDP_DGRAM_SIZE )
376 numBytes = UDP_DGRAM_SIZE;
378 numBytes = writeThisMany;
379 writeThisMany -= numBytes;
382 sprintf( mbuf, "udpsrv: UDP_Client(): write_this_many: %d, numbytes: %d\n",
383 writeThisMany, numBytes );
387 DPRINTF("udpsrv: UDP_Client(): SendTo(): socket\n" );
388 rv = PR_SendTo( cltSock, cltBuf, numBytes, 0, &netaddr, UDP_TIMEOUT );
394 "udpsrv: UDP_Client(): PR_SendTo(): failed with error: %ld\n",
399 ListNetAddr( "UDP_Client after SendTo", &netaddr );
401 /* --- RecvFrom the socket --- */
402 memset( cltBufin, 0, UDP_BUF_SIZE );
403 DPRINTF("udpsrv: UDP_Client(): RecvFrom(): socket\n" );
404 rv = PR_RecvFrom( cltSock, cltBufin, numBytes, 0, &netaddrx, UDP_TIMEOUT );
408 if (debug_mode) PR_fprintf(output,
409 "udpsrv: UDP_Client(): PR_RecvFrom(): failed with error: %ld\n",
414 ListNetAddr( "UDP_Client after RecvFrom()", &netaddr );
417 /* --- verify buffer --- */
418 for ( i = 0; i < rv ; i++ )
420 if ( cltBufin[i] != i )
422 /* --- special case, end of input --- */
423 if ( endOfInput && i == 0 && cltBufin[0] == 'E' )
426 if (debug_mode) PR_fprintf(output,
427 "udpsrv: UDP_Client(): return data mismatch\n" );
432 if (debug_mode) PR_fprintf(output, ".");
435 /* --- Close the socket --- */
436 DPRINTF("udpsrv: UDP_Server(): Closing socket\n" );
437 rv = PR_Close( cltSock );
438 if ( rv != PR_SUCCESS )
441 if (debug_mode) PR_fprintf(output,
442 "udpsrv: UDP_Client(): PR_Close(): failed to close socket\n" );
445 DPRINTF("udpsrv: UDP_Client(): ending\n" );
446 } /* --- end UDP_Client() --- */
448 /********************************************************************
454 ** 0 -- Successful execution
459 ** Standard test case setup.
461 ** Calls the function UDP_Server()
463 ********************************************************************
466 int main(int argc, char **argv)
469 /* The command line argument: -d is used to determine if the test is being run
470 in debug mode. The regress tool requires only one line output:PASS or FAIL.
471 All of the printfs associated with this test has been handled with a if (debug_mode)
473 Usage: test_name -d -v
476 PLOptState *opt = PL_CreateOptState(argc, argv, "dv");
477 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
479 if (PL_OPT_BAD == os) continue;
482 case 'd': /* debug mode */
485 case 'v': /* verbose mode */
492 PL_DestroyOptState(opt);
494 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
498 PR_SetConcurrency(4);
501 ** Create the Server thread
503 DPRINTF( "udpsrv: Creating Server Thread\n" );
504 srv = PR_CreateThread( PR_USER_THREAD,
513 if (debug_mode) PR_fprintf(output, "udpsrv: Cannot create server thread\n" );
518 ** Give the Server time to Start
520 DPRINTF( "udpsrv: Pausing to allow Server to start\n" );
521 PR_Sleep( PR_MillisecondsToInterval(200) );
524 ** Create the Client thread
526 DPRINTF( "udpsrv: Creating Client Thread\n" );
527 clt = PR_CreateThread( PR_USER_THREAD,
536 if (debug_mode) PR_fprintf(output, "udpsrv: Cannot create server thread\n" );
543 DPRINTF("udpsrv: Waiting to join Server & Client Threads\n" );
544 PR_JoinThread( srv );
545 PR_JoinThread( clt );
548 ** Evaluate test results
550 if (debug_mode) PR_fprintf(output, "\n\nudpsrv: main(): cltBytesRead(%ld), \
551 srvBytesRead(%ld), expected(%ld)\n",
552 cltBytesRead, srvBytesRead, UDP_AMOUNT_TO_WRITE );
553 if ( cltBytesRead != srvBytesRead || cltBytesRead != UDP_AMOUNT_TO_WRITE )
562 } /* --- end main() --- */