summaryrefslogtreecommitdiffstats
path: root/contrib/ofed/libsdp/src/port.c
blob: 26bcf7cf30e31950d41580537e5b875c2a4ef649 (plain)
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
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
/*
  This software is available to you under a choice of one of two
  licenses.  You may choose to be licensed under the terms of the GNU
  General Public License (GPL) Version 2, available at
  <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
  license, available in the LICENSE.TXT file accompanying this
  software.  These details are also available at
  <http://openib.org/license.html>.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

  Copyright (c) 2004 Topspin Communications.  All rights reserved.
  Copyright (c) 2005-2006 Mellanox Technologies Ltd.  All rights reserved.

  $Id$
*/

/*
 * system includes
 */
#if HAVE_CONFIG_H
#  include <config.h>
#endif							/* HAVE_CONFIG_H */

#ifdef SOLARIS_BUILD
/* Our prototypes for ioctl, get*name and accept do not strictly
   match the headers - we use the following lines to move the header
   versions 'out of the way' temporarily. */
#define ioctl __real_ioctl
#define getsockname __real_getsockname
#define getpeername __real_getpeername
#define accept __real_accept
#define FASYNC 0
#include <libgen.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __USE_GNU
#define _GNU_SOURCE				/* define RTLD_NEXT */
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/poll.h>
#ifdef __linux__
#include <sys/epoll.h>
#endif

#ifdef SOLARIS_BUILD
/* We're done protecting ourselves from the header prototypes */
#undef ioctl
#undef getsockname
#undef getpeername
#undef accept
#endif

/*
 * SDP specific includes
 */
#include "libsdp.h"

/* We can not use sizeof(sockaddr_in6) as the extra scope_id field is not a must have */
#define IPV6_ADDR_IN_MIN_LEN 24

/* setsockopt() level and optname declarations */
#define SOL_SDP		1025
#define SDP_UNBIND	259			/* Unbind socket */

/* Solaris has two entry socket creation functions */
#define SOCKET_SEMANTIC_DEFAULT 0
#define SOCKET_SEMANTIC_XNET 1

/* HACK: filter ioctl errors for FIONREAD */
#define FIONREAD 0x541B

void __attribute__ ((constructor)) __sdp_init(void);
void __attribute__ ((destructor)) __sdp_fini(void);

/* --------------------------------------------------------------------- */
/* library type definitions.                                             */
/* --------------------------------------------------------------------- */

typedef int (*ioctl_func_t) (int fd,
							 int request,
							 void *arg0,
							 void *arg1,
							 void *arg2,
							 void *arg3,
							 void *arg4, void *arg5, void *arg6, void *arg7);

typedef int (*fcntl_func_t) (int fd, int cmd, ...);

typedef int (*socket_func_t) (int domain, int type, int protocol);

typedef int (*setsockopt_func_t) (int s,
								  int level,
								  int optname,
								  const void *optval, socklen_t optlen);

typedef int (*connect_func_t) (int sockfd,
							   const struct sockaddr * serv_addr,
							   socklen_t addrlen);

typedef int (*listen_func_t) (int s, int backlog);

typedef int (*bind_func_t) (int sockfd,
							const struct sockaddr * my_addr, socklen_t addrlen);

typedef int (*close_func_t) (int fd);

typedef int (*dup_func_t) (int fd);

typedef int (*dup2_func_t) (int oldfd, int newfd);

typedef int (*getsockname_func_t) (int fd,
								   struct sockaddr * name, socklen_t * namelen);

typedef int (*getpeername_func_t) (int fd,
								   struct sockaddr * name, socklen_t * namelen);

typedef int (*accept_func_t) (int fd,
							  struct sockaddr * addr, socklen_t * addrlen);

typedef int (*select_func_t) (int n,
							  fd_set * readfds,
							  fd_set * writefds,
							  fd_set * exceptfds, struct timeval * timeout);

typedef int (*pselect_func_t) (int n,
							   fd_set * readfds,
							   fd_set * writefds,
							   fd_set * exceptfds,
							   const struct timespec * timeout,
							   const sigset_t * sigmask);

typedef int (*poll_func_t) (struct pollfd * ufds,
							unsigned long int nfds, int timeout);

#ifdef __linux__
typedef int (*epoll_create_func_t) (int size);

typedef int (*epoll_ctl_func_t) (int epfd,
								 int op, int fd, struct epoll_event * event);

typedef int (*epoll_wait_func_t) (int epfd,
								  struct epoll_event * events,
								  int maxevents, int timeout);

typedef int (*epoll_pwait_func_t) (int epfd,
								   struct epoll_event * events,
								   int maxevents,
								   int timeout, const sigset_t * sigmask);
#endif


struct socket_lib_funcs {
	ioctl_func_t ioctl;
	fcntl_func_t fcntl;
	socket_func_t socket;
	setsockopt_func_t setsockopt;
	connect_func_t connect;
	listen_func_t listen;
	bind_func_t bind;
	close_func_t close;
	dup_func_t dup;
	dup2_func_t dup2;
	getpeername_func_t getpeername;
	getsockname_func_t getsockname;
	accept_func_t accept;
	select_func_t select;
	pselect_func_t pselect;
	poll_func_t poll;
#ifdef __linux__
	epoll_create_func_t epoll_create;
	epoll_ctl_func_t epoll_ctl;
	epoll_wait_func_t epoll_wait;
	epoll_pwait_func_t epoll_pwait;
#endif
};								/* socket_lib_funcs */

#ifdef SOLARIS_BUILD
/* Solaris has another interface to socket functions prefixed with __xnet_ */
struct socket_lib_xnet_funcs {
	socket_func_t socket;
	connect_func_t connect;
	listen_func_t listen;
	bind_func_t bind;
};
#endif

static int simple_sdp_library;
static int max_file_descriptors;
static int dev_null_fd;
volatile static int init_status = 0;	/* 0: idle, 1:during, 2:ready */

/* --------------------------------------------------------------------- */
/* library static and global variables                                   */
/* --------------------------------------------------------------------- */

/* glibc provides these symbols - for Solaris builds we fake them
 * until _init is called, at which point we quiz libdl.. */
#ifdef SOLARIS_BUILD
char *program_invocation_name = "[progname]", *program_invocation_short_name =
	"[short_progname]";
#else
extern char *program_invocation_name, *program_invocation_short_name;
#endif

#ifdef RTLD_NEXT
static void *__libc_dl_handle = RTLD_NEXT;
#else
static void *__libc_dl_handle;
#endif

/* extra fd attributes we need for our algorithms */
struct sdp_extra_fd_attributes {
	int shadow_fd;				/* file descriptor of shadow sdp socket    */
	short last_accept_was_tcp;	/* used by accept to alternate tcp and sdp */
	short is_sdp;				/* 1 if the fd represents an sdp socket    */
};								/* sdp_extra_fd_attributes */

/* stores the extra attributes struct by fd */
static struct sdp_extra_fd_attributes *libsdp_fd_attributes;

static struct socket_lib_funcs _socket_funcs = {
	.socket = NULL,
	/* Automatically sets all other elements to NULL */
};								/* _socket_funcs */

#ifdef SOLARIS_BUILD
static struct socket_lib_xnet_funcs _socket_xnet_funcs = {
	.socket = NULL,
	/* Automatically sets all other elements to NULL */
};
#endif

/* --------------------------------------------------------------------- */
/* Prototypes                                                            */
/* --------------------------------------------------------------------- */
void __sdp_init(void);

/* --------------------------------------------------------------------- */
/*                                                                       */
/* local static functions.                                               */
/*                                                                       */
/* --------------------------------------------------------------------- */

/* ========================================================================= */
/*..init_extra_attribute -- initialize the set of extra attributes for a fd */
static void init_extra_attribute(int fd)
{
	if ((0 <= fd) && (max_file_descriptors > fd)) {
		libsdp_fd_attributes[fd].shadow_fd = -1;
		libsdp_fd_attributes[fd].is_sdp = 0;
		libsdp_fd_attributes[fd].last_accept_was_tcp = -1;
	}
}

static inline int is_valid_fd(int fd)
{
	return (0 <= fd) && (fd < max_file_descriptors);
}

/* ========================================================================= */
/*..get_shadow_fd_by_fd -- given an fd return its shadow fd if exists        */
static inline int get_shadow_fd_by_fd(int fd)
{
	if (is_valid_fd(fd))
		return libsdp_fd_attributes[fd].shadow_fd;
	else
		return -1;
}

/* ========================================================================= */
/*..set_shadow_for_fd --                                                     */
static inline void set_shadow_for_fd(int fd, int shadow_fd)
{
	if (is_valid_fd(fd))
		libsdp_fd_attributes[fd].shadow_fd = shadow_fd;
}

/* ========================================================================= */
/*..set_is_sdp_socket --                                                     */
static inline void set_is_sdp_socket(int fd, short is_sdp)
{
	if (is_valid_fd(fd))
		libsdp_fd_attributes[fd].is_sdp = is_sdp;
}

/* ========================================================================= */
/*..get_is_sdp_socket -- given an fd return 1 if it is an SDP socket         */
static inline int get_is_sdp_socket(int fd)
{
	if (is_valid_fd(fd))
		return libsdp_fd_attributes[fd].is_sdp;
	else
		return 0;
}

/* ========================================================================= */
/*..last_accept_was_tcp -- given an fd return 1 if last accept was tcp       */
static inline int last_accept_was_tcp(int fd)
{
	if (is_valid_fd(fd))
		return libsdp_fd_attributes[fd].last_accept_was_tcp;
	else
		return 0;
}

/* ========================================================================= */
/*..set_last_accept -- given an fd set last accept was tcp                   */
static inline void set_last_accept(int fd, int was_tcp)
{
	if (is_valid_fd(fd))
		libsdp_fd_attributes[fd].last_accept_was_tcp = was_tcp;
}

/* ========================================================================= */
/*..cleanup_shadow -- an error occured on an SDP socket, cleanup             */
static int cleanup_shadow(int fd)
{
	int shadow_fd = get_shadow_fd_by_fd(fd);

	if (shadow_fd == -1)
		return 0;
	libsdp_fd_attributes[fd].shadow_fd = -1;
	libsdp_fd_attributes[fd].last_accept_was_tcp = 0;
	return (_socket_funcs.close(shadow_fd));
}								/* cleanup_shadow */

