Codebase list mtr / debian/0.87-1 net.c
debian/0.87-1

Tree @debian/0.87-1 (Download .tar.gz)

net.c @debian/0.87-1raw · history · blame

   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
/*
    mtr  --  a network diagnostic tool
    Copyright (C) 1997,1998  Matt Kimball

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as 
    published by the Free Software Foundation.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#if defined(HAVE_SYS_XTI_H)
#include <sys/xti.h>
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <memory.h>
#include <unistd.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>

#include "mtr.h"
#include "net.h"
#include "display.h"
#include "dns.h"

/*  We can't rely on header files to provide this information, because
    the fields have different names between, for instance, Linux and 
    Solaris  */
struct ICMPHeader {
  uint8 type;
  uint8 code;
  uint16 checksum;
  uint16 id;
  uint16 sequence;
};

/* Structure of an UDP header.  */
struct UDPHeader {
  uint16 srcport;
  uint16 dstport;
  uint16 length;
  uint16 checksum;
};

/* Structure of an TCP header, as far as we need it.  */
struct TCPHeader {
  uint16 srcport;
  uint16 dstport;
  uint32 seq;
};

/* Structure of an SCTP header */
struct SCTPHeader {
  uint16 srcport;
  uint16 dstport;
  uint32 veri_tag;
};

/* Structure of an IPv4 UDP pseudoheader.  */
struct UDPv4PHeader {
  uint32 saddr;
  uint32 daddr;
  uint8 zero;
  uint8 protocol;
  uint16 len;
};

/*  Structure of an IP header.  */
struct IPHeader {
  uint8 version;
  uint8 tos;
  uint16 len;
  uint16 id;
  uint16 frag;
  uint8 ttl;
  uint8 protocol;
  uint16 check;
  uint32 saddr;
  uint32 daddr;
};
  

#define ICMP_ECHO		8
#define ICMP_ECHOREPLY		0

#define ICMP_TSTAMP		13
#define ICMP_TSTAMPREPLY	14

#define ICMP_TIME_EXCEEDED	11
#define ICMP_UNREACHABLE        3

#ifndef SOL_IP
#define SOL_IP 0
#endif

struct nethost {
  ip_t addr;
  ip_t addrs[MAXPATH];	/* for multi paths byMin */
  int xmit;
  int returned;
  int sent;
  int up;
  long long var;/* variance, could be overflowed */
  int last;
  int best;
  int worst;
  int avg;	/* average:  addByMin */
  int gmean;	/* geometric mean: addByMin */
  int jitter;	/* current jitter, defined as t1-t0 addByMin */
/*int jbest;*/	/* min jitter, of cause it is 0, not needed */
  int javg;	/* avg jitter */
  int jworst;	/* max jitter */
  int jinta;	/* estimated variance,? rfc1889's "Interarrival Jitter" */
  int transit;
  int saved[SAVED_PINGS];
  int saved_seq_offset;
  struct mplslen mpls;
  struct mplslen mplss[MAXPATH];
};


struct sequence {
  int index;
  int transit;
  int saved_seq;
  struct timeval time;
  int socket;
};


/* BSD-derived kernels use host byte order for the IP length and 
   offset fields when using raw sockets.  We detect this automatically at 
   run-time and do the right thing. */
static int BSDfix = 0;

static struct nethost host[MaxHost];
static struct sequence sequence[MaxSequence];
static struct timeval reset = { 0, 0 };

int    timestamp;
int    sendsock4;
int    sendsock4_icmp;
int    sendsock4_udp;
int    recvsock4;
int    sendsock6;
int    sendsock6_icmp;
int    sendsock6_udp;
int    recvsock6;
int    sendsock;
int    recvsock;

#ifdef ENABLE_IPV6
struct sockaddr_storage sourcesockaddr_struct;
struct sockaddr_storage remotesockaddr_struct;
struct sockaddr_in6 * ssa6 = (struct sockaddr_in6 *) &sourcesockaddr_struct;
struct sockaddr_in6 * rsa6 = (struct sockaddr_in6 *) &remotesockaddr_struct;
#else
struct sockaddr_in sourcesockaddr_struct;
struct sockaddr_in remotesockaddr_struct;
#endif

struct sockaddr * sourcesockaddr = (struct sockaddr *) &sourcesockaddr_struct;
struct sockaddr * remotesockaddr = (struct sockaddr *) &remotesockaddr_struct;
struct sockaddr_in * ssa4 = (struct sockaddr_in *) &sourcesockaddr_struct;
struct sockaddr_in * rsa4 = (struct sockaddr_in *) &remotesockaddr_struct;

ip_t * sourceaddress;
ip_t * remoteaddress;

/* XXX How do I code this to be IPV6 compatible??? */
#ifdef ENABLE_IPV6
char localaddr[INET6_ADDRSTRLEN];
#else
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif
char localaddr[INET_ADDRSTRLEN];
#endif

static int batch_at = 0;
static int numhosts = 10;

extern int fstTTL;		/* initial hub(ttl) to ping byMin */
extern int maxTTL;		/* last hub to ping byMin*/
extern int maxUnknown;	/* stop ping threshold */
extern int cpacketsize;		/* packet size used by ping */
static int packetsize;		/* packet size used by ping */
static int spacketsize;		/* packet size used by sendto */
extern int bitpattern;		/* packet bit pattern used by ping */
extern int tos;			/* type of service set in ping packet*/
extern int af;			/* address family of remote target */
extern int mtrtype;		/* type of query packet used */
extern int remoteport;          /* target port for TCP tracing */
extern int localport;  /* source port for UDP tracing */
extern int tcp_timeout;             /* timeout for TCP connections */
#ifdef SO_MARK
extern int mark;		/* SO_MARK to set for ping packet*/
#endif

/* return the number of microseconds to wait before sending the next
   ping */
int calc_deltatime (float waittime)
{
  waittime /= numhosts;
  return 1000000 * waittime;
}


int checksum(void *data, int sz) 
{
  uint16 *ch;
  uint32 sum;
  uint16 odd;

  sum = 0;
  ch = data;
  if (sz % 2) {
    ((char *)&odd)[0] = ((char *)data)[sz - 1];
    sum = odd;
  }
  sz = sz / 2;
  while (sz--) {
    sum += *(ch++);
  }
  while (sum >> 16) {
    sum = (sum >> 16) + (sum & 0xffff);
  }

  return (~sum & 0xffff);  
}


