File: sctp_darn.c

package info (click to toggle)
lksctp-tools 1.0.19%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 1,272 kB
  • sloc: ansic: 13,566; makefile: 270; sh: 26
file content (2414 lines) | stat: -rw-r--r-- 59,496 bytes parent folder | download | duplicates (2)
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
/* SCTP kernel Implementation
 * (C) Copyright IBM Corp. 2001, 2003
 * Copyright (c) 1999 Cisco
 * Copyright (c) 1999, 2000, 2001 Motorola
 * Copyright (c) 2001 Nokia
 * Copyright (c) 2001 La Monte H.P. Yarroll
 *
 * The SCTP implementation is free software;
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * The SCTP implementation is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Please send any bug reports or fixes you make to the
 * email address(es):
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
 *
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *
 * Any bugs reported to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 *
 * Written or modified by:
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson <karl@athena.chicago.il.us>
 *    Hui Huang <hui.huang@nokia.com>
 *    Daisy Chang <daisyc@us.ibm.com>
 *    Sridhar Samudrala <sri@us.ibm.com>
 */

/* This is a userspace test application for the SCTP kernel 
 * implementation state machine.  It is vaguely inspired by Stevens'
 * program "sock".
 *
 * It has the limited ability to send messages and to listen for messages
 * sent via SCTP.
 */

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
//#define _GNU_SOURCE
#include <getopt.h>
#include <netdb.h>
#include <inttypes.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/poll.h>
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/sctp.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include "sctp_darn.h"

char *TCID = __FILE__;
int TST_TOTAL = 1;
int TST_CNT = 0;

#define GEN_DATA_FIRST	0x21
#define GEN_DATA_LAST	0x7e

/* Display an IPv4 address in readable format.  */
#define NIPQUAD(addr) \
        ((unsigned char *)&addr)[0], \
        ((unsigned char *)&addr)[1], \
        ((unsigned char *)&addr)[2], \
        ((unsigned char *)&addr)[3]

/* Display an IPv6 address in readable format.  */
#define NIP6(addr) \
        ntohs((addr).s6_addr16[0]), \
        ntohs((addr).s6_addr16[1]), \
        ntohs((addr).s6_addr16[2]), \
        ntohs((addr).s6_addr16[3]), \
        ntohs((addr).s6_addr16[4]), \
        ntohs((addr).s6_addr16[5]), \
        ntohs((addr).s6_addr16[6]), \
        ntohs((addr).s6_addr16[7])

/* These are the global options.  */
char *local_host = NULL;
int local_port = 0;
char *remote_host = NULL;
int remote_port = 0;
command_t command = COMMAND_NONE;
struct sockaddr *bindx_add_addrs = NULL;
int bindx_add_count = 0;
struct sockaddr *bindx_rem_addrs = NULL;
int bindx_rem_count = 0;
struct sockaddr *connectx_addrs = NULL;
int connectx_count = 0;
int interactive_mode = 0;
int poll_skn = 0;
int nonblocking = 0;
int opt_space = 0;
char gen_data = GEN_DATA_FIRST;
char *inter_outbuf = NULL;
int inter_outlen = 0;
int inter_sk = 0;
int poll_snd_size = 0;
int use_poll = 0;
int socket_type = SOCK_SEQPACKET;
sctp_assoc_t associd = 0;
int echo = 0;
char *interface = "eth0";
int if_index = 0;
sockaddr_storage_t remote_addr;
sa_family_t ra_family;	/* What family is remote_addr? */
int ra_len = 0;		/* How long is remote_addr? */
void *ra_raw;		/* This is the addr part of remote_addr. */
int new_connection = 1;

enum inter_cmd_num {
	INTER_SND = 0,
	INTER_RCV,
	INTER_SNDBUF,
	INTER_RCVBUF,
	INTER_BINDX_ADD,
	INTER_BINDX_REM,
	INTER_SET_PRIM,
	INTER_SET_PEER_PRIM,
	INTER_SHUTDOWN,
	INTER_ABORT,
	INTER_NODELAY,
	INTER_MAXSEG,
	INTER_HEARTBEAT,
	INTER_GET_STATS
};

enum shutdown_type {
	SHUTDOWN_ABORT = 0,
	SHUTDOWN_SHUTDOWN
};

struct inter_entry {
	char *cmd;
	int cmd_num;
};

struct inter_entry inter_commands[] = {
	{"snd", INTER_SND},
	{"rcv",	INTER_RCV},
	{"sndbuf", INTER_SNDBUF},
	{"rcvbuf", INTER_RCVBUF},
	{"bindx-add", INTER_BINDX_ADD},
	{"bindx-rem", INTER_BINDX_REM},
	{"primary", INTER_SET_PRIM},
	{"peer_primary", INTER_SET_PEER_PRIM},
	{"shutdown", INTER_SHUTDOWN},
	{"abort", INTER_ABORT},
	{"nodelay", INTER_NODELAY},
	{"maxseg", INTER_MAXSEG},
	{"heartbeat", INTER_HEARTBEAT},
	{"stats", INTER_GET_STATS},
	{NULL, -1},
};

#define POLL_SK_MAX 	256	/* The max number of sockets to select/poll. */
int poll_sks[POLL_SK_MAX];		/* The array for using select(). */
struct pollfd poll_fds[POLL_SK_MAX];	/* The array for using poll().  */
#define POLL_SND_SIZE	16384	/* Default message size in the poll mode. */


struct sockaddr *append_addr(const char *parm, struct sockaddr *addrs,
			     int *ret_count) ;
int build_endpoint(char *argv0, int portnum);
static int parse_inter_commands(char *, char *, int);
static void snd_func(char *);
static void sndbuf_func(char *, int, int, int);
static void rcvbuf_func(char *, int, int, int);
static struct sockaddr *get_bindx_addr(char *, int *);
static int bindx_func(char *, int, struct sockaddr *, int, int, int);
static int connectx_func(char *, int, struct sockaddr *, int);
static void  primary_func(char *, int, char *, int);
static void  peer_primary_func(char *, int, char *, int);
static void  spp_hb_demand_func(char *, int, char *, int);
static int nodelay_func(char *, int, int val, int set);
static int maxseg_func(char *, int, int val, int set);
static int shutdown_func(char *argv0, int *skp, int shutdown_type);
static int get_assocstats_func(int, sctp_assoc_t);
static int test_sk_for_assoc(int sk, sctp_assoc_t assoc_id);
static char * gen_message(int);
static sctp_assoc_t test_recv_assoc_change(int);
static sctp_assoc_t test_verify_assoc_change(struct msghdr *);
void print_addr_buf(void * laddrs, int n_laddrs);
int print_sockaddr(struct sockaddr *sa_addr);

int
main(int argc, char *argv[]) {
	int sk = -1;
	int error = 0;
	int i;

	signal(SIGPIPE, SIG_IGN);

	parse_arguments(argc, argv);

	switch(command) {
	case COMMAND_NONE:
		fprintf(stderr, "%s: Please specify a command.\n",
			argv[0]);
		exit(1);
		break;
	case COMMAND_LISTEN:
		sk = build_endpoint(argv[0], local_port);
		error = command_listen(argv[0], sk);
		break;
	case COMMAND_SEND:
		sk = build_endpoint(argv[0], local_port);
		error = command_send(argv[0], &sk);
		break;
	case COMMAND_POLL:
		if (use_poll) {
			for (i = 0; i < poll_skn; i++) {
				poll_fds[i].fd = build_endpoint(argv[0],
					local_port + i);
			}
		} else {
			for (i = 0; i < poll_skn; i++) {
				poll_sks[i] = build_endpoint(argv[0],
					local_port + i);
			}
		}
		error = command_poll(argv[0]);
		break;
	default:
		fprintf(stderr, "%s: illegal command %d\n",
			argv[0], command);
		exit(1);
	}

	/* Shut down the link.  */
	if (COMMAND_POLL != command) {
		close(sk);
	} else {
		/* Shutdown all links.  */
		if (use_poll) {
			for (i = 0; i < poll_skn; i++) {
				close(poll_fds[i].fd);
			}
		} else {
			for (i = 0; i < poll_skn; i++) {
				close(poll_sks[i]);
			}
		}
	}

	exit(error);
}

/********************************************************************
 * 2nd Level Abstractions
 ********************************************************************/

