1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*******************************************************************
** udpsrc.c -- Test basic function of UDP server
**
** udpsrv operates on the same machine with program udpclt.
** udpsrv is the server side of a udp sockets application.
** udpclt is the client side of a udp sockets application.
**
** The test is designed to assist developers in porting/debugging
** the UDP socket functions of NSPR.
**
** This test is not a stress test.
**
** main() starts two threads: UDP_Server() and UDP_Client();
** main() uses PR_JoinThread() to wait for the threads to complete.
**
** UDP_Server() does repeated recvfrom()s from a socket.
** He detects an EOF condition set by UDP_Client(). For each
** packet received by UDP_Server(), he checks its content for
** expected content, then sends the packet back to UDP_Client().
**
** UDP_Client() sends packets to UDP_Server() using sendto()
** he recieves packets back from the server via recvfrom().
** After he sends enough packets containing UDP_AMOUNT_TO_WRITE
** bytes of data, he sends an EOF message.
**
** The test issues a pass/fail message at end.
**
** Notes:
** The variable "_debug_on" can be set to 1 to cause diagnostic
** messages related to client/server synchronization. Useful when
** the test hangs.
**
** Error messages are written to stdout.
**
********************************************************************
*/
/* --- include files --- */
#include "nspr.h"
#include "prpriv.h"
#include "plgetopt.h"
#include "prttools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef XP_PC
#define mode_t int
#endif
#define UDP_BUF_SIZE 4096
#define UDP_DGRAM_SIZE 128
#define UDP_AMOUNT_TO_WRITE (PRInt32)((UDP_DGRAM_SIZE * 1000l) +1)
#define NUM_UDP_CLIENTS 1
#define NUM_UDP_DATAGRAMS_PER_CLIENT 5
#define UDP_SERVER_PORT 9050
#define UDP_CLIENT_PORT 9053
#define MY_INADDR PR_INADDR_ANY
#define PEER_INADDR PR_INADDR_LOOPBACK
#define UDP_TIMEOUT 400000
/* #define UDP_TIMEOUT PR_INTERVAL_NO_TIMEOUT */
/* --- static data --- */
static PRIntn _debug_on = 0;
static PRBool passed = PR_TRUE;
static PRUint32 cltBytesRead = 0;
static PRUint32 srvBytesRead = 0;
static PRFileDesc *output = NULL;
/* --- static function declarations --- */
#define DPRINTF(arg) if (_debug_on) PR_fprintf(output, arg)
/*******************************************************************
** ListNetAddr() -- Display the Net Address on stdout
**
** Description: displays the component parts of a PRNetAddr struct
**
** Arguments: address of PRNetAddr structure to display
**
** Returns: void
**
** Notes:
**
********************************************************************
*/
void ListNetAddr( char *msg, PRNetAddr *na )
{
char mbuf[256];
sprintf( mbuf, "ListNetAddr: %s family: %d, port: %d, ip: %8.8X\n",
msg, na->inet.family, PR_ntohs( na->inet.port), PR_ntohl(na->inet.ip) );
#if 0
DPRINTF( mbuf );
#endif
} /* --- end ListNetAddr() --- */
/********************************************************************
** UDP_Server() -- Test a UDP server application
**
** Description: The Server side of a UDP Client/Server application.
**
** Arguments: none
**
** Returns: void
**
** Notes:
**
**
********************************************************************
*/
static void PR_CALLBACK UDP_Server( void *arg )
{
static char svrBuf[UDP_BUF_SIZE];
PRFileDesc *svrSock;
PRInt32 rv;
PRNetAddr netaddr;
PRBool bound = PR_FALSE;
PRBool endOfInput = PR_FALSE;
PRInt32 numBytes = UDP_DGRAM_SIZE;
DPRINTF("udpsrv: UDP_Server(): starting\n" );
/* --- Create the socket --- */
DPRINTF("udpsrv: UDP_Server(): Creating UDP Socket\n" );
svrSock = PR_NewUDPSocket();
if ( svrSock == NULL )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Server(): PR_NewUDPSocket() returned NULL\n" );
return;
}
/* --- Initialize the sockaddr_in structure --- */
memset( &netaddr, 0, sizeof( netaddr ));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.port = PR_htons( UDP_SERVER_PORT );
netaddr.inet.ip = PR_htonl( MY_INADDR );
/* --- Bind the socket --- */
while ( !bound )
{
DPRINTF("udpsrv: UDP_Server(): Binding socket\n" );
rv = PR_Bind( svrSock, &netaddr );
if ( rv < 0 )
{
if ( PR_GetError() == PR_ADDRESS_IN_USE_ERROR )
{
if (debug_mode) PR_fprintf(output, "udpsrv: UDP_Server(): \
PR_Bind(): reports: PR_ADDRESS_IN_USE_ERROR\n");
PR_Sleep( PR_MillisecondsToInterval( 2000 ));
continue;
}
else
{
passed = PR_FALSE;
if (debug_mode) PR_fprintf(output, "udpsrv: UDP_Server(): \
PR_Bind(): failed: %ld with error: %ld\n",
rv, PR_GetError() );
PR_Close( svrSock );
return;
}
}
else
bound = PR_TRUE;
}
ListNetAddr( "UDP_Server: after bind", &netaddr );
/* --- Recv the socket --- */
while( !endOfInput )
{
DPRINTF("udpsrv: UDP_Server(): RecvFrom() socket\n" );
rv = PR_RecvFrom( svrSock, svrBuf, numBytes, 0, &netaddr, UDP_TIMEOUT );
if ( rv == -1 )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Server(): PR_RecvFrom(): failed with error: %ld\n",
PR_GetError() );
PR_Close( svrSock );
return;
}
ListNetAddr( "UDP_Server after RecvFrom", &netaddr );
srvBytesRead += rv;
if ( svrBuf[0] == 'E' )
{
DPRINTF("udpsrv: UDP_Server(): EOF on input detected\n" );
endOfInput = PR_TRUE;
}
/* --- Send the socket --- */
DPRINTF("udpsrv: UDP_Server(): SendTo(): socket\n" );
rv = PR_SendTo( svrSock, svrBuf, rv, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT );
if ( rv == -1 )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Server(): PR_SendTo(): failed with error: %ld\n",
PR_GetError() );
PR_Close( svrSock );
return;
}
ListNetAddr( "UDP_Server after SendTo", &netaddr );
}
/* --- Close the socket --- */
DPRINTF("udpsrv: UDP_Server(): Closing socket\n" );
rv = PR_Close( svrSock );
if ( rv != PR_SUCCESS )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Server(): PR_Close(): failed to close socket\n" );
return;
}
DPRINTF("udpsrv: UDP_Server(): Normal end\n" );
} /* --- end UDP_Server() --- */
static char cltBuf[UDP_BUF_SIZE];
static char cltBufin[UDP_BUF_SIZE];
/********************************************************************
** UDP_Client() -- Test a UDP client application
**
** Description:
**
** Arguments:
**
**
** Returns:
** 0 -- Successful execution
** 1 -- Test failed.
**
** Notes:
**
**
********************************************************************
*/
static void PR_CALLBACK UDP_Client( void *arg )
{
PRFileDesc *cltSock;
PRInt32 rv;
PRBool bound = PR_FALSE;
PRNetAddr netaddr;
PRNetAddr netaddrx;
PRBool endOfInput = PR_FALSE;
PRInt32 numBytes = UDP_DGRAM_SIZE;
PRInt32 writeThisMany = UDP_AMOUNT_TO_WRITE;
int i;
DPRINTF("udpsrv: UDP_Client(): starting\n" );
/* --- Create the socket --- */
cltSock = PR_NewUDPSocket();
if ( cltSock == NULL )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Client(): PR_NewUDPSocket() returned NULL\n" );
return;
}
/* --- Initialize the sockaddr_in structure --- */
memset( &netaddr, 0, sizeof( netaddr ));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.ip = PR_htonl( MY_INADDR );
netaddr.inet.port = PR_htons( UDP_CLIENT_PORT );
/* --- Initialize the write buffer --- */
for ( i = 0; i < UDP_BUF_SIZE ; i++ )
cltBuf[i] = i;
/* --- Bind the socket --- */
while ( !bound )
{
DPRINTF("udpsrv: UDP_Client(): Binding socket\n" );
rv = PR_Bind( cltSock, &netaddr );
if ( rv < 0 )
{
if ( PR_GetError() == PR_ADDRESS_IN_USE_ERROR )
{
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Client(): PR_Bind(): reports: PR_ADDRESS_IN_USE_ERROR\n");
PR_Sleep( PR_MillisecondsToInterval( 2000 ));
continue;
}
else
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Client(): PR_Bind(): failed: %ld with error: %ld\n",
rv, PR_GetError() );
PR_Close( cltSock );
return;
}
}
else
bound = PR_TRUE;
}
ListNetAddr( "UDP_Client after Bind", &netaddr );
/* --- Initialize the sockaddr_in structure --- */
memset( &netaddr, 0, sizeof( netaddr ));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.ip = PR_htonl( PEER_INADDR );
netaddr.inet.port = PR_htons( UDP_SERVER_PORT );
/* --- send and receive packets until no more data left */
while( !endOfInput )
{
/*
** Signal EOF in the data stream on the last packet
*/
if ( writeThisMany <= UDP_DGRAM_SIZE )
{
DPRINTF("udpsrv: UDP_Client(): Send EOF packet\n" );
cltBuf[0] = 'E';
endOfInput = PR_TRUE;
}
/* --- SendTo the socket --- */
if ( writeThisMany > UDP_DGRAM_SIZE )
numBytes = UDP_DGRAM_SIZE;
else
numBytes = writeThisMany;
writeThisMany -= numBytes;
{
char mbuf[256];
sprintf( mbuf, "udpsrv: UDP_Client(): write_this_many: %d, numbytes: %d\n",
writeThisMany, numBytes );
DPRINTF( mbuf );
}
DPRINTF("udpsrv: UDP_Client(): SendTo(): socket\n" );
rv = PR_SendTo( cltSock, cltBuf, numBytes, 0, &netaddr, UDP_TIMEOUT );
if ( rv == -1 )
{
passed = PR_FALSE;
if (debug_mode)
PR_fprintf(output,
"udpsrv: UDP_Client(): PR_SendTo(): failed with error: %ld\n",
PR_GetError() );
PR_Close( cltSock );
return;
}
ListNetAddr( "UDP_Client after SendTo", &netaddr );
/* --- RecvFrom the socket --- */
memset( cltBufin, 0, UDP_BUF_SIZE );
DPRINTF("udpsrv: UDP_Client(): RecvFrom(): socket\n" );
rv = PR_RecvFrom( cltSock, cltBufin, numBytes, 0, &netaddrx, UDP_TIMEOUT );
if ( rv == -1 )
{
passed = PR_FALSE;
if (debug_mode) PR_fprintf(output,
"udpsrv: UDP_Client(): PR_RecvFrom(): failed with error: %ld\n",
PR_GetError() );
PR_Close( cltSock );
return;
}
ListNetAddr( "UDP_Client after RecvFrom()", &netaddr );
cltBytesRead += rv;
/* --- verify buffer --- */
for ( i = 0; i < rv ; i++ )
{
if ( cltBufin[i] != i )
{
/* --- special case, end of input --- */
if ( endOfInput && i == 0 && cltBufin[0] == 'E' )
continue;
passed = PR_FALSE;
if (debug_mode) PR_fprintf(output,
"udpsrv: UDP_Client(): return data mismatch\n" );
PR_Close( cltSock );
return;
}
}
if (debug_mode) PR_fprintf(output, ".");
}
/* --- Close the socket --- */
DPRINTF("udpsrv: UDP_Server(): Closing socket\n" );
rv = PR_Close( cltSock );
if ( rv != PR_SUCCESS )
{
passed = PR_FALSE;
if (debug_mode) PR_fprintf(output,
"udpsrv: UDP_Client(): PR_Close(): failed to close socket\n" );
return;
}
DPRINTF("udpsrv: UDP_Client(): ending\n" );
} /* --- end UDP_Client() --- */
/********************************************************************
** main() -- udpsrv
**
** arguments:
**
** Returns:
** 0 -- Successful execution
** 1 -- Test failed.
**
** Description:
**
** Standard test case setup.
**
** Calls the function UDP_Server()
**
********************************************************************
*/
int main(int argc, char **argv)
{
PRThread *srv, *clt;
/* The command line argument: -d is used to determine if the test is being run
in debug mode. The regress tool requires only one line output:PASS or FAIL.
All of the printfs associated with this test has been handled with a if (debug_mode)
test.
Usage: test_name -d -v
*/
PLOptStatus os;
PLOptState *opt = PL_CreateOptState(argc, argv, "dv");
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
{
if (PL_OPT_BAD == os) continue;
switch (opt->option)
{
case 'd': /* debug mode */
debug_mode = 1;
break;
case 'v': /* verbose mode */
_debug_on = 1;
break;
default:
break;
}
}
PL_DestroyOptState(opt);
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
PR_STDIO_INIT();
output = PR_STDERR;
PR_SetConcurrency(4);
/*
** Create the Server thread
*/
DPRINTF( "udpsrv: Creating Server Thread\n" );
srv = PR_CreateThread( PR_USER_THREAD,
UDP_Server,
(void *) 0,
PR_PRIORITY_LOW,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0 );
if ( srv == NULL )
{
if (debug_mode) PR_fprintf(output, "udpsrv: Cannot create server thread\n" );
passed = PR_FALSE;
}
/*
** Give the Server time to Start
*/
DPRINTF( "udpsrv: Pausing to allow Server to start\n" );
PR_Sleep( PR_MillisecondsToInterval(200) );
/*
** Create the Client thread
*/
DPRINTF( "udpsrv: Creating Client Thread\n" );
clt = PR_CreateThread( PR_USER_THREAD,
UDP_Client,
(void *) 0,
PR_PRIORITY_LOW,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0 );
if ( clt == NULL )
{
if (debug_mode) PR_fprintf(output, "udpsrv: Cannot create server thread\n" );
passed = PR_FALSE;
}
/*
**
*/
DPRINTF("udpsrv: Waiting to join Server & Client Threads\n" );
PR_JoinThread( srv );
PR_JoinThread( clt );
/*
** Evaluate test results
*/
if (debug_mode) PR_fprintf(output, "\n\nudpsrv: main(): cltBytesRead(%ld), \
srvBytesRead(%ld), expected(%ld)\n",
cltBytesRead, srvBytesRead, UDP_AMOUNT_TO_WRITE );
if ( cltBytesRead != srvBytesRead || cltBytesRead != UDP_AMOUNT_TO_WRITE )
{
passed = PR_FALSE;
}
PR_Cleanup();
if ( passed )
return 0;
else
return 1;
} /* --- end main() --- */
|