/* Prepend pseudoheader to the udp datagram and calculate checksum */
int udp_checksum(void *pheader, void *udata, int psize, int dsize, int alt_checksum)
{
  unsigned int tsize = psize + dsize;
  char csumpacket[tsize];
  memset(csumpacket, (unsigned char) abs(bitpattern), abs(tsize));
  if (alt_checksum && dsize >= 2) {
    csumpacket[psize + sizeof(struct UDPHeader)] = 0;
    csumpacket[psize + sizeof(struct UDPHeader) + 1] = 0;
  }

  struct UDPv4PHeader *prepend = (struct UDPv4PHeader *) csumpacket;
  struct UDPv4PHeader *udppheader = (struct UDPv4PHeader *) pheader;
  prepend->saddr = udppheader->saddr;
  prepend->daddr = udppheader->daddr;
  prepend->zero = 0;
  prepend->protocol = udppheader->protocol;
  prepend->len = udppheader->len;

  struct UDPHeader *content = (struct UDPHeader *)(csumpacket + psize);
  struct UDPHeader *udpdata = (struct UDPHeader *) udata;
  content->srcport = udpdata->srcport;
  content->dstport = udpdata->dstport;
  content->length = udpdata->length;
  content->checksum = udpdata->checksum;

  return checksum(csumpacket,tsize);
}


void save_sequence(int index, int seq)
{
  display_rawxmit(index, seq);

  sequence[seq].index = index;
  sequence[seq].transit = 1;
  sequence[seq].saved_seq = ++host[index].xmit;
  memset(&sequence[seq].time, 0, sizeof(sequence[seq].time));
  
  host[index].transit = 1;
  if (host[index].sent)
    host[index].up = 0;
  host[index].sent = 1;
  net_save_xmit(index);
}

int new_sequence(int index)
{
  static int next_sequence = MinSequence;
  int seq;

  seq = next_sequence++;
  if (next_sequence >= MaxSequence)
    next_sequence = MinSequence;

  save_sequence(index, seq);

  return seq;
}

/*  Attempt to connect to a TCP port with a TTL */
void net_send_tcp(int index)
{
  int ttl, s;
  int opt = 1;
  int port;
  struct sockaddr_storage local;
  struct sockaddr_storage remote;
  struct sockaddr_in *local4 = (struct sockaddr_in *) &local;
  struct sockaddr_in6 *local6 = (struct sockaddr_in6 *) &local;
  struct sockaddr_in *remote4 = (struct sockaddr_in *) &remote;
  struct sockaddr_in6 *remote6 = (struct sockaddr_in6 *) &remote;
  socklen_t len;

  ttl = index + 1;

  s = socket(af, SOCK_STREAM, 0);
  if (s < 0) {
    display_clear();
    perror("socket()");
    exit(EXIT_FAILURE);
  }

  memset(&local, 0, sizeof (local));
  memset(&remote, 0, sizeof (remote));
  local.ss_family = af;
  remote.ss_family = af;

  switch (af) {
  case AF_INET:
    addrcpy((void *) &local4->sin_addr, (void *) &ssa4->sin_addr, af);
    addrcpy((void *) &remote4->sin_addr, (void *) remoteaddress, af);
    remote4->sin_port = htons(remoteport);
    len = sizeof (struct sockaddr_in);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    addrcpy((void *) &local6->sin6_addr, (void *) &ssa6->sin6_addr, af);
    addrcpy((void *) &remote6->sin6_addr, (void *) remoteaddress, af);
    remote6->sin6_port = htons(remoteport);
    len = sizeof (struct sockaddr_in6);
    break;
#endif
  }

  if (bind(s, (struct sockaddr *) &local, len)) {
    display_clear();
    perror("bind()");
    exit(EXIT_FAILURE);
  }

  if (getsockname(s, (struct sockaddr *) &local, &len)) {
    display_clear();
    perror("getsockname()");
    exit(EXIT_FAILURE);
  }

  opt = 1;
  if (ioctl(s, FIONBIO, &opt)) {
    display_clear();
    perror("ioctl FIONBIO");
    exit(EXIT_FAILURE);
  }

  switch (af) {
  case AF_INET:
    if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl))) {
      display_clear();
      perror("setsockopt IP_TTL");
      exit(EXIT_FAILURE);
    }
    if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos))) {
      display_clear();
      perror("setsockopt IP_TOS");
      exit(EXIT_FAILURE);
    }
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl))) {
      display_clear();
      perror("setsockopt IP_TTL");
      exit(EXIT_FAILURE);
    }
    break;
#endif
  }

#ifdef SO_MARK
    if (mark >= 0 && setsockopt( s, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
      perror( "setsockopt SO_MARK" );
      exit( EXIT_FAILURE );
    }
#endif

  switch (local.ss_family) {
  case AF_INET:
    port = ntohs(local4->sin_port);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    port = ntohs(local6->sin6_port);
    break;
#endif
  default:
    display_clear();
    perror("unknown AF?");
    exit(EXIT_FAILURE);
  }

  save_sequence(index, port);
  gettimeofday(&sequence[port].time, NULL);
  sequence[port].socket = s;

  connect(s, (struct sockaddr *) &remote, len);
}

/*  Attempt to connect to a SCTP port with a TTL */
void net_send_sctp(int index)
{
  int ttl, s;
  int opt = 1;
  int port;
  struct sockaddr_storage local;
  struct sockaddr_storage remote;
  struct sockaddr_in *local4 = (struct sockaddr_in *) &local;
  struct sockaddr_in6 *local6 = (struct sockaddr_in6 *) &local;
  struct sockaddr_in *remote4 = (struct sockaddr_in *) &remote;
  struct sockaddr_in6 *remote6 = (struct sockaddr_in6 *) &remote;
  socklen_t len;

  ttl = index + 1;

  s = socket(af, SOCK_STREAM, IPPROTO_SCTP);
  if (s < 0) {
    display_clear();
    perror("socket()");
    exit(EXIT_FAILURE);
  }

  memset(&local, 0, sizeof (local));
  memset(&remote, 0, sizeof (remote));
  local.ss_family = af;
  remote.ss_family = af;

  switch (af) {
  case AF_INET:
    addrcpy((void *) &local4->sin_addr, (void *) &ssa4->sin_addr, af);
    addrcpy((void *) &remote4->sin_addr, (void *) remoteaddress, af);
    remote4->sin_port = htons(remoteport);
    len = sizeof (struct sockaddr_in);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    addrcpy((void *) &local6->sin6_addr, (void *) &ssa6->sin6_addr, af);
    addrcpy((void *) &remote6->sin6_addr, (void *) remoteaddress, af);
    remote6->sin6_port = htons(remoteport);
    len = sizeof (struct sockaddr_in6);
    break;
#endif
  }

  if (bind(s, (struct sockaddr *) &local, len)) {
    display_clear();
    perror("bind()");
    exit(EXIT_FAILURE);
  }

  if (getsockname(s, (struct sockaddr *) &local, &len)) {
    display_clear();
    perror("getsockname()");
    exit(EXIT_FAILURE);
  }

  opt = 1;
  if (ioctl(s, FIONBIO, &opt)) {
    display_clear();
    perror("ioctl FIONBIO");
    exit(EXIT_FAILURE);
  }

  switch (af) {
  case AF_INET:
    if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl))) {
      display_clear();
      perror("setsockopt IP_TTL");
      exit(EXIT_FAILURE);
    }
    if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos))) {
      display_clear();
      perror("setsockopt IP_TOS");
      exit(EXIT_FAILURE);
    }
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    if (setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl))) {
      display_clear();
      perror("setsockopt IP_TTL");
      exit(EXIT_FAILURE);
    }
    break;