void
parse_arguments(int argc, char *argv[]) {
	int option_index = 0;
	int c;
	struct sockaddr *tmp_addrs = NULL;

	static struct option long_options[] = {
		{"local",	1, 0, 1},
		{"local-port",	1, 0, 2},
		{"remote",	1, 0, 3},
		{"remote-port",	1, 0, 4},
		{"listen",	0, 0, 10},
		{"send",	0, 0, 11},
		{"bindx-add",	1, 0, 15},
		{"bindx-rem",	1, 0, 16},
		{"use-poll",	0, 0, 20},
		{"echo",        0, 0, 'e'},
		{"interface",   optional_argument, 0, 5,},
		{"connectx",    1, 0, 17},
		{0,		0, 0, 0}
	};

	/* Parse the arguments.  */
	while (1) {
		c = getopt_long (argc, argv, "B:H:IP:b:h:i:p:lm:nstz:ec:",
				 long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 0:
			printf("option %s", long_options[option_index].name);
			if (optarg) {
				printf(" with arg %s", optarg);
			}
			printf("\n");
			break;
		case 1:		/* local host */
		case 'H':
			local_host = optarg;
			break;
		case 2:		/* local port */
		case 'P':
			local_port = atoi(optarg);
			break;
		case 3:		/* remote host */
		case 'h':
			remote_host = optarg;
			break;
		case 4:		/* remote port */
		case 'p':
			remote_port = atoi(optarg);
			break;
		case 5:  /* interface for sin6_scope_id */
			if (optarg)
				interface = optarg;
			if_index = if_nametoindex(interface);
			if (!if_index) {
				printf("Interface %s unknown\n", interface);
				exit(1);
			}
			break;
			/* COMMANDS */
		case 10:	/* listen */
		case 'l':
			if (command) {
				fprintf(stderr,
					"%s: pick ONE of listen or send\n",
					argv[0]);
				exit(1);
			} else {
				command = COMMAND_LISTEN;
			}
			break;

		case 11:	/* send */
		case 's':
			if (command) {
				fprintf(stderr,
					"%s: pick ONE of listen or send\n",
					argv[0]);
				exit(1);
			} else {
				command = COMMAND_SEND;
			}
			break;

		case 15:		/* bindx_add */
		case 'B':
			tmp_addrs =
				append_addr(optarg, bindx_add_addrs,
					    &bindx_add_count);
			if (NULL == tmp_addrs) {
				/* We have no memory, so keep fprintf()
				 * from trying to allocate more.
				 */
				fprintf(stderr, "No memory to add ");
				fprintf(stderr, "%s\n", optarg);
				exit(2);
			}
			bindx_add_addrs = tmp_addrs;

			break;

		case 16:		/* bindx_rem */
		case 'b':
			tmp_addrs =
				append_addr(optarg, bindx_rem_addrs,
					    &bindx_rem_count);
			if (NULL == tmp_addrs) {
				/* We have no memory, so keep fprintf()
				 * from trying to allocate more.
				 */
				fprintf(stderr, "No memory to add ");
				fprintf(stderr, "%s\n", optarg);
				exit(2);
			}
			bindx_rem_addrs = tmp_addrs;
			break;
		case 17:		/* connectx */
		case 'c':
			tmp_addrs =
				append_addr(optarg, connectx_addrs,
					    &connectx_count);
			if (NULL == tmp_addrs) {
				/* We have no memory, so keep fprintf()
				 * from trying to allocate more.
				 */
				fprintf(stderr, "No memory to add ");
				fprintf(stderr, "%s\n", optarg);
				exit(2);
			}
			connectx_addrs = tmp_addrs;
			break;
		case 20:		/* use-poll */
			use_poll = 1;
			break;
		case 'I':
			interactive_mode = 1;
			break;
		case 'i':
			command = COMMAND_POLL;
			poll_skn = atoi(optarg);
			if (poll_skn <= 0 || poll_skn > POLL_SK_MAX) {
				fprintf(stderr, "Too many sockets for ");
				fprintf(stderr, "for polling\n");
				exit(2);
			}
			break;
		case 'm':
			opt_space = atoi(optarg);
			break;
		case 'n':
			nonblocking = 1;
			break;
		case 't':
			socket_type = SOCK_STREAM;
			break;
		case 'z':
			poll_snd_size = atoi(optarg);
			if (poll_snd_size <= 0) {
				fprintf(stderr, "Bad message size.\n");
				exit(2);
			}
			break;
		case 'e':
			echo = 1;
			break;
		case '?':
			usage(argv[0]);
			exit(1);

		default:
			printf ("%s: unrecognized option 0%c\n",
				argv[0], c);
			usage(argv[0]);
			exit(1);
		}
	}

	if (optind < argc)
	{
		fprintf(stderr, "%s: non-option arguments are illegal: ",
			argv[0]);
		while (optind < argc)
			fprintf(stderr, "%s ", argv[optind++]);
		fprintf (stderr, "\n");
		usage(argv[0]);
		exit(1);
	}


	if (NULL == local_host) {
		fprintf(stderr, "%s: You MUST provide a local host.\n",
			argv[0]);
		usage(argv[0]);
		exit(1);
	}

	if (command == COMMAND_SEND && NULL == remote_host
	    && connectx_count == 0) {
		fprintf(stderr, "%s: You MUST provide a remote host for sending.\n",
			argv[0]);
		usage(argv[0]);
		exit(1);
	}

	if (remote_host != NULL && connectx_count != 0) {
		fprintf(stderr, "%s: You can not provide both -h and -c options.\n",
			argv[0]);
		usage(argv[0]);
		exit(1);
	}
} /* parse_arguments() */

/* Set up the local endpoint.  */
int
build_endpoint(char *argv0, int portnum)
{
	int retval;
	struct hostent *hst;
	sockaddr_storage_t local_addr;
	sa_family_t la_family;	/* What family is local_addr? */
	int la_len;		/* How long is local_addr? */
	void *la_raw;		/* This is the addr part of local_addr. */
	int error;
	struct sctp_event_subscribe subscribe;

	/* Get the transport address for the local host name.  */
	hst = gethostbyname(local_host);
	if (hst == NULL) {
		hst = gethostbyname2(local_host, AF_INET6);
	}

	if (hst == NULL || hst->h_length < 1) {
		fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host);
		exit(1);
	}

	la_family = hst->h_addrtype;
	switch (la_family) {
	case AF_INET:
		la_len = sizeof(local_addr.v4);
		la_raw = &local_addr.v4.sin_addr;
		local_addr.v4.sin_port = htons(portnum);
		local_addr.v4.sin_family = AF_INET;
		break;
	case AF_INET6:
		la_len = sizeof(local_addr.v6);
		la_raw = &local_addr.v6.sin6_addr;
		local_addr.v6.sin6_port = htons(portnum);
		local_addr.v6.sin6_family = AF_INET6;
		local_addr.v6.sin6_scope_id = if_index;
		break;
	default:
		fprintf(stderr, "Invalid address type.\n");
		exit(1);
		break;
	}
	memcpy(la_raw, hst->h_addr_list[0], hst->h_length);

	/* Create the local endpoint.  */
	retval = socket(la_family, socket_type, IPPROTO_SCTP);
	if (retval < 0) {
		fprintf(stderr, "%s: failed to create socket:  %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	if (SOCK_SEQPACKET == socket_type) {
		memset(&subscribe, 0, sizeof(subscribe));
		subscribe.sctp_data_io_event = 1;
		subscribe.sctp_association_event = 1;
		error = setsockopt(retval, SOL_SCTP, SCTP_EVENTS,
				   (char *)&subscribe, sizeof(subscribe));
		if (error) {
			fprintf(stderr, "SCTP_EVENTS: error: %d\n", error);
			exit(1);
		}
	}

	/* Bind this socket to the test port.  */
	error = bind(retval, &local_addr.sa, la_len);
	if (error != 0) {
		fprintf(stderr, "%s: can not bind to %s:%d: %s.\n",
			argv0, local_host, portnum,
			strerror(errno));
		exit(1);
	}

	/* Do we need to do bindx() to add any additional addresses? */
	if (bindx_add_addrs) {
		if (0 != bindx_func(argv0, retval, bindx_add_addrs,
			bindx_add_count, SCTP_BINDX_ADD_ADDR, portnum)) {
			fprintf(stderr, "bindx_func (add) failed.\n");
			exit(1);
		}
	} /* if (bindx_add_addrs) */

	/* Do we need to do bindx() to remove any bound addresses? */
	if (bindx_rem_addrs) {
		if (0 != bindx_func(argv0, retval, bindx_rem_addrs,
			bindx_rem_count, SCTP_BINDX_REM_ADDR, portnum)) {
			fprintf(stderr, "bindx_func (remove) failed.\n");
			exit(1);
		}
	} /* if (bindx_rem_addrs) */

	/* Do we want to run in the non-blocking mode? */
	if (nonblocking) {
		error = fcntl(retval, F_SETFL, O_NONBLOCK);
		if (error != 0) {
			fprintf(stderr, "%s: error fcntl: %s.\n",
				argv0, strerror(errno));
			exit(1);
		}
	}

	if (opt_space) {
		sndbuf_func(argv0, retval, opt_space, 1);
		rcvbuf_func(argv0, retval, opt_space, 1);
	}

	return retval;

} /* build_endpoint() */

/* Convenience structure to determine space needed for cmsg. */
typedef union {
	struct sctp_initmsg init;
	struct sctp_sndrcvinfo sndrcvinfo;
} _sctp_cmsg_data_t;


