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:
| unit icmp;
interface
uses sysutils, classes, winsock, (*$ifdef ver80 *) winprocs, wintypes, (*$else *) windows, (*$endif *) messages, forms, ip_misc;
const
ip_status_base = 11000; ip_success = 0; ip_buf_too_small = ip_status_base + 1; ip_dest_net_unreachable = ip_status_base + 2; ip_dest_host_unreachable = ip_status_base + 3; ip_dest_prot_unreachable = ip_status_base + 4; ip_dest_port_unreachable = ip_status_base + 5; ip_no_resources = ip_status_base + 6; ip_bad_options = ip_status_base + 7; ip_hw_error = ip_status_base + 8; ip_packet_too_big = ip_status_base + 9; ip_req_timed_out = ip_status_base + 10; ip_bad_req = ip_status_base + 11; ip_bad_route = ip_status_base + 12; ip_ttl_expired_transmit = ip_status_base + 13; ip_ttl_expired_reassem = ip_status_base + 14; ip_param_problem = ip_status_base + 15; ip_source_quench = ip_status_base + 16; ip_option_too_big = ip_status_base + 17; ip_bad_destination = ip_status_base + 18; ip_addr_deleted = ip_status_base + 19; ip_sepc_mtu_change = ip_status_base + 20; ip_mtu_change = ip_status_base + 21; ip_unload = ip_status_base + 22; ip_general_failure = ip_status_base + 50; max_ip_status = ip_general_failure; ip_pending = ip_status_base +255;
type
p_ip_options=^t_ip_options; t_ip_options=packed record Ttl: byte; Tos: byte; flags: byte; optionssize: byte; optionsdata: pointer; end;
p_icmp_echo_reply=^t_icmp_echo_reply; t_icmp_echo_reply=packed record address: u_long; status: u_long; rttime: u_long; datasize: word; reserved: word; data: pointer; ip_options:t_ip_options; end;
var ICMPCreateFile:function:THandle; stdcall; ICMPCloseHandle:function(ICMPHandle:THandle):Boolean; stdcall; ICMPSendEcho:function(ICMPHandle: THandle; DestAddress:longint; Requestdata:pointer; requestsize:word; RequestOptns: p_ip_options; ReplyBuffer:pointer; Replysize:dword; Timeout: DWord ):dword; stdcall;
type
t_ip_header=packed record ip_hl_v: byte; ip_tos: byte; ip_len: word; ip_id: word; ip_off: word; ip_ttl: byte; ip_p: byte; ip_sum: word; ip_src, ip_dst:longint; end;
t_icmp_echo_request=packed record icmp_type: byte; icmp_code: byte; icmp_cksum: word; icmp_id: word; icmp_seq: word; end;
t_icmp_reply=packed record icmp_type: byte; icmp_code: byte; icmp_cksum: word; icmp_unused: longint; icmp_ip: t_ip_header; icmp_dgram:array[0..63] of byte; end;
const
ICMP_ECHOREPLY = 0 ; ICMP_UNREACH = 3 ; ICMP_UNREACH_NET = 0 ; ICMP_UNREACH_HOST = 1 ; ICMP_UNREACH_PROTOCOL = 2 ; ICMP_UNREACH_PORT = 3 ; ICMP_UNREACH_NEEDFRAG = 4 ; ICMP_UNREACH_SRCFAIL = 5 ; ICMP_SOURCEQUENCH = 4 ; ICMP_REDIRECT = 5 ; ICMP_REDIRECT_NET = 0 ; ICMP_REDIRECT_HOST = 1 ; ICMP_REDIRECT_TOSNET = 2 ; ICMP_REDIRECT_TOSHOST = 3 ; ICMP_ECHO = 8 ; ICMP_TIMXCEED = 11 ; ICMP_TIMXCEED_INTRANS = 0 ; ICMP_TIMXCEED_REASS = 1 ; ICMP_PARAMPROB = 12 ; ICMP_TSTAMP = 13 ; ICMP_TSTAMPREPLY = 14 ; ICMP_IREQ = 15 ; ICMP_IREQREPLY = 16 ; ICMP_MASKREQ = 17 ; ICMP_MASKREPLY = 18 ;
IP_TTL = 4; IP_MULTICAST_IF = 9; IP_MULTICAST_TTL = 10; IP_MULTICAST_LOOP = 11; IP_ADD_MEMBERSHIP = 12; IP_DROP_MEMBERSHIP = 13;
function ICMP_checksum(var buf; length:integer):word;
type t_icmp_call=( icmp_dll, icmp_winsock_ttl, icmp_winsock_dll, icmp_winsock, no_icmp ); var icmp_state:t_icmp_call;
type TPingEvent = procedure (sender:TObject; status: integer; ip:longint; roundtime:longint) of object; TRouteEvent = procedure (sender:TObject; hop: byte; ip:longint; roundtime:longint) of object; EICMPError=class(Exception);
TICMP=class(TComponent) protected icmp_handle: THandle; f_socket: TSocket; protected f_blocksize: byte; f_replysize: dword; f_timeout: cardinal; f_ttl: byte; f_address: longint; f_hostname: string; f_terminated: boolean; f_handle: THandle; procedure WndProc(var Msg : TMessage); virtual; procedure OpenDll; procedure OpenSocket; public constructor Create(Aowner:TComponent); override; procedure Terminate; procedure action; virtual; destructor Destroy; override; end;
TPing=class(TICMP) protected f_no_of_packets_rec: integer; f_no_of_packets_snd: integer; f_roundtime_max: longint; f_roundtime_min: longint; f_roundtime_med: extended; f_on_ping: TPingEvent; function GetRoundtimeMin:longint; function GetRoundtimeMed:extended; procedure UpdateStatistics(roundtime:longint); public property MinimumRoundttime: longint read GetRoundtimeMin; property MaximumRoundttime: longint read f_roundtime_max; property MeanRoundttime: extended read GetRoundTimeMed; property ReceivedPackets: integer read f_no_of_packets_rec; property SentPackets: integer read f_no_of_packets_snd; constructor Create(Aowner:TComponent); override; procedure action; override; procedure ReceiveSock; procedure SendSock; procedure WndProc(var Msg : TMessage); override; procedure ResetStatistics; published property Timeout:cardinal read f_timeout write f_timeout; property Blocksize: byte read f_blocksize write f_blocksize default 64; property TimeToLive: byte read f_ttl write f_ttl default 255; property Hostname:string read f_hostname write f_hostname; property OnPing: TPingEvent read f_on_ping write f_on_ping; end;
TTraceRoute=class(TICMP) protected f_on_route: TRouteEvent; f_route: TStringlist; f_resolve: boolean; public constructor Create(Aowner:TComponent); override; procedure action; override; destructor Destroy; override; property Route: TStringList read f_route; published property ResolveHostname:boolean read f_resolve write f_resolve default false; property Timeout:cardinal read f_timeout write f_timeout; property Blocksize: byte read f_blocksize write f_blocksize default 64; property TimeToLive: byte read f_ttl write f_ttl default 255; property Hostname:string read f_hostname write f_hostname; property OnRoute: TRouteEvent read f_on_route write f_on_route; end;
const uwm_socketevent = wm_user+$102;
procedure Register;
implementation
const f_packet_no: word = 0;
var hDll: THandle;
function ICMP_checksum(var buf; length:integer):word; var p: pointer; sum: longint; i: integer; begin p:=@buf; sum:=0; for i:=1 to length div 2 do begin sum:=sum+word(p^); p:=pointer(longint(p)+2); end; if length mod 1<>0 then sum:=sum+byte(p^);
sum:=(sum shr 16) + (sum and $ffff); sum:=sum+(sum shr 16); result:=word(NOT sum); end;
function now_ms:longint; var systime : TSystemTime; begin GetLocalTime(systime); result:=systime.wmilliseconds+1000*systime.wsecond+60000*systime.wminute; end;
constructor TICMP.Create(Aowner:TComponent); begin inherited create(AOwner); f_timeout:=5000; f_blocksize:=64; f_ttl:=255; f_socket:=INVALID_SOCKET; icmp_handle:=invalid_handle_value; f_handle:=AllocateHwnd(self.WndProc); end;
destructor TICMP.Destroy; begin if icmp_handle<>invalid_handle_value then ICMPCloseHandle(icmp_handle); if f_socket<>INVALID_SOCKET then Winsock.CloseSocket(f_socket); inherited destroy; end;
procedure TICMP.action; begin f_terminated:=false; f_replysize:=16+sizeof(t_icmp_echo_reply)+f_blocksize; f_address:=lookup_hostname(f_hostname); end;
procedure TICMP.Terminate; begin f_terminated:=true; end;
procedure TICMP.OpenDll; begin if icmp_handle=invalid_handle_value then icmp_handle:=ICMPCreateFile; end;
procedure TICMP.OpenSocket; begin if f_socket=INVALID_SOCKET then begin f_socket:=Winsock.Socket(PF_INET,SOCK_RAW,IPPROTO_ICMP); winsock.WSAAsyncSelect(f_socket,f_handle,uwm_socketevent,fd_read); end; end;
procedure TICMP.WndProc(var Msg : TMessage); begin if msg.msg=wm_queryendsession then msg.result:=1 end;
constructor TPing.Create(Aowner:TComponent); begin inherited create(AOwner); f_blocksize:=64; end;
procedure TPing.action; var requestdata,replybuffer: pointer; p_reply: p_icmp_echo_reply; requestoptions: t_ip_options; begin inherited action; case icmp_state of
icmp_dll: begin OpenDll; requestdata:=NIL; replybuffer:=NIL; f_replysize:=16+sizeof(t_icmp_echo_reply)+f_blocksize; try GetMem(requestdata,f_blocksize); fillchar(requestdata^,f_blocksize,#$a7); GetMem(replybuffer,f_replysize);
requestoptions.ttl:=f_ttl; requestoptions.tos:=0; requestoptions.flags:=0; requestoptions.optionssize:=0; requestoptions.optionsdata:=NIL;
if f_terminated then EXIT; inc(f_no_of_packets_snd); if ICMPSendEcho(icmp_handle,f_address, requestdata,f_blocksize, @requestoptions, replybuffer,f_replysize, f_timeout) = 1 then begin p_reply:=p_icmp_echo_reply(replybuffer); if assigned(f_on_ping) then f_on_ping(self,p_reply^.status,p_reply^.address,p_reply^.rttime); if (p_reply^.status=ip_success) and (p_reply^.address=f_address) then begin inc(f_no_of_packets_rec); UpdateStatistics(p_reply^.rttime); end; end else if assigned(f_on_ping) then f_on_ping(self,ip_req_timed_out,f_address,0); finally if requestdata<>NIL then FreeMem(requestdata,f_blocksize); if replybuffer<>NIL then FreeMem(replybuffer,f_replysize); end; end;
icmp_winsock_ttl, icmp_winsock_dll, icmp_winsock: begin OpenSocket; if f_terminated then EXIT; inc(f_no_of_packets_snd); SendSock; end;
no_icmp: raise EICMPError.Create('No ICMP available'); end; end;
procedure TPing.ReceiveSock; type
treply_buf=record ip_header : t_ip_header; icmp_header: t_icmp_echo_request; time : longint; end;
var replybuffer: pointer; from: TSockAddrIn; whereto_len: integer; begin replybuffer:=NIL; f_replysize:=sizeof(t_icmp_echo_request)+ sizeof(t_ip_header)+ sizeof(longint)+ f_blocksize; try GetMem(replybuffer,f_replysize); from.sin_family:=AF_INET; from.sin_port:=0; from.sin_addr.S_addr:=f_address; recvfrom(f_socket,replybuffer^,f_replysize,0,from,whereto_len); if t_ip_header(replybuffer^).ip_hl_v=$45 then begin if (treply_buf(replybuffer^).icmp_header.icmp_id=word(self)) and assigned(f_on_ping) then f_on_ping(self,0,treply_buf(replybuffer^).ip_header.ip_src, now_ms-treply_buf(replybuffer^).time); end; finally if replybuffer<>NIL then FreeMem(replybuffer,f_replysize); end; end;
procedure TPing.SendSock; type
ticmp_sendblock=record icmp_header: t_icmp_echo_request; time : longint; data : char; end;
treply_buf=record ip_header : t_ip_header; icmp_header: t_icmp_echo_request; time : longint; end;
var requestdata: pointer; whereto: TSockAddr; whereto_len: integer; rq_size: integer; begin requestdata:=NIL; rq_size:=sizeof(t_icmp_echo_request)+f_blocksize+sizeof(longint); try GetMem(requestdata,rq_size); ticmp_sendblock(requestdata^).icmp_header.icmp_type := ICMP_ECHO; ticmp_sendblock(requestdata^).icmp_header.icmp_code := 0; ticmp_sendblock(requestdata^).icmp_header.icmp_cksum := 0; ticmp_sendblock(requestdata^).icmp_header.icmp_seq := f_packet_no; inc(f_packet_no); ticmp_sendblock(requestdata^).icmp_header.icmp_id := word(self); fillchar(ticmp_sendblock(requestdata^).data,f_blocksize,#$a7); ticmp_sendblock(requestdata^).time:=now_ms; ticmp_sendblock(requestdata^).icmp_header.icmp_cksum := ICMP_checksum(requestdata^,rq_size); whereto_len:=sizeof(whereto); whereto.sin_family:=AF_INET; whereto.sin_port:=0; whereto.sin_addr.S_addr:=f_address; if sendto(f_socket,requestdata^,rq_size, 0,whereto,whereto_len)=SOCKET_ERROR then WSAGetLastError; finally if requestdata<>NIL then FreeMem(requestdata,sizeof(t_icmp_echo_request)+f_blocksize); end; end;
function TPing.GetRoundtimeMin:longint; begin if f_roundtime_min=maxint then result:=-1 else result:=f_roundtime_min; end;
function TPing.GetRoundtimeMed:extended; begin if f_no_of_packets_rec>0 then result:=f_roundtime_med/f_no_of_packets_rec else result:=0; end;
procedure TPing.ResetStatistics; begin f_no_of_packets_rec:=0; f_no_of_packets_snd:=0; f_roundtime_max:=-1; f_roundtime_min:=maxint; f_roundtime_med:=0; end;
procedure TPing.UpdateStatistics(roundtime:longint); begin if f_roundtime_min>roundtime then f_roundtime_min:=roundtime; if f_roundtime_max<roundtime then f_roundtime_max:=roundtime; f_roundtime_med:=f_roundtime_med+roundtime; end;
procedure TPing.WndProc(var Msg : TMessage); begin if msg.msg<>uwm_socketevent then inherited wndproc(msg) else begin if msg.lparamhi=socket_error then else begin case msg.lparamlo of fd_read: ReceiveSock; end; end; end; end;
constructor TTraceRoute.Create(Aowner:TComponent); begin inherited create(AOwner); f_blocksize:=64; f_route:=TStringlist.Create; f_resolve:=false; end;
procedure TTraceRoute.action; var requestdata,replybuffer: pointer; p_reply: p_icmp_echo_reply; requestoptions: t_ip_options; i: integer; begin inherited action; f_route.Clear; case icmp_state of icmp_dll, icmp_winsock_dll: OpenDll; icmp_winsock_ttl: raise EICMPError.Create('Not yet implemented'); icmp_winsock, no_icmp: raise EICMPError.Create('No ICMP.DLL found'); end;
requestdata:=NIL; replybuffer:=NIL; try GetMem(requestdata,f_blocksize); fillchar(requestdata^,f_blocksize,#$a7); f_replysize:=16+sizeof(t_icmp_echo_reply)+f_blocksize; GetMem(replybuffer,f_replysize);
i:=0; while (i<f_ttl) do begin if f_terminated then BREAK; requestoptions.ttl:=i+1; requestoptions.tos:=0; requestoptions.flags:=0; requestoptions.optionssize:=0; requestoptions.optionsdata:=NIL;
if ICMPSendEcho(icmp_handle,f_address, requestdata,f_blocksize, @requestoptions, replybuffer,f_replysize, f_timeout) = 1 then begin p_reply:=p_icmp_echo_reply(replybuffer); if (p_reply^.status=ip_success) and (p_reply^.address=f_address) then begin f_terminated:=true; inc(i); end else if (p_reply^.status=ip_ttl_expired_transmit) then inc(i) else ; if ((p_reply^.status=ip_success) or (p_reply^.status=ip_ttl_expired_transmit)) then begin if f_resolve then f_route.add(resolve_hostname(p_reply^.address)) else f_route.add(ip2string(p_reply^.address)); if assigned(f_on_route) then f_on_route(self,i,p_reply^.address,p_reply^.rttime); end; end; end; finally if requestdata<>NIL then FreeMem(requestdata,f_blocksize); if replybuffer<>NIL then FreeMem(replybuffer,f_replysize); end; end;
destructor TTraceRoute.Destroy; begin f_route.Free; inherited destroy; end;
procedure shutdown; FAR; begin if hDll<>0 then FreeLibrary(hDll); end;
procedure Register; begin RegisterComponents('Internet', [TPing]); RegisterComponents('Internet', [TTraceRoute]); end;
var f_socket: TSocket; h: integer;
function check_dll:boolean; begin SetErrorMode(sem_NoOpenFileErrorBox); hDll:=LoadLibrary('ICMP.DLL'); if hdll<>0 then begin @ICMPCreateFile:=GetProcAddress(hdll,'IcmpCreateFile'); @ICMPCloseHandle:=GetProcAddress(hdll,'IcmpCloseHandle'); @ICMPSendEcho:=GetProcAddress(hdll,'IcmpSendEcho'); result:=true; end else result:=false; end;
(*$ifdef ver80 *) begin (*$else *) initialization begin (*$endif *) f_socket:=Winsock.Socket(PF_INET,SOCK_RAW,IPPROTO_ICMP); if f_socket=INVALID_SOCKET then begin if check_dll then icmp_state:=icmp_dll else icmp_state:=no_icmp; end else begin h:=64; if Winsock.SetSockOpt(f_socket,IPPROTO_IP, IP_TTL, pchar(@h), sizeof(h))<>0 then begin winsock.WSAGetLastError; if check_dll then icmp_state:=icmp_winsock_dll else icmp_state:=icmp_winsock; end else icmp_state:=icmp_winsock_ttl; closesocket(f_socket); end;
(*$ifdef ver80 *) AddExitProc(Shutdown); (*$else *) end; finalization Shutdown; (*$endif *)
end.
unit ip_misc;
interface (*$x+ *)
uses sysutils, (*$ifdef ver80 *) winprocs, wintypes, (*$else *) windows, (*$endif *) winsock, classes;
var tcpip_ready: boolean; const INVALID_IP_ADDRESS= $ffffffff;
type ta_8u=packed array [0..65530] of byte; t_encoding=(uuencode,base64,mime);
function internet_date(date: TDateTime):string;
function lookup_hostname(const hostname:string):longint;
function my_hostname:string;
function my_ip_address:longint;
function ip2string(ip_address:longint):string;
function resolve_hostname(ip: longint):string;
function address_from(const s:string; count: integer):string;
function encode_base64(data: TStream):TStringList; function decode_base64(source:TStringList):TMemoryStream;
function posn(const s,t:string; count:integer):integer;
function poscn(c:char; const s:string; n: integer):integer;
function filename_of(const s:string):string;
(*$ifdef ver80 *) function trim(const s:string):string; procedure setlength(var s:string; l: byte); (*$endif *)
function TimeZoneBias:longint;
function eight2seven_quoteprint(const s:string):string; function eight2seven_german(const s:string):string; function seven2eight_quoteprint(const s:string):string;
implementation
function posn(const s,t:string; count:integer):integer;
var i,h,last: integer; u: string; begin u:=t; if count>0 then begin result:=length(t); for i:=1 to count do begin h:=pos(s,u); if h>0 then u:=copy(u,pos(s,u)+1,length(u)) else begin u:=''; inc(result); end; end; result:=result-length(u); end else if count<0 then begin last:=0; for i:=length(t) downto 1 do begin u:=copy(t,i,length(t)); h:=pos(s,u); if (h<>0) and (h+i<>last) then begin last:=h+i-1; inc(count); if count=0 then BREAK; end; end; if count=0 then result:=last else result:=0; end else result:=0; end;
function poscn(c:char; const s:string; n: integer):integer;
var i: integer; begin if n=0 then n:=1; if n>0 then begin for i:=1 to length(s) do begin if s[i]<>c then begin dec(n); result:=i; if n=0 then begin EXIT; end; end; end; end else begin for i:=length(s) downto 1 do begin if s[i]<>c then begin inc(n); result:=i; if n=0 then begin EXIT; end; end; end; end; poscn:=0; end;
function filename_of(const s:string):string; var t:integer; begin t:=posn('\',s,-1); if t>0 then result:=copy(s,t+1,length(s)) else begin t:=posn(':',s,-1); if t>0 then result:=copy(s,t+1,length(s)) else result:=s; end; end;
(*$ifdef ver80 *)
function trim(const s:string):string; var h: integer; begin h:=poscn(' ',s,1); if h>0 then result:=copy(s,h,length(s)) else result:=s; h:=poscn(' ',result,-1); if h>0 then result:=copy(result,1,h); end;
procedure setlength(var s:string; l: byte); begin s[0]:=char(l); end;
(*$endif *)
function TimeZoneBias:longint;
(*$ifdef ver80 *)
function GetEnvVar(const s:string):string; var L: Word; P: PChar; begin L := length(s); P := GetDosEnvironment; while P^ <> #0 do begin if (StrLIComp(P, PChar(@s[1]), L) = 0) and (P[L] = '=') then begin GetEnvVar := StrPas(P + L + 1); EXIT; end; Inc(P, StrLen(P) + 1); end; GetEnvVar := ''; end;
function day_in_month(month,year,weekday: word; count: integer):TDateTime; var h: integer; begin if count>0 then begin h:=dayofweek(encodedate(year,month,1)); h:=((weekday-h+7) mod 7) +1 + (count-1)*7; result:=encodedate(year,month,h); end else begin h:=dayofweek(encodedate(year,month,1)); h:=((weekday-h+7) mod 7) +1 + 6*7; while count<0 do begin h:=h-7; try result:=encodedate(year,month,h); inc(count); if count=0 then EXIT; except end; end; end; end;
function DayLight_Start:TDateTime; var y,m,d: word; begin DecodeDate(now,y,m,d); result:=day_in_month(4,y,1,1); end;
function DayLight_End:TDateTime; var y,m,d: word; begin DecodeDate(now,y,m,d); result:=day_in_month(10,y,1,-1); end;
type
PSystemTime = ^TSystemTime; TSystemTime = record wYear: Word; wMonth: Word; wDayOfWeek: Word; wDay: Word; wHour: Word; wMinute: Word; wSecond: Word; wMilliseconds: Word; end;
TTimeZoneInformation = record Bias: Longint; StandardName: array[0..31] of word; StandardDate: TSystemTime; StandardBias: Longint; DaylightName: array[0..31] of word; DaylightDate: TSystemTime; DaylightBias: Longint; end;
var tz_info: TTimeZoneInformation; LL32:function (LibFileName: PChar; handle: longint; special: longint):Longint; FL32:function (hDll: Longint):boolean; GA32:function (hDll: Longint; functionname: PChar):longint; CP32:function (buffer:TTimeZoneInformation; prochandle,adressconvert,dwParams:Longint):longint; hdll32,dummy,farproc: longint; hdll:THandle; sign: integer; s: string; begin hDll:=GetModuleHandle('kernel'); @LL32:=GetProcAddress(hdll,'LoadLibraryEx32W'); @FL32:=GetProcAddress(hdll,'FreeLibrary32W'); @GA32:=GetProcAddress(hdll,'GetProcAddress32W'); @CP32:=GetProcAddress(hdll,'CallProc32W');
if (@LL32<>NIL) and (@FL32<>NIL) and (@GA32<>NIL) and (@CP32<>NIL) then begin hDll32:=LL32('kernel32.dll',dummy,1); farproc:=GA32(hDll32,'GetTimeZoneInformation'); case CP32(tz_info,farproc,1,1) of 1: result:=tz_info.StandardBias+tz_info.Bias; 2: result:=tz_info.DaylightBias+tz_info.Bias; else result:=0; end; FL32(hDll32); end
else begin s:=GetEnvVar('TZ'); while (length(s)>0) and (not(s[1] in ['+','-','0'..'9'])) do s:=copy(s,2,length(s)); case s[1] of
'+': begin sign:=1; s:=copy(s,2,length(s)); end;
'-': begin sign:=-1; s:=copy(s,2,length(s)); end;
else sign:=1; end; try result:=strtoint(copy(s,1,2))*60; s:=copy(s,3,length(s)); except try result:=strtoint(s[1])*60; s:=copy(s,2,length(s)); except result:=0; end; end;
if s[1]=':' then begin try result:=result+strtoint(copy(s,2,2)); s:=copy(s,4,length(s)); except try result:=result+strtoint(s[2]); s:=copy(s,3,length(s)); except end; end; end;
if s[1]=':' then begin try strtoint(copy(s,2,2)); s:=copy(s,4,length(s)); except try strtoint(s[2]); s:=copy(s,3,length(s)); except end; end; end;
result:=result*sign;
if length(s)>0 then begin if (now>daylight_start) and (now<DayLight_End+1) then result:=result-60; end;
end;
end;
(*$else *) var tz_info: TTimeZoneInformation; begin case GetTimeZoneInformation(tz_info) of 1: result:=tz_info.StandardBias+tz_info.Bias; 2: result:=tz_info.DaylightBias+tz_info.Bias; else result:=0; end; end; (*$endif *)
const bin2uue:string='`!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'; bin2b64:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; uue2bin:string=' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ '; b642bin:string='~~~~~~~~~~~^~~~_TUVWXYZ[\]~~~|~~~ !"#$%&''()*+,-./0123456789~~~~~~:;<=>?@ABCDEFGHIJKLMNOPQRS'; linesize = 45;
function decode_line(mode:t_encoding; const inp:string):string; var count,pos1,pos2: integer; offset: shortint; s: string; out: string; begin s:=inp; setlength(out,length(s)*3 div 4 +3); fillchar(out[1],length(s)*3 div 4 +3,#0); if (mode=uuencode) and not (s[1] in [' '..'M','`']) then count:=0 else begin count:=0; pos1:=0; case mode of
uuencode: begin count:=(ord(s[1]) - $20) and $3f; for pos1:=2 to length(s) do s[pos1]:=char(ord(uue2bin[ord(s[pos1])-$20+1])-$20); pos1:=2; end;
base64,mime: begin
count:=poscn('=',s,-1)*3 div 4; for pos1:=1 to length(s) do s[pos1]:=char(ord(b642bin[ord(s[pos1])-$20+1])-$20); pos1:=1; end;
end; pos2:=1; offset:=2; while pos2<=count do begin if (pos1>length(s)) or ((mode<>uuencode) and (s[pos1]='\')) then begin if offset<>2 then inc(pos2); count:=pos2-1; end else if ((mode<>uuencode) and (s[pos1]='^')) then inc(pos1) else if offset>0 then begin out[pos2]:=char(ord(out[pos2]) or (ord(s[pos1]) shl offset)); inc(pos1); offset:=offset-6; end else if offset<0 then begin offset:=abs(offset); out[pos2]:=char(ord(out[pos2]) or (ord(s[pos1]) shr offset)); inc(pos2); offset:=8-offset; end else begin out[pos2]:=char(ord(out[pos2]) or ord(s[pos1])); inc(pos1); inc(pos2); offset:=2; end; end; end; decode_line:=copy(out,1,count); end;
function encode_line(mode:t_encoding; const buf; size:integer):string; var buff: ta_8u absolute buf; offset: shortint; pos1,pos2: byte; i: byte; out: string; begin setlength(out,size*4 div 3 + 4); fillchar(out[1],size*4 div 3 +2,#0); if mode=uuencode then begin out[1]:=char(((size-1) and $3f)+$21); size:=((size+2) div 3)*3; end; offset:=2; pos1:=0; pos2:=0; case mode of uuencode: pos2:=2; base64, mime: pos2:=1; end; out[pos2]:=#0;
while pos1<size do begin if offset > 0 then begin out[pos2]:=char(ord(out[pos2]) or ((buff[pos1] and ($3f shl offset)) shr offset)); offset:=offset-6; inc(pos2); out[pos2]:=#0; end else if offset < 0 then begin offset:=abs(offset); out[pos2]:=char(ord(out[pos2]) or ((buff[pos1] and ($3f shr offset)) shl offset)); offset:=8-offset; inc(pos1); end else begin out[pos2]:=char(ord(out[pos2]) or ((buff[pos1] and $3f))); inc(pos2); inc(pos1); out[pos2]:=#0; offset:=2; end; end;
case mode of
uuencode: begin if offset=2 then dec(pos2); for i:=2 to pos2 do out[i]:=bin2uue[ord(out[i])+1]; end;
base64, mime: begin if offset=2 then dec(pos2); for i:=1 to pos2 do out[i]:=bin2b64[ord(out[i])+1]; while (pos2 and 3)<>0 do begin inc(pos2); out[pos2]:='='; end; end;
end; encode_line:=copy(out,1,pos2); end;
function encode_base64(data: TStream):TStringList; var buf: pointer; size: integer; begin buf:=NIL;
try result:=TStringList.Create; getmem(buf,linesize); data.seek(0,0); size:=linesize; while size>0 do begin size:=data.read(buf^,linesize); if size>0 then result.add(encode_line(base64,buf^,size)); end; finally if buf<>NIL then freemem(buf,linesize); end; end;
function decode_base64(source:TStringList):TMemoryStream; var i: integer; s: string; begin result:=TMemoryStream.Create; for i:=0 to source.count-1 do begin s:=decode_line(base64,source[i]); result.write(s[1],length(s)); end; end;
function eight2seven_quoteprint(const s:string):string; var i: integer; begin result:=''; for i:=1 to length(s) do case s[i] of '=',#$80..#$FF: result:=result+'='+uppercase(inttohex(ord(s[i]),2)); else result:=result+s[i]; end; end;
function eight2seven_german(const s:string):string; var i: integer; begin result:=''; for i:=1 to length(s) do case s[i] of #192..#195,#197: result:=result+'A'; #196,#198: result:=result+'Ae'; #199: result:=result+'C'; #200..#203: result:=result+'E'; #204..#207: result:=result+'I'; #209: result:=result+'N'; #210..#213,#216: result:=result+'O'; #214: result:=result+'Oe'; #217..#219: result:=result+'U'; #220: result:=result+'Ue'; #221: result:=result+'Y'; #223: result:=result+'ss'; #224..#227,#229: result:=result+'a'; #228,#230: result:=result+'ae'; #231: result:=result+'c'; #232..#235: result:=result+'e'; #236..#239: result:=result+'i'; #241: result:=result+'n'; #242..#245,#248: result:=result+'o'; #246: result:=result+'oe'; #249..#251: result:=result+'u'; #252: result:=result+'ue'; #255: result:=result+'y'; #0..#60,#62..#127: result:=result+s[i]; else result:=result+'='+uppercase(inttohex(ord(s[i]),2)); end; end;
function seven2eight_quoteprint(const s:string):string; var i: integer; begin result:=''; i:=0; while i<length(s) do begin inc(i); case s[i] of '=': try result:=result+char(strtoint('$'+s[i+1]+s[i+2])); i:=i+2; except result:=result+'='; end; else result:=result+s[i]; end; end; end;
function my_hostname:string; const bufsize=255; var buf: pointer; RemoteHost : PHostEnt; begin buf:=NIL; my_hostname:=''; try getmem(buf,bufsize); winsock.gethostname(buf,bufsize); if char(buf^)<>#0 then begin RemoteHost:=Winsock.GetHostByName(buf); if RemoteHost<>NIL then (*$ifdef ver80 *) my_hostname:=strpas(pchar(RemoteHost^.h_name)) (*$else *) my_hostname:=pchar(RemoteHost^.h_name) (*$endif *) else my_hostname:='127.0.0.1'; end else my_hostname:='127.0.0.1'; finally if buf<>NIL then freemem(buf,bufsize); end; end;
function my_ip_address:longint; const bufsize=255; var buf: pointer; RemoteHost : PHostEnt; begin buf:=NIL; try getmem(buf,bufsize); winsock.gethostname(buf,bufsize); RemoteHost:=Winsock.GetHostByName(buf); if RemoteHost=NIL then my_ip_address:=winsock.htonl($7F000001) else my_ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^); finally if buf<>NIL then freemem(buf,bufsize); end; end;
function internet_date(date: TDateTime):string;
function myinttostr(value:integer; len:byte):string; begin myinttostr:=inttostr(value); while length(result)<len do result:='0'+result; end;
function timezone:string; var bias: longint; begin bias:=TimeZoneBias; if bias=0 then timezone:='GMT' else if bias<0 then timezone:='+' + myinttostr(abs(bias) div 60,2) + myinttostr(abs(bias) mod 60,2) else if bias>0 then timezone:='-' + myinttostr(bias div 60,2) + myinttostr(bias mod 60,2); end;
var d,m,y,w,h,mm,s,ms: word; const weekdays:array[1..7] of string[3]=('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); months:array[1..12] of string[3]=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); begin decodedate(date,y,m,d); decodetime(date,h,mm,s,ms); w:=dayofweek(date); internet_date:=weekdays[w]+', '+inttostr(d)+' '+months[m]+' '+inttostr(y)+' '+ myinttostr(h,2)+':'+myinttostr(mm,2)+':'+myinttostr(s,2)+' '+timezone; end;
function ip2string(ip_address:longint):string; begin ip_address:=winsock.ntohl(ip_address); result:= inttostr(ip_address shr 24)+'.'+ inttostr((ip_address shr 16) and $ff)+'.'+ inttostr((ip_address shr 8) and $ff)+'.'+ inttostr(ip_address and $ff); end;
function address_from(const s:string; count: integer):string; var p, ca, sp, co, se: integer; begin ca:=posn('@',s,count); if (ca=0) or (ca>length(s)) then result:='' else begin sp:=posn(' ',copy(s,1,ca),-1); co:=posn(',',copy(s,1,ca),-1); se:=posn(';',copy(s,1,ca),-1); p:=0; if (sp<ca) and (sp>p) then p:=sp; if (co<ca) and (co>p) then p:=co; if (se<ca) and (se>p) then p:=se; result:=copy(s,p+1,length(s)); sp:=posn(' ',result,1)-1; co:=posn(',',result,1)-1; se:=posn(';',result,1)-1; ca:=length(result); p:=ca+1; if (sp<p) and (sp>0) and (sp<ca) then p:=sp; if (co<p) and (co>0) and (co<ca) then p:=co; if (se<p) and (se>0) and (se<ca) then p:=se; result:=copy(result,1,p-1); while result[1] in ['"','(','<'] do result:=copy(result,2,length(result)); while result[length(result)] in ['"',')','>'] do result:=copy(result,1,length(result)-1); end; end;
function lookup_hostname(const hostname:string):longint; var RemoteHost : PHostEnt; ip_address: longint; (*$ifdef ver80 *) s: string; (*$else *) (*$ifopt h- *) s: string; (*$endif *) (*$endif *) begin ip_address:=INVALID_IP_ADDRESS; try if hostname='' then begin lookup_hostname:=ip_address; EXIT; end else begin
(*$ifdef ver80 *) s:=hostname+#0; ip_address:=Winsock.Inet_Addr(PChar(@s[1])); (*$else *) (*$ifopt h- *) s:=hostname+#0; ip_address:=Winsock.Inet_Addr(PChar(@s[1])); (*$else *) ip_address:=Winsock.Inet_Addr(PChar(hostname)); (*$endif *) (*$endif *)
if ip_address=SOCKET_ERROR then begin
(*$ifdef ver80 *) RemoteHost:=Winsock.GetHostByName(PChar(@s[1])); (*$else *) (*$ifopt h- *) RemoteHost:=Winsock.GetHostByName(PChar(@s[1])); (*$else *) RemoteHost:=Winsock.GetHostByName(PChar(hostname)); (*$endif *) (*$endif *)
if (RemoteHost=NIL) or (RemoteHost^.h_length<=0) then begin lookup_hostname:=ip_address; EXIT; end else ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^); end; end; except ip_address:=INVALID_IP_ADDRESS; end; lookup_hostname:=ip_address; end;
function resolve_hostname(ip: longint):string; var RemoteHost : PHostEnt; ip_address: longint; begin ip_address:=ip; RemoteHost:=Winsock.GetHostByAddr(@ip_address,4,pf_inet); if RemoteHost<>NIL then (*$ifdef ver80 *) resolve_hostname:=strpas(pchar(RemoteHost^.h_name)) (*$else *) resolve_hostname:=pchar(RemoteHost^.h_name) (*$endif *) else resolve_hostname:=ip2string(ip_address); end;
procedure init; var point: TWSAData; begin tcpip_ready:=false; if @Winsock.WSAStartup<>NIL then case Winsock.WSAStartup($0101,point) of WSAEINVAL, WSASYSNOTREADY, WSAVERNOTSUPPORTED: ; else tcpip_ready:=true; end; end;
procedure shutdown; FAR; begin if tcpip_ready then begin Winsock.WSACancelBlockingCall; Winsock.WSACleanup; end; end;
(*$ifdef ver80 *) begin (*$else *) initialization begin (*$endif *) init;
(*$ifdef ver80 *) AddExitProc(Shutdown); (*$else *) end; finalization Shutdown; (*$endif *)
end.
unit u_ping;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, icmp, ExtCtrls;
type
Tfrm_ping = class(TForm) Ping: TPing; lbl_host: TLabel; edt_hostname: TEdit; mem_out: TMemo; btn_start: TButton; btn_break: TButton; Timer: TTimer; procedure btn_startClick(Sender: TObject); procedure PingPing(sender:TObject; status:Integer; ip,roundtime:Longint); procedure btn_breakClick(Sender: TObject); procedure TimerTimer(Sender: TObject); end;
var frm_ping: Tfrm_ping;
implementation
{$R *.DFM}
uses ip_misc;
var nr: integer;
procedure Tfrm_ping.btn_startClick(Sender: TObject); begin ping.hostname:=edt_hostname.text; mem_out.lines.clear; nr:=0; timer.enabled:=true; end;
procedure Tfrm_ping.PingPing(sender:TObject; status:Integer; ip,roundtime:Longint); begin inc(nr); case status of ip_success: mem_out.lines.add(inttostr(nr)+'. '+inttostr(roundtime)+'ms'); else mem_out.lines.add(inttostr(nr)+'. Failed') end; end;
procedure Tfrm_ping.btn_breakClick(Sender: TObject); begin ping.terminate; timer.enabled:=false; end;
procedure Tfrm_ping.TimerTimer(Sender: TObject); begin ping.action; end;
end.
Inhalt der Datei AH_COMP.DPK
package ah_comp;
{$R *.RES} {$R 'icmp.dcr'} {$ALIGN ON} {$ASSERTIONS ON} {$BOOLEVAL OFF} {$DEBUGINFO ON} {$EXTENDEDSYNTAX ON} {$IMPORTEDDATA ON} {$IOCHECKS ON} {$LOCALSYMBOLS ON} {$LONGSTRINGS ON} {$OPENSTRINGS ON} {$OPTIMIZATION ON} {$OVERFLOWCHECKS OFF} {$RANGECHECKS OFF} {$REFERENCEINFO OFF} {$SAFEDIVIDE OFF} {$STACKFRAMES OFF} {$TYPEDADDRESS OFF} {$VARSTRINGCHECKS ON} {$WRITEABLECONST ON} {$MINENUMSIZE 1} {$IMAGEBASE $00400000} {$DESCRIPTION 'Andreas Hörstemeier''s components'} {$DESIGNONLY} {$IMPLICITBUILD ON}
requires Vcl30;
contains Icmp;
end. |