#endif
  }

#ifdef SO_MARK
    if (mark >= 0 && setsockopt( s, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
      perror( "setsockopt SO_MARK" );
      exit( EXIT_FAILURE );
    }
#endif

  switch (local.ss_family) {
  case AF_INET:
    port = ntohs(local4->sin_port);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    port = ntohs(local6->sin6_port);
    break;
#endif
  default:
    display_clear();
    perror("unknown AF?");
    exit(EXIT_FAILURE);
  }

  save_sequence(index, port);
  gettimeofday(&sequence[port].time, NULL);
  sequence[port].socket = s;

  connect(s, (struct sockaddr *) &remote, len);
}

/*  Attempt to find the host at a particular number of hops away  */
void net_send_query(int index) 
{
  if (mtrtype == IPPROTO_TCP) {
    net_send_tcp(index);
    return;
  }
  
  if (mtrtype == IPPROTO_SCTP) {
    net_send_sctp(index);
    return;
  }

  /*ok  char packet[sizeof(struct IPHeader) + sizeof(struct ICMPHeader)];*/
  char packet[MAXPACKET];
  struct IPHeader *ip = (struct IPHeader *) packet;
  struct ICMPHeader *icmp = NULL;
  struct UDPHeader *udp = NULL;
  struct UDPv4PHeader *udpp = NULL;
  uint16 checksum_result;
  uint16 mypid;

  /*ok  int packetsize = sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + datasize;*/
  int rv;
  static int first=1;
  int ttl, iphsize = 0, echotype = 0, salen = 0;

  ttl = index + 1;

#ifdef ENABLE_IPV6
  /* offset for ipv6 checksum calculation */
  int offset = 6;
#endif

  if ( packetsize < MINPACKET ) packetsize = MINPACKET;
  if ( packetsize > MAXPACKET ) packetsize = MAXPACKET;
  if ( mtrtype == IPPROTO_UDP && remoteport && packetsize < (MINPACKET + 2)) {
    packetsize = MINPACKET + 2;
  }

  memset(packet, (unsigned char) abs(bitpattern), abs(packetsize));

  switch ( af ) {
  case AF_INET:
#if !defined(IP_HDRINCL) && defined(IP_TOS) && defined(IP_TTL)
    iphsize = 0;
    if ( setsockopt( sendsock, IPPROTO_IP, IP_TOS, &tos, sizeof tos ) ) {
      perror( "setsockopt IP_TOS" );
      exit( EXIT_FAILURE );
    }    
    if ( setsockopt( sendsock, IPPROTO_IP, IP_TTL, &ttl, sizeof ttl ) ) {
      perror( "setsockopt IP_TTL" );
      exit( EXIT_FAILURE );
    }    
#else
    iphsize = sizeof (struct IPHeader);

  ip->version = 0x45;
  ip->tos = tos;
  ip->len = BSDfix ? abs(packetsize): htons (abs(packetsize));
  ip->id = 0;
  ip->frag = 0;    /* 1, if want to find mtu size? Min */
    ip->ttl = ttl;
  ip->protocol = mtrtype;
  ip->check = 0;

  /* BSD needs the source address here, Linux & others do not... */
    addrcpy( (void *) &(ip->saddr), (void *) &(ssa4->sin_addr), AF_INET );
    addrcpy( (void *) &(ip->daddr), (void *) remoteaddress, AF_INET );
#endif
    echotype = ICMP_ECHO;
    salen = sizeof (struct sockaddr_in);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    iphsize = 0;
    if ( setsockopt( sendsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
                     &ttl, sizeof ttl ) ) {
      perror( "setsockopt IPV6_UNICAST_HOPS" );
      exit( EXIT_FAILURE);
    }
    echotype = ICMP6_ECHO_REQUEST;
    salen = sizeof (struct sockaddr_in6);
    break;
#endif
  }

#ifdef SO_MARK
    if (mark >= 0 && setsockopt( sendsock, SOL_SOCKET, SO_MARK, &mark, sizeof mark ) ) {
      perror( "setsockopt SO_MARK" );
      exit( EXIT_FAILURE );
    }
#endif

  switch ( mtrtype ) {
  case IPPROTO_ICMP:
    icmp = (struct ICMPHeader *)(packet + iphsize);
    icmp->type     = echotype;
    icmp->code     = 0;
    icmp->checksum = 0;
    icmp->id       = getpid();
    icmp->sequence = new_sequence(index);
    icmp->checksum = checksum(icmp, abs(packetsize) - iphsize);
    
    gettimeofday(&sequence[icmp->sequence].time, NULL);
    break;

  case IPPROTO_UDP:
    udp = (struct UDPHeader *)(packet + iphsize);
    udp->checksum  = 0;
    if (!localport) {
      mypid = (uint16)getpid();
      if (mypid < MinPort)
        mypid += MinPort;
    } else {
      mypid = (uint16)localport;
    }
    udp->srcport = htons(mypid);
    udp->length = htons(abs(packetsize) - iphsize);

    if (!remoteport) {
      udp->dstport = new_sequence(index);
      gettimeofday(&sequence[udp->dstport].time, NULL);
      udp->dstport = htons(udp->dstport);
    } else {
      // keep dstport constant, stuff sequence into the checksum
      udp->dstport = htons(remoteport);
      udp->checksum = new_sequence(index);
      gettimeofday(&sequence[udp->checksum].time, NULL);
      udp->checksum = htons(udp->checksum);
    }
    break;
  }

  switch ( af ) {
  case AF_INET:
    switch ( mtrtype ) {
    case IPPROTO_UDP:
      /* checksum is not mandatory. only calculate if we know ip->saddr */
      if (udp->checksum) {
        udpp = (struct UDPv4PHeader *)(malloc(sizeof(struct UDPv4PHeader)));
        udpp->saddr = ip->saddr;
        udpp->daddr = ip->daddr;
        udpp->protocol = ip->protocol;
        udpp->len = udp->length;
        checksum_result = udp_checksum(udpp, udp, sizeof(struct UDPv4PHeader), abs(packetsize) - iphsize, 1);
        packet[iphsize + sizeof(struct UDPHeader)] = ((char *)&checksum_result)[0];
        packet[iphsize + sizeof(struct UDPHeader) + 1] = ((char *)&checksum_result)[1];
      } else if (ip->saddr) {
        udpp = (struct UDPv4PHeader *)(malloc(sizeof(struct UDPv4PHeader)));
        udpp->saddr = ip->saddr;
        udpp->daddr = ip->daddr;
        udpp->protocol = ip->protocol;
        udpp->len = udp->length;
        udp->checksum = udp_checksum(udpp, udp, sizeof(struct UDPv4PHeader), abs(packetsize) - iphsize, 0);
      }
      break;
    }

    ip->check = checksum(packet, abs(packetsize));
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    switch ( mtrtype ) {
    case IPPROTO_UDP:
      /* kernel checksum calculation */
      if (udp->checksum) {
        offset = sizeof(struct UDPHeader);
      }
      if ( setsockopt(sendsock, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset)) ) {
        perror( "setsockopt IPV6_CHECKSUM" );
        exit( EXIT_FAILURE);
      }
      break;
    }
    break;
#endif
  }

  /* sendto() assumes packet length includes the IPv4 header but not the 
     IPv6 header. */
  spacketsize = abs(packetsize)	-
		( ( af == AF_INET ) ? 0 : sizeof (struct ip6_hdr) );
  rv = sendto(sendsock, packet, spacketsize, 0, remotesockaddr, salen);
  if (first && (rv < 0) && ((errno == EINVAL) || (errno == EMSGSIZE))) {
    /* Try the first packet again using host byte order. */
    ip->len = spacketsize;
    rv = sendto(sendsock, packet, spacketsize, 0, remotesockaddr, salen);
    if (rv >= 0) {
      BSDfix = 1;
    }
  }
  first = 0;
}