/* Listen on the socket, printing out anything that arrives.  */
int
command_listen(char *argv0, int sk)
{
	char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
	struct iovec iov;
	struct msghdr inmessage;
	sockaddr_storage_t msgname;
	char message[REALLY_BIG];
	int done = 0;
	int error;
	int c;
	int recvsk = 0;

 	/* Mark sk as being able to accept new associations */
	error = listen(sk, 5);
	if (error != 0) {
		printf("\n\n\t\tlisten Failure:  %s.\n\n\n",
		       strerror(errno));
		exit(1);
	}

	if (nonblocking) {
		if (!interactive_mode) {
			printf("Use -I for interactive mode with");
		       printf("	-n nonblocking\n");
		       exit(1);
		 }
	}

	/* Initialize the global value for interactive mode functions.  */
	if (interactive_mode) {
		inter_sk = sk;
	}

	/* Initialize inmessage with enough space for DATA... */
	memset(&inmessage, 0, sizeof(inmessage));
	if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
		printf("%s: Can't allocate memory.\n", argv0);
		exit(1);
	}
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	/* or a control message.  */
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);
	inmessage.msg_name = &msgname;
	inmessage.msg_namelen = sizeof(msgname);

	printf("%s listening...\n", argv0);
	/* Get the messages sent */
	done = 0;
	while (!done) {
		if (interactive_mode) {
			/* Read from the user.  */
			if (remote_host) {
				printf("%s:%d-%s:%d Interactive mode> ",
					local_host, local_port, remote_host,
					remote_port);
			} else {
				printf("%s:%d-", local_host, local_port);
				if (associd) {
					print_sockaddr(&remote_addr.sa);
				} else {
					printf("?:%d", remote_port);
				}
				printf(" Interactive mode> ");
			}
			fflush(stdout);
			if (NULL == fgets(message, REALLY_BIG, stdin)) {
				done = 1;
				continue;
			}

			if (0 <= (c = parse_inter_commands(argv0, message,
				0))) {
				if (INTER_RCV != c) {
					continue;
				}
			} else {
				continue;
			}
		}

		if (socket_type == SOCK_STREAM) {
			socklen_t len = 0;

			if (!recvsk) {
				if ((recvsk = accept(sk, NULL, &len)) < 0) {
					fprintf(stderr, "%s: error: %s.\n",
						argv0, strerror(errno));
					exit(1);
				}
			}

		} else {
			recvsk = sk;
		}

		error = recvmsg(recvsk, &inmessage, MSG_WAITALL);
		if (error < 0) {
			if (nonblocking && (EAGAIN == errno)) {
				error = 0;
				continue;
			}

			if (socket_type == SOCK_STREAM) {
				if (ENOTCONN != errno)
					break;
				printf("No association is present now!!\n");
				close(recvsk);
				recvsk = 0;
				continue;
			}
			break;
		}

		/* Update the associd when a notification is received on a
		 * UDP-style socket.
		 */
		if (inmessage.msg_flags & MSG_NOTIFICATION)
			associd = test_verify_assoc_change(&inmessage);

		if (echo) {
			if( !(MSG_NOTIFICATION & inmessage.msg_flags)) {
				if (sendto(recvsk, inmessage.msg_iov->iov_base,
					   error, 0,
					   (struct sockaddr *)&msgname,
					   sizeof(msgname)) == -1) {
					fprintf(stderr, "%s: error: %s.\n",
						argv0, strerror(errno));
					exit(1);
				}
			}
		}

		test_print_message(sk, &inmessage, error);

		inmessage.msg_control = incmsg;
		inmessage.msg_controllen = sizeof(incmsg);
		inmessage.msg_name = &msgname;
		inmessage.msg_namelen = sizeof(msgname);
		iov.iov_len = REALLY_BIG;

		/* Verify that the association is no longer present.  */
		if (0 != test_sk_for_assoc(recvsk, associd)) {
			printf("No association is present now!!\n");
			if (socket_type == SOCK_STREAM) {
				close(recvsk);
				recvsk = 0;
			}
		}
	}

	if (error < 0) {
		fprintf(stderr, "%s: error: %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	return error;

} /* command_listen() */

/* Read lines from stdin and send them to the socket.  */
int
command_send(char *argv0, int *skp)
{
	struct msghdr outmsg;
	struct iovec iov;
	int done = 0;
	char message[REALLY_BIG];
	struct hostent *hst;
	int c;
	struct sockaddr *addrs;
	int msglen;
	int error = 0;
	int sk = *skp;

	/* Set up the destination.  */
	if (remote_host != NULL) {
		hst = gethostbyname(remote_host);
		if (hst == NULL) {
			hst = gethostbyname2(remote_host, AF_INET6);
		}

		if (hst == NULL || hst->h_length < 1) {
			fprintf(stderr, "%s: bad hostname: %s\n",
				argv0, remote_host);
			exit(1);
		}

		ra_family = hst->h_addrtype;
		switch (ra_family) {
		case AF_INET:
			ra_len = sizeof(remote_addr.v4);
			ra_raw = &remote_addr.v4.sin_addr;
			remote_addr.v4.sin_port = htons(remote_port);
			remote_addr.v4.sin_family = AF_INET;
			break;
		case AF_INET6:
			ra_len = sizeof(remote_addr.v6);
			ra_raw = &remote_addr.v6.sin6_addr;
			remote_addr.v6.sin6_port = htons(remote_port);
			remote_addr.v6.sin6_family = AF_INET6;
			remote_addr.v6.sin6_scope_id = if_index;
			break;
		default:
			fprintf(stderr, "Invalid address type.\n");
			exit(1);
			break;
		}
		memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);
	}

	/* Initialize the global value for interactive mode functions.  */
	if (interactive_mode) {
		inter_sk = sk;
	}

	printf("%s ready to send...\n", argv0);
	while (!done) {
		/* Read from the user.  */
		if (remote_host) {
			if (interactive_mode) {
				printf("%s:%d-%s:%d Interactive mode> ",
					local_host, local_port, remote_host,
					remote_port);
			} else {
				printf("%s:%d-%s:%d> ",
				       local_host, local_port,
				       remote_host, remote_port);
			}
		} else {
			printf("%s:%d-", local_host, local_port);
			if (associd) {
				print_sockaddr(&remote_addr.sa);
			} else {
				printf("XXXXXX:%d", remote_port);
			}
			if (interactive_mode) {
				printf(" Interactive mode> ");
			} else {
				printf("> ");
			}
		}
		fflush(stdout);
		if (NULL == fgets(message, REALLY_BIG, stdin)) {
			done = 1;
			continue;
		}

		if (interactive_mode) {
			/* This is the send only agent.  */
			if (0 <= (c = parse_inter_commands(argv0, message,
				1))) {
				if (INTER_SND == c) {
					iov.iov_base = inter_outbuf;
					msglen = inter_outlen;
					iov.iov_len = msglen;

				} else {
					continue;
				}

			} else {
				continue;
			}
		} else {
			/* Send to our neighbor.  */
			msglen = strlen(message) + 1;
			iov.iov_len = msglen;
		}

		/* For a UDP-style socket, verify if an existing association
		 * has gone. If so, receive the pending SCTP_ASSOC_CHANGE
		 * notification.
		 */
		if ((SOCK_SEQPACKET == socket_type) && associd &&
		    (0 != test_sk_for_assoc(sk, associd))) {
			associd = test_recv_assoc_change(sk);
			printf("Old association gone, Starting a new one!\n");
			new_connection = 1;
		}

		if (new_connection && connectx_count != 0) {
			/* Do a sctp_connectx() to establish a connection. */
			error = connectx_func(argv0, sk, connectx_addrs,
					      connectx_count);
			if (0 != error) {
				if (error == -2) {
					printf("Connection refused\n");
					if (SOCK_SEQPACKET == socket_type) {
						associd = test_recv_assoc_change(sk);
					}
					continue;
				}
				fprintf(stderr, "connectx failed.\n");
				exit(1);
			}
			if (SOCK_SEQPACKET == socket_type) {
				associd = test_recv_assoc_change(sk);
			} else {
				associd = 1;
			}
			int rc = sctp_getpaddrs(sk, associd, &addrs);
			if (0 >= rc) {
				if (rc == 0) {
					fprintf(stderr, "sctp_getpaddrs failed, no peers.\n");
				} else {
					fprintf(stderr, "sctp_getpaddrs failed %s(%d).\n", strerror(errno), errno);
				}
				exit(1);
			}
			printf("New connection, peer addresses\n");
			print_addr_buf(addrs, rc);
			ra_family = addrs[0].sa_family;
			switch (ra_family) {
			case AF_INET:
				ra_len = sizeof(remote_addr.v4);
				break;
			case AF_INET6:
				ra_len = sizeof(remote_addr.v6);
				break;
			default:
				fprintf(stderr, "Invalid address type.\n");
				exit(1);
			}
			memcpy(&remote_addr, &addrs[0], ra_len);
			sctp_freepaddrs(addrs);
			new_connection = 0;
		}

		do {
			if (SOCK_SEQPACKET == socket_type ||
			    (connectx_count == 0 && new_connection)) {
				/* Initialize the message struct we use to pass
				 * messages to the remote socket.
				 */
				if (!interactive_mode) {
					iov.iov_base = message;
					iov.iov_len = msglen;
				}
				outmsg.msg_iov = &iov;
				outmsg.msg_iovlen = 1;
				outmsg.msg_control = NULL;
				outmsg.msg_controllen = 0;
				outmsg.msg_name = &remote_addr;
				outmsg.msg_namelen = ra_len;
				outmsg.msg_flags = 0;

				error = sendmsg(sk, &outmsg, 0);
			} else {
				error = send(sk, message, msglen, 0);
				if (error == -1 && errno == EPIPE) {
					error = close(sk);
					if (error != 0) {
						fprintf(stderr, "close failed %s\n", strerror(errno));
						exit(1);
					}
					*skp = sk = build_endpoint(argv0, local_port);
					break;
				}
			}

			if (error != msglen) {
				fprintf(stderr, "%s: error: %s.\n",
					argv0, strerror(errno));
				if (nonblocking && EAGAIN == errno) {
					if (interactive_mode) {
						break;
					}
					continue;
				}
				exit(1);
			} else {
				break;
			}
		} while (error != msglen);

		/* If this is the first message sent over a UDP-style socket,
		 * get the associd from the SCTP_ASSOC_CHANGE notification.
		 */
		if ((SOCK_SEQPACKET == socket_type) && (0 == associd))
			associd = test_recv_assoc_change(sk);

		/* Verify there is no association.  */
		if (0 != test_sk_for_assoc(sk, associd)) {
			printf("No association is present now!!\n");
			new_connection = 1;
		} else {
			if (new_connection) {
				int rc = sctp_getpaddrs(sk, associd, &addrs);
				if (0 >= rc) {
					if (rc == 0) {
						fprintf(stderr, "sctp_getpaddrs failed, no peers.\n");
					} else {
						fprintf(stderr, "sctp_getpaddrs failed %s(%d).\n", strerror(errno), errno);
					}
					exit(1);
				}
				printf("New connection, peer addresses\n");
				print_addr_buf(addrs, rc);
				sctp_freepaddrs(addrs);
				new_connection = 0;
			}
		}

		/* Clean up.  */
		if (interactive_mode) {
			free(inter_outbuf);
			inter_outbuf = NULL;
		}
	} /* while(!done) */

	return error;

} /* command_send() */