/* ========================================================================= */
/*..replace_fd_with_its_shadow -- perform all required for such promotion    */
static int replace_fd_with_its_shadow(int fd)
{
	int shadow_fd = libsdp_fd_attributes[fd].shadow_fd;

	if (shadow_fd == -1) {
		__sdp_log(9, "Error replace_fd_with_its_shadow: no shadow for fd:%d\n",
				  fd);
		return EINVAL;
	}

	/* copy the attributes of the shadow before we clean them up */
	libsdp_fd_attributes[fd] = libsdp_fd_attributes[shadow_fd];
	libsdp_fd_attributes[fd].shadow_fd = -1;
	if (_socket_funcs.dup2(shadow_fd, fd) < 0) {
		init_extra_attribute(fd);
		_socket_funcs.close(shadow_fd);
		return EINVAL;
	}
	_socket_funcs.close(shadow_fd);
	return 0;
}

static sa_family_t get_sdp_domain(int domain)
{
	if (AF_INET_SDP == domain || AF_INET6_SDP == domain)
		return domain;

	if (AF_INET == domain)
		return AF_INET_SDP;
	else if (AF_INET6 == domain)
		return AF_INET6_SDP;

	__sdp_log(9, "Error %s: unknown TCP domain: %d\n", __func__, domain);

	return -1;
}

static int get_sock_domain(int sd)
{
	struct sockaddr_storage tmp_sin;
	socklen_t tmp_sinlen = sizeof(tmp_sin);

	if (_socket_funcs.getsockname(sd, (struct sockaddr *) &tmp_sin, &tmp_sinlen) < 0) {
		__sdp_log(9, "Error %s: getsockname return <%d> for socket\n", __func__, errno);
		return -1;
	}

	return ((struct sockaddr *)&tmp_sin)->sa_family;
}

/* ========================================================================= */
/*..is_filtered_unsuported_sockopt -- return 1 if to filter sockopt failure  */
static inline int is_filtered_unsuported_sockopt(int level, int optname)
{
	/* 
	 * TODO: until we know exactly which unsupported opts are really 
	 * a don't care we always pass the error
	 */
	return 0;
#if 0
	/* these are the SOL_TCP OPTS we should consider filterring */
	TCP_NODELAY 1				/* Don't delay send to coalesce packets  */
		TCP_MAXSEG 2			/* Set maximum segment size  */
		TCP_CORK 3				/* Control sending of partial frames  */
		TCP_KEEPIDLE 4			/* Start keeplives after this period */
		TCP_KEEPINTVL 5			/* Interval between keepalives */
		TCP_KEEPCNT 6			/* Number of keepalives before death */
		TCP_SYNCNT 7			/* Number of SYN retransmits */
		TCP_LINGER2 8			/* Life time of orphaned FIN-WAIT-2 state */
		TCP_DEFER_ACCEPT 9		/* Wake up listener only when data arrive */
		TCP_WINDOW_CLAMP 10		/* Bound advertised window */
		TCP_INFO 11				/* Information about this connection. */
		TCP_QUICKACK 12			/* Bock/reenable quick ACKs.  */
#endif
}

/* ========================================================================= */
/*..is_invalid_addr -- return 1 if given pointer is not valid                */
/* NOTE: invalidation of the size is going to happen during actual call      */
static inline int is_invalid_addr(const void *p)
{
	/* HACK: on some systems we can not write to check for pointer validity */
	size_t ret = fcntl(dev_null_fd, F_GETLK, p);

	ret = (errno == EFAULT);
	errno = 0;
	return ret;
}

/* ========================================================================= */
/*..get_addr_str -- fill in the given buffer with addr str or return 1       */
static int get_addr_str(const struct sockaddr *addr, char *buf, size_t len)
{
	const struct sockaddr_in *sin = (struct sockaddr_in *) addr;
	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
	char const *conv_res;

	if (sin->sin_family == AF_INET) {
		conv_res = inet_ntop(AF_INET, (void *) &(sin->sin_addr), buf, len);
	} else if (sin6->sin6_family == AF_INET6) {
		conv_res = inet_ntop(AF_INET6, (void *) &sin6->sin6_addr, buf, len);
	} else {
		strncpy(buf, "unknown address family", len);
		conv_res = (char *) 1;
	}
	return conv_res == NULL;
}

/* --------------------------------------------------------------------- */
/*                                                                       */
/* Socket library function overrides.                                    */
/*                                                                       */
/* --------------------------------------------------------------------- */