/*   We got a return on something we sent out.  Record the address and
     time.  */
void net_process_ping(int seq, struct mplslen mpls, void * addr, struct timeval now) 
{
  int index;
  int totusec;
  int oldavg;	/* usedByMin */
  int oldjavg;	/* usedByMin */
  int i;	/* usedByMin */
#ifdef ENABLE_IPV6
  char addrcopy[sizeof(struct in6_addr)];
#else
  char addrcopy[sizeof(struct in_addr)];
#endif

  /* Copy the from address ASAP because it can be overwritten */
  addrcpy( (void *) &addrcopy, addr, af );

  if (seq < 0 || seq >= MaxSequence)
    return;

  if (!sequence[seq].transit)
    return;
  sequence[seq].transit = 0;

  if (sequence[seq].socket > 0) {
    close(sequence[seq].socket);
    sequence[seq].socket = 0;
  }

  index = sequence[seq].index;

  totusec = (now.tv_sec  - sequence[seq].time.tv_sec ) * 1000000 +
            (now.tv_usec - sequence[seq].time.tv_usec);
  /* impossible? if( totusec < 0 ) totusec = 0 */;

  if ( addrcmp( (void *) &(host[index].addr),
		(void *) &unspec_addr, af ) == 0 ) {
    /* should be out of if as addr can change */
    addrcpy( (void *) &(host[index].addr), addrcopy, af );
    host[index].mpls = mpls;
    display_rawhost(index, (void *) &(host[index].addr));

  /* multi paths */
    addrcpy( (void *) &(host[index].addrs[0]), addrcopy, af );
    host[index].mplss[0] = mpls;
  } else {
    for( i=0; i<MAXPATH; ) {
      if( addrcmp( (void *) &(host[index].addrs[i]), (void *) &addrcopy,
                   af ) == 0 ||
          addrcmp( (void *) &(host[index].addrs[i]),
		   (void *) &unspec_addr, af ) == 0 ) break;
      i++;
    }
    if( addrcmp( (void *) &(host[index].addrs[i]), addrcopy, af ) != 0 && 
        i<MAXPATH ) {
      addrcpy( (void *) &(host[index].addrs[i]), addrcopy, af );
      host[index].mplss[i] = mpls;
      display_rawhost(index, (void *) &(host[index].addrs[i]));
    }
  }

  host[index].jitter = totusec - host[index].last;
  if (host[index].jitter < 0 ) host[index].jitter = - host[index].jitter;
  host[index].last = totusec;

  if (host[index].returned < 1) {
    host[index].best = host[index].worst = host[index].gmean = totusec;
    host[index].avg  = host[index].var  = 0;

    host[index].jitter = host[index].jworst = host[index].jinta= 0;
  }

  /* some time best can be too good to be true, experienced 
   * at least in linux 2.4.x.
   *  safe guard 1) best[index]>=best[index-1] if index>0
   *             2) best >= average-20,000 usec (good number?)
  if (index > 0) {
    if (totusec < host[index].best &&
       totusec>= host[index-1].best) host[index].best  = totusec;
  } else {
    if(totusec < host[index].best) host[index].best  = totusec;
  }
   */
  if (totusec < host[index].best ) host[index].best  = totusec;
  if (totusec > host[index].worst) host[index].worst = totusec;

  if (host[index].jitter > host[index].jworst)
	host[index].jworst = host[index].jitter;

  host[index].returned++;
  oldavg = host[index].avg;
  host[index].avg += (totusec - oldavg +.0) / host[index].returned;
  host[index].var += (totusec - oldavg +.0) * (totusec - host[index].avg) / 1000000;

  oldjavg = host[index].javg;
  host[index].javg += (host[index].jitter - oldjavg) / host[index].returned;
  /* below algorithm is from rfc1889, A.8 */
  host[index].jinta += host[index].jitter - ((host[index].jinta + 8) >> 4);

  if ( host[index].returned > 1 )
  host[index].gmean = pow( (double) host[index].gmean, (host[index].returned-1.0)/host[index].returned )
			* pow( (double) totusec, 1.0/host[index].returned );
  host[index].sent = 0;
  host[index].up = 1;
  host[index].transit = 0;

  net_save_return(index, sequence[seq].saved_seq, totusec);
  display_rawping(index, totusec, seq);
}


/*  We know a packet has come in, because the main select loop has called us,
    now we just need to read it, see if it is for us, and if it is a reply 
    to something we sent, then call net_process_ping()  */