/* Listen on the array of sockets, printing out anything that arrives.  */
int
command_poll(char *argv0)
{
	char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
	struct iovec iov;
	struct msghdr inmessage;
	int done = 0;
	int error = 0;
	int max_fd, i, ret;
	int size;
	fd_set *ibitsp = NULL;
	fd_set *obitsp = NULL;
	fd_set *xbitsp = NULL;

	struct msghdr outmsg;
	struct hostent *hst;
	int msglen;
	int temp_fd, temp_set;



	/* If a remote host is specified, initialize the destination. */
	if (remote_host) {
		/* Set up the destination.  */
		hst = gethostbyname(remote_host);
		if (hst == NULL) {
			hst = gethostbyname2(remote_host, AF_INET6);
		}

		if (hst == NULL || hst->h_length < 1) {
			fprintf(stderr, "%s: bad hostname: %s\n",
				argv0, remote_host);
			exit(1);
		}

		ra_family = hst->h_addrtype;
		switch (ra_family) {
		case AF_INET:
			ra_len = sizeof(remote_addr.v4);
			ra_raw = &remote_addr.v4.sin_addr;
			remote_addr.v4.sin_port = htons(remote_port);
			remote_addr.v4.sin_family = AF_INET;
			break;
		case AF_INET6:
			ra_len = sizeof(remote_addr.v6);
			ra_raw = &remote_addr.v6.sin6_addr;
			remote_addr.v6.sin6_port = htons(remote_port);
			remote_addr.v6.sin6_family = AF_INET6;
			remote_addr.v6.sin6_scope_id = if_index;
			break;
		default:
			fprintf(stderr, "Invalid address type.\n");
			exit(1);
			break;
		}
		memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);

		/* Initialize the message struct we use to pass messages to
	 	 * the remote socket.
	 	 */
		outmsg.msg_iov = &iov;
		outmsg.msg_iovlen = 1;
		outmsg.msg_control = NULL;
		outmsg.msg_controllen = 0;
		outmsg.msg_name = &remote_addr;
		outmsg.msg_namelen = ra_len;
		outmsg.msg_flags = 0;
	}


	max_fd = -1;

	/* Set all of the sockets to be ready for listening. */
	if (use_poll) {
		for (i = 0; i < poll_skn; i++) {
			error = listen(poll_fds[i].fd, 1);
			if (error != 0) {
				printf("%s: Listen failed on socket number ",
					argv0);
				printf("%d: %s.\n", i, strerror(errno));
				exit(1);
			}
		}
		printf("%s listening...\n", argv0);
	} else {
		for (i = 0; i < poll_skn; i++) {
			error = listen(poll_sks[i], 1);
			if (error != 0) {
				printf("%s: Listen failed on socket number ",
					argv0);
				printf("%d: %s.\n", i, strerror(errno));
				exit(1);
			}
			if (poll_sks[i] > max_fd) {
				max_fd = poll_sks[i];
			}
		}
		printf("%s listening...\n", argv0);

		size = howmany(max_fd + 1, NFDBITS) * sizeof(fd_mask);
		if ((ibitsp = (fd_set *)malloc(size)) == NULL) {
			printf("%s: Can't allocate memory.\n", argv0);
			exit(1);
		}
		if ((obitsp = (fd_set *)malloc(size)) == NULL) {
			printf("%s: Can't allocate memory.\n", argv0);
			exit(1);
		}
		if ((xbitsp = (fd_set *)malloc(size)) == NULL) {
			printf("%s: Can't allocate memory.\n", argv0);
			exit(1);
		}
		memset(ibitsp, 0, size);
		memset(obitsp, 0, size);
		memset(xbitsp, 0, size);
	}


	/* Initialize inmessage with enough space for DATA... */
	memset(&inmessage, 0, sizeof(inmessage));
	if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
		printf("%s: Can't allocate memory.\n", argv0);
		exit(1);
	}
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	/* or a control message.  */
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);


	done = 0;
	/* Set the default send message size.  */
	if (!poll_snd_size) {
		poll_snd_size = POLL_SND_SIZE;
	}

	while (!done) {

		if (use_poll) {
			for (i = 0; i < poll_skn; i++) {
				poll_fds[i].events = POLLIN;
			}
			if (remote_host) {
				/* Poll output on the first socket.  */
				poll_fds[0].events |= POLLOUT;
			}

			if ((ret = poll(poll_fds, poll_skn, -1))) {
				if (ret == -1) {
					break;
				}
			}
		} else {
			for (i = 0; i < poll_skn; i++) {
				FD_SET(poll_sks[i], ibitsp);
				FD_SET(poll_sks[i], xbitsp);
			}
			if (remote_host) {
				/* Only select output on the first socket.  */
				FD_SET(poll_sks[0], obitsp);
			}


			if ((ret = select(max_fd + 1, ibitsp, obitsp, xbitsp,
				(struct timeval *)0)) < 0) {
				if (ret == -1) {
					break;
				}
			}

		}

		if (remote_host) {
			if (use_poll) {
				temp_set = poll_fds[0].revents & POLLOUT;
				temp_fd = poll_fds[0].fd;
			} else {
				temp_set = FD_ISSET(poll_sks[0], obitsp);
				temp_fd = poll_sks[0];
			}

			if (temp_set) {
				inter_outbuf = gen_message(poll_snd_size);
				if (!inter_outbuf) {
					fprintf(stderr,
					"Cannot allocate out message.\n");
					exit(1);
				}
				iov.iov_base = inter_outbuf;
				msglen = poll_snd_size;
				iov.iov_len = msglen;

				error = sendmsg(temp_fd, &outmsg, 0);
				fprintf(stderr,
					"sent a message, msglen = %d\n",
					msglen);

				if (error != msglen) {
					fprintf(stderr, "%s: error: %s.\n",
						argv0, strerror(errno));
					if ((!nonblocking) ||
					    (EAGAIN != errno)) {
						exit(1);
					}
				}

				/* Clean up.  */
				free(inter_outbuf);
				inter_outbuf = NULL;
			}

		} /* while(!done) */

		for (i = 0; !done && (i < poll_skn); i++) {
			if (use_poll) {
				temp_set = poll_fds[i].revents & POLLIN;
				temp_fd = poll_fds[i].fd;
			} else {
				temp_set = FD_ISSET(poll_sks[i], ibitsp);
				temp_fd = poll_sks[i];
			}
			if (temp_set) {
				error = recvmsg(temp_fd, &inmessage,
					MSG_WAITALL);
				if (error < 0) {
					if ((EAGAIN == errno)) {
						error = 0;
						continue;
					}
					else {
						fprintf(stderr,
							"%s: error: %s.\n",
							argv0,
							strerror(errno));
						exit(1);
					}
				}
				test_print_message(temp_fd, &inmessage, error);
				inmessage.msg_control = incmsg;
				inmessage.msg_controllen = sizeof(incmsg);
				iov.iov_len = REALLY_BIG;
			}

			/* Update the associd when a notification is received
			 * on a UDP-style socket.
			 */
			if (inmessage.msg_flags & MSG_NOTIFICATION)
				associd = test_verify_assoc_change(&inmessage);

			/* Verify there is no association. */
			if (0 != test_sk_for_assoc(poll_sks[i], associd)) {
				printf("No association is present in sk "
				       "No.%d now!!\n",i);
			}
		}

	}

	if (!use_poll) {
		free(ibitsp);
		free(obitsp);
		free(xbitsp);
	}

	return error;

} /* command_poll() */

