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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
|
/* -*- 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/. */
/***********************************************************************
**
** Name: socket.c
**
** Description: Test socket functionality.
**
** Modification History:
*/
#include "primpl.h"
#include "plgetopt.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef XP_UNIX
#include <sys/mman.h>
#endif
#if defined(_PR_PTHREADS)
#include <pthread.h>
#endif
#ifdef WIN32
#include <process.h>
#endif
static int _debug_on = 0;
static int test_cancelio = 0;
#include "obsolete/prsem.h"
#ifdef XP_PC
#define mode_t int
#endif
#define DPRINTF(arg) if (_debug_on) printf arg
#ifdef XP_PC
char *TEST_DIR = "prdir";
char *SMALL_FILE_NAME = "prsmallf";
char *LARGE_FILE_NAME = "prlargef";
#elif defined(SYMBIAN)
char *TEST_DIR = "c:\\data\\prsocket";
char *SMALL_FILE_NAME = "c:\\data\\prsocket\\small_file";
char *LARGE_FILE_NAME = "c:\\data\\prsocket\\large_file";
#else
char *TEST_DIR = "/tmp/prsocket_test_dir";
char *SMALL_FILE_NAME = "/tmp/prsocket_test_dir/small_file";
char *LARGE_FILE_NAME = "/tmp/prsocket_test_dir/large_file";
#endif
#define SMALL_FILE_SIZE (3 * 1024) /* 3 KB */
#define SMALL_FILE_OFFSET_1 (512)
#define SMALL_FILE_LEN_1 (1 * 1024) /* 1 KB */
#define SMALL_FILE_OFFSET_2 (75)
#define SMALL_FILE_LEN_2 (758)
#define SMALL_FILE_OFFSET_3 (1024)
#define SMALL_FILE_LEN_3 (SMALL_FILE_SIZE - SMALL_FILE_OFFSET_3)
#define SMALL_FILE_HEADER_SIZE (64) /* 64 bytes */
#define SMALL_FILE_TRAILER_SIZE (128) /* 128 bytes */
#define LARGE_FILE_SIZE (3 * 1024 * 1024) /* 3 MB */
#define LARGE_FILE_OFFSET_1 (0)
#define LARGE_FILE_LEN_1 (2 * 1024 * 1024) /* 2 MB */
#define LARGE_FILE_OFFSET_2 (64)
#define LARGE_FILE_LEN_2 (1 * 1024 * 1024 + 75)
#define LARGE_FILE_OFFSET_3 (2 * 1024 * 1024 - 128)
#define LARGE_FILE_LEN_3 (LARGE_FILE_SIZE - LARGE_FILE_OFFSET_3)
#define LARGE_FILE_OFFSET_4 PR_GetPageSize()
#define LARGE_FILE_LEN_4 769
#define LARGE_FILE_HEADER_SIZE (512)
#define LARGE_FILE_TRAILER_SIZE (64)
#define BUF_DATA_SIZE (2 * 1024)
#define TCP_MESG_SIZE 1024
/*
* set UDP datagram size small enough that datagrams sent to a port on the
* local host will not be lost
*/
#define UDP_DGRAM_SIZE 128
#define NUM_TCP_CLIENTS 5 /* for a listen queue depth of 5 */
#define NUM_UDP_CLIENTS 10
#ifdef SYMBIAN
#define NUM_TRANSMITFILE_CLIENTS 1
#else
#define NUM_TRANSMITFILE_CLIENTS 4
#endif
#define NUM_TCP_CONNECTIONS_PER_CLIENT 5
#define NUM_TCP_MESGS_PER_CONNECTION 10
#define NUM_UDP_DATAGRAMS_PER_CLIENT 5
#define TCP_SERVER_PORT 10000
#define UDP_SERVER_PORT TCP_SERVER_PORT
#define SERVER_MAX_BIND_COUNT 100
#ifdef WINCE
#define perror(s)
#endif
static PRInt32 num_tcp_clients = NUM_TCP_CLIENTS;
static PRInt32 num_udp_clients = NUM_UDP_CLIENTS;
static PRInt32 num_transmitfile_clients = NUM_TRANSMITFILE_CLIENTS;
static PRInt32 num_tcp_connections_per_client = NUM_TCP_CONNECTIONS_PER_CLIENT;
static PRInt32 tcp_mesg_size = TCP_MESG_SIZE;
static PRInt32 num_tcp_mesgs_per_connection = NUM_TCP_MESGS_PER_CONNECTION;
static PRInt32 num_udp_datagrams_per_client = NUM_UDP_DATAGRAMS_PER_CLIENT;
static PRInt32 udp_datagram_size = UDP_DGRAM_SIZE;
static PRInt32 thread_count;
PRUint16 server_domain = PR_AF_INET, client_domain = PR_AF_INET;
/* an I/O layer that uses the emulated senfile method */
static PRDescIdentity emuSendFileIdentity;
static PRIOMethods emuSendFileMethods;
int failed_already=0;
typedef struct buffer {
char data[BUF_DATA_SIZE];
} buffer;
PRNetAddr tcp_server_addr, udp_server_addr;
typedef struct Serve_Client_Param {
PRFileDesc *sockfd; /* socket to read from/write to */
PRInt32 datalen; /* bytes of data transfered in each read/write */
} Serve_Client_Param;
typedef struct Server_Param {
PRSemaphore *addr_sem; /* sem to post on, after setting up the address */
PRMonitor *exit_mon; /* monitor to signal on exit */
PRInt32 *exit_counter; /* counter to decrement, before exit */
PRInt32 datalen; /* bytes of data transfered in each read/write */
} Server_Param;
typedef struct Client_Param {
PRNetAddr server_addr;
PRMonitor *exit_mon; /* monitor to signal on exit */
PRInt32 *exit_counter; /* counter to decrement, before exit */
PRInt32 datalen;
PRInt32 udp_connect; /* if set clients connect udp sockets */
} Client_Param;
/* the sendfile method in emuSendFileMethods */
static PRInt32 PR_CALLBACK
emu_SendFile(PRFileDesc *sd, PRSendFileData *sfd,
PRTransmitFileFlags flags, PRIntervalTime timeout)
{
return PR_EmulateSendFile(sd, sfd, flags, timeout);
}
/* the transmitfile method in emuSendFileMethods */
static PRInt32 PR_CALLBACK
emu_TransmitFile(PRFileDesc *sd, PRFileDesc *fd, const void *headers,
PRInt32 hlen, PRTransmitFileFlags flags, PRIntervalTime timeout)
{
PRSendFileData sfd;
sfd.fd = fd;
sfd.file_offset = 0;
sfd.file_nbytes = 0;
sfd.header = headers;
sfd.hlen = hlen;
sfd.trailer = NULL;
sfd.tlen = 0;
return emu_SendFile(sd, &sfd, flags, timeout);
}
/*
* readn
* read data from sockfd into buf
*/
static PRInt32
readn(PRFileDesc *sockfd, char *buf, int len)
{
int rem;
int bytes;
int offset = 0;
int err;
PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT;
if (test_cancelio)
timeout = PR_SecondsToInterval(2);
for (rem=len; rem; offset += bytes, rem -= bytes) {
DPRINTF(("thread = 0x%lx: calling PR_Recv, bytes = %d\n",
PR_GetCurrentThread(), rem));
retry:
bytes = PR_Recv(sockfd, buf + offset, rem, 0,
timeout);
DPRINTF(("thread = 0x%lx: returning from PR_Recv, bytes = %d\n",
PR_GetCurrentThread(), bytes));
if (bytes < 0) {
#ifdef WINNT
printf("PR_Recv: error = %d oserr = %d\n",(err = PR_GetError()),
PR_GetOSError());
if ((test_cancelio) && (err == PR_IO_TIMEOUT_ERROR)) {
if (PR_NT_CancelIo(sockfd) != PR_SUCCESS)
printf("PR_NT_CancelIO: error = %d\n",PR_GetError());
timeout = PR_INTERVAL_NO_TIMEOUT;
goto retry;
}
#endif
return -1;
}
}
return len;
}
/*
* writen
* write data from buf to sockfd
*/
static PRInt32
writen(PRFileDesc *sockfd, char *buf, int len)
{
int rem;
int bytes;
int offset = 0;
for (rem=len; rem; offset += bytes, rem -= bytes) {
DPRINTF(("thread = 0x%lx: calling PR_Send, bytes = %d\n",
PR_GetCurrentThread(), rem));
bytes = PR_Send(sockfd, buf + offset, rem, 0,
PR_INTERVAL_NO_TIMEOUT);
DPRINTF(("thread = 0x%lx: returning from PR_Send, bytes = %d\n",
PR_GetCurrentThread(), bytes));
if (bytes <= 0)
return -1;
}
return len;
}
/*
* Serve_Client
* Thread, started by the server, for serving a client connection.
* Reads data from socket and writes it back, unmodified, and
* closes the socket
*/
static void PR_CALLBACK
Serve_Client(void *arg)
{
Serve_Client_Param *scp = (Serve_Client_Param *) arg;
PRFileDesc *sockfd;
buffer *in_buf;
PRInt32 bytes, j;
sockfd = scp->sockfd;
bytes = scp->datalen;
in_buf = PR_NEW(buffer);
if (in_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
goto exit;
}
for (j = 0; j < num_tcp_mesgs_per_connection; j++) {
/*
* Read data from client and send it back to the client unmodified
*/
if (readn(sockfd, in_buf->data, bytes) < bytes) {
fprintf(stderr,"prsocket_test: ERROR - Serve_Client:readn\n");
failed_already=1;
goto exit;
}
/* Shutdown only RCV will cause error on Symbian OS */
#if !defined(SYMBIAN)
/*
* shutdown reads, after the last read
*/
if (j == num_tcp_mesgs_per_connection - 1)
if (PR_Shutdown(sockfd, PR_SHUTDOWN_RCV) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Shutdown\n");
}
#endif
DPRINTF(("Serve_Client [0x%lx]: inbuf[0] = 0x%lx\n",PR_GetCurrentThread(),
(*((int *) in_buf->data))));
if (writen(sockfd, in_buf->data, bytes) < bytes) {
fprintf(stderr,"prsocket_test: ERROR - Serve_Client:writen\n");
failed_already=1;
goto exit;
}
}
/*
* shutdown reads and writes
*/
if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Shutdown\n");
failed_already=1;
}
exit:
PR_Close(sockfd);
if (in_buf) {
PR_DELETE(in_buf);
}
}
PRThread* create_new_thread(PRThreadType type,
void (*start)(void *arg),
void *arg,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state,
PRUint32 stackSize, PRInt32 index)
{
PRInt32 native_thread = 0;
PR_ASSERT(state == PR_UNJOINABLE_THREAD);
#if defined(_PR_PTHREADS) || defined(WIN32)
switch(index % 4) {
case 0:
scope = (PR_LOCAL_THREAD);
break;
case 1:
scope = (PR_GLOBAL_THREAD);
break;
case 2:
scope = (PR_GLOBAL_BOUND_THREAD);
break;
case 3:
native_thread = 1;
break;
default:
PR_NOT_REACHED("Invalid scope");
break;
}
if (native_thread) {
#if defined(_PR_PTHREADS)
pthread_t tid;
if (!pthread_create(&tid, NULL, (void * (*)(void *)) start, arg))
return((PRThread *) tid);
else
return (NULL);
#else
HANDLE thandle;
unsigned tid;
thandle = (HANDLE) _beginthreadex(
NULL,
stackSize,
(unsigned (__stdcall *)(void *))start,
arg,
STACK_SIZE_PARAM_IS_A_RESERVATION,
&tid);
return((PRThread *) thandle);
#endif
} else {
return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize));
}
#else
return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize));
#endif
}
/*
* TCP Server
* Server Thread
* Bind an address to a socket and listen for incoming connections
* Start a Serve_Client thread for each incoming connection.
*/
static void PR_CALLBACK
TCP_Server(void *arg)
{
PRThread *t;
Server_Param *sp = (Server_Param *) arg;
Serve_Client_Param *scp;
PRFileDesc *sockfd, *newsockfd;
PRNetAddr netaddr;
PRInt32 i;
/*
* Create a tcp socket
*/
if ((sockfd = PR_OpenTCPSocket(server_domain)) == NULL) {
fprintf(stderr,"prsocket_test: PR_NewTCPSocket failed\n");
goto exit;
}
memset(&netaddr, 0 , sizeof(netaddr));
if (PR_SetNetAddr(PR_IpAddrAny, server_domain, TCP_SERVER_PORT,
&netaddr) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: PR_SetNetAddr failed\n");
goto exit;
}
/*
* try a few times to bind server's address, if addresses are in
* use
*/
i = 0;
while (PR_Bind(sockfd, &netaddr) < 0) {
if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
netaddr.inet.port += 2;
if (i++ < SERVER_MAX_BIND_COUNT)
continue;
}
fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n");
perror("PR_Bind");
failed_already=1;
goto exit;
}
if (PR_Listen(sockfd, 32) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Listen failed\n");
failed_already=1;
goto exit;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n");
failed_already=1;
goto exit;
}
DPRINTF(("TCP_Server: PR_BIND netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n",
netaddr.inet.ip, netaddr.inet.port));
if (PR_SetNetAddr(PR_IpAddrLoopback, client_domain,
PR_ntohs(PR_NetAddrInetPort(&netaddr)),
&tcp_server_addr) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: PR_SetNetAddr failed\n");
goto exit;
}
if ((client_domain == PR_AF_INET6) && (server_domain == PR_AF_INET))
PR_ConvertIPv4AddrToIPv6(PR_htonl(INADDR_LOOPBACK),
&tcp_server_addr.ipv6.ip);
/*
* Wake up parent thread because server address is bound and made
* available in the global variable 'tcp_server_addr'
*/
PR_PostSem(sp->addr_sem);
for (i = 0; i < (num_tcp_clients * num_tcp_connections_per_client); i++) {
/* test both null and non-null 'addr' argument to PR_Accept */
PRNetAddr *addrp = (i%2 ? &netaddr: NULL);
DPRINTF(("TCP_Server: Accepting connection\n"));
if ((newsockfd = PR_Accept(sockfd, addrp,
PR_INTERVAL_NO_TIMEOUT)) == NULL) {
fprintf(stderr,"prsocket_test: ERROR - PR_Accept failed\n");
goto exit;
}
DPRINTF(("TCP_Server: Accepted connection\n"));
scp = PR_NEW(Serve_Client_Param);
if (scp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
goto exit;
}
/*
* Start a Serve_Client thread for each incoming connection
*/
scp->sockfd = newsockfd;
scp->datalen = sp->datalen;
t = create_new_thread(PR_USER_THREAD,
Serve_Client, (void *)scp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0, i);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
goto exit;
}
DPRINTF(("TCP_Server: Created Serve_Client = 0x%lx\n", t));
}
exit:
if (sockfd) {
PR_Close(sockfd);
}
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(sp->exit_mon);
--(*sp->exit_counter);
PR_Notify(sp->exit_mon);
PR_ExitMonitor(sp->exit_mon);
DPRINTF(("TCP_Server [0x%lx] exiting\n", PR_GetCurrentThread()));
}
/*
* UDP Server
* Server Thread
* Bind an address to a socket, read data from clients and send data
* back to clients
*/
static void PR_CALLBACK
UDP_Server(void *arg)
{
Server_Param *sp = (Server_Param *) arg;
PRFileDesc *sockfd;
buffer *in_buf;
PRNetAddr netaddr;
PRInt32 bytes, i, rv = 0;
bytes = sp->datalen;
/*
* Create a udp socket
*/
if ((sockfd = PR_OpenUDPSocket(server_domain)) == NULL) {
fprintf(stderr,"prsocket_test: PR_NewUDPSocket failed\n");
failed_already=1;
return;
}
memset(&netaddr, 0 , sizeof(netaddr));
if (PR_SetNetAddr(PR_IpAddrAny, server_domain, UDP_SERVER_PORT,
&netaddr) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: PR_SetNetAddr failed\n");
failed_already=1;
return;
}
/*
* try a few times to bind server's address, if addresses are in
* use
*/
i = 0;
while (PR_Bind(sockfd, &netaddr) < 0) {
if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
netaddr.inet.port += 2;
if (i++ < SERVER_MAX_BIND_COUNT)
continue;
}
fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n");
perror("PR_Bind");
failed_already=1;
return;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n");
failed_already=1;
return;
}
DPRINTF(("PR_Bind: UDP Server netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n",
netaddr.inet.ip, netaddr.inet.port));
/*
* We can't use the IP address returned by PR_GetSockName in
* netaddr.inet.ip because netaddr.inet.ip is returned
* as 0 (= PR_INADDR_ANY).
*/
if (PR_SetNetAddr(PR_IpAddrLoopback, client_domain,
PR_ntohs(PR_NetAddrInetPort(&netaddr)),
&udp_server_addr) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: PR_SetNetAddr failed\n");
failed_already=1;
return;
}
if ((client_domain == PR_AF_INET6) && (server_domain == PR_AF_INET))
PR_ConvertIPv4AddrToIPv6(PR_htonl(INADDR_LOOPBACK),
&udp_server_addr.ipv6.ip);
/*
* Wake up parent thread because server address is bound and made
* available in the global variable 'udp_server_addr'
*/
PR_PostSem(sp->addr_sem);
bytes = sp->datalen;
in_buf = PR_NEW(buffer);
if (in_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
return;
}
/*
* Receive datagrams from clients and send them back, unmodified, to the
* clients
*/
memset(&netaddr, 0 , sizeof(netaddr));
for (i = 0; i < (num_udp_clients * num_udp_datagrams_per_client); i++) {
DPRINTF(("UDP_Server: calling PR_RecvFrom client - ip = 0x%lx, port = %d bytes = %d inbuf = 0x%lx, inbuf[0] = 0x%lx\n",
netaddr.inet.ip, netaddr.inet.port, bytes, in_buf->data,
in_buf->data[0]));
rv = PR_RecvFrom(sockfd, in_buf->data, bytes, 0, &netaddr,
PR_INTERVAL_NO_TIMEOUT);
DPRINTF(("UDP_Server: PR_RecvFrom client - ip = 0x%lx, port = %d bytes = %d inbuf = 0x%lx, inbuf[0] = 0x%lx\n",
netaddr.inet.ip, netaddr.inet.port, rv, in_buf->data,
in_buf->data[0]));
if (rv != bytes) {
return;
}
rv = PR_SendTo(sockfd, in_buf->data, bytes, 0, &netaddr,
PR_INTERVAL_NO_TIMEOUT);
if (rv != bytes) {
return;
}
}
PR_DELETE(in_buf);
PR_Close(sockfd);
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(sp->exit_mon);
--(*sp->exit_counter);
PR_Notify(sp->exit_mon);
PR_ExitMonitor(sp->exit_mon);
DPRINTF(("UDP_Server [0x%x] exiting\n", PR_GetCurrentThread()));
}
/*
* TCP_Client
* Client Thread
* Connect to the server at the address specified in the argument.
* Fill in a buffer, write data to server, read it back and check
* for data corruption.
* Close the socket for server connection
*/
static void PR_CALLBACK
TCP_Client(void *arg)
{
Client_Param *cp = (Client_Param *) arg;
PRFileDesc *sockfd;
buffer *in_buf, *out_buf;
union PRNetAddr netaddr;
PRInt32 bytes, i, j;
bytes = cp->datalen;
out_buf = PR_NEW(buffer);
if (out_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
return;
}
in_buf = PR_NEW(buffer);
if (in_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
return;
}
netaddr = cp->server_addr;
for (i = 0; i < num_tcp_connections_per_client; i++) {
if ((sockfd = PR_OpenTCPSocket(client_domain)) == NULL) {
fprintf(stderr,"prsocket_test: PR_OpenTCPSocket failed\n");
failed_already=1;
return;
}
if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){
fprintf(stderr, "PR_Connect failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
return;
}
for (j = 0; j < num_tcp_mesgs_per_connection; j++) {
/*
* fill in random data
*/
memset(out_buf->data, ((PRInt32) (&netaddr)) + i + j, bytes);
/*
* write to server
*/
#ifdef WINNT
if (test_cancelio && (j == 0))
PR_Sleep(PR_SecondsToInterval(12));
#endif
if (writen(sockfd, out_buf->data, bytes) < bytes) {
fprintf(stderr,"prsocket_test: ERROR - TCP_Client:writen\n");
failed_already=1;
return;
}
DPRINTF(("TCP Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n",
PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data))));
if (readn(sockfd, in_buf->data, bytes) < bytes) {
fprintf(stderr,"prsocket_test: ERROR - TCP_Client:readn\n");
failed_already=1;
return;
}
/*
* verify the data read
*/
if (memcmp(in_buf->data, out_buf->data, bytes) != 0) {
fprintf(stderr,"prsocket_test: ERROR - data corruption\n");
failed_already=1;
return;
}
}
/*
* shutdown reads and writes
*/
if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Shutdown\n");
#if defined(SYMBIAN)
if (EPIPE != errno)
#endif
failed_already=1;
}
PR_Close(sockfd);
}
PR_DELETE(out_buf);
PR_DELETE(in_buf);
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(cp->exit_mon);
--(*cp->exit_counter);
PR_Notify(cp->exit_mon);
PR_ExitMonitor(cp->exit_mon);
DPRINTF(("TCP_Client [0x%x] exiting\n", PR_GetCurrentThread()));
}
/*
* UDP_Client
* Client Thread
* Create a socket and bind an address
* Communicate with the server at the address specified in the argument.
* Fill in a buffer, write data to server, read it back and check
* for data corruption.
* Close the socket
*/
static void PR_CALLBACK
UDP_Client(void *arg)
{
Client_Param *cp = (Client_Param *) arg;
PRFileDesc *sockfd;
buffer *in_buf, *out_buf;
union PRNetAddr netaddr;
PRInt32 bytes, i, rv;
bytes = cp->datalen;
out_buf = PR_NEW(buffer);
if (out_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
return;
}
in_buf = PR_NEW(buffer);
if (in_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n");
failed_already=1;
return;
}
if ((sockfd = PR_OpenUDPSocket(client_domain)) == NULL) {
fprintf(stderr,"prsocket_test: PR_OpenUDPSocket failed\n");
failed_already=1;
return;
}
/*
* bind an address for the client, let the system chose the port
* number
*/
memset(&netaddr, 0 , sizeof(netaddr));
if (PR_SetNetAddr(PR_IpAddrAny, client_domain, 0,
&netaddr) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: PR_SetNetAddr failed\n");
failed_already=1;
return;
}
if (PR_Bind(sockfd, &netaddr) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n");
perror("PR_Bind");
return;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n");
failed_already=1;
return;
}
DPRINTF(("PR_Bind: UDP Client netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n",
netaddr.inet.ip, netaddr.inet.port));
netaddr = cp->server_addr;
if (cp->udp_connect) {
if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){
fprintf(stderr,"prsocket_test: PR_Connect failed\n");
failed_already=1;
return;
}
}
for (i = 0; i < num_udp_datagrams_per_client; i++) {
/*
* fill in random data
*/
DPRINTF(("UDP_Client [0x%lx]: out_buf = 0x%lx bytes = 0x%lx\n",
PR_GetCurrentThread(), out_buf->data, bytes));
memset(out_buf->data, ((PRInt32) (&netaddr)) + i, bytes);
/*
* write to server
*/
if (cp->udp_connect)
rv = PR_Send(sockfd, out_buf->data, bytes, 0,
PR_INTERVAL_NO_TIMEOUT);
else
rv = PR_SendTo(sockfd, out_buf->data, bytes, 0, &netaddr,
PR_INTERVAL_NO_TIMEOUT);
if (rv != bytes) {
return;
}
DPRINTF(("UDP_Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n",
PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data))));
if (cp->udp_connect)
rv = PR_Recv(sockfd, in_buf->data, bytes, 0,
PR_INTERVAL_NO_TIMEOUT);
else
rv = PR_RecvFrom(sockfd, in_buf->data, bytes, 0, &netaddr,
PR_INTERVAL_NO_TIMEOUT);
if (rv != bytes) {
return;
}
DPRINTF(("UDP_Client [0x%lx]: in_buf = 0x%lx in_buf[0] = 0x%lx\n",
PR_GetCurrentThread(), in_buf, (*((int *) in_buf->data))));
/*
* verify the data read
*/
if (memcmp(in_buf->data, out_buf->data, bytes) != 0) {
fprintf(stderr,"prsocket_test: ERROR - UDP data corruption\n");
failed_already=1;
return;
}
}
PR_Close(sockfd);
PR_DELETE(in_buf);
PR_DELETE(out_buf);
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(cp->exit_mon);
--(*cp->exit_counter);
PR_Notify(cp->exit_mon);
PR_ExitMonitor(cp->exit_mon);
PR_DELETE(cp);
DPRINTF(("UDP_Client [0x%x] exiting\n", PR_GetCurrentThread()));
}
/*
* TCP_Socket_Client_Server_Test - concurrent server test
*
* One server and several clients are started
* Each client connects to the server and sends a chunk of data
* For each connection, server starts another thread to read the data
* from the client and send it back to the client, unmodified.
* Each client checks that data received from server is same as the
* data it sent to the server.
*
*/
static PRInt32
TCP_Socket_Client_Server_Test(void)
{
int i;
PRThread *t;
PRSemaphore *server_sem;
Server_Param *sparamp;
Client_Param *cparamp;
PRMonitor *mon2;
PRInt32 datalen;
datalen = tcp_mesg_size;
thread_count = 0;
/*
* start the server thread
*/
sparamp = PR_NEW(Server_Param);
if (sparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
return -1;
}
server_sem = PR_NewSem(0);
if (server_sem == NULL) {
fprintf(stderr,"prsocket_test: PR_NewSem failed\n");
failed_already=1;
return -1;
}
mon2 = PR_NewMonitor();
if (mon2 == NULL) {
fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n");
failed_already=1;
return -1;
}
PR_EnterMonitor(mon2);
sparamp->addr_sem = server_sem;
sparamp->exit_mon = mon2;
sparamp->exit_counter = &thread_count;
sparamp->datalen = datalen;
t = PR_CreateThread(PR_USER_THREAD,
TCP_Server, (void *)sparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
return -1;
}
DPRINTF(("Created TCP server = 0x%lx\n", t));
thread_count++;
/*
* wait till the server address is setup
*/
PR_WaitSem(server_sem);
/*
* Now start a bunch of client threads
*/
cparamp = PR_NEW(Client_Param);
if (cparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
return -1;
}
cparamp->server_addr = tcp_server_addr;
cparamp->exit_mon = mon2;
cparamp->exit_counter = &thread_count;
cparamp->datalen = datalen;
for (i = 0; i < num_tcp_clients; i++) {
t = create_new_thread(PR_USER_THREAD,
TCP_Client, (void *) cparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0, i);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
return -1;
}
DPRINTF(("Created TCP client = 0x%lx\n", t));
thread_count++;
}
/* Wait for server and client threads to exit */
while (thread_count) {
PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
DPRINTF(("TCP Server - thread_count = %d\n", thread_count));
}
PR_ExitMonitor(mon2);
printf("%30s","TCP_Socket_Client_Server_Test:");
printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l,
num_tcp_clients, num_tcp_connections_per_client);
printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":",
num_tcp_mesgs_per_connection, tcp_mesg_size);
return 0;
}
/*
* UDP_Socket_Client_Server_Test - iterative server test
*
* One server and several clients are started
* Each client connects to the server and sends a chunk of data
* For each connection, server starts another thread to read the data
* from the client and send it back to the client, unmodified.
* Each client checks that data received from server is same as the
* data it sent to the server.
*
*/
static PRInt32
UDP_Socket_Client_Server_Test(void)
{
int i;
PRThread *t;
PRSemaphore *server_sem;
Server_Param *sparamp;
Client_Param *cparamp;
PRMonitor *mon2;
PRInt32 datalen;
PRInt32 udp_connect = 1;
datalen = udp_datagram_size;
thread_count = 0;
/*
* start the server thread
*/
sparamp = PR_NEW(Server_Param);
if (sparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
return -1;
}
server_sem = PR_NewSem(0);
if (server_sem == NULL) {
fprintf(stderr,"prsocket_test: PR_NewSem failed\n");
failed_already=1;
return -1;
}
mon2 = PR_NewMonitor();
if (mon2 == NULL) {
fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n");
failed_already=1;
return -1;
}
PR_EnterMonitor(mon2);
sparamp->addr_sem = server_sem;
sparamp->exit_mon = mon2;
sparamp->exit_counter = &thread_count;
sparamp->datalen = datalen;
DPRINTF(("Creating UDP server"));
t = PR_CreateThread(PR_USER_THREAD,
UDP_Server, (void *)sparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
return -1;
}
thread_count++;
/*
* wait till the server address is setup
*/
PR_WaitSem(server_sem);
/*
* Now start a bunch of client threads
*/
for (i = 0; i < num_udp_clients; i++) {
cparamp = PR_NEW(Client_Param);
if (cparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
return -1;
}
cparamp->server_addr = udp_server_addr;
cparamp->exit_mon = mon2;
cparamp->exit_counter = &thread_count;
cparamp->datalen = datalen;
/*
* Cause every other client thread to connect udp sockets
*/
cparamp->udp_connect = udp_connect;
if (udp_connect)
udp_connect = 0;
else
udp_connect = 1;
DPRINTF(("Creating UDP client %d\n", i));
t = PR_CreateThread(PR_USER_THREAD,
UDP_Client, (void *) cparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
return -1;
}
thread_count++;
}
/* Wait for server and client threads to exit */
while (thread_count) {
PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
DPRINTF(("UDP Server - thread_count = %d\n", thread_count));
}
PR_ExitMonitor(mon2);
printf("%30s","UDP_Socket_Client_Server_Test: ");
printf("%2ld Server %2ld Clients\n",1l, num_udp_clients);
printf("%30s %2ld datagrams_per_client %4ld bytes_per_datagram\n",":",
num_udp_datagrams_per_client, udp_datagram_size);
return 0;
}
static PRFileDesc *small_file_fd, *large_file_fd;
static void *small_file_addr, *small_file_header, *large_file_addr;
static void *small_file_trailer, *large_file_header, *large_file_trailer;
/*
* TransmitFile_Client
* Client Thread
*/
static void
TransmitFile_Client(void *arg)
{
PRFileDesc *sockfd;
union PRNetAddr netaddr;
char *small_buf, *large_buf;
Client_Param *cp = (Client_Param *) arg;
PRInt32 rlen;
small_buf = (char*)PR_Malloc(SMALL_FILE_SIZE + SMALL_FILE_HEADER_SIZE +
SMALL_FILE_TRAILER_SIZE);
if (small_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer\n");
failed_already=1;
return;
}
large_buf = (char*)PR_Malloc(LARGE_FILE_SIZE + LARGE_FILE_HEADER_SIZE +
LARGE_FILE_TRAILER_SIZE);
if (large_buf == NULL) {
fprintf(stderr,"prsocket_test: failed to alloc buffer\n");
failed_already=1;
return;
}
netaddr.inet.family = cp->server_addr.inet.family;
netaddr.inet.port = cp->server_addr.inet.port;
netaddr.inet.ip = cp->server_addr.inet.ip;
if ((sockfd = PR_NewTCPSocket()) == NULL) {
fprintf(stderr,"prsocket_test: PR_NewTCPSocket failed\n");
failed_already=1;
return;
}
if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){
fprintf(stderr,"prsocket_test: PR_Connect failed\n");
failed_already=1;
return;
}
/*
* read the small file and verify the data
*/
if (readn(sockfd, small_buf, SMALL_FILE_SIZE + SMALL_FILE_HEADER_SIZE)
!= (SMALL_FILE_SIZE + SMALL_FILE_HEADER_SIZE)) {
fprintf(stderr,
"prsocket_test: TransmitFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
/* File transmission test can not be done because of large file's size */
if (memcmp(small_file_header, small_buf, SMALL_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"prsocket_test: TransmitFile_Client ERROR - small file header data corruption\n");
failed_already=1;
return;
}
if (memcmp(small_file_addr, small_buf + SMALL_FILE_HEADER_SIZE,
SMALL_FILE_SIZE) != 0) {
fprintf(stderr,
"prsocket_test: TransmitFile_Client ERROR - small file data corruption\n");
failed_already=1;
return;
}
#endif
/*
* read the large file and verify the data
*/
if (readn(sockfd, large_buf, LARGE_FILE_SIZE) != LARGE_FILE_SIZE) {
fprintf(stderr,
"prsocket_test: TransmitFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(large_file_addr, large_buf, LARGE_FILE_SIZE) != 0) {
fprintf(stderr,
"prsocket_test: TransmitFile_Client ERROR - large file data corruption\n");
failed_already=1;
}
#endif
/*
* receive data from PR_SendFile
*/
/*
* case 1: small file with header and trailer
*/
rlen = SMALL_FILE_SIZE + SMALL_FILE_HEADER_SIZE +
SMALL_FILE_TRAILER_SIZE;
if (readn(sockfd, small_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(small_file_header, small_buf, SMALL_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 1. ERROR - small file header corruption\n");
failed_already=1;
return;
}
if (memcmp(small_file_addr, small_buf + SMALL_FILE_HEADER_SIZE,
SMALL_FILE_SIZE) != 0) {
fprintf(stderr,
"SendFile 1. ERROR - small file data corruption\n");
failed_already=1;
return;
}
if (memcmp(small_file_trailer,
small_buf + SMALL_FILE_HEADER_SIZE + SMALL_FILE_SIZE,
SMALL_FILE_TRAILER_SIZE) != 0) {
fprintf(stderr,
"SendFile 1. ERROR - small file trailer corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 2: partial large file at zero offset, file with header and trailer
*/
rlen = LARGE_FILE_LEN_1 + LARGE_FILE_HEADER_SIZE +
LARGE_FILE_TRAILER_SIZE;
if (readn(sockfd, large_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(large_file_header, large_buf, LARGE_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 2. ERROR - large file header corruption\n");
failed_already=1;
return;
}
if (memcmp(large_file_addr, large_buf + LARGE_FILE_HEADER_SIZE,
LARGE_FILE_LEN_1) != 0) {
fprintf(stderr,
"SendFile 2. ERROR - large file data corruption\n");
failed_already=1;
return;
}
if (memcmp(large_file_trailer,
large_buf + LARGE_FILE_HEADER_SIZE + LARGE_FILE_LEN_1,
LARGE_FILE_TRAILER_SIZE) != 0) {
fprintf(stderr,
"SendFile 2. ERROR - large file trailer corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 3: partial small file at non-zero offset, with header
*/
rlen = SMALL_FILE_LEN_1 + SMALL_FILE_HEADER_SIZE;
if (readn(sockfd, small_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(small_file_header, small_buf, SMALL_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 3. ERROR - small file header corruption\n");
failed_already=1;
return;
}
if (memcmp((char *) small_file_addr + SMALL_FILE_OFFSET_1,
small_buf + SMALL_FILE_HEADER_SIZE, SMALL_FILE_LEN_1) != 0) {
fprintf(stderr,
"SendFile 3. ERROR - small file data corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 4: partial small file at non-zero offset, with trailer
*/
rlen = SMALL_FILE_LEN_2 + SMALL_FILE_TRAILER_SIZE;
if (readn(sockfd, small_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp((char *) small_file_addr + SMALL_FILE_OFFSET_2, small_buf,
SMALL_FILE_LEN_2) != 0) {
fprintf(stderr,
"SendFile 4. ERROR - small file data corruption\n");
failed_already=1;
return;
}
if (memcmp(small_file_trailer, small_buf + SMALL_FILE_LEN_2,
SMALL_FILE_TRAILER_SIZE) != 0) {
fprintf(stderr,
"SendFile 4. ERROR - small file trailer corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 5: partial large file at non-zero offset, file with header
*/
rlen = LARGE_FILE_LEN_2 + LARGE_FILE_HEADER_SIZE;
if (readn(sockfd, large_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(large_file_header, large_buf, LARGE_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 5. ERROR - large file header corruption\n");
failed_already=1;
return;
}
if (memcmp((char *)large_file_addr + LARGE_FILE_OFFSET_2,
large_buf + LARGE_FILE_HEADER_SIZE,
LARGE_FILE_LEN_2) != 0) {
fprintf(stderr,
"SendFile 5. ERROR - large file data corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 6: partial small file at non-zero offset, with header
*/
rlen = SMALL_FILE_LEN_3 + SMALL_FILE_HEADER_SIZE;
if (readn(sockfd, small_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(small_file_header, small_buf, SMALL_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 6. ERROR - small file header corruption\n");
return;
}
if (memcmp((char *) small_file_addr + SMALL_FILE_OFFSET_3,
small_buf + SMALL_FILE_HEADER_SIZE, SMALL_FILE_LEN_3) != 0) {
#if 0
char *i, *j;
int k;
i = (char *) small_file_addr + SMALL_FILE_OFFSET_3;
j = small_buf + SMALL_FILE_HEADER_SIZE;
k = SMALL_FILE_LEN_3;
while (k-- > 0) {
if (*i++ != *j++)
printf("i = %d j = %d\n",
(int) (i - ((char *) small_file_addr + SMALL_FILE_OFFSET_3)),
(int) (j - (small_buf + SMALL_FILE_HEADER_SIZE)));
}
#endif
fprintf(stderr,
"SendFile 6. ERROR - small file data corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 7: partial large file at non-zero offset, with header
*/
rlen = LARGE_FILE_LEN_3 + LARGE_FILE_HEADER_SIZE;
if (readn(sockfd, large_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(large_file_header, large_buf, LARGE_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 7. ERROR - large file header corruption\n");
failed_already=1;
return;
}
if (memcmp((char *)large_file_addr + LARGE_FILE_OFFSET_3,
large_buf + LARGE_FILE_HEADER_SIZE,
LARGE_FILE_LEN_3) != 0) {
fprintf(stderr,
"SendFile 7. ERROR - large file data corruption\n");
failed_already=1;
return;
}
#endif
/*
* case 8: partial large file at non-zero, page-aligned offset, with
* header and trailer
*/
rlen = LARGE_FILE_LEN_4 + LARGE_FILE_HEADER_SIZE +
LARGE_FILE_TRAILER_SIZE;
if (readn(sockfd, large_buf, rlen) != rlen) {
fprintf(stderr,
"prsocket_test: SendFile_Client failed to receive file\n");
failed_already=1;
return;
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
if (memcmp(large_file_header, large_buf, LARGE_FILE_HEADER_SIZE) != 0){
fprintf(stderr,
"SendFile 2. ERROR - large file header corruption\n");
failed_already=1;
return;
}
if (memcmp((char *)large_file_addr + LARGE_FILE_OFFSET_4,
large_buf + LARGE_FILE_HEADER_SIZE,
LARGE_FILE_LEN_4) != 0) {
fprintf(stderr,
"SendFile 2. ERROR - large file data corruption\n");
failed_already=1;
return;
}
if (memcmp(large_file_trailer,
large_buf + LARGE_FILE_HEADER_SIZE + LARGE_FILE_LEN_4,
LARGE_FILE_TRAILER_SIZE) != 0) {
fprintf(stderr,
"SendFile 2. ERROR - large file trailer corruption\n");
failed_already=1;
return;
}
#endif
PR_DELETE(small_buf);
PR_DELETE(large_buf);
PR_Close(sockfd);
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(cp->exit_mon);
--(*cp->exit_counter);
PR_Notify(cp->exit_mon);
PR_ExitMonitor(cp->exit_mon);
DPRINTF(("TransmitFile_Client [0x%lx] exiting\n", PR_GetCurrentThread()));
}
/*
* Serve_TransmitFile_Client
* Thread, started by the server, for serving a client connection.
* Trasmits a small file, with a header, and a large file, without
* a header
*/
static void
Serve_TransmitFile_Client(void *arg)
{
Serve_Client_Param *scp = (Serve_Client_Param *) arg;
PRFileDesc *sockfd;
PRInt32 bytes;
PRFileDesc *local_small_file_fd=NULL;
PRFileDesc *local_large_file_fd=NULL;
PRSendFileData sfd;
PRInt32 slen;
sockfd = scp->sockfd;
local_small_file_fd = PR_Open(SMALL_FILE_NAME, PR_RDONLY,0);
if (local_small_file_fd == NULL) {
fprintf(stderr,"prsocket_test failed to open file for transmitting %s\n",
SMALL_FILE_NAME);
failed_already=1;
goto done;
}
local_large_file_fd = PR_Open(LARGE_FILE_NAME, PR_RDONLY,0);
if (local_large_file_fd == NULL) {
fprintf(stderr,"prsocket_test failed to open file for transmitting %s\n",
LARGE_FILE_NAME);
failed_already=1;
goto done;
}
bytes = PR_TransmitFile(sockfd, local_small_file_fd, small_file_header,
SMALL_FILE_HEADER_SIZE, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
if (bytes != (SMALL_FILE_SIZE+ SMALL_FILE_HEADER_SIZE)) {
fprintf(stderr,
"prsocet_test: PR_TransmitFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
bytes = PR_TransmitFile(sockfd, local_large_file_fd, NULL, 0,
PR_TRANSMITFILE_KEEP_OPEN, PR_INTERVAL_NO_TIMEOUT);
if (bytes != LARGE_FILE_SIZE) {
fprintf(stderr,
"prsocket_test: PR_TransmitFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* PR_SendFile test cases
*/
/*
* case 1: small file with header and trailer
*/
sfd.fd = local_small_file_fd;
sfd.file_offset = 0;
sfd.file_nbytes = 0;
sfd.header = small_file_header;
sfd.hlen = SMALL_FILE_HEADER_SIZE;
sfd.trailer = small_file_trailer;
sfd.tlen = SMALL_FILE_TRAILER_SIZE;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = SMALL_FILE_SIZE+ SMALL_FILE_HEADER_SIZE +
SMALL_FILE_TRAILER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 1. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 2: partial large file at zero offset, file with header and trailer
*/
sfd.fd = local_large_file_fd;
sfd.file_offset = 0;
sfd.file_nbytes = LARGE_FILE_LEN_1;
sfd.header = large_file_header;
sfd.hlen = LARGE_FILE_HEADER_SIZE;
sfd.trailer = large_file_trailer;
sfd.tlen = LARGE_FILE_TRAILER_SIZE;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = LARGE_FILE_LEN_1 + LARGE_FILE_HEADER_SIZE +
LARGE_FILE_TRAILER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 2. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 3: partial small file at non-zero offset, with header
*/
sfd.fd = local_small_file_fd;
sfd.file_offset = SMALL_FILE_OFFSET_1;
sfd.file_nbytes = SMALL_FILE_LEN_1;
sfd.header = small_file_header;
sfd.hlen = SMALL_FILE_HEADER_SIZE;
sfd.trailer = NULL;
sfd.tlen = 0;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = SMALL_FILE_LEN_1 + SMALL_FILE_HEADER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 3. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 4: partial small file at non-zero offset, with trailer
*/
sfd.fd = local_small_file_fd;
sfd.file_offset = SMALL_FILE_OFFSET_2;
sfd.file_nbytes = SMALL_FILE_LEN_2;
sfd.header = NULL;
sfd.hlen = 0;
sfd.trailer = small_file_trailer;
sfd.tlen = SMALL_FILE_TRAILER_SIZE;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = SMALL_FILE_LEN_2 + SMALL_FILE_TRAILER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 4. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 5: partial large file at non-zero offset, file with header
*/
sfd.fd = local_large_file_fd;
sfd.file_offset = LARGE_FILE_OFFSET_2;
sfd.file_nbytes = LARGE_FILE_LEN_2;
sfd.header = large_file_header;
sfd.hlen = LARGE_FILE_HEADER_SIZE;
sfd.trailer = NULL;
sfd.tlen = 0;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = LARGE_FILE_LEN_2 + LARGE_FILE_HEADER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 5. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 6: partial small file from non-zero offset till end of file, with header
*/
sfd.fd = local_small_file_fd;
sfd.file_offset = SMALL_FILE_OFFSET_3;
sfd.file_nbytes = 0; /* data from offset to end-of-file */
sfd.header = small_file_header;
sfd.hlen = SMALL_FILE_HEADER_SIZE;
sfd.trailer = NULL;
sfd.tlen = 0;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = SMALL_FILE_LEN_3 + SMALL_FILE_HEADER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 6. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 7: partial large file at non-zero offset till end-of-file, with header
*/
sfd.fd = local_large_file_fd;
sfd.file_offset = LARGE_FILE_OFFSET_3;
sfd.file_nbytes = 0; /* data until end-of-file */
sfd.header = large_file_header;
sfd.hlen = LARGE_FILE_HEADER_SIZE;
sfd.trailer = NULL;
sfd.tlen = 0;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
PR_INTERVAL_NO_TIMEOUT);
slen = LARGE_FILE_LEN_3 + LARGE_FILE_HEADER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 7. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
/*
* case 8: partial large file at non-zero page-aligned offset,
* with header and trailer
*/
sfd.fd = local_large_file_fd;
sfd.file_offset = LARGE_FILE_OFFSET_4;
sfd.file_nbytes = LARGE_FILE_LEN_4;
sfd.header = large_file_header;
sfd.hlen = LARGE_FILE_HEADER_SIZE;
sfd.trailer = large_file_trailer;
sfd.tlen = LARGE_FILE_TRAILER_SIZE;
bytes = PR_SendFile(sockfd, &sfd, PR_TRANSMITFILE_CLOSE_SOCKET,
PR_INTERVAL_NO_TIMEOUT);
slen = LARGE_FILE_LEN_4 + LARGE_FILE_HEADER_SIZE +
LARGE_FILE_TRAILER_SIZE;
if (bytes != slen) {
fprintf(stderr,
"socket: Error - 2. PR_SendFile send_size = %d, bytes sent = %d\n",
slen, bytes);
fprintf(stderr,
"prsocket_test: PR_SendFile failed: (%ld, %ld)\n",
PR_GetError(), PR_GetOSError());
failed_already=1;
}
done:
if (local_small_file_fd != NULL)
PR_Close(local_small_file_fd);
if (local_large_file_fd != NULL)
PR_Close(local_large_file_fd);
}
/*
* TransmitFile Server
* Server Thread
* Bind an address to a socket and listen for incoming connections
* Create worker threads to service clients
*/
static void
TransmitFile_Server(void *arg)
{
PRThread **t = NULL; /* an array of PRThread pointers */
Server_Param *sp = (Server_Param *) arg;
Serve_Client_Param *scp;
PRFileDesc *sockfd = NULL, *newsockfd;
PRNetAddr netaddr;
PRInt32 i;
t = (PRThread**)PR_MALLOC(num_transmitfile_clients * sizeof(PRThread *));
if (t == NULL) {
fprintf(stderr, "prsocket_test: run out of memory\n");
failed_already=1;
goto exit;
}
/*
* Create a tcp socket
*/
if ((sockfd = PR_OpenTCPSocket(PR_AF_INET)) == NULL) {
fprintf(stderr,"prsocket_test: PR_OpenTCPSocket failed\n");
failed_already=1;
goto exit;
}
memset(&netaddr, 0 , sizeof(netaddr));
netaddr.inet.family = PR_AF_INET;
netaddr.inet.port = PR_htons(TCP_SERVER_PORT);
netaddr.inet.ip = PR_htonl(PR_INADDR_ANY);
/*
* try a few times to bind server's address, if addresses are in
* use
*/
i = 0;
while (PR_Bind(sockfd, &netaddr) < 0) {
if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
netaddr.inet.port += 2;
if (i++ < SERVER_MAX_BIND_COUNT)
continue;
}
fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n");
failed_already=1;
perror("PR_Bind");
goto exit;
}
if (PR_Listen(sockfd, 32) < 0) {
fprintf(stderr,"prsocket_test: ERROR - PR_Listen failed\n");
failed_already=1;
goto exit;
}
if (PR_GetSockName(sockfd, &netaddr) < 0) {
fprintf(stderr,
"prsocket_test: ERROR - PR_GetSockName failed\n");
failed_already=1;
goto exit;
}
DPRINTF(("TCP_Server: PR_BIND netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n",
netaddr.inet.ip, netaddr.inet.port));
tcp_server_addr.inet.family = netaddr.inet.family;
tcp_server_addr.inet.port = netaddr.inet.port;
tcp_server_addr.inet.ip = netaddr.inet.ip;
/*
* Wake up parent thread because server address is bound and made
* available in the global variable 'tcp_server_addr'
*/
PR_PostSem(sp->addr_sem);
for (i = 0; i < num_transmitfile_clients ; i++) {
/* test both null and non-null 'addr' argument to PR_Accept */
PRNetAddr *addrp = (i%2 ? &netaddr: NULL);
if ((newsockfd = PR_Accept(sockfd, addrp,
PR_INTERVAL_NO_TIMEOUT)) == NULL) {
fprintf(stderr,
"prsocket_test: ERROR - PR_Accept failed\n");
failed_already=1;
goto exit;
}
/* test both regular and emulated PR_SendFile */
if (i%2) {
PRFileDesc *layer = PR_CreateIOLayerStub(
emuSendFileIdentity, &emuSendFileMethods);
if (layer == NULL) {
fprintf(stderr,
"prsocket_test: ERROR - PR_CreateIOLayerStub failed\n");
failed_already=1;
goto exit;
}
if (PR_PushIOLayer(newsockfd, PR_TOP_IO_LAYER, layer)
== PR_FAILURE) {
fprintf(stderr,
"prsocket_test: ERROR - PR_PushIOLayer failed\n");
failed_already=1;
goto exit;
}
}
scp = PR_NEW(Serve_Client_Param);
if (scp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
goto exit;
}
/*
* Start a Serve_Client thread for each incoming connection
*/
scp->sockfd = newsockfd;
scp->datalen = sp->datalen;
t[i] = PR_CreateThread(PR_USER_THREAD,
Serve_TransmitFile_Client, (void *)scp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_JOINABLE_THREAD,
0);
if (t[i] == NULL) {
fprintf(stderr,
"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
goto exit;
}
DPRINTF(("TransmitFile_Server: Created Serve_TransmitFile_Client = 0x%lx\n", t));
}
/*
* Wait for all the worker threads to end, so that we know
* they are no longer using the small and large file fd's.
*/
for (i = 0; i < num_transmitfile_clients; i++) {
PR_JoinThread(t[i]);
}
exit:
if (t) {
PR_DELETE(t);
}
if (sockfd) {
PR_Close(sockfd);
}
/*
* Decrement exit_counter and notify parent thread
*/
PR_EnterMonitor(sp->exit_mon);
--(*sp->exit_counter);
PR_Notify(sp->exit_mon);
PR_ExitMonitor(sp->exit_mon);
DPRINTF(("TransmitFile_Server [0x%lx] exiting\n", PR_GetCurrentThread()));
}
/*
* Socket_Misc_Test - test miscellaneous functions
*
*/
static PRInt32
Socket_Misc_Test(void)
{
PRIntn i, rv = 0, bytes, count, len;
PRThread *t;
PRSemaphore *server_sem;
Server_Param *sparamp;
Client_Param *cparamp;
PRMonitor *mon2;
PRInt32 datalen;
/*
* We deliberately pick a buffer size that is not a nice multiple
* of 1024.
*/
#define TRANSMITFILE_BUF_SIZE (4 * 1024 - 11)
typedef struct {
char data[TRANSMITFILE_BUF_SIZE];
} file_buf;
file_buf *buf = NULL;
/*
* create file(s) to be transmitted
*/
if ((PR_MkDir(TEST_DIR, 0777)) < 0) {
printf("prsocket_test failed to create dir %s\n",TEST_DIR);
failed_already=1;
return -1;
}
small_file_fd = PR_Open(SMALL_FILE_NAME, PR_RDWR | PR_CREATE_FILE,0777);
if (small_file_fd == NULL) {
fprintf(stderr,"prsocket_test failed to create/open file %s\n",
SMALL_FILE_NAME);
failed_already=1;
rv = -1;
goto done;
}
buf = PR_NEW(file_buf);
if (buf == NULL) {
fprintf(stderr,"prsocket_test failed to allocate buffer\n");
failed_already=1;
rv = -1;
goto done;
}
/*
* fill in random data
*/
for (i = 0; i < TRANSMITFILE_BUF_SIZE; i++) {
buf->data[i] = i;
}
count = 0;
do {
len = (SMALL_FILE_SIZE - count) > TRANSMITFILE_BUF_SIZE ?
TRANSMITFILE_BUF_SIZE : (SMALL_FILE_SIZE - count);
bytes = PR_Write(small_file_fd, buf->data, len);
if (bytes <= 0) {
fprintf(stderr,
"prsocket_test failed to write to file %s\n",
SMALL_FILE_NAME);
failed_already=1;
rv = -1;
goto done;
}
count += bytes;
} while (count < SMALL_FILE_SIZE);
#ifdef XP_UNIX
/*
* map the small file; used in checking for data corruption
*/
small_file_addr = mmap(0, SMALL_FILE_SIZE, PROT_READ,
MAP_SHARED, small_file_fd->secret->md.osfd, 0);
if (small_file_addr == (void *) -1) {
fprintf(stderr,"prsocket_test failed to mmap file %s\n",
SMALL_FILE_NAME);
failed_already=1;
rv = -1;
goto done;
}
#endif
/*
* header for small file
*/
small_file_header = PR_MALLOC(SMALL_FILE_HEADER_SIZE);
if (small_file_header == NULL) {
fprintf(stderr,"prsocket_test failed to malloc header file\n");
failed_already=1;
rv = -1;
goto done;
}
memset(small_file_header, (int) PR_IntervalNow(),
SMALL_FILE_HEADER_SIZE);
/*
* trailer for small file
*/
small_file_trailer = PR_MALLOC(SMALL_FILE_TRAILER_SIZE);
if (small_file_trailer == NULL) {
fprintf(stderr,"prsocket_test failed to malloc header trailer\n");
failed_already=1;
rv = -1;
goto done;
}
memset(small_file_trailer, (int) PR_IntervalNow(),
SMALL_FILE_TRAILER_SIZE);
/*
* setup large file
*/
large_file_fd = PR_Open(LARGE_FILE_NAME, PR_RDWR | PR_CREATE_FILE,0777);
if (large_file_fd == NULL) {
fprintf(stderr,"prsocket_test failed to create/open file %s\n",
LARGE_FILE_NAME);
failed_already=1;
rv = -1;
goto done;
}
/*
* fill in random data
*/
for (i = 0; i < TRANSMITFILE_BUF_SIZE; i++) {
buf->data[i] = i;
}
count = 0;
do {
len = (LARGE_FILE_SIZE - count) > TRANSMITFILE_BUF_SIZE ?
TRANSMITFILE_BUF_SIZE : (LARGE_FILE_SIZE - count);
bytes = PR_Write(large_file_fd, buf->data, len);
if (bytes <= 0) {
fprintf(stderr,
"prsocket_test failed to write to file %s: (%ld, %ld)\n",
LARGE_FILE_NAME,
PR_GetError(), PR_GetOSError());
failed_already=1;
rv = -1;
goto done;
}
count += bytes;
} while (count < LARGE_FILE_SIZE);
#if defined(XP_UNIX) && !defined(SYMBIAN)
/*
* map the large file; used in checking for data corruption
*/
large_file_addr = mmap(0, LARGE_FILE_SIZE, PROT_READ,
MAP_SHARED, large_file_fd->secret->md.osfd, 0);
if (large_file_addr == (void *) -1) {
fprintf(stderr,"prsocket_test failed to mmap file %s\n",
LARGE_FILE_NAME);
failed_already=1;
rv = -1;
goto done;
}
#endif
/*
* header for large file
*/
large_file_header = PR_MALLOC(LARGE_FILE_HEADER_SIZE);
if (large_file_header == NULL) {
fprintf(stderr,"prsocket_test failed to malloc header file\n");
failed_already=1;
rv = -1;
goto done;
}
memset(large_file_header, (int) PR_IntervalNow(),
LARGE_FILE_HEADER_SIZE);
/*
* trailer for large file
*/
large_file_trailer = PR_MALLOC(LARGE_FILE_TRAILER_SIZE);
if (large_file_trailer == NULL) {
fprintf(stderr,"prsocket_test failed to malloc header trailer\n");
failed_already=1;
rv = -1;
goto done;
}
memset(large_file_trailer, (int) PR_IntervalNow(),
LARGE_FILE_TRAILER_SIZE);
datalen = tcp_mesg_size;
thread_count = 0;
/*
* start the server thread
*/
sparamp = PR_NEW(Server_Param);
if (sparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
rv = -1;
goto done;
}
server_sem = PR_NewSem(0);
if (server_sem == NULL) {
fprintf(stderr,"prsocket_test: PR_NewSem failed\n");
failed_already=1;
rv = -1;
goto done;
}
mon2 = PR_NewMonitor();
if (mon2 == NULL) {
fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n");
failed_already=1;
rv = -1;
goto done;
}
PR_EnterMonitor(mon2);
sparamp->addr_sem = server_sem;
sparamp->exit_mon = mon2;
sparamp->exit_counter = &thread_count;
sparamp->datalen = datalen;
t = PR_CreateThread(PR_USER_THREAD,
TransmitFile_Server, (void *)sparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
failed_already=1;
rv = -1;
goto done;
}
DPRINTF(("Created TCP server = 0x%x\n", t));
thread_count++;
/*
* wait till the server address is setup
*/
PR_WaitSem(server_sem);
/*
* Now start a bunch of client threads
*/
cparamp = PR_NEW(Client_Param);
if (cparamp == NULL) {
fprintf(stderr,"prsocket_test: PR_NEW failed\n");
failed_already=1;
rv = -1;
goto done;
}
cparamp->server_addr = tcp_server_addr;
cparamp->server_addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
cparamp->exit_mon = mon2;
cparamp->exit_counter = &thread_count;
cparamp->datalen = datalen;
for (i = 0; i < num_transmitfile_clients; i++) {
t = create_new_thread(PR_USER_THREAD,
TransmitFile_Client, (void *) cparamp,
PR_PRIORITY_NORMAL,
PR_LOCAL_THREAD,
PR_UNJOINABLE_THREAD,
0, i);
if (t == NULL) {
fprintf(stderr,"prsocket_test: PR_CreateThread failed\n");
rv = -1;
failed_already=1;
goto done;
}
DPRINTF(("Created TransmitFile client = 0x%lx\n", t));
thread_count++;
}
/* Wait for server and client threads to exit */
while (thread_count) {
PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
DPRINTF(("Socket_Misc_Test - thread_count = %d\n", thread_count));
}
PR_ExitMonitor(mon2);
done:
if (buf) {
PR_DELETE(buf);
}
#if defined(XP_UNIX) && !defined(SYMBIAN)
munmap((char*)small_file_addr, SMALL_FILE_SIZE);
munmap((char*)large_file_addr, LARGE_FILE_SIZE);
#endif
PR_Close(small_file_fd);
PR_Close(large_file_fd);
if ((PR_Delete(SMALL_FILE_NAME)) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: failed to unlink file %s\n",
SMALL_FILE_NAME);
failed_already=1;
}
if ((PR_Delete(LARGE_FILE_NAME)) == PR_FAILURE) {
fprintf(stderr,"prsocket_test: failed to unlink file %s\n",
LARGE_FILE_NAME);
failed_already=1;
}
if ((PR_RmDir(TEST_DIR)) == PR_FAILURE) {
fprintf(stderr,"prsocket_test failed to rmdir %s: (%ld, %ld)\n",
TEST_DIR, PR_GetError(), PR_GetOSError());
failed_already=1;
}
printf("%-29s%s","Socket_Misc_Test",":");
printf("%2d Server %2d Clients\n",1, num_transmitfile_clients);
printf("%30s Sizes of Transmitted Files - %4d KB, %2d MB \n",":",
SMALL_FILE_SIZE/1024, LARGE_FILE_SIZE/(1024 * 1024));
return rv;
}
/************************************************************************/
/*
* Test Socket NSPR APIs
*/
int main(int argc, char **argv)
{
/*
* -d debug mode
*/
PLOptStatus os;
PLOptState *opt = PL_CreateOptState(argc, argv, "d");
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
{
if (PL_OPT_BAD == os) continue;
switch (opt->option)
{
case 'd': /* debug mode */
_debug_on = 1;
break;
default:
break;
}
}
PL_DestroyOptState(opt);
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
PR_STDIO_INIT();
PR_SetConcurrency(4);
emuSendFileIdentity = PR_GetUniqueIdentity("Emulated SendFile");
emuSendFileMethods = *PR_GetDefaultIOMethods();
emuSendFileMethods.transmitfile = emu_TransmitFile;
emuSendFileMethods.sendfile = emu_SendFile;
/*
* run client-server test with TCP, Ipv4-Ipv4
*/
printf("TCP Client/Server Test - IPv4/Ipv4\n");
if (TCP_Socket_Client_Server_Test() < 0) {
printf("TCP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("TCP_Socket_Client_Server_Test Passed\n");
/*
* client-server test, Ipv6-Ipv4
*/
client_domain = PR_AF_INET6;
printf("TCP Client/Server Test - IPv6/Ipv4\n");
if (TCP_Socket_Client_Server_Test() < 0) {
printf("TCP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("TCP_Socket_Client_Server_Test Passed\n");
/*
* client-server test, Ipv4-Ipv6
*/
client_domain = PR_AF_INET;
server_domain = PR_AF_INET6;
printf("TCP Client/Server Test - IPv4/Ipv6\n");
if (TCP_Socket_Client_Server_Test() < 0) {
printf("TCP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("TCP_Socket_Client_Server_Test Passed\n");
/*
* client-server test, Ipv6-Ipv6
*/
client_domain = PR_AF_INET6;
server_domain = PR_AF_INET6;
printf("TCP Client/Server Test - IPv6/Ipv6\n");
if (TCP_Socket_Client_Server_Test() < 0) {
printf("TCP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("TCP_Socket_Client_Server_Test Passed\n");
test_cancelio = 0;
#if defined(SYMBIAN) && !defined(__WINSCW__)
/* UDP tests only run on Symbian devices but not emulator */
/*
* run client-server test with UDP, IPv4/IPv4
*/
printf("UDP Client/Server Test - IPv4/Ipv4\n");
client_domain = PR_AF_INET;
server_domain = PR_AF_INET;
if (UDP_Socket_Client_Server_Test() < 0) {
printf("UDP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("UDP_Socket_Client_Server_Test Passed\n");
/*
* run client-server test with UDP, IPv6/IPv4
*/
printf("UDP Client/Server Test - IPv6/Ipv4\n");
client_domain = PR_AF_INET6;
server_domain = PR_AF_INET;
if (UDP_Socket_Client_Server_Test() < 0) {
printf("UDP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("UDP_Socket_Client_Server_Test Passed\n");
/*
* run client-server test with UDP,IPv4-IPv6
*/
printf("UDP Client/Server Test - IPv4/Ipv6\n");
client_domain = PR_AF_INET;
server_domain = PR_AF_INET6;
if (UDP_Socket_Client_Server_Test() < 0) {
printf("UDP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("UDP_Socket_Client_Server_Test Passed\n");
/*
* run client-server test with UDP,IPv6-IPv6
*/
printf("UDP Client/Server Test - IPv6/Ipv6\n");
client_domain = PR_AF_INET6;
server_domain = PR_AF_INET6;
if (UDP_Socket_Client_Server_Test() < 0) {
printf("UDP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("UDP_Socket_Client_Server_Test Passed\n");
#endif
/*
* Misc socket tests - including transmitfile, etc.
*/
/* File transmission test can not be done in Symbian OS because of
* large file's size and the incomplete mmap() implementation. */
#if !defined(WIN16) && !defined(SYMBIAN)
/*
** The 'transmit file' test does not run because
** transmit file is not implemented in NSPR yet.
**
*/
if (Socket_Misc_Test() < 0) {
printf("Socket_Misc_Test failed\n");
failed_already=1;
goto done;
} else
printf("Socket_Misc_Test passed\n");
/*
* run client-server test with TCP again to test
* recycling used sockets from PR_TransmitFile().
*/
if (TCP_Socket_Client_Server_Test() < 0) {
printf("TCP_Socket_Client_Server_Test failed\n");
goto done;
} else
printf("TCP_Socket_Client_Server_Test Passed\n");
#endif
done:
PR_Cleanup();
if (failed_already) return 1;
else return 0;
}
|