void net_process_return(void) 
{
  char packet[MAXPACKET];
#ifdef ENABLE_IPV6
  struct sockaddr_storage fromsockaddr_struct;
  struct sockaddr_in6 * fsa6 = (struct sockaddr_in6 *) &fromsockaddr_struct;
#else
  struct sockaddr_in fromsockaddr_struct;
#endif
  struct sockaddr * fromsockaddr = (struct sockaddr *) &fromsockaddr_struct;
  struct sockaddr_in * fsa4 = (struct sockaddr_in *) &fromsockaddr_struct;
  socklen_t fromsockaddrsize;
  int num;
  struct ICMPHeader *header = NULL;
  struct UDPHeader *udpheader = NULL;
  struct TCPHeader *tcpheader = NULL;
  struct SCTPHeader *sctpheader = NULL;
  struct timeval now;
  ip_t * fromaddress = NULL;
  int echoreplytype = 0, timeexceededtype = 0, unreachabletype = 0;
  int sequence = 0;

  /* MPLS decoding */
  struct mplslen mpls;
  mpls.labels = 0;

  gettimeofday(&now, NULL);
  switch ( af ) {
  case AF_INET:
    fromsockaddrsize = sizeof (struct sockaddr_in);
    fromaddress = (ip_t *) &(fsa4->sin_addr);
    echoreplytype = ICMP_ECHOREPLY;
    timeexceededtype = ICMP_TIME_EXCEEDED;
    unreachabletype = ICMP_UNREACHABLE;
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    fromsockaddrsize = sizeof (struct sockaddr_in6);
    fromaddress = (ip_t *) &(fsa6->sin6_addr);
    echoreplytype = ICMP6_ECHO_REPLY;
    timeexceededtype = ICMP6_TIME_EXCEEDED;
    unreachabletype = ICMP6_DST_UNREACH;
    break;
#endif
  }

  num = recvfrom(recvsock, packet, MAXPACKET, 0, 
		 fromsockaddr, &fromsockaddrsize);

  switch ( af ) {
  case AF_INET:
    if((size_t) num < sizeof(struct IPHeader) + sizeof(struct ICMPHeader))
      return;
    header = (struct ICMPHeader *)(packet + sizeof(struct IPHeader));
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    if(num < sizeof(struct ICMPHeader))
      return;

    header = (struct ICMPHeader *) packet;
    break;
#endif
  }

  switch ( mtrtype ) {
  case IPPROTO_ICMP:
    if (header->type == echoreplytype) {
      if(header->id != (uint16)getpid())
        return;

      sequence = header->sequence;
    } else if (header->type == timeexceededtype) {
      switch ( af ) {
      case AF_INET:

        if ((size_t) num < sizeof(struct IPHeader) + 
                           sizeof(struct ICMPHeader) + 
                           sizeof (struct IPHeader) + 
                           sizeof (struct ICMPHeader))
          return;
        header = (struct ICMPHeader *)(packet + sizeof (struct IPHeader) + 
                                                sizeof (struct ICMPHeader) + 
                                                sizeof (struct IPHeader));

        if(num > 160)
          decodempls(num, packet, &mpls, 156);

      break;
#ifdef ENABLE_IPV6
      case AF_INET6:
        if ( num < sizeof (struct ICMPHeader) + 
                   sizeof (struct ip6_hdr) + sizeof (struct ICMPHeader) )
          return;
        header = (struct ICMPHeader *) ( packet + 
                                         sizeof (struct ICMPHeader) +
                                         sizeof (struct ip6_hdr) );

        if(num > 140)
          decodempls(num, packet, &mpls, 136);

        break;
#endif
      }
  
      if (header->id != (uint16)getpid())
        return;
  
      sequence = header->sequence;
    }
    break;
  
  case IPPROTO_UDP:
    if (header->type == timeexceededtype || header->type == unreachabletype) {
      switch ( af ) {
      case AF_INET:

        if ((size_t) num < sizeof(struct IPHeader) +
                           sizeof(struct ICMPHeader) +
                           sizeof (struct IPHeader) +
                           sizeof (struct UDPHeader))
          return;
        udpheader = (struct UDPHeader *)(packet + sizeof (struct IPHeader) +
                                                  sizeof (struct ICMPHeader) +
                                                  sizeof (struct IPHeader));

        if(num > 160)
          decodempls(num, packet, &mpls, 156);

      break;
#ifdef ENABLE_IPV6
      case AF_INET6:
        if ( num < sizeof (struct ICMPHeader) +
                   sizeof (struct ip6_hdr) + sizeof (struct UDPHeader) )
          return;
        udpheader = (struct UDPHeader *) ( packet +
                                           sizeof (struct ICMPHeader) +
                                           sizeof (struct ip6_hdr) );

        if(num > 140)
          decodempls(num, packet, &mpls, 136);

        break;
#endif
      }
      if (remoteport && remoteport == ntohs(udpheader->dstport)) {
        sequence = ntohs(udpheader->checksum);
      } else if (!remoteport) {
        sequence = ntohs(udpheader->dstport);
      }
    }
    break;

  case IPPROTO_TCP:
    if (header->type == timeexceededtype || header->type == unreachabletype) {
      switch ( af ) {
      case AF_INET:

        if ((size_t) num < sizeof(struct IPHeader) +
                           sizeof(struct ICMPHeader) +
                           sizeof (struct IPHeader) +
                           sizeof (struct TCPHeader))
          return;
        tcpheader = (struct TCPHeader *)(packet + sizeof (struct IPHeader) +
                                                  sizeof (struct ICMPHeader) +
                                                  sizeof (struct IPHeader));

        if(num > 160)
          decodempls(num, packet, &mpls, 156);

      break;
#ifdef ENABLE_IPV6
      case AF_INET6:
        if ( num < sizeof (struct ICMPHeader) +
                   sizeof (struct ip6_hdr) + sizeof (struct TCPHeader) )
          return;
        tcpheader = (struct TCPHeader *) ( packet +
                                           sizeof (struct ICMPHeader) +
                                           sizeof (struct ip6_hdr) );

        if(num > 140)
          decodempls(num, packet, &mpls, 136);

        break;
#endif
      }
      sequence = ntohs(tcpheader->srcport);
    }
    break;
    
  case IPPROTO_SCTP:
    if (header->type == timeexceededtype || header->type == unreachabletype) {
      switch ( af ) {
      case AF_INET:

        if ((size_t) num < sizeof(struct IPHeader) +
                           sizeof(struct ICMPHeader) +
                           sizeof (struct IPHeader) +
                           sizeof (struct SCTPHeader))
          return;
        sctpheader = (struct SCTPHeader *)(packet + sizeof (struct IPHeader) +
                                                  sizeof (struct ICMPHeader) +
                                                  sizeof (struct IPHeader));

        if(num > 160)
          decodempls(num, packet, &mpls, 156);

      break;
#ifdef ENABLE_IPV6
      case AF_INET6:
        if ( num < sizeof (struct ICMPHeader) +
                   sizeof (struct ip6_hdr) + sizeof (struct SCTPHeader) )
          return;
        sctpheader = (struct SCTPHeader *) ( packet +
                                           sizeof (struct ICMPHeader) +
                                           sizeof (struct ip6_hdr) );

        if(num > 140)
          decodempls(num, packet, &mpls, 136);

        break;
#endif
      }
      sequence = ntohs(sctpheader->srcport);
    }
    break;
  }
  if (sequence)
    net_process_ping (sequence, mpls, (void *) fromaddress, now);
}


ip_t *net_addr(int at) 
{
  return (ip_t *)&(host[at].addr);
}


ip_t *net_addrs(int at, int i) 
{
  return (ip_t *)&(host[at].addrs[i]);
}

void *net_mpls(int at)
{
  return (struct mplslen *)&(host[at].mplss);
}