/********************************************************************
 * 3rd Level Abstractions
 ********************************************************************/

#define FPS(arg) fprintf(stderr, arg)

void
usage(char *argv0)
{
	/*
	 * The bindx options, --bindx-add and --bindx-rem, are added to
	 *
	 * 1. provide first testcases for the new bindx system call
	 *
	 * 2. continue to grow sctp_darn with more functions and
	 * features so it will be equivalent to the "sock" tool for
	 * TCP as for SCTP.
	 *
	 * FIXME -
	 *
	 * It is not very effective to use these two options in the
	 * current command line mode of sctp_darn. For example, the
	 * --bindx-rem option can only be used in conjunction with the
	 * --bindx-add simply to test the function in the kernel
	 * path. Ideally, bindx needs to be tested by a tool which
	 * provides an interactive mode for users to change parameters
	 * and configuration dynamically with existing endpoints and
	 * associations.
	 */
	fprintf(stderr, "Usage: %s -H <localhost> -P <localport> "
		"[-h <remotehost>] [-p <remoteport>] -l|s\n"
		" -H, --local\t\tspecify one of the local addresses,\n"
		" -P, --local-port\tspecify the port number for local addresses,\n"
		" -h, --remote\t\tspecify the peer address,\n"
		" -p, --remote-port\tspecify the port number for the peer address,\n"
		" -l, --listen\t\tprint messages received from the peer,\n"
		" -s, --send\t\tsend messages to the peer,\n"
		" -B, --bindx-add"
		"\tadd the specified address(es) as additional bind\n"
		"\t\t\taddresses to the local socket. Multiple addresses can\n"
		"\t\t\tbe specified by using this argument multiple times.\n"
		"\t\t\tFor example, '-B 10.0.0.1 -B 20.0.0.2'.\n"
		" -b, --bindx-rem"
		"\tremove the specified address(es) from the bind\n"
		"\t\t\taddresses of the local socket. Multiple addresses can\n"
		"\t\t\tbe specified by using this argument multiple times.\n"
		"\t\t\tFor example, '-b 10.0.0.1 -b 20.0.0.2'.\n"
		" -c, --connectx"
		"\t\tuse the specified address(es) for connection to the\n"
		"\t\t\tpeer socket. Multiple addresses can be specified by\n"
		"\t\t\tusing this argument multiple times.\n"
		"\t\t\tFor example, '-c 10.0.0.1 -c 20.0.0.2'.\n"
		"\t\t\tThis option is incompatible with the -h option.\n"
		" -I\t\t\tuse the interactive mode.\n"
		" -i\t\t\tsetup the specified number of endpoints by using the\n"
		"\t\t\tspecified local host (-H) and local port (-P). The port\n"
        	"\t\t\tnumber will be incremented by one for each additional\n"
        	"\t\t\tendpoint.  All of these endpoints will be listening.\n"
		"\t\t\tIf a remote host (-h) and a remote port are also\n"
		"\t\t\tspecified, the first endpoint will start sending fixed\n"
		"\t\t\tsized messages to the remote host.\n"
		" -m\t\t\tspecify the sockopt sndbuf/rcvbuf size.\n"
		" -n\t\t\tset the socket(s) to be in the non-blocking mode.\n"
		"\t\t\tcollect messages from stdin and deliver them to the\n"
		"\t\t\tpeer,\n"
		"--use-poll\t\tuse system call poll() for polling among the\n"
		"\t\t\tnumber of endpoints specified by the -i option. Without\n"
		"\t\t\tthis option, select() would be used as default.\n"
		" -t\t\t\tuse SOCK_STREAM tcp-style sockets.\n"
		" -z\t\t\tspecify the message size to be sent.  The default\n"
		"\t\t\tmessage size generated would be 16K.\n"
		" --interface=\"ifname\"\tselect interface for sin6_scope_id.\n",
		argv0);
}


/* This function checks messages to see if they are of type 'event'
 * and if they are well-formed.
 */
int
user_test_check_message(struct msghdr *msg,
                        int controllen,
                        sctp_cmsg_t event)
{


	if (msg->msg_controllen != controllen) {
		fprintf(stderr,
			"Got control structure of length %zu, not %d\n",
			msg->msg_controllen, controllen);
		exit(1);
	}
	if (controllen > 0 && event != CMSG_FIRSTHDR(msg)->cmsg_type) {
		fprintf(stderr, "Wrong kind of event: %d, not %d\n",
			CMSG_FIRSTHDR(msg)->cmsg_type, event);
		exit(1);
	}

	return 1;

} /* user_test_check_message() */

/* Add another address represented as the string 'parm' to the list
 * addrs.  The argument count is the number of addrs on input and is
 * adjusted for output.
 */
struct sockaddr *
append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
{
	struct sockaddr *new_addrs = NULL;
	void *aptr;
	struct sockaddr *sa_addr;
	struct sockaddr_in *b4ap;
	struct sockaddr_in6 *b6ap;
	struct hostent *hst4 = NULL;
	struct hostent *hst6 = NULL;
	int i4 = 0;
	int i6 = 0;
	int j;
	int orig_count = *ret_count;
	int count = orig_count;


	if (!parm)
		return NULL;
	/* Get the entries for this host.  */
	hst4 = gethostbyname(parm);
	hst6 = gethostbyname2(parm, AF_INET6);

	if ((NULL == hst4 || hst4->h_length < 1)
	    && (NULL == hst6 || hst6->h_length < 1)) {
		fprintf(stderr, "bad hostname: %s\n", parm);
		goto finally;
	}


	/* Figure out the number of addresses.  */
	if (NULL != hst4) {
		for (i4 = 0; NULL != hst4->h_addr_list[i4]; ++i4) {
			count++;
		}
	}
	if (NULL != hst6) {
		for (i6 = 0; NULL != hst6->h_addr_list[i6]; ++i6) {
			count++;
		}
	}

	/* Expand memory for the new addresses.  Assume all the addresses 
	 * are v6 addresses.
	 */
	new_addrs = (struct sockaddr *)
		realloc(addrs, sizeof(struct sockaddr_in6) * count);

	if (NULL == new_addrs) {
		count = *ret_count;
		goto finally;
	}

	/* Skip the existing addresses. */
	aptr = new_addrs; 
	for (j = 0; j < orig_count; j++) {
		sa_addr = (struct sockaddr *)aptr;
		switch(sa_addr->sa_family) {
		case AF_INET:
			aptr += sizeof(struct sockaddr_in);
			break;
		case AF_INET6:
			aptr += sizeof(struct sockaddr_in6);
			break;
		default:
			count = orig_count;
			goto finally;
		}
	}	
					
	/* Put the new addresses away.  */
	if (NULL != hst4) {
		for (j = 0; j < i4; ++j) {
			b4ap = (struct sockaddr_in *)aptr;
			memset(b4ap, 0x00, sizeof(*b4ap));
			b4ap->sin_family = AF_INET;
			b4ap->sin_port = htons(local_port);
			bcopy(hst4->h_addr_list[j], &b4ap->sin_addr,
			      hst4->h_length);

			aptr += sizeof(struct sockaddr_in);
		} /* for (loop through the new v4 addresses) */
	}

	if (NULL != hst6) {
		for (j = 0; j < i6; ++j) {
			b6ap = (struct sockaddr_in6 *)aptr;
			memset(b6ap, 0x00, sizeof(*b6ap));
			b6ap->sin6_family = AF_INET6;
			b6ap->sin6_port =  htons(local_port);
			b6ap->sin6_scope_id = if_index;
			bcopy(hst6->h_addr_list[j], &b6ap->sin6_addr,
			      hst6->h_length);

			aptr += sizeof(struct sockaddr_in6);
		} /* for (loop through the new v6 addresses) */
	}

 finally:

	*ret_count = count;

	return new_addrs;

} /* append_addr() */