/* ========================================================================= */
/*..ioctl -- replacement ioctl call. */
int
ioctl(int fd,
	  int request,
	  void *arg0,
	  void *arg1,
	  void *arg2, void *arg3, void *arg4, void *arg5, void *arg6, void *arg7)
{
	int shadow_fd;
	int sret = 0;
	int ret = 0;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.ioctl) {
		__sdp_log(9, "Error ioctl: no implementation for ioctl found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	__sdp_log(2, "IOCTL: <%s:%d:%d> request <%d>\n",
			  program_invocation_short_name, fd, shadow_fd, request);

	ret = _socket_funcs.ioctl(fd, request,
							  arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);

	/* HACK: avoid failing on FIONREAD error as SDP does not support it at the moment */
	if ((ret < 0) && get_is_sdp_socket(fd) && (request == FIONREAD)) {
		__sdp_log(8, "Warning ioctl: "
				  "Ignoring FIONREAD error for SDP socket.\n");
		ret = 0;
	}

	/* if shadow and no error on tcp */
	if ((ret >= 0) && (-1 != shadow_fd)) {
		sret = _socket_funcs.ioctl(shadow_fd, request,
								   arg0, arg1, arg2, arg3, arg4, arg5, arg6,
								   arg7);
		/* HACK: avoid failing on FIONREAD error as SDP does not support it at the moment */
		if ((sret < 0) && (request == FIONREAD)) {
			__sdp_log(8, "Warning ioctl: "
					  "Ignoring FIONREAD error for shadow SDP socket.\n");
			sret = 0;
		}

		if (sret < 0) {
			__sdp_log(9, "Error ioctl: "
					  "<%d> calling ioctl for SDP socket, closing it.\n",
					  errno);
			cleanup_shadow(fd);
		}
	}

	__sdp_log(2, "IOCTL: <%s:%d:%d> result <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret, sret);

	return ret;
}								/* ioctl */

/* ========================================================================= */
/*..fcntl -- replacement fcntl call.                                         */
int fcntl(int fd, int cmd, ...)
{
	int shadow_fd;
	int sret = 0;
	int ret = 0;

	void *arg;
	va_list ap;

	va_start(ap, cmd);
	arg = va_arg(ap, void *);
	va_end(ap);


	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.fcntl) {
		__sdp_log(9, "Error fcntl: no implementation for fcntl found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	__sdp_log(2, "FCNTL: <%s:%d:%d> command <%d> argument <%p>\n",
			  program_invocation_short_name, fd, shadow_fd, cmd, arg);

	ret = _socket_funcs.fcntl(fd, cmd, arg);
	if ((ret >= 0) && (-1 != shadow_fd)) {
		sret = _socket_funcs.fcntl(shadow_fd, cmd, arg);
		if (sret < 0) {
			__sdp_log(9, "Error fcntl:"
					  " <%d> calling fcntl(%d, %d, %p) for SDP socket. Closing it.\n",
					  shadow_fd, cmd, arg, errno);
			cleanup_shadow(fd);
		}
	}

	__sdp_log(2, "FCNTL: <%s:%d:%d> result <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret, sret);

	return ret;
}								/* fcntl */

/* ========================================================================= */
/*..setsockopt -- replacement setsockopt call.                               */
int
setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
{
	int shadow_fd;
	int sret = 0;
	int ret = 0;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.setsockopt) {
		__sdp_log(9, "Error setsockopt:"
				  " no implementation for setsockopt found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	__sdp_log(2, "SETSOCKOPT: <%s:%d:%d> level <%d> name <%d>\n",
			  program_invocation_short_name, fd, shadow_fd, level, optname);

	if (level == SOL_SOCKET && optname == SO_KEEPALIVE && get_is_sdp_socket(fd)) {
		level = AF_INET_SDP;
		__sdp_log(2, "SETSOCKOPT: <%s:%d:%d> substitute level %d\n",
				  program_invocation_short_name, fd, shadow_fd, level);
	}

	ret = _socket_funcs.setsockopt(fd, level, optname, optval, optlen);
	if ((ret >= 0) && (shadow_fd != -1)) {
		if (level == SOL_SOCKET && optname == SO_KEEPALIVE &&
			get_is_sdp_socket(shadow_fd)) {
			level = AF_INET_SDP;
			__sdp_log(2, "SETSOCKOPT: <%s:%d:%d> substitute level %d\n",
					  program_invocation_short_name, fd, shadow_fd, level);
		}

		sret = _socket_funcs.setsockopt(shadow_fd, level, optname, optval, optlen);
		if (sret < 0) {
			__sdp_log(8, "Warning sockopts:"
					  " ignoring error on shadow SDP socket fd:<%d>\n", fd);
			/* 
			 * HACK: we should allow some errors as some sock opts are unsupported  
			 * __sdp_log(9, "Error %d calling setsockopt for SDP socket, closing\n", errno); 
			 * cleanup_shadow(fd); 
			 */
		}
	}

	/* Due to SDP limited implmentation of sockopts we ignore some errors */
	if ((ret < 0) && get_is_sdp_socket(fd) &&
		is_filtered_unsuported_sockopt(level, optname)) {
		__sdp_log(8, "Warning sockopts: "
				  "ignoring error on non implemented sockopt on SDP socket"
				  " fd:<%d> level:<%d> opt:<%d>\n", fd, level, optval);
		ret = 0;
	}

	__sdp_log(2, "SETSOCKOPT: <%s:%d:%d> result <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret, sret);

	return ret;
}								/* setsockopt */

/* ========================================================================= */
/*..socket -- replacement socket call.                                       */

static inline int __create_socket_semantic(int domain,
										   int type,
										   int protocol, int semantics)
{
	return
#ifdef SOLARIS_BUILD
		(semantics == SOCKET_SEMANTIC_XNET) ?
		_socket_xnet_funcs.socket(domain, type, protocol) :
#endif
		_socket_funcs.socket(domain, type, protocol);
}

/* Contains the main logic for creating shadow SDP sockets */
static int __create_socket(int domain, int type, int protocol, int semantics)
{
	int s = -1;
	int shadow_fd = -1;
	use_family_t family_by_prog;
	int sdp_domain;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.socket) {
		__sdp_log(9, "Error socket: no implementation for socket found\n");
		return -1;
	}

	__sdp_log(2, "SOCKET: <%s> domain <%d> type <%d> protocol <%d>\n",
			  program_invocation_short_name, domain, type, protocol);

	sdp_domain = get_sdp_domain(domain);
	if (sdp_domain < 0) {
		errno = EAFNOSUPPORT;
		s = -1;
		goto done;
	}

	/* check to see if we can skip the shadow */
	if ((AF_INET == domain || AF_INET6 == domain) && (SOCK_STREAM == type))
		if (simple_sdp_library)
			family_by_prog = USE_SDP;
		else
			family_by_prog = __sdp_match_by_program();
	else if (AF_INET_SDP == domain || AF_INET6_SDP == domain)
		family_by_prog = USE_SDP;
	else
		family_by_prog = USE_TCP;

	if (family_by_prog == USE_TCP) {
		__sdp_log(1, "SOCKET: making TCP only socket (no shadow)\n");
		s = __create_socket_semantic(domain, type, protocol, semantics);
		init_extra_attribute(s);
		set_is_sdp_socket(s, 0);
		goto done;
	}

	if (family_by_prog == USE_SDP) {
		/* HACK: convert the protocol if IPPROTO_IP */
		if (protocol == 0)
			protocol = IPPROTO_TCP;

		__sdp_log(1, "SOCKET: making SDP socket type:%d proto:%d\n",
				  type, protocol);
		s = __create_socket_semantic(sdp_domain, type, protocol, semantics);
		init_extra_attribute(s);
		set_is_sdp_socket(s, 1);
		goto done;
	}

	/* HACK: if we fail creating the TCP socket should we abort ? */
	__sdp_log(1, "SOCKET: making TCP socket\n");
	s = __create_socket_semantic(domain, type, protocol, semantics);
	init_extra_attribute(s);
	set_is_sdp_socket(s, 0);
	if (is_valid_fd(s)) {
		if (((AF_INET == domain) || (AF_INET6 == domain)) &&
			(SOCK_STREAM == type)) {

			if (protocol == 0)
				protocol = IPPROTO_TCP;
			__sdp_log(1, "SOCKET: making SDP shadow socket type:%d proto:%d\n",
					  type, protocol);
			shadow_fd =
				__create_socket_semantic(sdp_domain, type, protocol,
										 semantics);
			if (is_valid_fd(shadow_fd)) {
				init_extra_attribute(shadow_fd);
				if (libsdp_fd_attributes[s].shadow_fd != -1) {
					__sdp_log(8, "Warning socket: "
							  "overriding existing shadow fd:%d for fd:%d\n",
							  libsdp_fd_attributes[s].shadow_fd, s);
				}
				set_is_sdp_socket(shadow_fd, 1);
				set_shadow_for_fd(s, shadow_fd);
			} else {
				__sdp_log(9,
						  "Error socket: <%d> calling socket for SDP socket\n",
						  errno);
				/* fail if we did not make the SDP socket */
				__sdp_log(1, "SOCKET: closing TCP socket:<%d>\n", s);
				_socket_funcs.close(s);
				s = -1;
			}
		}
	} else {
		__sdp_log(9, "Error socket: "
				  "ignoring SDP socket since TCP fd:%d out of range\n", s);
	}

done:
	__sdp_log(2, "SOCKET: <%s:%d:%d>\n",
			  program_invocation_short_name, s, shadow_fd);

	return s;
}								/* socket */

int socket(int domain, int type, int protocol)
{
	return __create_socket(domain, type, protocol, SOCKET_SEMANTIC_DEFAULT);
}

#ifdef SOLARIS_BUILD
int __xnet_socket(int domain, int type, int protocol)
{
	return __create_socket(domain, type, protocol, SOCKET_SEMANTIC_XNET);
}
#endif

/* ========================================================================= */
/*..get_fd_addr_port_num - obtain the port the fd is attached to             */
static int get_fd_addr_port_num(int sd)
{
	struct sockaddr_storage addr;
	int ret;
	const struct sockaddr_in *sin;
	socklen_t addrlen = sizeof(addr);

	ret = _socket_funcs.getsockname(sd, (struct sockaddr *) &addr, &addrlen);

	if (ret) {
		__sdp_log(9, "Error: in get_fd_addr_port_num - Failed to get getsockname\n");
		return -1;
	}

	/* port num is in same location for IPv4 and IPv6 */
	sin = (const struct sockaddr_in *) &addr;
	return ntohs(sin->sin_port);
}

/* ========================================================================= */
/*..set_addr_port_num - sets the port in the given address                   */
static int set_addr_port_num(const struct sockaddr *addr, int port)
{
	struct sockaddr_in *sin = (struct sockaddr_in *) addr;

	/* port num is in same location for IPv4 and IPv6 */
	sin->sin_port = htons(port);
	return 0;
}

/* ========================================================================= */
/*  perform a bind with the given socket semantics                           */
static inline int
__bind_semantics(int fd,
				 const struct sockaddr *my_addr,
				 socklen_t addrlen, int semantics)
{
	return
#ifdef SOLARIS_BUILD
		(semantics == SOCKET_SEMANTIC_XNET) ?
		_socket_xnet_funcs.bind(fd, my_addr, addrlen) :
#endif
		_socket_funcs.bind(fd, my_addr, addrlen);
}

/* ========================================================================= */
/*..find_free_port - find same free port on both TCP and SDP                 */
#define MAX_BIND_ANY_PORT_TRIES 20000
static int
find_free_port(const struct sockaddr *sin_addr,
			   const socklen_t addrlen,
			   int orig_sd,
			   int *sdp_sd, int *tcp_sd, int semantics)
{
	static int tcp_turn = 1;
	int tmp_turn = tcp_turn;
	int num_of_loops = 0;
	int port = -1;
	int tmp_sd[2];
	unsigned int yes = 1;
	int ret;
	int domain, sdp_domain;

	__sdp_log(2, "find_free_port: starting search for common free port\n");

	/* need to obtain the address family from the fd */
	domain = get_sock_domain(orig_sd);
	if (domain == -1) {
		errno = EFAULT;
		goto done;
	}

	sdp_domain = get_sdp_domain(domain);
	if (sdp_domain < 0) {
		errno = EFAULT;
		goto done;
	}

	do {
		__sdp_log(1, "find_free_port: taking loop (%d)\n", ++num_of_loops);

		__sdp_log(1, "find_free_port: creating the two sockets\n");
		tmp_sd[0] = _socket_funcs.socket(sdp_domain, SOCK_STREAM, IPPROTO_TCP);
		if (tmp_sd[0] < 0) {
			__sdp_log(8, "Warning find_free_port: creating first socket failed\n");
			goto done;
		}

		_socket_funcs.setsockopt(tmp_sd[0], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

		tmp_sd[1] = _socket_funcs.socket(domain, SOCK_STREAM, IPPROTO_TCP);
		if (tmp_sd[1] < 0) {
			__sdp_log(8, "Warning find_free_port: creating second socket failed\n");
			_socket_funcs.close(tmp_sd[0]);
			goto done;
		}

		_socket_funcs.setsockopt(tmp_sd[1], SOL_SOCKET, SO_REUSEADDR, &yes,
								 sizeof(yes));

		__sdp_log(1, "find_free_port: binding first %s socket\n",
				  tmp_turn ? "tcp" : "sdp");
		ret = __bind_semantics(tmp_sd[tmp_turn], sin_addr, addrlen, semantics);
		if (ret < 0) {
			__sdp_log(8,
					  "Warning find_free_port: binding first socket failed:%s\n",
					  strerror(errno));
			_socket_funcs.close(tmp_sd[0]);
			_socket_funcs.close(tmp_sd[1]);
			goto done;
		}

		__sdp_log(1, "find_free_port: listening on first socket\n");
		ret = _socket_funcs.listen(tmp_sd[tmp_turn], 5);
		if (ret < 0) {
			__sdp_log(8, "Warning find_free_port: listening on first socket failed:%s\n",
					strerror(errno));
			_socket_funcs.close(tmp_sd[0]);
			_socket_funcs.close(tmp_sd[1]);
			goto done;
		}

		port = get_fd_addr_port_num(tmp_sd[tmp_turn]);
		if (port < 0) {
			__sdp_log(8, "Warning find_free_port: first socket port:%d < 0\n",
					port);
			_socket_funcs.close(tmp_sd[0]);
			_socket_funcs.close(tmp_sd[1]);
			goto done;
		}
		__sdp_log(1, "find_free_port: first socket port:%u\n", port);

		set_addr_port_num(sin_addr, port);

		__sdp_log(1, "find_free_port: binding second socket\n");
		ret = __bind_semantics(tmp_sd[1 - tmp_turn], sin_addr, addrlen, semantics);
		if (ret < 0) {
			/* bind() for sdp socket failed. It is acceptable only
			 * if the IP is not part of IB network. */

			if (errno != EADDRINUSE) {
				__sdp_log(8, "Warning find_free_port: "
						  "binding second socket failed with %s\n",
						  strerror(errno));
				goto close_and_mark;
			} else {
				int err;
#ifdef __linux__
				socklen_t len = sizeof(int);

				ret = getsockopt(tmp_sd[1 - tmp_turn], SOL_TCP,
								 SDP_LAST_BIND_ERR, &err, &len);
				if (-1 == ret) {
					__sdp_log(9, "Error %s:getsockopt: %s\n",
							  __func__, strerror(errno));
					goto close_and_mark;
				}
#else
				err = -errno;
#endif
				if (-ENOENT == err || -EADDRINUSE != err) {
					/* bind() failed due to either:
					 * 1. IP is ETH, not IB, so can't bind() to sdp socket.
					 * 2. real error.
					 * Continue only with TCP */
					goto close_and_mark;
				}
				__sdp_log(1, "find_free_port: %s port %u was busy\n",
					  1 - tmp_turn ? "tcp" : "sdp",
					  ntohs(((const struct sockaddr_in *)sin_addr)->sin_port));
			}

			/* close the sockets - we will need new ones ... */
			__sdp_log(1,
					  "find_free_port: closing the two sockets before next loop\n");
			_socket_funcs.close(tmp_sd[0]);
			_socket_funcs.close(tmp_sd[1]);

			port = -1;
			/* we always start with tcp so we keep the original setting for now */
			/* tmp_turn = 1 - tmp_turn; */
		}

	} while ((port < 0) && (num_of_loops < MAX_BIND_ANY_PORT_TRIES));

setfds:
	tcp_turn = tmp_turn;
	*sdp_sd = tmp_sd[0];
	*tcp_sd = tmp_sd[1];

done:
	__sdp_log(2, "find_free_port: return port:<%d>\n", port);
	return port;

close_and_mark:
	_socket_funcs.close(tmp_sd[0]);
	tmp_sd[0] = -1;				/* mark with error */
	goto setfds;

}

/* ========================================================================= */
/*..check_legal_bind - check if given address is okay for both TCP and SDP   */
static int
check_legal_bind(const struct sockaddr *sin_addr,
				 const socklen_t addrlen,
				 int orig_sd,
				 int *sdp_sd, int *tcp_sd, int semantics)
{
	unsigned int yes = 1;
	int ret = -1;
	int sret = -1;
	int domain, sdp_domain;

	/* need to obtain the address family from the fd */
	domain = get_sock_domain(orig_sd);
	if (domain == -1) {
		errno = EFAULT;
		ret = -1;
		goto done;
	}

	sdp_domain = get_sdp_domain(domain);
	if (sdp_domain < 0) {
		errno = EFAULT;
		goto done;
	}

	__sdp_log(2, "check_legal_bind: binding two temporary sockets\n");
	*sdp_sd = _socket_funcs.socket(sdp_domain, SOCK_STREAM, IPPROTO_TCP);
	if (*sdp_sd < 0) {
		__sdp_log(9, "Error check_legal_bind: " "creating SDP socket failed\n");
		goto done;
	}

	__sdp_log(2, "check_legal_bind: reusing <%d> \n", *sdp_sd);
	sret =
		_socket_funcs.setsockopt(*sdp_sd, SOL_SOCKET, SO_REUSEADDR, &yes,
								 sizeof(yes));
	if (sret < 0) {
		__sdp_log(9, "Error bind: Could not setsockopt sdp_sd\n");
	}

	*tcp_sd = _socket_funcs.socket(domain, SOCK_STREAM, IPPROTO_TCP);
	if (*tcp_sd < 0) {
		__sdp_log(9, "Error check_legal_bind: "
				  "creating second socket failed:%s\n", strerror(errno));
		_socket_funcs.close(*sdp_sd);
		goto done;
	}

	__sdp_log(2, "check_legal_bind: reusing <%d> \n", *tcp_sd);
	sret =
		_socket_funcs.setsockopt(*tcp_sd, SOL_SOCKET, SO_REUSEADDR, &yes,
								 sizeof(yes));
	if (sret < 0) {
		__sdp_log(9, "Error bind: Could not setsockopt tcp_sd\n");
	}

	__sdp_log(1, "check_legal_bind: binding SDP socket\n");
	ret = __bind_semantics(*sdp_sd, sin_addr, addrlen, semantics);
	if (ret < 0) {
		/* bind() for sdp socket failed. It is acceptable only if
		 * the IP is not part of IB network. */
		int err;
		socklen_t len = sizeof(int);

		if (EADDRINUSE != errno)
			goto done;
#ifdef __linux__
		if (-1 == getsockopt(*sdp_sd, SOL_TCP, SDP_LAST_BIND_ERR, &err, &len)) {
			__sdp_log(9, "Error check_legal_bind:getsockopt: %s\n",
					  strerror(errno));
			goto done;
		}
#else
		err = -errno;
#endif
		if (-ENOENT != err) {
			/* bind() failed due to real error. Can't continue */
			__sdp_log(9, "Error check_legal_bind: "
					  "binding SDP socket failed:%s\n", strerror(errno));
			_socket_funcs.close(*sdp_sd);
			_socket_funcs.close(*tcp_sd);

			/* TCP and SDP without library return EINVAL */
			if (errno == EADDRINUSE)
				errno = EINVAL;

			goto done;
		}
		/* IP is ETH, not IB, so can't bind() to sdp socket */
		/* Continue only with TCP */
		_socket_funcs.close(*sdp_sd);
		*sdp_sd = -1;
	}

	__sdp_log(1, "check_legal_bind: binding TCP socket\n");
	ret = __bind_semantics(*tcp_sd, sin_addr, addrlen, semantics);
	if (ret < 0) {
		__sdp_log(9, "Error check_legal_bind: "
				  "binding TCP socket failed:%s\n", strerror(errno));
		if (-1 != *sdp_sd)
			_socket_funcs.close(*sdp_sd);
		_socket_funcs.close(*tcp_sd);
		goto done;
	}
	ret = 0;
	__sdp_log(2, "check_legal_bind: result:<%d>\n", ret);
done:
	return ret;
}

/* ========================================================================= */
/*..close_and_bind - close an open fd and bind another one immediately       */
static int
close_and_bind(int old_sd,
			   int new_sd,
			   const struct sockaddr *addr, socklen_t addrlen, int semantics)
{
	int ret;

	__sdp_log(2, "close_and_bind: closing <%d> binding <%d>\n", old_sd, new_sd);
	ret = _socket_funcs.close(old_sd);
	if (ret < 0) {
		__sdp_log(9, "Error bind: Could not close old_sd\n");
		goto done;
	}

	ret = __bind_semantics(new_sd, addr, addrlen, semantics);
	if (ret < 0)
		__sdp_log(9, "Error bind: Could not bind new_sd\n");

done:
	__sdp_log(2, "close_and_bind: returning <%d>\n", ret);
	return ret;
}

/* ========================================================================= */
/*..bind -- replacement bind call.                                           */
/* 
   As we do not know the role of this socket yet so we cannot choose AF. 
   We need to be able to handle shadow too.
   SDP sockets (may be shadow or not) must be using converted address 
   
	Since there is no way to "rebind" a socket we have to avoid "false" bind:
	1. When the given address for the bind includes a port we need to 
	   guarantee the port is free on both address families. We do that 
      by creating temporary sockets and biding them first. Then we close and
		re-use the address on the real sockets. 
	2. When ANY_PORT is requested we need to make sure the port we obtain from 
	   the first address family is also free on the second one. We use temporary
		sockets for that task too. We loop several times to find such common 
		available socket
*/
static int
__perform_bind(int fd,
			   const struct sockaddr *addr, socklen_t addrlen, int semantics)
{
	int shadow_fd;
	struct sockaddr_in *sin_addr = (struct sockaddr_in *) addr;
	int ret, sret = -1;
	char buf[MAX_ADDR_STR_LEN];

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.bind) {
		__sdp_log(9, "Error bind: no implementation for bind found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	if ((addr == NULL) || is_invalid_addr(addr)) {
		errno = EFAULT;
		__sdp_log(9, "Error bind: illegal address provided\n");
		return -1;
	}

	if (get_addr_str(addr, buf, MAX_ADDR_STR_LEN)) {
		__sdp_log(9, "Error bind: provided illegal address: %s\n",
				  strerror(errno));
		return -1;
	}

	__sdp_log(2, "BIND: <%s:%d:%d> type <%d> IP <%s> port <%d>\n",
			  program_invocation_short_name, fd, shadow_fd,
			  sin_addr->sin_family, buf, ntohs(sin_addr->sin_port));

	if (get_is_sdp_socket(fd)) {
		__sdp_log(1, "BIND: binding SDP socket:<%d>\n", fd);
		ret = __bind_semantics(fd, addr, addrlen, semantics);
		goto done;
	} else if (shadow_fd != -1) {
		/* has shadow */
		/* we need to validate the given address or find a common port 
		 * so we use the following tmp address and sockets */
		struct sockaddr_storage tmp_addr;
		int sdp_sd = -1, tcp_sd = -1, port;

		memcpy(&tmp_addr, addr, addrlen);
		ret = 0;
		if (ntohs(sin_addr->sin_port) == 0) {
			/* When we get ANY_PORT we need to make sure that both TCP 
			 * and SDP sockets will use the same port */

			port = find_free_port(addr, addrlen, fd, &sdp_sd, &tcp_sd, semantics);
			if (port < 0) {
				ret = -1;
				__sdp_log(9, "BIND: Failed to find common free port\n");
				/* We cannot bind both tcp and sdp on the same port, we will close
				 * the sdp and continue with tcp only */
				goto done;
			} else {
				/* copy the port to the tmp address */
				set_addr_port_num((struct sockaddr *) &tmp_addr, port);
			}
		} else {
			/* have a shadow but requested specific port - check that we 
			 * can actually bind the two addresses and then reuse */
			ret = check_legal_bind(addr, addrlen, fd, &sdp_sd, &tcp_sd, semantics);
			if (ret < 0) {
				__sdp_log(9, "Error bind: "
						  "Provided address can not bind on the two sockets\n");
			}
		}

		/* if we fail to find a common port or given address can not be used 
		 * we return error */
		if (ret < 0) {
			/* Temporary sockets already closed by check_legal_bind or 
			 * find_free_port */
			errno = EADDRINUSE;
			goto done;
		}

		/* close temporary sockets and reuse their address */
		/* HACK: close_and_bind might race with other applications. */
		/* When the race occur we return EADDRINUSE */
		ret = close_and_bind(tcp_sd, fd, (struct sockaddr *) &tmp_addr,
						   addrlen, semantics);
		if (ret < 0) {
			__sdp_log(9, "Error bind: " "Could not close_and_bind TCP side\n");
			if (-1 != sdp_sd)
				_socket_funcs.close(sdp_sd);
			goto done;
		}

		if (-1 != sdp_sd) {
			ret = close_and_bind(sdp_sd, shadow_fd, (struct sockaddr *) &tmp_addr,
							   addrlen, semantics);

			if (ret < 0) {
				__sdp_log(9,
						  "Error bind: " "Could not close_and_bind sdp side\n");
				goto done;
			}
		}
		goto done;
	}

	/* we can only get here on single TCP socket */
	__sdp_log(1, "BIND: binding TCP socket:<%d>\n", fd);
	ret = __bind_semantics(fd, addr, addrlen, semantics);

done:
	__sdp_log(2, "BIND: <%s:%d:%d> result <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret, sret);

	return ret;
}								/* bind */


int bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
{
	return __perform_bind(fd, my_addr, addrlen, SOCKET_SEMANTIC_DEFAULT);
}

#ifdef SOLARIS_BUILD
int __xnet_bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
{
	return __perform_bind(fd, my_addr, addrlen, SOCKET_SEMANTIC_XNET);
}
#endif


/* ========================================================================= */
/*..connect -- replacement connect call.                                     */
/*
  Given the connect address we can take out AF decision                     
  if target AF == both it means SDP and fall back to TCP                   
  if any connect worked we are fine
*/
static inline int
__connect_semantics(int fd,
					const struct sockaddr *serv_addr,
					socklen_t addrlen, int semantics)
{
	return
#ifdef SOLARIS_BUILD
		(semantics == SOCKET_SEMANTIC_XNET) ?
		_socket_xnet_funcs.connect(fd, serv_addr, addrlen) :
#endif
		_socket_funcs.connect(fd, serv_addr, addrlen);
}

static int
__perform_connect(int fd, const struct sockaddr *serv_addr,
		socklen_t addrlen, int semantics)
{
	struct sockaddr_in *serv_sin = (struct sockaddr_in *) serv_addr;
	char buf[MAX_ADDR_STR_LEN];
	int shadow_fd;
	int ret = -1, dup_ret;
	use_family_t target_family;
	int fopts;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.connect) {
		__sdp_log(9, "Error connect: no implementation for connect found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	if ((serv_addr == NULL) || is_invalid_addr(serv_addr)) {
		errno = EFAULT;
		__sdp_log(9, "Error connect: illegal address provided\n");
		return -1;
	}

	if (get_addr_str(serv_addr, buf, MAX_ADDR_STR_LEN)) {
		__sdp_log(9, "Error connect: provided illegal address: %s\n",
				  strerror(errno));
		return EADDRNOTAVAIL;
	}

	__sdp_log(2, "CONNECT: <%s:%d:%d> domain <%d> IP <%s> port <%d>\n",
			  program_invocation_short_name, fd, shadow_fd,
			  serv_sin->sin_family, buf, ntohs(serv_sin->sin_port));


	/* obtain the target address family */
	target_family = __sdp_match_connect(serv_addr, addrlen);

	/* if we do not have a shadow - just do the work */
	if (shadow_fd == -1) {
		__sdp_log(1, "CONNECT: connectingthrough %s\n",
				get_is_sdp_socket(fd) ? "SDP" : "TCP");
		ret = __connect_semantics(fd, serv_addr, addrlen, semantics);
		if ((ret == 0) || (errno == EINPROGRESS)) {
			__sdp_log(7, "CONNECT: connected SDP fd:%d to:%s port %d\n",
					fd, buf, ntohs(serv_sin->sin_port));
		}
		goto done;
	}

	if ((target_family == USE_SDP) || (target_family == USE_BOTH)) {
		/* NOTE: the entire if sequence is negative logic */
		__sdp_log(1, "CONNECT: connecting SDP fd:%d\n", shadow_fd);

		/* make the socket blocking on shadow SDP */
		fopts = _socket_funcs.fcntl(shadow_fd, F_GETFL);
		if ((target_family == USE_BOTH) && (fopts & O_NONBLOCK)) {
			__sdp_log(1,
					  "CONNECT: shadow_fd <%d> will be blocking during connect\n",
					  shadow_fd);
			_socket_funcs.fcntl(shadow_fd, F_SETFL, fopts & (~O_NONBLOCK));
		}

		ret = __connect_semantics(shadow_fd, serv_addr, addrlen, semantics);
		if ((ret < 0) && (errno != EINPROGRESS)) {
			__sdp_log(9, "Error connect: "
					  "failed for SDP fd:%d with error:%m\n", shadow_fd);
		} else {
			__sdp_log(7, "CONNECT: connected SDP fd:%d to:%s port %d\n",
					  fd, buf, ntohs(serv_sin->sin_port));
		}

		/* restore socket options */
		_socket_funcs.fcntl(shadow_fd, F_SETFL, fopts);

		/* if target is SDP or we succeeded we need to dup SDP fd into TCP fd */
		if ((target_family == USE_SDP) || (ret >= 0)) {
			dup_ret = replace_fd_with_its_shadow(fd);
			if (dup_ret < 0) {
				__sdp_log(9, "Error connect: "
						  "failed to dup2 shadow into orig fd:%d\n", fd);
				ret = dup_ret;
			} else {
				/* we can skip the TCP option if we are done */
				__sdp_log(1, "CONNECT: "
						  "matched SDP fd:%d so shadow dup into TCP\n", fd);
				goto done;
			}
		}
	}

	if ((target_family == USE_TCP) || (target_family == USE_BOTH)) {
		__sdp_log(1, "CONNECT: connecting TCP fd:%d\n", fd);
		ret = __connect_semantics(fd, serv_addr, addrlen, semantics);
		if ((ret < 0) && (errno != EINPROGRESS))
			__sdp_log(9, "Error connect: for TCP fd:%d failed with error:%m\n",
					  fd);
		else
			__sdp_log(7, "CONNECT: connected TCP fd:%d to:%s port %d\n",
					  fd, buf, ntohs(serv_sin->sin_port));

		if ((target_family == USE_TCP) || (ret >= 0) || (errno == EINPROGRESS)) {
			if (cleanup_shadow(fd) < 0)
				__sdp_log(9,
						  "Error connect: failed to cleanup shadow for fd:%d\n",
						  fd);
		}
	}

done:
	__sdp_log(2, "CONNECT: <%s:%d:%d> result <%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret);

	return ret;
}								/* connect */

int connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
{
	return __perform_connect(fd, serv_addr, addrlen, SOCKET_SEMANTIC_DEFAULT);
}

#if defined( SOLARIS_BUILD )
int __xnet_connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
{
	return __perform_connect(fd, serv_addr, addrlen, SOCKET_SEMANTIC_XNET);
}
#endif

/* ========================================================================= */
/*..listen -- replacement listen call.                                       */
/* 
   Now we know our role (passive/server) and our address so we can get AF.
   If both we should try listening on both
*/

static inline int __listen_semantics(int fd, int backlog, int semantics)
{
	return
#ifdef SOLARIS_BUILD
		(semantics == SOCKET_SEMANTIC_XNET) ?
		_socket_xnet_funcs.listen(fd, backlog) :
#endif
		_socket_funcs.listen(fd, backlog);
}

static int __perform_listen(int fd, int backlog, int semantics)
{
	use_family_t target_family;
	int shadow_fd;
	int ret = 0, sret = 0;
	struct sockaddr_storage tmp_sin;
	socklen_t tmp_sinlen = sizeof(tmp_sin);
	struct sockaddr_in *sin4 = (struct sockaddr_in *) &tmp_sin;
	char buf[MAX_ADDR_STR_LEN];
	int actual_port;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.listen) {
		__sdp_log(9, "Error listen: no implementation for listen found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);
	__sdp_log(2, "LISTEN: <%s:%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd);

	/* if there is no shadow - simply call listen */
	if (shadow_fd == -1) {
		__sdp_log(1, "LISTEN: calling listen on fd:%d\n", fd);
		ret = __listen_semantics(fd, backlog, semantics);
		goto done;
	}

	/* we need to obtain the address from the fd */
	if (_socket_funcs.getsockname(fd, (struct sockaddr *) &tmp_sin, &tmp_sinlen)
		< 0) {
		__sdp_log(9, "Error listen: getsockname return <%d> for TCP socket\n",
				  errno);
		errno = EADDRNOTAVAIL;
		sret = -1;
		goto done;
	}

	if (get_addr_str((struct sockaddr *) &tmp_sin, buf, MAX_ADDR_STR_LEN)) {
		__sdp_log(9, "Error listen: provided illegal address: %s\n",
				  strerror(errno));
	}

	__sdp_log(2, "LISTEN: <%s:%d:%d> domain <%d> IP <%s> port <%d>\n",
			  program_invocation_short_name, fd, shadow_fd,
			  sin4->sin_family, buf, ntohs(sin4->sin_port));

	target_family =
		__sdp_match_listen((struct sockaddr *) &tmp_sin, sizeof(tmp_sin));

	/* 
	 * in case of an implicit bind and "USE_BOTH" rule we need to first bind the 
	 * two sockets to the same port number 
	 */
	actual_port = ntohs(sin4->sin_port);

	/* do we need to implicit bind both */
	if ((actual_port == 0) && (target_family == USE_BOTH)) {
		int sdp_sd = -1, tcp_sd = -1;

		actual_port = find_free_port((struct sockaddr *) &tmp_sin, tmp_sinlen,
				fd, &sdp_sd, &tcp_sd, semantics);
		if (actual_port < 0) {
			ret = -1;
			__sdp_log(8, "LISTEN: Failed to find common free port. Only TCP will be used.\n");
			target_family = USE_TCP;
		} else {
			/* copy the port to the tmp address */
			set_addr_port_num((struct sockaddr *) sin4, actual_port);

			__sdp_log(2, "LISTEN: BOTH on IP <%s> port <%d>\n",
					  buf, actual_port);
			/* perform the bind */
			ret = close_and_bind(tcp_sd, fd, (struct sockaddr *) sin4,
					tmp_sinlen, semantics);
			if (ret < 0) {
				__sdp_log(9, "Error listen: "
						  "Could not close_and_bind TCP side\n");
			}

			ret = close_and_bind(sdp_sd, shadow_fd, (struct sockaddr *) sin4,
					tmp_sinlen, semantics);
			if (ret < 0) {
				__sdp_log(9, "Error listen: "
						  "Could not close_and_bind SDP side\n");
			}
		}
	}

	if ((target_family == USE_TCP) || (target_family == USE_BOTH)) {
		__sdp_log(1, "LISTEN: calling listen on TCP fd:%d\n", fd);
		ret = __listen_semantics(fd, backlog, semantics);
		if (ret < 0) {
			__sdp_log(9, "Error listen: failed with code <%d> on TCP fd:<%d>\n",
					  errno, fd);
		} else {
			__sdp_log(7, "LISTEN: fd:%d listening on TCP bound to:%s port:%d\n",
					  fd, buf, actual_port);
		}
	}

	if ((target_family == USE_SDP) || (target_family == USE_BOTH)) {
		__sdp_log(1, "LISTEN: calling listen on SDP fd:<%d>\n", shadow_fd);
		sret = __listen_semantics(shadow_fd, backlog, semantics);
		if (sret < 0) {
			__sdp_log(9, "Error listen: failed with code <%d> SDP fd:<%d>\n",
					  errno, shadow_fd);
		} else {
			__sdp_log(7, "LISTEN: fd:%d listening on SDP bound to:%s port:%d\n",
					  fd, buf, actual_port);
		}
	}

	/* cleanup the un-needed shadow if TCP and did not fail */
	if ((target_family == USE_TCP) && (ret >= 0)) {
		__sdp_log(1, "LISTEN: cleaning up shadow SDP\n");
		if (cleanup_shadow(fd) < 0)
			__sdp_log(9, "Error listen: failed to cleanup shadow for fd:%d\n",
					  fd);
	}

	/* cleanup the TCP socket and replace with SDP */
	if ((target_family == USE_SDP) && (sret >= 0)) {
		__sdp_log(1, "LISTEN: cleaning TCP socket and dup2 SDP into it\n");
		if (0 > (sret = replace_fd_with_its_shadow(fd)))
			__sdp_log(9, "Error listen: "
					  "failed to dup2 shadow into orig fd:%d\n", fd);
	}

done:
	__sdp_log(2, "LISTEN: <%s:%d:%d> result <%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret);
	/* its a success only if both are ok */
	if (ret < 0)
		return (ret);
	if (sret < 0)
		return (sret);
	return 0;
}								/* listen */

int listen(int fd, int backlog)
{
	return __perform_listen(fd, backlog, SOCKET_SEMANTIC_DEFAULT);
}

#ifdef SOLARIS_BUILD
int __xnet_listen(int fd, int backlog)
{
	return __perform_listen(fd, backlog, SOCKET_SEMANTIC_XNET);
}
#endif

/* ========================================================================= */
/*..close -- replacement close call. */
int close(int fd)
{
	int shadow_fd;
	int ret;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.close) {
		__sdp_log(9, "Error close: no implementation for close found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	__sdp_log(2, "CLOSE: <%s:%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd);

	if (shadow_fd != -1) {
		__sdp_log(1, "CLOSE: closing shadow fd:<%d>\n", shadow_fd);
		if (cleanup_shadow(fd) < 0)
			__sdp_log(9, "Error close: failed to cleanup shadow for fd:%d\n",
					  fd);
	}

	init_extra_attribute(fd);
	ret = _socket_funcs.close(fd);
	__sdp_log(2, "CLOSE: <%s:%d:%d> result <%d>\n",
			  program_invocation_short_name, fd, shadow_fd, ret);
	return ret;
}								/* close */

/* ========================================================================= */
/*..dup -- replacement dup call.                                             */
/* we duplicate the fd and its shadow if exists - ok if the main worked      */
int dup(int fd)
{
	int newfd, new_shadow_fd = -1;
	int shadow_fd;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.dup) {
		__sdp_log(9, "Error dup: no implementation for dup found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);

	__sdp_log(2, "DUP: <%s:%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd);

	__sdp_log(1, "DUP: duplication fd:<%d>\n", fd);
	newfd = _socket_funcs.dup(fd);

	if (newfd == fd)
		return (fd);

	if (!is_valid_fd(newfd)) {
		__sdp_log(9, "Error dup: new fd <%d> out of range.\n", newfd);
	} else {
		/* copy attributes from old fd */
		libsdp_fd_attributes[newfd] = libsdp_fd_attributes[fd];
		libsdp_fd_attributes[newfd].shadow_fd = -1;

		if (shadow_fd != -1) {
			__sdp_log(1, "DUP: duplication shadow fd:<%d>\n", shadow_fd);
			new_shadow_fd = _socket_funcs.dup(shadow_fd);
			if ((new_shadow_fd > max_file_descriptors) || (new_shadow_fd < 0)) {
				__sdp_log(9, "Error dup: new shadow fd <%d> out of range.\n",
						  new_shadow_fd);
			} else {
				libsdp_fd_attributes[new_shadow_fd] =
					libsdp_fd_attributes[shadow_fd];
				libsdp_fd_attributes[newfd].shadow_fd = new_shadow_fd;
			}
		}						/* shadow exists */
	}

	__sdp_log(2, "DUP: <%s:%d:%d> return <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, newfd,
			  new_shadow_fd);

	return newfd;
}								/* dup */

/* ========================================================================= */
/*..dup2 -- replacement dup2 call.                                           */
/* since only the main new fd is given we only move the shadow if exists     */
int dup2(int fd, int newfd)
{
	int shadow_fd;
	int shadow_newfd;
	int new_shadow_fd = -1;
	int ret = 0;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.dup2) {
		__sdp_log(9, "Error dup2: no implementation for dup2 found\n");
		return -1;
	}

	shadow_fd = get_shadow_fd_by_fd(fd);
	shadow_newfd = get_shadow_fd_by_fd(newfd);

	__sdp_log(2, "DUP2: <%s:%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd);

	if (newfd == fd) {
		__sdp_log(1, "DUP2: skip duplicating fd:<%d> into:<%d>\n", fd, newfd);
		goto done;
	}

	/* dup2 closes the target file desc if it is a valid fd */
	if (shadow_newfd != -1) {
		__sdp_log(1, "DUP2: closing newfd:<%d> shadow:<%d>\n", newfd,
				  shadow_newfd);
		ret = _socket_funcs.close(shadow_newfd);
		if (ret != 0) {
			__sdp_log(9,
					  "DUP2: fail to close newfd:<%d> shadow:<%d> with: %d %s\n",
					  newfd, shadow_newfd, ret, strerror(errno));
		}
	}

	__sdp_log(1, "DUP2: duplicating fd:<%d> into:<%d>\n", fd, newfd);
	newfd = _socket_funcs.dup2(fd, newfd);
	if ((newfd > max_file_descriptors) || (newfd < 0)) {
		__sdp_log(9, "Error dup2: new fd <%d> out of range.\n", newfd);
	} else {
		/* copy attributes from old fd */
		libsdp_fd_attributes[fd].shadow_fd = -1;
		libsdp_fd_attributes[newfd] = libsdp_fd_attributes[fd];

		/* if it had a shadow create a new shadow */
		if (shadow_fd != -1) {
			__sdp_log(1, "DUP2: duplication shadow fd:<%d>\n", shadow_fd);
			new_shadow_fd = _socket_funcs.dup(shadow_fd);
			if ((new_shadow_fd > max_file_descriptors) || (new_shadow_fd < 0)) {
				__sdp_log(9, "Error dup2: new shadow fd <%d> out of range.\n",
						  new_shadow_fd);
			} else {
				libsdp_fd_attributes[new_shadow_fd] =
					libsdp_fd_attributes[shadow_fd];
				libsdp_fd_attributes[newfd].shadow_fd = new_shadow_fd;
			}
		}						/* newfd is ok */
	}

done:
	__sdp_log(2, "DUP2: <%s:%d:%d> return <%d:%d>\n",
			  program_invocation_short_name, fd, shadow_fd, newfd,
			  new_shadow_fd);

	return newfd;
}								/* dup */

/* ========================================================================= */
/*..getsockname -- replacement getsocknanme call.                            */
int getsockname(int fd, struct sockaddr *name, socklen_t * namelen)
{
	int ret = 0;
	char buf[MAX_ADDR_STR_LEN];

	if (init_status == 0)
		__sdp_init();

	/*
	 * ensure the SDP protocol family is not exposed to the user, since
	 * this is meant to be a transparency layer.
	 */
	if (NULL == _socket_funcs.getsockname) {
		__sdp_log(9,
				  "Error getsockname: no implementation for getsockname found\n");
		return -1;
	}

	/* double check provided pointers */
	if ((name == NULL) || is_invalid_addr(name)) {
		errno = EFAULT;
		__sdp_log(9, "Error getsockname: illegal address provided\n");
		return -1;
	}

	if ((namelen != NULL) && is_invalid_addr(namelen)) {
		errno = EFAULT;
		__sdp_log(9, "Error getsockname: illegal address length pointer provided\n");
		return -1;
	}

	__sdp_log(2, "GETSOCKNAME <%s:%d>\n", program_invocation_short_name, fd);

	ret = _socket_funcs.getsockname(fd, name, namelen);

	if (__sdp_log_get_level() <= 1) {
		if (get_addr_str(name, buf, MAX_ADDR_STR_LEN)) {
			__sdp_log(1, "GETSOCKNAME: " "address is illegal\n");
		} else {
			__sdp_log(1, "GETSOCKNAME: address is:%s port:%d\n", buf,
					  ntohs(((struct sockaddr_in *) name)->sin_port));
		}
	}
	__sdp_log(2, "GETSOCKNAME <%s:%d> result <%d>\n",
			  program_invocation_short_name, fd, ret);

	return ret;
}								/* getsockname */

/* ========================================================================= */
/*..getpeername -- replacement getpeername call. */
int getpeername(int fd, struct sockaddr *name, socklen_t * namelen)
{
	int ret = 0;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.getpeername) {
		__sdp_log(9, "Error getpeername: "
				  "no implementation for getpeername found\n");
		return -1;
	}

	/* double check provided pointers */
	if ((name == NULL) || is_invalid_addr(name)) {
		errno = EFAULT;
		__sdp_log(9, "Error getsockname: illegal address provided\n");
		return -1;
	}

	if ((namelen != NULL) && is_invalid_addr(namelen)) {
		errno = EFAULT;
		__sdp_log(9,
				  "Error getsockname: illegal address length pointer provided\n");
		return -1;
	}

	__sdp_log(2, "GETPEERNAME <%s:%d>\n", program_invocation_short_name, fd);

	ret = _socket_funcs.getpeername(fd, name, namelen);

	__sdp_log(2, "GETPEERNAME <%s:%d> result <%d:%d> family=%d s_addr=%d\n",
			  program_invocation_short_name, fd, ret,
			  (!(0 > ret) ? 0 : -1), name->sa_family,
			  ((struct sockaddr_in *) name)->sin_addr.s_addr);

	return ret;
}								/* getpeername */



/* ========================================================================= */
/*..accept -- replacement accept call.                                       */
/*
  If we have a shadow we need to decide which socket we want to accept on
  so we select first and then give priority based on previous selection
*/
int accept(int fd, struct sockaddr *addr, socklen_t * addrlen)
{
	int shadow_fd;
	int ret = 0;
	fd_set fds;
	socklen_t saved_addrlen = 0;
	int fopts;
	char buf[MAX_ADDR_STR_LEN];

	if (init_status == 0)
		__sdp_init();

	shadow_fd = get_shadow_fd_by_fd(fd);

	/*
	 * ensure the SDP protocol family is not exposed to the user, since
	 * this is meant to be a transparency layer.
	 */
	if (NULL == _socket_funcs.accept) {
		__sdp_log(9, "Error accept: no implementation for accept found\n");
		return -1;
	}

	/* double check provided pointers */
	if ((addr != NULL) && is_invalid_addr(addr)) {
		errno = EINVAL;
		__sdp_log(9, "Error accept: illegal address provided\n");
		return -1;
	}

	if ((addrlen != NULL) && is_invalid_addr(addrlen)) {
		errno = EINVAL;
		__sdp_log(9, "Error accept: illegal address length pointer provided\n");
		return -1;
	}

	if (addr && addrlen)
		saved_addrlen = *addrlen;

	__sdp_log(2, "ACCEPT: <%s:%d>\n", program_invocation_short_name, fd);

	if (shadow_fd == -1) {
		fopts = _socket_funcs.fcntl(fd, F_GETFL);
		__sdp_log(1, "ACCEPT: fd <%d> opts are <0x%x>\n", fd, fopts);

		__sdp_log(7, "ACCEPT: accepting on single fd:<%d>\n", fd);
		ret = _socket_funcs.accept(fd, addr, addrlen);
		if (ret < 0) {
			if (!(fopts & O_NONBLOCK && errno == EWOULDBLOCK))
				__sdp_log(9, "Error accept: accept returned :<%d> %s\n",
						  ret, strerror(errno));
		} else {
			set_is_sdp_socket(ret, get_is_sdp_socket(fd));
		}
	} else {

		fopts = _socket_funcs.fcntl(shadow_fd, F_GETFL);
		__sdp_log(1, "ACCEPT: shadow_fd <%d> opts are <0x%x>\n",
				  shadow_fd, fopts);

		/* we need different behavior for NONBLOCK or signal IO and BLOCK */
		if ((fopts > 0) && (fopts & (O_NONBLOCK | FASYNC))) {
			__sdp_log(1, "ACCEPT: accepting (nonblock) on SDP fd:<%d>\n", shadow_fd);

			ret = _socket_funcs.accept(shadow_fd, addr, addrlen);
			if (ret >= 0) {
				set_is_sdp_socket(ret, 1);

				__sdp_log(7, "ACCEPT: accepted (nonblock) SDP fd:<%d>\n",
						  shadow_fd);
			} else {
				__sdp_log(1, "ACCEPT: accept on SDP fd:<%d> return:%d errno:%d\n",
						  shadow_fd, ret, errno);

				__sdp_log(1, "ACCEPT: accepting (nonblock) on TCP fd:<%d>\n", fd);
				ret = _socket_funcs.accept(fd, addr, addrlen);
				if (ret >= 0) {
					__sdp_log(7, "ACCEPT: accepted (nonblock) TCP fd:<%d>\n",
							  shadow_fd);
				} else {
					__sdp_log(1, "ACCEPT: accept on TCP fd:<%d> "
							  "return:%d errno:%d\n", fd, ret, errno);
				}
			}
		} else {
			__sdp_log(1, "ACCEPT: selecting both fd:<%d> and shadow:<%d>\n",
					  fd, shadow_fd);
			FD_ZERO(&fds);
			FD_SET(fd, &fds);
			FD_SET(shadow_fd, &fds);
			ret =
				_socket_funcs.select(1 + ((fd > shadow_fd) ? fd : shadow_fd),
									 &fds, NULL, NULL, NULL);
			if (ret >= 0) {
				if (last_accept_was_tcp(fd) == 0) {
					if (FD_ISSET(fd, &fds)) {
						set_last_accept(fd, 1);
						__sdp_log(7, "ACCEPT: accepting on TCP fd:<%d>\n", fd);
						ret = _socket_funcs.accept(fd, addr, addrlen);
					} else {
						__sdp_log(7, "ACCEPT: accepting on SDP fd:<%d>\n",
								  shadow_fd);
						ret = _socket_funcs.accept(shadow_fd, addr, addrlen);
						if (ret >= 0)
							set_is_sdp_socket(ret, 1);
					}
				} else {
					if (FD_ISSET(shadow_fd, &fds)) {
						set_last_accept(fd, 1);
						__sdp_log(7, "ACCEPT: accepting on SDP fd:<%d>\n",
								  shadow_fd);
						ret = _socket_funcs.accept(shadow_fd, addr, addrlen);
						if (ret >= 0)
							set_is_sdp_socket(ret, 1);
					} else {
						__sdp_log(7, "ACCEPT: accepting on TCP fd:<%d>\n", fd);
						ret = _socket_funcs.accept(fd, addr, addrlen);
					}
				}
			} else {
				if (errno != EINTR) {
					__sdp_log(9,
							  "Error accept: select returned :<%d> (%d) %s\n",
							  ret, errno, strerror(errno));
				} else {
					__sdp_log(1, "ACCEPT: select returned :<%d> (%d) %s\n",
							  ret, errno, strerror(errno));
				}
			}
		}						/* blocking mode */
	}							/* shadow fd */

	if ((__sdp_log_get_level() <= 1) && (ret >= 0) && addr && addrlen) {
		get_addr_str(addr, buf, *addrlen);
		__sdp_log(1, "ACCEPT: accepted from:%s port:%d into fd:%d\n",
				  buf, ntohs(((struct sockaddr_in *) addr)->sin_port), ret);
	}
	__sdp_log(2, "ACCEPT: <%s:%d> return <%d>\n",
			  program_invocation_short_name, fd, ret);

	return ret;
}								/* accept */

/* ========================================================================= */
/*..select -- replacement socket call.                                       */
/* 
   if we have shadow we must select on it too - which requires a hack back 
   and forth
*/
int
select(int n,
	   fd_set * readfds,
	   fd_set * writefds, fd_set * exceptfds, struct timeval *timeout)
{
	int shadow_fd;
	int ret;
	int current;
	int maxi = 0;
	fd_set new_fds;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.select) {
		__sdp_log(9, "Error select: no implementation for select found\n");
		return -1;
	}

	__sdp_log(2, "SELECT: <%s:%d>\n", program_invocation_short_name, n);

	/* if we do not read - nothing to do */
	if (readfds == NULL) {
		ret = _socket_funcs.select(n, readfds, writefds, exceptfds, timeout);
		goto done;
	}

	FD_ZERO(&new_fds);
	if (n > 0) {
		maxi = n - 1;
	}

	/* add shadow bits */
	for (current = 0; current < n; current++) {
		if (FD_ISSET(current, readfds)) {
			FD_SET(current, &new_fds);
			if (current > maxi) {
				maxi = current;
			}
			shadow_fd = get_shadow_fd_by_fd(current);
			if (shadow_fd != -1) {
				__sdp_log(1,
						  "SELECT: adding fd:<%d> shadow_fd:<%d> to readfs\n",
						  current, shadow_fd);
				FD_SET(shadow_fd, &new_fds);
				if (shadow_fd > maxi) {
					maxi = shadow_fd;
				}
			}
		}
	}

	__sdp_log(1, "SELECT: invoking select n=<%d>\n", 1 + maxi);
	ret = _socket_funcs.select(1 + maxi,
							   &new_fds, writefds, exceptfds, timeout);

	/* remove the count and bits of the shadows */
	if (ret >= 0) {
		for (current = 0; current < n; current++) {
			shadow_fd = get_shadow_fd_by_fd(current);
			if (shadow_fd == -1) {
				if (FD_ISSET(current, readfds) &&
					FD_ISSET(current, &new_fds) == 0) {
					FD_CLR(current, readfds);
				}
			} else {
				if (FD_ISSET(current, readfds) && FD_ISSET(current, &new_fds)
					&& FD_ISSET(shadow_fd, &new_fds)) {
					ret -= 1;
				}
				if (FD_ISSET(current, readfds) &&
					FD_ISSET(current, &new_fds) == 0 &&
					FD_ISSET(shadow_fd, &new_fds) == 0) {
					FD_CLR(current, readfds);
				}
			}
		}
	}

done:

	__sdp_log(2, "SELECT: <%s:%d> return <%d>\n",
			  program_invocation_short_name, n, ret);
	return ret;
}								/* select */

/* ========================================================================= */
/*..pselect -- replacement socket call.                                      */
/* 
   if we have shadow we must pselect on it too - which requires a hack back 
   and forth
*/
int
pselect(int n,
		fd_set * readfds,
		fd_set * writefds,
		fd_set * exceptfds,
		const struct timespec *timeout, const sigset_t * sigmask)
{
	int shadow_fd;
	int ret;
	int current;
	int maxi = 0;
	fd_set new_fds;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.pselect) {
		__sdp_log(9, "Error pselect: no implementation for pselect found\n");
		return -1;
	}

	__sdp_log(2, "PSELECT: <%s:%d>\n", program_invocation_short_name, n);

	/* if we do not read - nothing to do */
	if (readfds == NULL) {
		ret =
			_socket_funcs.pselect(n, readfds, writefds, exceptfds, timeout,
								  sigmask);
		goto done;
	}

	FD_ZERO(&new_fds);
	if (n > 0) {
		maxi = n - 1;
	}

	/* add shadow bits */
	for (current = 0; current < n; current++) {
		if (FD_ISSET(current, readfds)) {
			FD_SET(current, &new_fds);
			if (current > maxi) {
				maxi = current;
			}
			shadow_fd = get_shadow_fd_by_fd(current);
			if (shadow_fd != -1) {
				__sdp_log(1,
						  "PSELECT: adding fd:<%d> shadow_fd:<%d> to readfs\n",
						  current, shadow_fd);
				FD_SET(shadow_fd, &new_fds);
				if (shadow_fd > maxi) {
					maxi = shadow_fd;
				}
			}
		}
	}

	__sdp_log(1, "PSELECT: invoking pselect n=<%d>\n", 1 + maxi);
	ret = _socket_funcs.pselect(1 + maxi,
								&new_fds, writefds, exceptfds,
								timeout, sigmask);

	/* remove the count and bits of the shadows */
	if (ret >= 0) {
		for (current = 0; current < n; current++) {
			shadow_fd = get_shadow_fd_by_fd(current);
			if (shadow_fd == -1) {
				if (FD_ISSET(current, readfds) &&
					FD_ISSET(current, &new_fds) == 0) {
					FD_CLR(current, readfds);
				}
			} else {
				if (FD_ISSET(current, readfds) && FD_ISSET(current, &new_fds)
					&& FD_ISSET(shadow_fd, &new_fds)) {
					ret -= 1;
				}
				if (FD_ISSET(current, readfds) &&
					FD_ISSET(current, &new_fds) == 0 &&
					FD_ISSET(shadow_fd, &new_fds) == 0) {
					FD_CLR(current, readfds);
				}
			}
		}
	}

done:

	__sdp_log(2, "PSELECT: <%s:%d> return <%d>\n",
			  program_invocation_short_name, n, ret);
	return ret;
}								/* pselect */

/* ========================================================================= */
/*..poll -- replacement socket call.                                      */
/* 
   if we have shadow we must poll on it too - which requires a hack back 
   and forth
*/
int poll(struct pollfd *ufds, nfds_t nfds, int timeout)
{
	int ret;
	int shadow_fd;
	int current;
	int extra = 0;
	struct pollfd *poll_fds = NULL;
	struct pollfd *poll_fd_ptr = NULL;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.poll) {
		__sdp_log(9, "Error poll: no implementation for poll found\n");
		return -1;
	}

	__sdp_log(2, "POLL: <%s:%d>\n", program_invocation_short_name, nfds);

	/* if we do not have any file desc - nothing to do */
	if (ufds == NULL) {
		ret = _socket_funcs.poll(ufds, nfds, timeout);
		goto done;
	}

	/* scan for how many extra fds are required */
	for (current = 0; current < nfds; current++) {
		shadow_fd = get_shadow_fd_by_fd(ufds[current].fd);
		if (shadow_fd != -1)
			extra++;
	}

	if (!extra) {
		poll_fds = ufds;
	} else {
		poll_fds =
			(struct pollfd *) malloc((nfds + extra) * sizeof(struct pollfd));
		if (!poll_fds) {
			__sdp_log(9,
					  "Error poll: malloc of extended pollfd array failed\n");
			ret = -1;
			errno = ENOMEM;
			goto done;
		}
		poll_fd_ptr = poll_fds;
		for (current = 0; current < nfds; current++) {
			*poll_fd_ptr = ufds[current];
			poll_fd_ptr++;
			shadow_fd = get_shadow_fd_by_fd(ufds[current].fd);
			if (shadow_fd != -1) {
				__sdp_log(1, "POLL: adding fd:<%d> shadow_fd:<%d> to readfs\n",
						  current, shadow_fd);
				*poll_fd_ptr = ufds[current];
				poll_fd_ptr->fd = shadow_fd;
				poll_fd_ptr++;
			}
		}
	}

	__sdp_log(1, "POLL: invoking poll nfds=<%d>\n", nfds + extra);
	ret = _socket_funcs.poll(poll_fds, nfds + extra, timeout);

	/* refactor into original list if any events */
	if ((ret > 0) && extra) {
		poll_fd_ptr = poll_fds;
		for (current = 0; current < nfds; current++) {
			shadow_fd = get_shadow_fd_by_fd(ufds[current].fd);
			if (shadow_fd == -1) {
				ufds[current] = *poll_fd_ptr;
			} else {
				ufds[current] = *poll_fd_ptr;
				poll_fd_ptr++;
				if (poll_fd_ptr->revents) {
					if (ufds[current].revents)
						ret--;
					ufds[current].revents |= poll_fd_ptr->revents;
				}
			}
			poll_fd_ptr++;
		}
	}

	if (extra)
		free(poll_fds);
done:

	__sdp_log(2, "POLL: <%s:%d> return <%d>\n",
			  program_invocation_short_name, nfds, ret);
	return ret;
}								/* poll */

#ifdef __linux__
/* ========================================================================= */
/*..epoll_create -- replacement socket call.                                 */
/*
   Need to make the size twice as large for shadow fds
*/
int epoll_create(int size)
{
	int epfd;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.epoll_create) {
		__sdp_log(9,
				  "Error epoll_create: no implementation for epoll_create found\n");
		return -1;
	}

	__sdp_log(2, "EPOLL_CREATE: <%s:%d>\n", program_invocation_short_name,
			  size);

	epfd = _socket_funcs.epoll_create(size * 2);

	__sdp_log(2, "EPOLL_CREATE: <%s:%d> return %d\n",
			  program_invocation_short_name, size, epfd);
	return epfd;
}								/* epoll_create */

/* ========================================================================= */
/*..epoll_ctl -- replacement socket call.                                   */
/*
   Need to add/delete/modify shadow fds as well
*/
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
	int ret, shadow_fd, ret2;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.epoll_ctl) {
		__sdp_log(9,
				  "Error epoll_ctl: no implementation for epoll_ctl found\n");
		return -1;
	}

	__sdp_log(2, "EPOLL_CTL: <%s:%d> op <%d:%d>\n",
			  program_invocation_short_name, epfd, op, fd);

	ret = _socket_funcs.epoll_ctl(epfd, op, fd, event);

	shadow_fd = get_shadow_fd_by_fd(fd);
	if (shadow_fd != -1) {
		ret2 = _socket_funcs.epoll_ctl(epfd, op, shadow_fd, event);
		if (ret2 < 0) {
			__sdp_log(9, "Error epoll_ctl <%s:%d:%d>",
					  program_invocation_short_name, fd, shadow_fd);
			return ret2;
		}
	}

	__sdp_log(2, "EPOLL_CTL: <%s:%d> return <%d>\n",
			  program_invocation_short_name, epfd, ret);
	return ret;
}								/* epoll_ctl */

/* ========================================================================= */
/*..epoll_wait -- replacement socket call.                                   */
/*
   We don't care who generated the event because all we get is user-context
   values.
*/
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
{
	int ret;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.epoll_wait) {
		__sdp_log(9,
				  "Error epoll_wait: no implementation for epoll_wait found\n");
		return -1;
	}

	__sdp_log(2, "EPOLL_WAIT: <%s:%d>\n", program_invocation_short_name, epfd);

	ret = _socket_funcs.epoll_wait(epfd, events, maxevents, timeout);

	__sdp_log(2, "EPOLL_WAIT: <%s:%d> return <%d>\n",
			  program_invocation_short_name, epfd, ret);
	return ret;
}								/* epoll_wait */

/* ========================================================================= */
/*..epoll_pwait -- replacement socket call.                                  */
/*
   We don't care who generated the event because all we get is user-context
   values.
*/
int
epoll_pwait(int epfd,
			struct epoll_event *events,
			int maxevents, int timeout, const sigset_t * sigmask)
{
	int ret;

	if (init_status == 0)
		__sdp_init();

	if (NULL == _socket_funcs.epoll_pwait) {
		__sdp_log(9,
				  "Error epoll_pwait: no implementation for epoll_pwait found\n");
		return -1;
	}

	__sdp_log(2, "EPOLL_PWAIT: <%s:%d>\n", program_invocation_short_name, epfd);

	ret = _socket_funcs.epoll_pwait(epfd, events, maxevents, timeout, sigmask);

	__sdp_log(2, "EPOLL_PWAIT: <%s:%d> return <%d>\n",
			  program_invocation_short_name, epfd, ret);
	return ret;
}								/* epoll_pwait */
#endif

/* ========================================================================= */

/* --------------------------------------------------------------------- */
/*                                                                       */
/* Library load/unload initialization/cleanup                            */
/*                                                                       */
/* --------------------------------------------------------------------- */
/* ========================================================================= */
/*..__sdp_init -- intialize the library */
void __sdp_init(void)
{
	char *config_file, *error_str;
	int fd;
	struct rlimit nofiles_limit;

	/* HACK: races might apply here: can we assume init is happening
	   only within one thread ? */
	if (init_status != 0)
		return;
	init_status = 1;

	dev_null_fd = open("/dev/null", O_WRONLY);

	/* figure out the max number of file descriptors */
	if (getrlimit(RLIMIT_NOFILE, &nofiles_limit))
		max_file_descriptors = 1024;
	else
		max_file_descriptors = nofiles_limit.rlim_cur;

	/* allocate and initialize the shadow sdp sockets array */
	libsdp_fd_attributes =
		(struct sdp_extra_fd_attributes *) calloc(max_file_descriptors,
												  sizeof(struct
														 sdp_extra_fd_attributes));
	for (fd = 0; fd < max_file_descriptors; fd++)
		init_extra_attribute(fd);

#ifndef RTLD_NEXT
	/*
	 * open libc for original socket call.
	 * Solaris relies on RTLD next - since the socket calls are
	 * actually in libsocket rather than libc.
	 */
	__libc_dl_handle = dlopen("/lib64/libc.so.6", RTLD_LAZY);
	if (NULL == __libc_dl_handle) {
		__libc_dl_handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
		if (NULL == __libc_dl_handle) {
			fprintf(stderr, "%s\n", dlerror());
			return;
		}
	}
#endif

	/*
	 * Get the original functions
	 */
	_socket_funcs.ioctl = dlsym(__libc_dl_handle, "ioctl");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.fcntl = dlsym(__libc_dl_handle, "fcntl");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.socket = dlsym(__libc_dl_handle, "socket");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.setsockopt = dlsym(__libc_dl_handle, "setsockopt");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.connect = dlsym(__libc_dl_handle, "connect");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.listen = dlsym(__libc_dl_handle, "listen");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.bind = dlsym(__libc_dl_handle, "bind");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.close = dlsym(__libc_dl_handle, "close");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.dup = dlsym(__libc_dl_handle, "dup");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.dup2 = dlsym(__libc_dl_handle, "dup2");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.getpeername = dlsym(__libc_dl_handle, "getpeername");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.getsockname = dlsym(__libc_dl_handle, "getsockname");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.accept = dlsym(__libc_dl_handle, "accept");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.select = dlsym(__libc_dl_handle, "select");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.pselect = dlsym(__libc_dl_handle, "pselect");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.poll = dlsym(__libc_dl_handle, "poll");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

#ifdef __linux__
	_socket_funcs.epoll_create = dlsym(__libc_dl_handle, "epoll_create");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.epoll_ctl = dlsym(__libc_dl_handle, "epoll_ctl");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.epoll_wait = dlsym(__libc_dl_handle, "epoll_wait");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_funcs.epoll_pwait = dlsym(__libc_dl_handle, "epoll_pwait");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}
#endif
#ifdef SOLARIS_BUILD
	_socket_xnet_funcs.socket = dlsym(__libc_dl_handle, "__xnet_socket");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_xnet_funcs.connect = dlsym(__libc_dl_handle, "__xnet_connect");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_xnet_funcs.listen = dlsym(__libc_dl_handle, "__xnet_listen");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	_socket_xnet_funcs.bind = dlsym(__libc_dl_handle, "__xnet_bind");
	if (NULL != (error_str = dlerror())) {
		fprintf(stderr, "%s\n", error_str);
	}

	/* Determine program name by asking libdl */
	Dl_argsinfo args_info;
	if (NULL != dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args_info)) {
		fprintf(stderr, "args_info: %s\n", dlerror());
	} else {
		program_invocation_name = args_info.dla_argv[0];
		program_invocation_short_name = basename(args_info.dla_argv[0]);
	}
#endif

	if (getenv("SIMPLE_LIBSDP") != NULL) {
		simple_sdp_library = 1;
	}

	if (getenv("ALWAYS_USE_SDP") != NULL) {
		simple_sdp_library = 1;
	}
#define LIBSDP_DEFAULT_CONFIG_FILE  SYSCONFDIR "/libsdp.conf"
	if (!simple_sdp_library) {
		config_file = getenv("LIBSDP_CONFIG_FILE");
		if (!config_file)
			config_file = LIBSDP_DEFAULT_CONFIG_FILE;

		if (__sdp_parse_config(config_file)) {
			fprintf(stderr,
					"libsdp Error: failed to parse config file:%s. Using defaults.\n",
					config_file);
		}
	}

	__sdp_log(1, "Max file descriptors:%d\n", max_file_descriptors);
	init_status = 2;

}								/* __sdp_init */

/* ========================================================================= */
/*..__sdp_fini -- when the library is unloaded this is called */
void __sdp_fini(void)
{
	struct use_family_rule *rule;
	for (rule = __sdp_clients_family_rules_head; rule != NULL;
		 rule = rule->next)
		free(rule->prog_name_expr);
	for (rule = __sdp_servers_family_rules_head; rule != NULL;
		 rule = rule->next)
		free(rule->prog_name_expr);

	free(libsdp_fd_attributes);

#ifndef RTLD_NEXT
	dlclose(__libc_dl_handle);
#endif
}								/* _fini */
OpenPOWER on IntegriCloud