void *net_mplss(int at, int i)
{
  return (struct mplslen *)&(host[at].mplss[i]);
}

int net_loss(int at) 
{
  if ((host[at].xmit - host[at].transit) == 0) 
    return 0;
  /* times extra 1000 */
  return 1000*(100 - (100.0 * host[at].returned / (host[at].xmit - host[at].transit)) );
}


int net_drop(int at) 
{
  return (host[at].xmit - host[at].transit) - host[at].returned;
}


int net_last(int at) 
{
  return (host[at].last);
}


int net_best(int at) 
{
  return (host[at].best);
}


int net_worst(int at) 
{
  return (host[at].worst);
}


int net_avg(int at) 
{
  return (host[at].avg);
}


int net_gmean(int at) 
{
  return (host[at].gmean);
}


int net_stdev(int at) 
{
  if( host[at].returned > 1 ) {
    return ( 1000.0 * sqrt( host[at].var/(host[at].returned -1.0) ) );
  } else {
    return( 0 );
  }
}


int net_jitter(int at) 
{ 
  return (host[at].jitter); 
}


int net_jworst(int at) 
{ 
  return (host[at].jworst); 
}


int net_javg(int at) 
{ 
  return (host[at].javg); 
}


int net_jinta(int at) 
{ 
  return (host[at].jinta); 
}


int net_max(void) 
{
  int at;
  int max;

  max = 0;
  /* for(at = 0; at < MaxHost-2; at++) { */
  for(at = 0; at < maxTTL-1; at++) {
    if ( addrcmp( (void *) &(host[at].addr),
                  (void *) remoteaddress, af ) == 0 ) {
      return at + 1;
    } else if ( addrcmp( (void *) &(host[at].addr),
			 (void *) &unspec_addr, af ) != 0 ) {
      max = at + 2;
    }
  }

  return max;
}


int net_min (void) 
{
  return ( fstTTL - 1 );
}


int net_returned(int at) 
{ 
  return host[at].returned;
}


int net_xmit(int at) 
{ 
  return host[at].xmit;
}


int net_transit(int at) 
{ 
  return host[at].transit;
}


int net_up(int at) 
{
   return host[at].up;
}


char * net_localaddr (void)
{
  return localaddr;
}


void net_end_transit(void) 
{
  int at;
  
  for(at = 0; at < MaxHost; at++) {
    host[at].transit = 0;
  }
}

int net_send_batch(void) 
{
  int n_unknown=0, i;

  /* randomized packet size and/or bit pattern if packetsize<0 and/or 
     bitpattern<0.  abs(packetsize) and/or abs(bitpattern) will be used 
  */
  if( batch_at < fstTTL ) {
    if( cpacketsize < 0 ) {
	/* Someone used a formula here that tried to correct for the 
           "end-error" in "rand()". By "end-error" I mean that if you 
           have a range for "rand()" that runs to 32768, and the 
           destination range is 10000, you end up with 4 out of 32768 
           0-2768's and only 3 out of 32768 for results 2769 .. 9999. 
           As our detination range (in the example 10000) is much 
           smaller (reasonable packet sizes), and our rand() range much 
           larger, this effect is insignificant. Oh! That other formula
           didn't work. */
      packetsize = MINPACKET + rand () % (-cpacketsize - MINPACKET);
    } else {
      packetsize = cpacketsize;
    }
    if( bitpattern < 0 ) {
      bitpattern = - (int)(256 + 255*(rand()/(RAND_MAX+0.1)));
    }
  }

  /* printf ("cpacketsize = %d, packetsize = %d\n", cpacketsize, packetsize);  */

  net_send_query(batch_at);

  for (i=fstTTL-1;i<batch_at;i++) {
    if ( addrcmp( (void *) &(host[i].addr), (void *) &unspec_addr, af ) == 0 )
      n_unknown++;

    /* The second condition in the next "if" statement was added in mtr-0.56, 
	but I don't remember why. It makes mtr stop skipping sections of unknown
	hosts. Removed in 0.65. 
	If the line proves necessary, it should at least NOT trigger that line
	when host[i].addr == 0 */
    if ( ( addrcmp( (void *) &(host[i].addr),
                    (void *) remoteaddress, af ) == 0 )
	/* || (host[i].addr == host[batch_at].addr)  */)
      n_unknown = MaxHost; /* Make sure we drop into "we should restart" */
  }

  if (	/* success in reaching target */
     ( addrcmp( (void *) &(host[batch_at].addr),
                (void *) remoteaddress, af ) == 0 ) ||
      /* fail in consecutive maxUnknown (firewall?) */
      (n_unknown > maxUnknown) ||
      /* or reach limit  */
      (batch_at >= maxTTL-1)) {
    numhosts = batch_at+1;
    batch_at = fstTTL - 1;
    return 1;
  }

  batch_at++;
  return 0;
}


static void set_fd_flags(int fd)
{
#if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
  int oldflags;

  if (fd < 0) return; 

  oldflags = fcntl(fd, F_GETFD);
  if (oldflags == -1) {
    perror("Couldn't get fd's flags");
    return;
  }
  if (fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC))
    perror("Couldn't set fd's flags");
#endif
}

int net_preopen(void) 
{
  int trueopt = 1;

#if !defined(IP_HDRINCL) && defined(IP_TOS) && defined(IP_TTL)
  sendsock4_icmp = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  sendsock4_udp = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
#else
  sendsock4 = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
#endif
  if (sendsock4 < 0) 
    return -1;
#ifdef ENABLE_IPV6
  sendsock6_icmp = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
  sendsock6_udp = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
#endif

#ifdef IP_HDRINCL
  /*  FreeBSD wants this to avoid sending out packets with protocol type RAW
      to the network.  */
  if (setsockopt(sendsock4, SOL_IP, IP_HDRINCL, &trueopt, sizeof(trueopt))) {
    perror("setsockopt(IP_HDRINCL,1)");
    return -1;
  }
#endif /* IP_HDRINCL */

  recvsock4 = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  if (recvsock4 < 0)
    return -1;
  set_fd_flags(recvsock4);
#ifdef ENABLE_IPV6
  recvsock6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
  if (recvsock6 >= 0)
     set_fd_flags(recvsock6);
#endif

  return 0;
}


int net_selectsocket(void)
{
#if !defined(IP_HDRINCL) && defined(IP_TOS) && defined(IP_TTL)
  switch ( mtrtype ) {
  case IPPROTO_ICMP:
    sendsock4 = sendsock4_icmp;
    break;
  case IPPROTO_UDP:
    sendsock4 = sendsock4_udp;
    break;
  }
#endif
  if (sendsock4 < 0)
    return -1;
#ifdef ENABLE_IPV6
  switch ( mtrtype ) {
  case IPPROTO_ICMP:
    sendsock6 = sendsock6_icmp;
    break;
  case IPPROTO_UDP:
    sendsock6 = sendsock6_udp;
    break;
  }
  if ((sendsock6 < 0) && (sendsock4 < 0))
    return -1;
#endif

 return 0;
}