static int
parse_inter_commands(char *argv0, char *input, int snd_only)
{
	int i;
	char *p;
	int len;
	int set = 0;
	int val;
	struct sockaddr *tmp_addrs = NULL;


	p = input;
	if (*p == '?' || *p == '\n') {
		printf("Interactive commands:\n");
		printf("snd=<int>        - Do a sendmsg with the specified");
		printf(" length.\n");
		printf("rcv=<int>        - Do a recvmsg.");
		printf("The length is ignored for now.\n");
		printf("bindx-add=<addr> - Add a local address");
		printf(" with bindx. \n");
		printf("bindx-rem=<addr> - Remove a local address");
		printf(" with bindx. \n");
		printf("rcvbuf=<int>     - Get/Set receive buffer size\n");
		printf("sndbuf=<int>     - Get/Set send buffer size.\n");
		printf("primary=<addr>   - Get/Set association's primary\n");
		printf("peer_primary=addr- Set association's peer_primary\n");
		printf("heartbeat=<addr> - Request a user initiated heartbeat\n");
		printf("maxseg=<int>     - Get/Set Maximum fragment size.\n");
		printf("nodelay=<0|1>    - Get/Set NODELAY option.\n");
		printf("shutdown         - Shutdown the association.\n");
		printf("abort            - Abort the association.\n");
		printf("stats            - Print GET_ASSOC_STATS (if available in kernel).\n");
		printf("?                - Help. Display this message.\n");
		return -1;
	}

	for (i = 0; i < REALLY_BIG; i++) {
		if (('=' == *p) ||
		    ('?' == *p) ||
		    ('\n' == *p)) {
			if ('=' == *p) {
				set = 1;
			}
			*p++ = '\0';
			break;
		}
		p++;
	}
	if (i >= REALLY_BIG) {
		printf("Invalid input.\n");
		return -1;
	}

	i = 0;
	while (NULL != inter_commands[i].cmd) {
		if (!strcmp(input, inter_commands[i].cmd)) {
			switch (i) {
			case INTER_SND:
				if (snd_only) {
					if (*p < '0' || *p > '9') {
						goto err_input;
					}
					snd_func(p);
				} else {
					goto err_input;
				}
				break;
			case INTER_RCV:
				if (snd_only) {
					goto err_input;
				}
				break;
			case INTER_SNDBUF:
				if (set) {
					if (*p < '0' || *p > '9') {
						goto err_input;
					}
				}
				len = (set) ? atoi(p) : 0;
				sndbuf_func(argv0, inter_sk, len, set);
				break;
			case INTER_RCVBUF:
				if (set) {
					if (*p < '0' || *p > '9') {
						goto err_input;
					}
				}
				len = (set) ? atoi(p) : 0;
				rcvbuf_func(argv0, inter_sk, len, set);
				break;
			case INTER_BINDX_ADD:
				tmp_addrs = get_bindx_addr(p, &len);
				bindx_func(argv0, inter_sk, tmp_addrs, len,
					SCTP_BINDX_ADD_ADDR, local_port);
				free(tmp_addrs);
				break;
			case INTER_BINDX_REM:
				tmp_addrs = get_bindx_addr(p, &len);
				bindx_func(argv0, inter_sk, tmp_addrs, len,
					SCTP_BINDX_REM_ADDR, local_port);
				free(tmp_addrs);
				break;
			case INTER_SET_PRIM:
				primary_func(argv0, inter_sk, p, set);
				break;
			case INTER_SET_PEER_PRIM:
				peer_primary_func(argv0, inter_sk, p, set);
				break;
			case INTER_HEARTBEAT:
				spp_hb_demand_func(argv0, inter_sk, p, set);
				break;
			case INTER_SHUTDOWN:
				shutdown_func(argv0, &inter_sk, SHUTDOWN_SHUTDOWN);
				break;
			case INTER_ABORT:
				shutdown_func(argv0, &inter_sk, SHUTDOWN_ABORT);
				break;
			case INTER_NODELAY:
				if (set) {
					if (*p < '0' || *p > '9') {
						goto err_input;
					}
				}
				val = (set) ? atoi(p) : 0;
				nodelay_func(argv0, inter_sk, val, set);
				break;
			case INTER_MAXSEG:
				if (set) {
					if (*p < '0' || *p > '9') {
						goto err_input;
					}
				}
				val = (set) ? atoi(p) : 0;
				maxseg_func(argv0, inter_sk, val, set);
				break;
			case INTER_GET_STATS:
				get_assocstats_func(inter_sk, associd);
				break;
			default:
				goto err_input;
				break;
			}

			return i;
		}
		i++;
	}

err_input:
	printf("Invalid input.\n");
	return -1;

} /* parse_inter_commands() */

static char *
gen_message(int len)
{

	char *buf;
	char *p;
	int i;

	buf = malloc(len);

	if (NULL != buf) {
		for (i = 0, p = buf; i < len; i++, p++) {
			if (gen_data > GEN_DATA_LAST) {
				gen_data = GEN_DATA_FIRST;
			}
			*p = gen_data++;
		}
	}

	return(buf);

} /* gen_message() */

static void
snd_func(char *input)
{

	int len;

	len = atoi(input);
	if (!(inter_outbuf = gen_message(len))) {
		fprintf(stderr, "Cannot allocate out message.\n");
		exit(1);
	}
	inter_outlen = len;

} /* snd_func() */

static void
sndbuf_func(char *argv0, int sk, int len, int set)
{
	int error;
	socklen_t optlen;

	if (set) {
		error = setsockopt(sk, SOL_SOCKET, SO_SNDBUF,
			(char *)&len, sizeof(len));
	} else {
		optlen = sizeof(len);
		error = getsockopt(sk, SOL_SOCKET, SO_SNDBUF,
			(char *)&len, &optlen);
	}
	if (error != 0) {
		fprintf(stderr, "%s: Error setting/getting sndbuf: %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	if (!set) {
		printf("sndbuf is %d.\n", len);
	}

} /* sndbuf_func() */

static void
rcvbuf_func(char *argv0, int sk, int len, int set)
{
	int error;
	socklen_t optlen;

	if (set) {
		error = setsockopt(sk, SOL_SOCKET, SO_RCVBUF,
			(char *)&len, sizeof(len));
	} else {
		optlen = sizeof(len);
		error = getsockopt(sk, SOL_SOCKET, SO_RCVBUF,
			(char *)&len, &optlen);
	}
	if (error != 0) {
		fprintf(stderr, "%s: Error setting/getting rcvbuf: %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	if (!set) {
		printf("rcvbuf is %d.\n", len);
	}

} /* rcvbuf_func() */


static struct sockaddr *
get_bindx_addr(char *in, int *count)
{

	struct sockaddr *tmp_addrs = NULL;
	char *p = in;

	/* Set the buffer for address parsing.  */
	while ('\n' != *p) {
		p++;
	}
	*p = '\0';

	*count = 0;

	tmp_addrs = append_addr(in, tmp_addrs, count);
	if (NULL == tmp_addrs) {
		/* We have no memory, so keep fprintf()
		 * from trying to allocate more.
		 */
		fprintf(stderr, "No memory to add ");
		fprintf(stderr, "%s\n", in);
		exit(2);
	}
	return tmp_addrs;

} /* get_bindx_addr() */

static int
bindx_func(char *argv0, int sk, struct sockaddr *addrs, int count, int flag, int portnum)
{

	int error;
	int i;
	struct sockaddr *sa_addr;
	void *aptr;


	if (0 == portnum) {
		fprintf(stderr, "%s: A non-0 local port number is ", argv0);
		fprintf(stderr, "required for bindx to work!\n");
		return -1 ;
	}

	/* Set the port in every address.  */
	aptr = addrs;
	for (i = 0; i < count; i++) {
		sa_addr = (struct sockaddr *)aptr;

		switch(sa_addr->sa_family) {
		case AF_INET:
			((struct sockaddr_in *)sa_addr)->sin_port =
				htons(portnum);
			aptr += sizeof(struct sockaddr_in);
			break;
		case AF_INET6:
			((struct sockaddr_in6 *)sa_addr)->sin6_port =
				htons(portnum);
			aptr += sizeof(struct sockaddr_in6);
			break;
		default:
			fprintf(stderr, "Invalid address family\n");
			return -1;
		}
	}

	error = sctp_bindx(sk, addrs, count, flag);

	if (error != 0) {
		if (flag == SCTP_BINDX_ADD_ADDR) {
			fprintf(stderr, "%s: error adding addrs: %s.\n",
				argv0, strerror(errno));
			return -1;
		} else {
			fprintf(stderr, "%s: error removing addrs: %s.\n",
				argv0, strerror(errno));
			return -1;
		}
	}

	return 0;

} /* bindx_func() */

static int
connectx_func(char *argv0, int sk, struct sockaddr *addrs, int count)
{

	int error;
	int i;
	struct sockaddr *sa_addr;
	void *aptr;


	if (0 == remote_port) {
		fprintf(stderr, "%s: A non-0 remote port number is ", argv0);
		fprintf(stderr, "required for connectx to work!\n");
		return -1 ;
	}

	/* Set the port in every address.  */
	aptr = addrs;
	for (i = 0; i < count; i++) {
		sa_addr = (struct sockaddr *)aptr;

		switch(sa_addr->sa_family) {
		case AF_INET:
			((struct sockaddr_in *)sa_addr)->sin_port =
				htons(remote_port);
			aptr += sizeof(struct sockaddr_in);
			break;
		case AF_INET6:
			((struct sockaddr_in6 *)sa_addr)->sin6_port =
				htons(remote_port);
			aptr += sizeof(struct sockaddr_in6);
			break;
		default:
			fprintf(stderr, "Invalid address family\n");
			return -1;
		}
	}

	error = sctp_connectx(sk, addrs, count, NULL);

	if (error != 0) {
		if (errno == ECONNREFUSED)
			return -2;
		fprintf(stderr, "%s: error connecting to addrs: %s.\n",
			argv0, strerror(errno));
		return -1;
	}

	return 0;

} /* connectx_func() */

static void
primary_func(char *argv0, int sk, char *cp, int set)
{
	struct sctp_prim prim;
	struct sockaddr_in *in_addr;
	struct sockaddr_in6 *in6_addr;
	struct sockaddr *saddr;
	socklen_t prim_len;
	int ret;
	char *p = cp;
	char addr_buf[INET6_ADDRSTRLEN];
	const char *ap = NULL;

	prim_len = sizeof(struct sctp_prim);
	if (!set) {
		prim.ssp_assoc_id = associd;
		ret = getsockopt(sk, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
				   &prim, &prim_len); 
		if (ret < 0)
			goto err;
	
		saddr = (struct sockaddr *)&prim.ssp_addr;	
		if (AF_INET == saddr->sa_family) {
			in_addr = (struct sockaddr_in *)&prim.ssp_addr;
			ap = inet_ntop(AF_INET, &in_addr->sin_addr, addr_buf,
				       INET6_ADDRSTRLEN);
		} else if (AF_INET6 == saddr->sa_family) {
			in6_addr = (struct sockaddr_in6 *)&prim.ssp_addr;
			ap = inet_ntop(AF_INET6, &in6_addr->sin6_addr, addr_buf,
				       INET6_ADDRSTRLEN);
		}
		if (!ap)
			goto err;
		printf("%s\n", ap);
		return;
	}

	/* Set the buffer for address parsing.  */
	while ('\n' != *p)
		p++;
	*p = '\0';

	prim.ssp_assoc_id = associd;	
	if (strchr(cp, '.')) {
		in_addr = (struct sockaddr_in *)&prim.ssp_addr;
		in_addr->sin_port = htons(remote_port);
		in_addr->sin_family = AF_INET;
		ret = inet_pton (AF_INET, cp, &in_addr->sin_addr);
		if (ret <= 0)
			goto err;		
	} else if (strchr(cp, ':')) {
		in6_addr = (struct sockaddr_in6 *)&prim.ssp_addr;
		in6_addr->sin6_port = htons(remote_port);
		in6_addr->sin6_family = AF_INET6;
		ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr);
		if (ret <= 0)
			goto err;		
	} else
		goto err;

	ret = setsockopt(sk, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
			    &prim, sizeof(struct sctp_prim)); 
	if (ret < 0)
		goto err;

	return;
err:
	if (!errno)
		errno = EINVAL;
	fprintf(stderr, "%s: error %s primary: %s.\n", argv0,
	        (set)?"setting":"getting", strerror(errno));
}

static void
peer_primary_func(char *argv0, int sk, char *cp, int set)
{
	struct sctp_setpeerprim setpeerprim;
	struct sockaddr_in *in_addr;
	struct sockaddr_in6 *in6_addr;
	int ret;
	char *p = cp;

	if (!set) {
		goto err;
	}

	/* Set the buffer for address parsing.  */
	while ('\n' != *p)
		p++;
	*p = '\0';

	setpeerprim.sspp_assoc_id = associd;	
	if (strchr(cp, '.')) {
		in_addr = (struct sockaddr_in *)&setpeerprim.sspp_addr;
		in_addr->sin_port = htons(local_port);
		in_addr->sin_family = AF_INET;
		ret = inet_pton (AF_INET, cp, &in_addr->sin_addr);
		if (ret <= 0)
			goto err;		
	} else if (strchr(cp, ':')) {
		in6_addr = (struct sockaddr_in6 *)&setpeerprim.sspp_addr;
		in6_addr->sin6_port = htons(local_port);
		in6_addr->sin6_family = AF_INET6;
		ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr);
		if (ret <= 0)
			goto err;		
	} else
		goto err;

	ret = setsockopt(sk, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR,
			    &setpeerprim, sizeof(struct sctp_setpeerprim)); 
	if (ret < 0)
		goto err;

	return;
err:
	if (!errno)
		errno = EINVAL;
	fprintf(stderr, "%s: error %s peer_primary: %s.\n", argv0,
	        (set)?"setting":"getting", strerror(errno));
}

static void
spp_hb_demand_func(char *argv0, int sk, char *cp, int set)
{
	struct sctp_paddrparams params;
	struct sockaddr_in *in_addr;
	struct sockaddr_in6 *in6_addr;
	int ret;
	char *p = cp;

	memset(&params, 0, sizeof(struct sctp_paddrparams));
	params.spp_assoc_id = associd;
	params.spp_flags = SPP_HB_DEMAND;

	if (set) {
		/* Set the buffer for address parsing.  */
		while ('\n' != *p)
			p++;
		*p = '\0';

		if (strchr(cp, '.')) {
			in_addr = (struct sockaddr_in *)&params.spp_address;
			in_addr->sin_port = htons(remote_port);
			in_addr->sin_family = AF_INET;
			ret = inet_pton(AF_INET, cp, &in_addr->sin_addr);
			if (ret <= 0)
				goto err;
		} else if (strchr(cp, ':')) {
			in6_addr = (struct sockaddr_in6 *)&params.spp_address;
			in6_addr->sin6_port = htons(remote_port);
			in6_addr->sin6_family = AF_INET6;
			ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr);
			if (ret <= 0)
				goto err;
		} else
			goto err;
	}

	ret = setsockopt(sk, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS,
			    &params, sizeof(struct sctp_paddrparams));
	if (ret < 0)
		goto err;

	return;
err:
	if (!errno)
		errno = EINVAL;
	fprintf(stderr, "%s: error %s peer_addr_params: %s.\n", argv0,
	        (set)?"setting":"getting", strerror(errno));
}