int net_open(struct hostent * host) 
{
#ifdef ENABLE_IPV6
  struct sockaddr_storage name_struct;
#else
  struct sockaddr_in name_struct; 
#endif
  struct sockaddr * name = (struct sockaddr *) &name_struct;
  socklen_t len; 

  net_reset();

  remotesockaddr->sa_family = host->h_addrtype;

  switch ( host->h_addrtype ) {
  case AF_INET:
    sendsock = sendsock4;
    recvsock = recvsock4;
    addrcpy( (void *) &(rsa4->sin_addr), host->h_addr, AF_INET );
    sourceaddress = (ip_t *) &(ssa4->sin_addr);
    remoteaddress = (ip_t *) &(rsa4->sin_addr);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    if (sendsock6 < 0 || recvsock6 < 0) {
      fprintf( stderr, "Could not open IPv6 socket\n" );
      exit( EXIT_FAILURE );
    }
    sendsock = sendsock6;
    recvsock = recvsock6;
    addrcpy( (void *) &(rsa6->sin6_addr), host->h_addr, AF_INET6 );
    sourceaddress = (ip_t *) &(ssa6->sin6_addr);
    remoteaddress = (ip_t *) &(rsa6->sin6_addr);
    break;
#endif
  default:
    fprintf( stderr, "net_open bad address type\n" );
    exit( EXIT_FAILURE );
  }

  len = sizeof name_struct; 
  getsockname (recvsock, name, &len);
  sockaddrtop( name, localaddr, sizeof localaddr );
#if 0
  printf ("got localaddr: %s\n", localaddr); 
#endif

  return 0;
}


void net_reopen(struct hostent * addr) 
{
  int at;

  for(at = 0; at < MaxHost; at++) {
    memset(&host[at], 0, sizeof(host[at]));
  }

  remotesockaddr->sa_family = addr->h_addrtype;
  addrcpy( (void *) remoteaddress, addr->h_addr, addr->h_addrtype );

  switch ( addr->h_addrtype ) {
  case AF_INET:
    addrcpy( (void *) &(rsa4->sin_addr), addr->h_addr, AF_INET );
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    addrcpy( (void *) &(rsa6->sin6_addr), addr->h_addr, AF_INET6 );
    break;
#endif
  default:
    fprintf( stderr, "net_reopen bad address type\n" );
    exit( EXIT_FAILURE );
  }

  net_reset ();
  net_send_batch();
}


void net_reset(void) 
{
  int at;
  int i;

  batch_at = fstTTL - 1;	/* above replacedByMin */
  numhosts = 10;

  for (at = 0; at < MaxHost; at++) {
    host[at].xmit = 0;
    host[at].transit = 0;
    host[at].returned = 0;
    host[at].sent = 0;
    host[at].up = 0;
    host[at].last = 0;
    host[at].avg  = 0;
    host[at].best = 0;
    host[at].worst = 0;
    host[at].gmean = 0;
    host[at].var = 0;
    host[at].jitter = 0;
    host[at].javg = 0;
    host[at].jworst = 0;
    host[at].jinta = 0;
    for (i=0; i<SAVED_PINGS; i++) {
      host[at].saved[i] = -2;	/* unsent */
    }
    host[at].saved_seq_offset = -SAVED_PINGS+2;
  }
  
  for (at = 0; at < MaxSequence; at++) {
    sequence[at].transit = 0;
    if (sequence[at].socket > 0) {
      close(sequence[at].socket);
      sequence[at].socket = 0;
    }
  }

  gettimeofday(&reset, NULL);
}

int net_set_interfaceaddress_udp()
{
#ifdef ENABLE_IPV6
  struct sockaddr_storage name_struct;
#else
  struct sockaddr_in name_struct;
#endif
  struct sockaddr_in *  sa4;
  struct sockaddr_in6 * sa6;
  struct sockaddr * name = (struct sockaddr *) &name_struct;
  struct sockaddr_storage remote;
  struct sockaddr_in *remote4 = (struct sockaddr_in *) &remote;
  struct sockaddr_in6 *remote6 = (struct sockaddr_in6 *) &remote;
  socklen_t len;
  int s;

  memset(&remote, 0, sizeof (remote));
  remote.ss_family = af;

  switch (af) {
  case AF_INET:
    addrcpy((void *) &remote4->sin_addr, (void *) remoteaddress, af);
    remote4->sin_port = htons(remoteport);
    len = sizeof (struct sockaddr_in);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    addrcpy((void *) &remote6->sin6_addr, (void *) remoteaddress, af);
    remote6->sin6_port = htons(remoteport);
    len = sizeof (struct sockaddr_in6);
    break;
#endif
  }

  s = socket (af, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("udp socket()");
    exit(EXIT_FAILURE);
  }

  if (connect(s, (struct sockaddr *) &remote, len)) {
    perror("udp connect() failed");
    exit(EXIT_FAILURE);
  }

  getsockname(s, name, &len);
  sockaddrtop( name, localaddr, sizeof localaddr );
  switch (af) {
  case AF_INET:
    sa4 = (struct sockaddr_in *) name;
    addrcpy((void*)&ssa4->sin_addr, (void *) &(sa4->sin_addr), af );
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    sa6 = (struct sockaddr_in6 *) name;
    addrcpy((void*)&ssa6->sin6_addr, (void *) &(sa6->sin6_addr), af );
    break;
#endif
  }
  close(s);

  return 0;
}


int net_set_interfaceaddress (char *InterfaceAddress)
{
#ifdef ENABLE_IPV6
  struct sockaddr_storage name_struct;
#else
  struct sockaddr_in name_struct;
#endif
  struct sockaddr * name = (struct sockaddr *) &name_struct;
  socklen_t len = 0;

  if (mtrtype == IPPROTO_UDP && remoteport && !InterfaceAddress) {
    return net_set_interfaceaddress_udp();
  }
  if (!InterfaceAddress) return 0; 

  sourcesockaddr->sa_family = af;
  switch ( af ) {
  case AF_INET:
    ssa4->sin_port = 0;
    if ( inet_aton( InterfaceAddress, &(ssa4->sin_addr) ) < 1 ) {
      fprintf( stderr, "mtr: bad interface address: %s\n", InterfaceAddress );
      return( 1 );
  }
    len = sizeof (struct sockaddr);
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    ssa6->sin6_port = 0;
    if ( inet_pton( af, InterfaceAddress, &(ssa6->sin6_addr) ) < 1 ) {
      fprintf( stderr, "mtr: bad interface address: %s\n", InterfaceAddress );
      return( 1 );
    }
    len = sizeof (struct sockaddr_in6);
    break;
#endif
  }

  if ( bind( sendsock, sourcesockaddr, len ) == -1 ) {
    perror("mtr: failed to bind to interface");
      return( 1 );
  }
  getsockname (sendsock, name, &len);
  sockaddrtop( name, localaddr, sizeof localaddr );
  return 0; 
}