static int
nodelay_func(char *argv0, int sk, int val, int set)
{
	socklen_t optlen;
	int error;

	if (set) {
		error = setsockopt(sk, SOL_SCTP, SCTP_NODELAY,
			(char *)&val, sizeof(val));
	} else {
		optlen = sizeof(val);
		error = getsockopt(sk, SOL_SCTP, SCTP_NODELAY,
			(char *)&val, &optlen);
	}
	if (error != 0) {
		fprintf(stderr, "%s: Error setting/getting nodelay: %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	if (!set) {
		printf("nodelay is %d.\n", val);
	}

	return error;
}

static int
maxseg_func(char *argv0, int sk, int val, int set)
{
	socklen_t optlen;
	int error;

	if (set) {
		error = setsockopt(sk, SOL_SCTP, SCTP_MAXSEG,
			(char *)&val, sizeof(val));
	} else {
		optlen = sizeof(val);
		error = getsockopt(sk, SOL_SCTP, SCTP_MAXSEG,
			(char *)&val, &optlen);
	}
	if (error != 0) {
		fprintf(stderr, "%s: Error setting/getting maxseg: %s.\n",
			argv0, strerror(errno));
		exit(1);
	}

	if (!set) {
		printf("maxseg is %d.\n", val);
	}

	return error;
}

static int
shutdown_func(char *argv0, int *skp, int shutdown_type)
{
	struct msghdr outmessage;
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
        int error=0, bytes_sent;
	struct sctp_sndrcvinfo *sinfo;
	struct hostent *hst;
	char *sd_type;
	int sk = *skp;

	if (shutdown_type == SHUTDOWN_ABORT)
		sd_type = "ABORT";
	else
		sd_type = "SHUTDOWN";

	/* Verify that the association is present. */
	error = test_sk_for_assoc(sk, associd);
	if (error != 0) {
		printf("The association isn't present yet! Cannot %s!\n", sd_type);
		return -1;
	}

	if (socket_type == SOCK_SEQPACKET) {
		/* Set up the destination.  */
		if (remote_host) {
			hst = gethostbyname(remote_host);
			if (hst == NULL) {
				hst = gethostbyname2(remote_host, AF_INET6);
			}

			if (hst == NULL || hst->h_length < 1) {
				fprintf(stderr, "%s: bad hostname: %s\n",
					argv0, remote_host);
				exit(1);
			}

			ra_family = hst->h_addrtype;
			switch (ra_family) {
			case AF_INET:
				ra_len = sizeof(remote_addr.v4);
				ra_raw = &remote_addr.v4.sin_addr;
				remote_addr.v4.sin_port = htons(remote_port);
				remote_addr.v4.sin_family = AF_INET;
				break;
			case AF_INET6:
				ra_len = sizeof(remote_addr.v6);
				ra_raw = &remote_addr.v6.sin6_addr;
				remote_addr.v6.sin6_port = htons(remote_port);
				remote_addr.v6.sin6_family = AF_INET6;
				break;
			default:
				fprintf(stderr, "Invalid address type.\n");
				exit(1);
				break;
			}
			memcpy(ra_raw, hst->h_addr_list[0], hst->h_length);
		}

		/* Initialize the message struct we use to pass messages to
		 * the remote socket.
		 */
		outmessage.msg_name = &remote_addr;
		outmessage.msg_namelen = ra_len;

		outmessage.msg_iov = NULL;
		outmessage.msg_iovlen = 0;
		outmessage.msg_control = outcmsg;
		outmessage.msg_controllen = sizeof(outcmsg);
		outmessage.msg_flags = 0;

		cmsg = CMSG_FIRSTHDR(&outmessage);
		cmsg->cmsg_level = IPPROTO_SCTP;
		cmsg->cmsg_type = SCTP_SNDRCV;
		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
		outmessage.msg_controllen = cmsg->cmsg_len;
		sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
		memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
		if (shutdown_type == SHUTDOWN_ABORT)
			sinfo->sinfo_flags |= SCTP_ABORT;
		else
			sinfo->sinfo_flags |= SCTP_EOF;

		sinfo->sinfo_assoc_id = associd;
	
		bytes_sent = sendmsg(sk, &outmessage, 0);
		if (bytes_sent != 0) {
			printf("Failure:  %s.\n", strerror(errno));
			return -1;
		}

		/* Receive the COMM_LOST or SHUTDOWN_COMP event. */
		test_recv_assoc_change(sk);
	} else {
		if (shutdown_type == SHUTDOWN_ABORT) {
			struct  linger {
				int  l_onoff; 
				int  l_linger;
			} data = {1, 0};
			error = setsockopt(sk, SOL_SOCKET, SO_LINGER,
					   (char *)&data, sizeof(data));
			if (error != 0) {
				printf("setsockopt failed %s\n", strerror(errno));
				exit(1);
			}
		}
		error = close(sk);
		if (error != 0) {
			printf("close failed %s\n", strerror(errno));
			exit(1);
		}
		*skp = sk = build_endpoint(argv0, local_port);
	}

	/* Verify that the association is no longer present.  */
	error = test_sk_for_assoc(sk, associd);
	if (error != 0) {
		printf("Successfully %s the original association\n", sd_type);
		associd = 0;
		new_connection = 1;
	} else {
		printf("%s failed\n", sd_type);
		exit(1);
	}

	return 0;
}

static int
get_assocstats_func(int sk, sctp_assoc_t assoc_id)
{
	int error = 0;
	struct sctp_assoc_stats stats;
	socklen_t len;

	if (assoc_id == 0) {
		printf("No association present yet\n");
		return -1;
	}

	memset(&stats, 0, sizeof(struct sctp_assoc_stats));
	stats.sas_assoc_id = assoc_id;
	len = sizeof(struct sctp_assoc_stats);
	error = getsockopt(sk, SOL_SCTP, SCTP_GET_ASSOC_STATS,
			(char *)&stats, &len);
	if (error != 0) {
		printf("get_assoc_stats() failed %s\n", strerror(errno));
		return error;
	}

	printf("Retransmitted Chunks: %" PRIu64 "\n", (uint64_t) stats.sas_rtxchunks);
	printf("Gap Acknowledgements Received: %" PRIu64 "\n", (uint64_t) stats.sas_gapcnt);
	printf("TSN received > next expected: %" PRIu64 "\n", (uint64_t) stats.sas_outofseqtsns);
	printf("SACKs sent: %" PRIu64 "\n", (uint64_t) stats.sas_osacks);
	printf("SACKs received: %" PRIu64 "\n", (uint64_t) stats.sas_isacks);
	printf("Control chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_octrlchunks);
	printf("Control chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_ictrlchunks);
	printf("Ordered data chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_oodchunks);
	printf("Ordered data chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_iodchunks);
	printf("Unordered data chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_ouodchunks);
	printf("Unordered data chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_iuodchunks);
	printf("Dups received (ordered+unordered): %" PRIu64 "\n", (uint64_t) stats.sas_idupchunks);
	printf("Packets sent: %" PRIu64 "\n", (uint64_t) stats.sas_opackets);
	printf("Packets received: %" PRIu64 "\n", (uint64_t) stats.sas_ipackets);
	printf("Maximum Observed RTO this period: %" PRIu64 " - Transport: ", (uint64_t) stats.sas_maxrto);
	print_sockaddr((struct sockaddr *)&stats.sas_obs_rto_ipaddr);
	printf("\n");

	return 0;
}

static int
test_sk_for_assoc(int sk, sctp_assoc_t assoc_id)
{
	int error = 0;
	struct sctp_status status;
	socklen_t status_len;

	memset(&status, 0, sizeof(status));
	if (assoc_id)
		status.sstat_assoc_id = assoc_id;
	status_len = sizeof(struct sctp_status);
	error = getsockopt(sk, SOL_SCTP, SCTP_STATUS,
               		(char *)&status, &status_len);
	return error;
}

/* Receive a notification and return the corresponding associd if the event is
 * SCTP_COMM_UP. Return 0 for any other event.
 */
static sctp_assoc_t
test_recv_assoc_change(int sk)
{
	struct msghdr inmessage;
	struct iovec iov;
	char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
	int error;

	/* Initialize inmessage with enough space for DATA... */
	memset(&inmessage, 0, sizeof(inmessage));
	if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
		printf("%s: Can't allocate memory.\n", __FUNCTION__);
		exit(1);
	}
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	/* or a control message.  */
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	error = recvmsg(sk, &inmessage, MSG_WAITALL);
	if (error < 0) {
		printf("%s: recvmsg: %s\n", __FUNCTION__, strerror(errno));
		exit(1);
	}

	return test_verify_assoc_change(&inmessage);
}

/* Verify a notification and return the corresponding associd if the event is
 * SCTP_COMM_UP. Return 0 for any other event.
 */
static sctp_assoc_t
test_verify_assoc_change(struct msghdr *msg)
{
	union sctp_notification *sn;

	if (!(msg->msg_flags & MSG_NOTIFICATION)) {
		fprintf(stderr, "%s: Received data when notification is expected\n",
		       __FUNCTION__);
		exit(1);
	}

	sn = (union sctp_notification *)msg->msg_iov->iov_base;
	if (SCTP_ASSOC_CHANGE != sn->sn_header.sn_type) {
		fprintf(stderr, "%s: Received unexpected notification: %d",
			__FUNCTION__, sn->sn_header.sn_type);
		exit(1);
	}

	switch(sn->sn_assoc_change.sac_state)
	{
	case SCTP_COMM_UP:
		printf("Received SCTP_COMM_UP\n");
		break;
	case SCTP_COMM_LOST:
		printf("Received SCTP_COMM_LOST\n");
		break;
	case SCTP_RESTART:
		printf("Received SCTP_RESTART\n");
		break;
	case SCTP_SHUTDOWN_COMP:
		printf("Received SCTP_SHUTDOWN_COMP\n");
		break;
	case SCTP_CANT_STR_ASSOC:
		printf("Received SCTP_CANT_STR_ASSOC\n");
		break;
	}

	if (SCTP_COMM_UP == sn->sn_assoc_change.sac_state)
		return sn->sn_assoc_change.sac_assoc_id;
	else
		return 0;
}

void print_addr_buf(void * laddrs, int n_laddrs)
{
	void *addr_buf = laddrs;
	int i;

	for (i = 0; i < n_laddrs; i++) {
		addr_buf += print_sockaddr((struct sockaddr *)addr_buf);
		printf("\n");
	}
}

int print_sockaddr(struct sockaddr *sa_addr)
{
	struct sockaddr_in *in_addr;
	struct sockaddr_in6 *in6_addr;

	if (AF_INET == sa_addr->sa_family) {
		in_addr = (struct sockaddr_in *)sa_addr;
		printf("%d.%d.%d.%d:%d",
		       NIPQUAD(in_addr->sin_addr),
		       ntohs(in_addr->sin_port));
		return sizeof(struct sockaddr_in);
	} else {
		in6_addr = (struct sockaddr_in6 *)sa_addr;
		printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%d",
		       NIP6(in6_addr->sin6_addr),
		       ntohs(in6_addr->sin6_port));
		return sizeof(struct sockaddr_in6);
	}
}