void net_close(void)
{
  if (sendsock4 >= 0) {
    close(sendsock4_icmp);
    close(sendsock4_udp);
  }
  if (recvsock4 >= 0) close(recvsock4);
  if (sendsock6 >= 0) {
    close(sendsock6_icmp);
    close(sendsock6_udp);
  }
  if (recvsock6 >= 0) close(recvsock6);
}


int net_waitfd(void)
{
  return recvsock;
}


int* net_saved_pings(int at)
{
  return host[at].saved;
}


void net_save_increment(void)
{
  int at;
  for (at = 0; at < MaxHost; at++) {
    memmove(host[at].saved, host[at].saved+1, (SAVED_PINGS-1)*sizeof(int));
    host[at].saved[SAVED_PINGS-1] = -2;
    host[at].saved_seq_offset += 1;
  }
}


void net_save_xmit(int at)
{
  if (host[at].saved[SAVED_PINGS-1] != -2) 
    net_save_increment();
  host[at].saved[SAVED_PINGS-1] = -1;
}


void net_save_return(int at, int seq, int ms)
{
  int idx;
  idx = seq - host[at].saved_seq_offset;
  if (idx < 0 || idx >= SAVED_PINGS) {
    return;
  }
  host[at].saved[idx] = ms;
}

/* Similar to inet_ntop but uses a sockaddr as it's argument. */
void sockaddrtop( struct sockaddr * saddr, char * strptr, size_t len ) {
  struct sockaddr_in *  sa4;
#ifdef ENABLE_IPV6
  struct sockaddr_in6 * sa6;
#endif

  switch ( saddr->sa_family ) {
  case AF_INET:
    sa4 = (struct sockaddr_in *) saddr;
    strncpy( strptr, inet_ntoa( sa4->sin_addr ), len - 1 );
    strptr[ len - 1 ] = '\0';
    return;
#ifdef ENABLE_IPV6
  case AF_INET6:
    sa6 = (struct sockaddr_in6 *) saddr;
    inet_ntop( sa6->sin6_family, &(sa6->sin6_addr), strptr, len );
    return;
#endif
  default:
    fprintf( stderr, "sockaddrtop unknown address type\n" );
    strptr[0] = '\0';
    return;
  }
}

/* Address comparison. */
int addrcmp( char * a, char * b, int af ) {
  int rc = -1;

  switch ( af ) {
  case AF_INET:
    rc = memcmp( a, b, sizeof (struct in_addr) );
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    rc = memcmp( a, b, sizeof (struct in6_addr) );
    break;
#endif
  }

  return rc;
}

/* Address copy. */
void addrcpy( char * a, char * b, int af ) {

  switch ( af ) {
  case AF_INET:
    memcpy( a, b, sizeof (struct in_addr) );
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    memcpy( a, b, sizeof (struct in6_addr) );
    break;
#endif
  }
}

/* Decode MPLS */
void decodempls(int num, char *packet, struct mplslen *mpls, int offset) {

  int i;
  unsigned int ext_ver, ext_res, ext_chk, obj_hdr_len;
  u_char obj_hdr_class, obj_hdr_type;

  /* loosely derived from the traceroute-nanog.c
   * decoding by Jorge Boncompte */
  ext_ver = packet[offset]>>4;
  ext_res = (packet[offset]&15)+ packet[offset+1];
  ext_chk = ((unsigned int)packet[offset+2]<<8)+packet[offset+3];

  /* Check for ICMP extension header */
  if (ext_ver == 2 && ext_res == 0 && ext_chk != 0 && num >= (offset+6)) {
    obj_hdr_len = ((int)packet[offset+4]<<8)+packet[offset+5];
    obj_hdr_class = packet[offset+6];
    obj_hdr_type = packet[offset+7];

    /* make sure we have an MPLS extension */
    if (obj_hdr_len >= 8 && obj_hdr_class == 1 && obj_hdr_type == 1) {
      /* how many labels do we have?  will be at least 1 */
      mpls->labels = (obj_hdr_len-4)/4;

      /* save all label objects */
      for(i=0; (i<mpls->labels) && (i < MAXLABELS) && (num >= (offset+8)+(i*4)); i++) {

        /* piece together the 20 byte label value */
        mpls->label[i] = ((unsigned long) (packet[(offset+8)+(i*4)] << 12 & 0xff000) +
            (unsigned int) (packet[(offset+9)+(i*4)] << 4 & 0xff0) +
            (packet[(offset+10)+(i*4)] >> 4 & 0xf));
        mpls->exp[i] = (packet[(offset+10)+(i*4)] >> 1) & 0x7;
        mpls->s[i] = (packet[(offset+10)+(i*4)] & 0x1); /* should be 1 if only one label */
        mpls->ttl[i] = packet[(offset+11)+(i*4)];
      }
    }
  }
}

/* Add open sockets to select() */
void net_add_fds(fd_set *writefd, int *maxfd)
{
  int at, fd;
  for (at = 0; at < MaxSequence; at++) {
    fd = sequence[at].socket;
    if (fd > 0) {
      FD_SET(fd, writefd);
      if (fd >= *maxfd)
        *maxfd = fd + 1;
    }
  }
}

/* check if we got connection or error on any fds */
void net_process_fds(fd_set *writefd)
{
  int at, fd, r;
  struct timeval now;
  uint64_t unow, utime;

  /* Can't do MPLS decoding */
  struct mplslen mpls;
  mpls.labels = 0;

  gettimeofday(&now, NULL);
  unow = now.tv_sec * 1000000L + now.tv_usec;

  for (at = 0; at < MaxSequence; at++) {
    fd = sequence[at].socket;
    if (fd > 0 && FD_ISSET(fd, writefd)) {
      r = write(fd, "G", 1);
      /* if write was successful, or connection refused we have
       * (probably) reached the remote address. Anything else happens to the
       * connection, we write it off to avoid leaking sockets */
      if (r == 1 || errno == ECONNREFUSED)
        net_process_ping(at, mpls, remoteaddress, now);
      else if (errno != EAGAIN) {
        close(fd);
        sequence[at].socket = 0;
      }
    }
    if (fd > 0) {
      utime = sequence[at].time.tv_sec * 1000000L + sequence[at].time.tv_usec;
      if (unow - utime > tcp_timeout) {
        close(fd);
        sequence[at].socket = 0;
      }
    }
  }
}

/* for GTK frontend */
void net_harvest_fds(void)
{
  fd_set writefd;
  int maxfd = 0;
  struct timeval tv;

  FD_ZERO(&writefd);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  net_add_fds(&writefd, &maxfd);
  select(maxfd, NULL, &writefd, NULL, &tv);
  net_process_fds(&writefd);
}