@litmusio/miravo-cli-linux-x64 0.0.0-stage → 0.15.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/LICENSE +199 -0
  2. package/NOTICE +9 -0
  3. package/README.md +6 -2
  4. package/THIRD_PARTY_LICENSES.txt +15289 -0
  5. package/THIRD_PARTY_NOTICES.md +995 -0
  6. package/console-dist/assets/AnimatePresence-DWGUB15i.js +9 -0
  7. package/console-dist/assets/activity-DruTYzT1.js +1 -0
  8. package/console-dist/assets/api-V5Tf5xfh.js +57 -0
  9. package/console-dist/assets/arrow-up-right-DOENs04V.js +1 -0
  10. package/console-dist/assets/asset-detail-D_icTa2K.js +1 -0
  11. package/console-dist/assets/asset-detail-actions-DIYego9D.js +1 -0
  12. package/console-dist/assets/backup-power-Q84DVs_z.js +1 -0
  13. package/console-dist/assets/badge-CdRB4Nd_.js +1 -0
  14. package/console-dist/assets/boiler-s_q0j7rz.js +1 -0
  15. package/console-dist/assets/by-path-Cm2TEP7z.js +1 -0
  16. package/console-dist/assets/check-BTUv4z-H.js +1 -0
  17. package/console-dist/assets/chevron-down-CqA_2_CH.js +1 -0
  18. package/console-dist/assets/chevron-right-CVrgKBLR.js +1 -0
  19. package/console-dist/assets/chiller-r7DzAhJq.js +1 -0
  20. package/console-dist/assets/chunk-6KYCQHYJ-ga3Yekab.js +3 -0
  21. package/console-dist/assets/circle-x-BRkh0A1P.js +1 -0
  22. package/console-dist/assets/commercial-building-Cezt2R6W.js +1 -0
  23. package/console-dist/assets/compressor-ChZLMC94.js +1 -0
  24. package/console-dist/assets/console-panel-BnSjjW6l.js +1 -0
  25. package/console-dist/assets/dialog-BYkdndf9.js +1 -0
  26. package/console-dist/assets/district-heating-By_6QdGQ.js +1 -0
  27. package/console-dist/assets/dom-BxZbg3NX.js +1 -0
  28. package/console-dist/assets/download-CvjDP5mu.js +1 -0
  29. package/console-dist/assets/fira-code-cyrillic-ext-wght-normal-DhYMMuQd.woff2 +0 -0
  30. package/console-dist/assets/fira-code-cyrillic-wght-normal-Y3u8pIsh.woff2 +0 -0
  31. package/console-dist/assets/fira-code-greek-ext-wght-normal-wWus70Ix.woff2 +0 -0
  32. package/console-dist/assets/fira-code-greek-wght-normal-B2SviObF.woff2 +0 -0
  33. package/console-dist/assets/fira-code-latin-ext-wght-normal-Dvfvaomy.woff2 +0 -0
  34. package/console-dist/assets/fira-code-latin-wght-normal-CHoedHDv.woff2 +0 -0
  35. package/console-dist/assets/fira-code-symbols2-wght-normal-CE6EOz_n.woff2 +0 -0
  36. package/console-dist/assets/floor-page-DIUJBBhh.js +1 -0
  37. package/console-dist/assets/floor-page-E-0txA4z.css +1 -0
  38. package/console-dist/assets/format-YLUJ7AXt.js +1 -0
  39. package/console-dist/assets/genset-C6_EUoFo.js +1 -0
  40. package/console-dist/assets/index-BrlZMcSu.js +15 -0
  41. package/console-dist/assets/index-eZjXAdLh.css +2 -0
  42. package/console-dist/assets/loader-circle-w3T2fxvn.js +1 -0
  43. package/console-dist/assets/login-CpbaxAVs.js +136 -0
  44. package/console-dist/assets/not-found-CZjkulKD.js +1 -0
  45. package/console-dist/assets/pharma-cleanroom-CT2M_Mbh.js +1 -0
  46. package/console-dist/assets/preload-helper-7y9sWmk5.js +1 -0
  47. package/console-dist/assets/react-DRt7cCXI.js +1 -0
  48. package/console-dist/assets/rolldown-runtime-BWaJ3C7D.js +1 -0
  49. package/console-dist/assets/rtu-ByogcRPI.js +1 -0
  50. package/console-dist/assets/runtime-link-D88Db2wY.js +1 -0
  51. package/console-dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
  52. package/console-dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
  53. package/console-dist/assets/server-B0q1NVkV.js +1 -0
  54. package/console-dist/assets/shallow-mIK1Qu8X.js +1 -0
  55. package/console-dist/assets/smart-factory-gfMWfMWM.js +1 -0
  56. package/console-dist/assets/source-host-miravo-BW-SKMTe.js +1 -0
  57. package/console-dist/assets/stages-EYRT12Yp.js +1 -0
  58. package/console-dist/assets/status-DQYLUbXs.js +1 -0
  59. package/console-dist/assets/tank-Btg5piqB.js +1 -0
  60. package/console-dist/assets/tooltip-Dfmuq2yz.js +1 -0
  61. package/console-dist/assets/trash-2-BAh5-hxl.js +1 -0
  62. package/console-dist/assets/triangle-alert-3Ab-dBrN.js +1 -0
  63. package/console-dist/assets/use-engine-command-mutation-BmSUqrCp.js +1 -0
  64. package/console-dist/assets/use-transition-navigate-DIfW9U34.js +2 -0
  65. package/console-dist/assets/useQuery-yLeYL5Es.js +1 -0
  66. package/console-dist/assets/utils-D6wrZy-b.js +1 -0
  67. package/console-dist/assets/validation-DEb_QCoK.js +1 -0
  68. package/console-dist/assets/vendor-charts-Bquh2dWE.js +56 -0
  69. package/console-dist/assets/vendor-table-5YnGQUhO.js +4 -0
  70. package/console-dist/assets/vendor-ui-BGeFlRLR.js +1 -0
  71. package/console-dist/assets/water-treatment-DbSVGWoj.js +1 -0
  72. package/console-dist/icon.png +0 -0
  73. package/console-dist/index.html +53 -0
  74. package/console-dist/miravo-logo.webp +0 -0
  75. package/licenses/EPL-2.0.txt +80 -0
  76. package/licenses/GCC-exception-3.1.txt +33 -0
  77. package/licenses/GPL-2.0-only.txt +117 -0
  78. package/licenses/GPL-3.0-only.txt +232 -0
  79. package/licenses/LGPL-2.1-only.txt +176 -0
  80. package/licenses/MPL-2.0.txt +373 -0
  81. package/licenses/OFL-1.1.txt +93 -0
  82. package/licenses/overrides/@headlessui+vue@1.7.23.txt +24 -0
  83. package/licenses/overrides/@next+env@16.4.6.txt +24 -0
  84. package/licenses/overrides/@radix-ui+react-compose-refs@1.1.2.txt +24 -0
  85. package/licenses/overrides/@radix-ui+react-context@1.1.2.txt +24 -0
  86. package/licenses/overrides/@radix-ui+react-direction@1.1.1.txt +24 -0
  87. package/licenses/overrides/@radix-ui+react-id@1.1.1.txt +24 -0
  88. package/licenses/overrides/@radix-ui+react-use-callback-ref@1.1.1.txt +24 -0
  89. package/licenses/overrides/@radix-ui+react-use-escape-keydown@1.1.1.txt +24 -0
  90. package/licenses/overrides/@radix-ui+react-use-layout-effect@1.1.1.txt +24 -0
  91. package/licenses/overrides/@radix-ui+react-use-rect@1.1.1.txt +24 -0
  92. package/licenses/overrides/@radix-ui+react-use-size@1.1.1.txt +24 -0
  93. package/licenses/overrides/@radix-ui+rect@1.1.1.txt +24 -0
  94. package/licenses/overrides/@replit+codemirror-css-color-picker@6.3.0.txt +6 -0
  95. package/licenses/overrides/@scalar+openapi-types@0.8.0.txt +24 -0
  96. package/licenses/overrides/@scalar+openapi-types@0.9.10.txt +24 -0
  97. package/licenses/overrides/@scalar+openapi-types@0.9.7.txt +24 -0
  98. package/licenses/overrides/@scalar+openapi-types@0.9.9.txt +24 -0
  99. package/licenses/overrides/@scalar+postman-to-openapi@0.7.1.txt +24 -0
  100. package/licenses/overrides/@scalar+schemas@0.19.0.txt +24 -0
  101. package/licenses/overrides/@scalar+workspace-store@0.46.3.txt +24 -0
  102. package/licenses/overrides/@scalar+workspace-store@0.75.0.txt +24 -0
  103. package/licenses/overrides/assert-plus@1.0.0.txt +24 -0
  104. package/licenses/overrides/client-only@0.0.1.txt +24 -0
  105. package/licenses/overrides/embla-carousel-react@8.6.0.txt +24 -0
  106. package/licenses/overrides/embla-carousel-reactive-utils@8.6.0.txt +24 -0
  107. package/licenses/overrides/embla-carousel@8.6.0.txt +24 -0
  108. package/licenses/overrides/fastdom@1.0.12.txt +12 -0
  109. package/licenses/overrides/precond@0.2.3.txt +22 -0
  110. package/licenses/overrides/react-remove-scroll-bar@2.3.8.txt +24 -0
  111. package/licenses/overrides/strictdom@1.0.1.txt +12 -0
  112. package/licenses/overrides/victory-vendor@36.9.2.txt +33 -0
  113. package/licenses/runtime/Bun-1.4.3-LICENSE.md +85 -0
  114. package/licenses/runtime/ICU-78.1-LICENSE.txt +568 -0
  115. package/licenses/runtime/bun-components/boringssl.txt +238 -0
  116. package/licenses/runtime/bun-components/brotli.txt +19 -0
  117. package/licenses/runtime/bun-components/c-ares.txt +24 -0
  118. package/licenses/runtime/bun-components/highway.txt +371 -0
  119. package/licenses/runtime/bun-components/libarchive.txt +65 -0
  120. package/licenses/runtime/bun-components/libbase64.txt +28 -0
  121. package/licenses/runtime/bun-components/libdeflate.txt +22 -0
  122. package/licenses/runtime/bun-components/libjpeg-turbo.txt +147 -0
  123. package/licenses/runtime/bun-components/libspng.txt +25 -0
  124. package/licenses/runtime/bun-components/libuv.txt +19 -0
  125. package/licenses/runtime/bun-components/libwebp.txt +30 -0
  126. package/licenses/runtime/bun-components/llvm.txt +311 -0
  127. package/licenses/runtime/bun-components/lol-html.txt +27 -0
  128. package/licenses/runtime/bun-components/ls-hpack.txt +21 -0
  129. package/licenses/runtime/bun-components/ls-qpack.txt +21 -0
  130. package/licenses/runtime/bun-components/lsquic-chrome.txt +32 -0
  131. package/licenses/runtime/bun-components/lsquic.txt +21 -0
  132. package/licenses/runtime/bun-components/mimalloc.txt +21 -0
  133. package/licenses/runtime/bun-components/picohttpparser.txt +24 -0
  134. package/licenses/runtime/bun-components/simdutf-apache.txt +201 -0
  135. package/licenses/runtime/bun-components/simdutf-mit.txt +18 -0
  136. package/licenses/runtime/bun-components/tigerbeetle.txt +177 -0
  137. package/licenses/runtime/bun-components/tinycc.txt +504 -0
  138. package/licenses/runtime/bun-components/typescript-go.txt +55 -0
  139. package/licenses/runtime/bun-components/usockets.txt +201 -0
  140. package/licenses/runtime/bun-components/uucode.txt +30 -0
  141. package/licenses/runtime/bun-components/uwebsockets.txt +201 -0
  142. package/licenses/runtime/bun-components/xxhash.txt +31 -0
  143. package/licenses/runtime/bun-components/zlib-ng.txt +19 -0
  144. package/licenses/runtime/bun-components/zstd-copying.txt +339 -0
  145. package/licenses/runtime/bun-components/zstd.txt +30 -0
  146. package/licenses/runtime/musl-1.2.6-COPYRIGHT.txt +193 -0
  147. package/miravo +4 -0
  148. package/native/S7-NOTICES.txt +11 -0
  149. package/native/miravo-s7.node +0 -0
  150. package/native/s7-source/CMakeLists.txt +23 -0
  151. package/native/s7-source/NODE-SNAP7-LICENSE +24 -0
  152. package/native/s7-source/addon.cpp +124 -0
  153. package/native/s7-source/bridge.cpp +150 -0
  154. package/native/s7-source/node-api-headers/LICENSE +21 -0
  155. package/native/s7-source/node-api-headers/include/js_native_api.h +591 -0
  156. package/native/s7-source/node-api-headers/include/js_native_api_types.h +213 -0
  157. package/native/s7-source/node-api-headers/include/node_api.h +265 -0
  158. package/native/s7-source/node-api-headers/include/node_api_types.h +58 -0
  159. package/native/s7-source/node-api-windows.h +78 -0
  160. package/native/s7-source/snap7/HISTORY.txt +277 -0
  161. package/native/s7-source/snap7/gpl.txt +674 -0
  162. package/native/s7-source/snap7/lgpl-3.0.txt +165 -0
  163. package/native/s7-source/snap7/src/core/miravo_request_validation.h +72 -0
  164. package/native/s7-source/snap7/src/core/s7_client.cpp +503 -0
  165. package/native/s7-source/snap7/src/core/s7_client.h +104 -0
  166. package/native/s7-source/snap7/src/core/s7_firmware.h +1256 -0
  167. package/native/s7-source/snap7/src/core/s7_isotcp.cpp +541 -0
  168. package/native/s7-source/snap7/src/core/s7_isotcp.h +271 -0
  169. package/native/s7-source/snap7/src/core/s7_micro_client.cpp +3328 -0
  170. package/native/s7-source/snap7/src/core/s7_micro_client.h +364 -0
  171. package/native/s7-source/snap7/src/core/s7_partner.cpp +1178 -0
  172. package/native/s7-source/snap7/src/core/s7_partner.h +284 -0
  173. package/native/s7-source/snap7/src/core/s7_peer.cpp +121 -0
  174. package/native/s7-source/snap7/src/core/s7_peer.h +58 -0
  175. package/native/s7-source/snap7/src/core/s7_server.cpp +2161 -0
  176. package/native/s7-source/snap7/src/core/s7_server.h +261 -0
  177. package/native/s7-source/snap7/src/core/s7_text.cpp +788 -0
  178. package/native/s7-source/snap7/src/core/s7_text.h +49 -0
  179. package/native/s7-source/snap7/src/core/s7_types.h +1066 -0
  180. package/native/s7-source/snap7/src/lib/snap7.def +129 -0
  181. package/native/s7-source/snap7/src/lib/snap7_libmain.cpp +1196 -0
  182. package/native/s7-source/snap7/src/lib/snap7_libmain.h +201 -0
  183. package/native/s7-source/snap7/src/sys/snap_msgsock.cpp +958 -0
  184. package/native/s7-source/snap7/src/sys/snap_msgsock.h +339 -0
  185. package/native/s7-source/snap7/src/sys/snap_platform.h +152 -0
  186. package/native/s7-source/snap7/src/sys/snap_sysutils.cpp +73 -0
  187. package/native/s7-source/snap7/src/sys/snap_sysutils.h +39 -0
  188. package/native/s7-source/snap7/src/sys/snap_tcpsrvr.cpp +487 -0
  189. package/native/s7-source/snap7/src/sys/snap_tcpsrvr.h +247 -0
  190. package/native/s7-source/snap7/src/sys/snap_threads.cpp +162 -0
  191. package/native/s7-source/snap7/src/sys/snap_threads.h +45 -0
  192. package/native/s7-source/snap7/src/sys/sol_threads.h +208 -0
  193. package/native/s7-source/snap7/src/sys/unix_threads.h +228 -0
  194. package/native/s7-source/snap7/src/sys/win_threads.h +159 -0
  195. package/nodesets/.ignore +1 -0
  196. package/nodesets/1.02/FTNIR.NodeSet2.xml +819 -0
  197. package/nodesets/1.02/Opc.Ua.Adi.NodeSet2.xml +8301 -0
  198. package/nodesets/1.02/Opc.Ua.Di.NodeSet2.xml +1785 -0
  199. package/nodesets/Opc.ISA95.NodeSet2.xml +3589 -0
  200. package/nodesets/Opc.Ua.AMB.NodeSet2.xml +1395 -0
  201. package/nodesets/Opc.Ua.Adi.NodeSet2.xml +8301 -0
  202. package/nodesets/Opc.Ua.Adi.Types.xsd +104 -0
  203. package/nodesets/Opc.Ua.AutoID.NodeSet2.xml +6062 -0
  204. package/nodesets/Opc.Ua.CNC.NodeSet.xml +15875 -0
  205. package/nodesets/Opc.Ua.CommercialKitchenEquipment.NodeSet2.xml +12096 -0
  206. package/nodesets/Opc.Ua.Di.NodeSet2.xml +6042 -0
  207. package/nodesets/Opc.Ua.Di.Types.xsd +161 -0
  208. package/nodesets/Opc.Ua.Eumabois.Nodeset2.xml +2483 -0
  209. package/nodesets/Opc.Ua.Gds.NodeSet2.xml +8282 -0
  210. package/nodesets/Opc.Ua.Glass.NodeSet2.xml +5100 -0
  211. package/nodesets/Opc.Ua.IA.NodeSet2.xml +1782 -0
  212. package/nodesets/Opc.Ua.IOLink.NodeSet2.xml +4374 -0
  213. package/nodesets/Opc.Ua.IOLinkIODD.NodeSet2.xml +139 -0
  214. package/nodesets/Opc.Ua.IRDI.NodeSet2.xml +2243 -0
  215. package/nodesets/Opc.Ua.Ijt.Base.NodeSet2.xml +19459 -0
  216. package/nodesets/Opc.Ua.Ijt.Tightening.NodeSet2.xml +883 -0
  217. package/nodesets/Opc.Ua.LADS.NodeSet2.xml +7086 -0
  218. package/nodesets/Opc.Ua.MachineTool.NodeSet2.xml +7884 -0
  219. package/nodesets/Opc.Ua.MachineVision.NodeSet2.xml +17324 -0
  220. package/nodesets/Opc.Ua.Machinery.Jobs.Nodeset2.xml +1249 -0
  221. package/nodesets/Opc.Ua.Machinery.NodeSet2.xml +2099 -0
  222. package/nodesets/Opc.Ua.Machinery.ProcessValues.NodeSet2.xml +1858 -0
  223. package/nodesets/Opc.Ua.Machinery_Result.NodeSet2.xml +2640 -0
  224. package/nodesets/Opc.Ua.MetalForming.NodeSet2.xml +1577 -0
  225. package/nodesets/Opc.Ua.NodeSet2.xml +81975 -0
  226. package/nodesets/Opc.Ua.PADIM.NodeSet2.xml +14131 -0
  227. package/nodesets/Opc.Ua.PackML.NodeSet2.xml +3030 -0
  228. package/nodesets/Opc.Ua.Robotics.NodeSet2.xml +7409 -0
  229. package/nodesets/Opc.Ua.Scales.NodeSet2.xml +15817 -0
  230. package/nodesets/Opc.Ua.Types.bsd +3499 -0
  231. package/nodesets/Opc.Ua.Weihenstephan.NodeSet2.xml +821 -0
  232. package/nodesets/Opc.Ua.Woodworking.NodeSet2.xml +17831 -0
  233. package/nodesets/UANodeSet.xsd +497 -0
  234. package/nodesets/opc.ua.isa95-jobcontrol.nodeset2.xml +4594 -0
  235. package/package.json +37 -4
@@ -0,0 +1,958 @@
1
+ /*=============================================================================|
2
+ | PROJECT SNAP7 1.3.0 |
3
+ |==============================================================================|
4
+ | Copyright (C) 2013, 2015 Davide Nardella |
5
+ | All rights reserved. |
6
+ |==============================================================================|
7
+ | SNAP7 is free software: you can redistribute it and/or modify |
8
+ | it under the terms of the Lesser GNU General Public License as published by |
9
+ | the Free Software Foundation, either version 3 of the License, or |
10
+ | (at your option) any later version. |
11
+ | |
12
+ | It means that you can distribute your commercial software linked with |
13
+ | SNAP7 without the requirement to distribute the source code of your |
14
+ | application and without the requirement that your application be itself |
15
+ | distributed under LGPL. |
16
+ | |
17
+ | SNAP7 is distributed in the hope that it will be useful, |
18
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
+ | Lesser GNU General Public License for more details. |
21
+ | |
22
+ | You should have received a copy of the GNU General Public License and a |
23
+ | copy of Lesser GNU General Public License along with Snap7. |
24
+ | If not, see http://www.gnu.org/licenses/ |
25
+ |=============================================================================*/
26
+
27
+ #include "snap_msgsock.h"
28
+ #ifndef OS_WINDOWS
29
+ #include <poll.h>
30
+ #endif
31
+
32
+ //---------------------------------------------------------------------------
33
+
34
+ static SocketsLayer SocketsLayerInitializer;
35
+
36
+ //---------------------------------------------------------------------------
37
+ // Base class endian aware
38
+ //---------------------------------------------------------------------------
39
+ TSnapBase::TSnapBase()
40
+ {
41
+ int x = 1;
42
+ LittleEndian=*(char *)&x == 1;
43
+ }
44
+ //---------------------------------------------------------------------------
45
+ word TSnapBase::SwapWord(word Value)
46
+ {
47
+ if (LittleEndian)
48
+ return ((Value >> 8) & 0xFF) | ((Value << 8) & 0xFF00);
49
+ else
50
+ return Value;
51
+ }
52
+ //---------------------------------------------------------------------------
53
+ longword TSnapBase::SwapDWord(longword Value)
54
+ {
55
+ if (LittleEndian)
56
+ return (Value >> 24) | ((Value << 8) & 0x00FF0000) | ((Value >> 8) & 0x0000FF00) | (Value << 24);
57
+ else
58
+ return Value;
59
+ }
60
+ //---------------------------------------------------------------------------
61
+ void Msg_CloseSocket(socket_t FSocket)
62
+ {
63
+ #ifdef OS_WINDOWS
64
+ closesocket(FSocket);
65
+ #else
66
+ close(FSocket);
67
+ #endif
68
+ }
69
+ //---------------------------------------------------------------------------
70
+ longword Msg_GetSockAddr(socket_t FSocket)
71
+ {
72
+ sockaddr_in RemoteSin;
73
+ #ifdef OS_WINDOWS
74
+ int namelen = sizeof(RemoteSin);
75
+ #else
76
+ uint32_t namelen = sizeof(RemoteSin);
77
+ #endif
78
+ namelen=sizeof(sockaddr_in);
79
+ if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
80
+ return RemoteSin.sin_addr.s_addr;
81
+ else
82
+ return 0;
83
+ }
84
+ //---------------------------------------------------------------------------
85
+ TMsgSocket::TMsgSocket()
86
+ {
87
+ Pinger = new TPinger();
88
+ // Set Defaults
89
+ strcpy(LocalAddress,"0.0.0.0");
90
+ LocalPort=0;
91
+ strcpy(RemoteAddress,"127.0.0.1");
92
+ RemotePort=0;
93
+ WorkInterval=100;
94
+ RecvTimeout=500;
95
+ SendTimeout=10;
96
+ PingTimeout=750;
97
+ Connected=false;
98
+ FSocket=INVALID_SOCKET;
99
+ LastTcpError=0;
100
+ LocalBind=0;
101
+ }
102
+ //---------------------------------------------------------------------------
103
+ TMsgSocket::~TMsgSocket()
104
+ {
105
+ DestroySocket();
106
+ delete Pinger;
107
+ }
108
+ //---------------------------------------------------------------------------
109
+ void TMsgSocket::SetSin(sockaddr_in &sin, char *Address, u_short Port)
110
+ {
111
+ uint32_t in_addr;
112
+ in_addr=inet_addr(Address);
113
+ memset(&sin, 0, sizeof(sin));
114
+ LastTcpError=0;
115
+
116
+ if (in_addr!=INADDR_NONE)
117
+ {
118
+ sin.sin_addr.s_addr = in_addr;
119
+ sin.sin_family = AF_INET;
120
+ sin.sin_port = htons(Port);
121
+ }
122
+ else
123
+ LastTcpError=WSAEINVALIDADDRESS;
124
+ }
125
+ //---------------------------------------------------------------------------
126
+ void TMsgSocket::GetSin(sockaddr_in sin, char *Address, u_short &Port)
127
+ {
128
+ strcpy(Address,inet_ntoa(sin.sin_addr));
129
+ Port=htons(sin.sin_port);
130
+ }
131
+ //---------------------------------------------------------------------------
132
+ void TMsgSocket::GetLocal()
133
+ {
134
+ #ifdef OS_WINDOWS
135
+ int namelen = sizeof(LocalSin);
136
+ #else
137
+ uint32_t namelen = sizeof(LocalSin);
138
+ #endif
139
+ if (getsockname(FSocket, (struct sockaddr*)&LocalSin, &namelen)==0)
140
+ GetSin(LocalSin, LocalAddress, LocalPort);
141
+ }
142
+ //---------------------------------------------------------------------------
143
+ void TMsgSocket::GetRemote()
144
+ {
145
+ #ifdef OS_WINDOWS
146
+ int namelen = sizeof(RemoteSin);
147
+ #else
148
+ uint32_t namelen = sizeof(RemoteSin);
149
+ #endif
150
+ if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
151
+ GetSin(RemoteSin, RemoteAddress, RemotePort);
152
+ }
153
+ //---------------------------------------------------------------------------
154
+ int TMsgSocket::GetLastSocketError()
155
+ {
156
+ #ifdef OS_WINDOWS
157
+ return WSAGetLastError();
158
+ #else
159
+ return errno;
160
+ #endif
161
+ }
162
+ //---------------------------------------------------------------------------
163
+ void TMsgSocket::Purge()
164
+ {
165
+ // small buffer to empty the socket
166
+ char Trash[512];
167
+ int Read;
168
+ if (LastTcpError!=WSAECONNRESET)
169
+ {
170
+ if (CanRead(0)) {
171
+ do
172
+ {
173
+ Read=recv(FSocket, Trash, 512, MSG_NOSIGNAL );
174
+ } while(Read==512);
175
+ }
176
+ }
177
+ }
178
+ //---------------------------------------------------------------------------
179
+ void TMsgSocket::CreateSocket()
180
+ {
181
+ DestroySocket();
182
+ LastTcpError=0;
183
+ FSocket =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
184
+ if (FSocket!=INVALID_SOCKET)
185
+ SetSocketOptions();
186
+ else
187
+ LastTcpError =GetLastSocketError();
188
+ }
189
+ //---------------------------------------------------------------------------
190
+ void TMsgSocket::GotSocket()
191
+ {
192
+ ClientHandle=RemoteSin.sin_addr.s_addr;
193
+ // could be inherited it if wee need further actions on the socket
194
+ }
195
+ //---------------------------------------------------------------------------
196
+ void TMsgSocket::SetSocket(socket_t s)
197
+ {
198
+ FSocket=s;
199
+ if (FSocket!=INVALID_SOCKET)
200
+ {
201
+ SetSocketOptions();
202
+ GetLocal();
203
+ GetRemote();
204
+ GotSocket();
205
+ }
206
+ Connected=FSocket!=INVALID_SOCKET;
207
+ }
208
+ //---------------------------------------------------------------------------
209
+ void TMsgSocket::DestroySocket()
210
+ {
211
+ if(FSocket != INVALID_SOCKET)
212
+ {
213
+ if (shutdown(FSocket, SD_SEND)==0)
214
+ Purge();
215
+ #ifdef OS_WINDOWS
216
+ closesocket(FSocket);
217
+ #else
218
+ close(FSocket);
219
+ #endif
220
+ FSocket=INVALID_SOCKET;
221
+ }
222
+ LastTcpError=0;
223
+ }
224
+ //---------------------------------------------------------------------------
225
+ int TMsgSocket::WaitingData()
226
+ {
227
+ int result = 0;
228
+ u_long x = 0;
229
+ #ifdef OS_WINDOWS
230
+ if (ioctlsocket(FSocket, FIONREAD, &x) == 0)
231
+ result = x;
232
+ #else
233
+ if (ioctl(FSocket, FIONREAD, &x) == 0)
234
+ result = x;
235
+ #endif
236
+ if (result>MaxPacketSize)
237
+ result = MaxPacketSize;
238
+ return result;
239
+ }
240
+ //---------------------------------------------------------------------------
241
+ int TMsgSocket::WaitForData(int Size, int Timeout)
242
+ {
243
+ longword Elapsed;
244
+
245
+ // Check for connection active
246
+ if (CanRead(0) && (WaitingData()==0))
247
+ LastTcpError=WSAECONNRESET;
248
+ else
249
+ LastTcpError=0;
250
+
251
+ // Enter main loop
252
+ if (LastTcpError==0)
253
+ {
254
+ Elapsed =SysGetTick();
255
+ while((WaitingData()<Size) && (LastTcpError==0))
256
+ {
257
+ // Checks timeout
258
+ if (DeltaTime(Elapsed)>=(longword)(Timeout))
259
+ LastTcpError =WSAETIMEDOUT;
260
+ else
261
+ SysSleep(1);
262
+ }
263
+ }
264
+ if(LastTcpError==WSAECONNRESET)
265
+ Connected =false;
266
+
267
+ return LastTcpError;
268
+ }
269
+ //---------------------------------------------------------------------------
270
+ void TMsgSocket::SetSocketOptions()
271
+ {
272
+ int NoDelay = 1;
273
+ int KeepAlive = 1;
274
+ LastTcpError=0;
275
+ SockCheck(setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY,(char*)&NoDelay, sizeof(NoDelay)));
276
+
277
+ if (LastTcpError==0)
278
+ SockCheck(setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&KeepAlive, sizeof(KeepAlive)));
279
+ }
280
+ //---------------------------------------------------------------------------
281
+ int TMsgSocket::SockCheck(int SockResult)
282
+ {
283
+ if (SockResult == (int)(SOCKET_ERROR))
284
+ LastTcpError = GetLastSocketError();
285
+
286
+ return LastTcpError;
287
+ }
288
+ //---------------------------------------------------------------------------
289
+ bool TMsgSocket::CanWrite(int Timeout)
290
+ {
291
+ #ifndef OS_WINDOWS
292
+ if (FSocket == INVALID_SOCKET) return false;
293
+ struct pollfd descriptor = {static_cast<int>(FSocket), POLLOUT, 0};
294
+ const int ready = poll(&descriptor, 1, Timeout);
295
+ if (ready < 0) {
296
+ LastTcpError = GetLastSocketError();
297
+ return false;
298
+ }
299
+ if (descriptor.revents & POLLNVAL) {
300
+ LastTcpError = EBADF;
301
+ return false;
302
+ }
303
+ // HUP/ERR are readiness: recv/send must observe EOF or the socket error.
304
+ return ready > 0;
305
+ #else
306
+ timeval TimeV;
307
+ int64_t x;
308
+ fd_set FDset;
309
+
310
+ if(FSocket == INVALID_SOCKET)
311
+ return false;
312
+
313
+ TimeV.tv_usec = (Timeout % 1000) * 1000;
314
+ TimeV.tv_sec = Timeout / 1000;
315
+
316
+ FD_ZERO(&FDset);
317
+ FD_SET(FSocket, &FDset);
318
+
319
+ x = select(FSocket + 1, NULL, &FDset, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio
320
+ if (x==(int)SOCKET_ERROR)
321
+ {
322
+ LastTcpError = GetLastSocketError();
323
+ x=0;
324
+ }
325
+ return (x > 0);
326
+ #endif
327
+ }
328
+ //---------------------------------------------------------------------------
329
+ bool TMsgSocket::CanRead(int Timeout)
330
+ {
331
+ #ifndef OS_WINDOWS
332
+ if (FSocket == INVALID_SOCKET) return false;
333
+ struct pollfd descriptor = {static_cast<int>(FSocket), POLLIN, 0};
334
+ const int ready = poll(&descriptor, 1, Timeout);
335
+ if (ready < 0) {
336
+ LastTcpError = GetLastSocketError();
337
+ return false;
338
+ }
339
+ if (descriptor.revents & POLLNVAL) {
340
+ LastTcpError = EBADF;
341
+ return false;
342
+ }
343
+ // HUP/ERR are readiness: recv/send must observe EOF or the socket error.
344
+ return ready > 0;
345
+ #else
346
+ timeval TimeV;
347
+ int64_t x;
348
+ fd_set FDset;
349
+
350
+ if(FSocket == INVALID_SOCKET)
351
+ return false;
352
+
353
+ TimeV.tv_usec = (Timeout % 1000) * 1000;
354
+ TimeV.tv_sec = Timeout / 1000;
355
+
356
+ FD_ZERO(&FDset);
357
+ FD_SET(FSocket, &FDset);
358
+
359
+ x = select(FSocket + 1, &FDset, NULL, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio
360
+ if (x==(int)SOCKET_ERROR)
361
+ {
362
+ LastTcpError = GetLastSocketError();
363
+ x=0;
364
+ }
365
+ return (x > 0);
366
+ #endif
367
+ }
368
+ //---------------------------------------------------------------------------
369
+ #ifdef NON_BLOCKING_CONNECT
370
+ //
371
+ // Non blocking connection (UNIX) Thanks to Rolf Stalder
372
+ //
373
+ int TMsgSocket::SckConnect()
374
+ {
375
+ int n, flags, err;
376
+ socklen_t len;
377
+ fd_set rset, wset;
378
+ struct timeval tval;
379
+
380
+ SetSin(RemoteSin, RemoteAddress, RemotePort);
381
+
382
+ if (LastTcpError == 0) {
383
+ CreateSocket();
384
+ if (LastTcpError == 0) {
385
+ flags = fcntl(FSocket, F_GETFL, 0);
386
+ if (flags >= 0) {
387
+ if (fcntl(FSocket, F_SETFL, flags | O_NONBLOCK) != -1) {
388
+ n = connect(FSocket, (struct sockaddr*)&RemoteSin, sizeof(RemoteSin));
389
+ if (n < 0) {
390
+ if (errno != EINPROGRESS) {
391
+ LastTcpError = GetLastSocketError();
392
+ }
393
+ else {
394
+ // still connecting ...
395
+ FD_ZERO(&rset);
396
+ FD_SET(FSocket, &rset);
397
+ wset = rset;
398
+ tval.tv_sec = PingTimeout / 1000;
399
+ tval.tv_usec = (PingTimeout % 1000) * 1000;
400
+
401
+ n = select(FSocket+1, &rset, &wset, NULL,
402
+ (PingTimeout ? &tval : NULL));
403
+ if (n == 0) {
404
+ // timeout
405
+ LastTcpError = WSAEHOSTUNREACH;
406
+ }
407
+ else {
408
+ if (FD_ISSET(FSocket, &rset) || FD_ISSET(FSocket, &wset)) {
409
+ err = 0;
410
+ len = sizeof(err);
411
+ if (getsockopt(
412
+ FSocket, SOL_SOCKET, SO_ERROR, &err, &len) == 0) {
413
+ if (err) {
414
+ LastTcpError = err;
415
+ }
416
+ else {
417
+ if (fcntl(FSocket, F_SETFL, flags) != -1) {
418
+ GetLocal();
419
+ ClientHandle = LocalSin.sin_addr.s_addr;
420
+ }
421
+ else {
422
+ LastTcpError = GetLastSocketError();
423
+ }
424
+ }
425
+ }
426
+ else {
427
+ LastTcpError = GetLastSocketError();
428
+ }
429
+ }
430
+ else {
431
+ LastTcpError = -1;
432
+ }
433
+ }
434
+ } // still connecting
435
+ }
436
+ else if (n == 0) {
437
+ // connected immediatly
438
+ GetLocal();
439
+ ClientHandle = LocalSin.sin_addr.s_addr;
440
+ }
441
+ }
442
+ else {
443
+ LastTcpError = GetLastSocketError();
444
+ } // fcntl(F_SETFL)
445
+ }
446
+ else {
447
+ LastTcpError = GetLastSocketError();
448
+ } // fcntl(F_GETFL)
449
+ } //valid socket
450
+ } // LastTcpError==0
451
+ Connected=LastTcpError==0;
452
+ return LastTcpError;
453
+ }
454
+ #else
455
+ //
456
+ // Regular connection (Windows)
457
+ //
458
+ int TMsgSocket::SckConnect()
459
+ {
460
+ int Result;
461
+ SetSin(RemoteSin, RemoteAddress, RemotePort);
462
+ if (LastTcpError==0)
463
+ {
464
+ if (Ping(RemoteSin))
465
+ {
466
+ CreateSocket();
467
+ if (LastTcpError==0)
468
+ {
469
+ Result=connect(FSocket, (struct sockaddr*)&RemoteSin, sizeof(RemoteSin));
470
+ if (SockCheck(Result)==0)
471
+ {
472
+ GetLocal();
473
+ // Client handle is self_address (here the connection is ACTIVE)
474
+ ClientHandle=LocalSin.sin_addr.s_addr;
475
+ }
476
+ }
477
+ }
478
+ else
479
+ LastTcpError=WSAEHOSTUNREACH;
480
+ }
481
+ Connected=LastTcpError==0;
482
+ return LastTcpError;
483
+ }
484
+ #endif
485
+ //---------------------------------------------------------------------------
486
+ void TMsgSocket::SckDisconnect()
487
+ {
488
+ DestroySocket();
489
+ Connected=false;
490
+ }
491
+ //---------------------------------------------------------------------------
492
+ void TMsgSocket::ForceClose()
493
+ {
494
+ if(FSocket != INVALID_SOCKET)
495
+ {
496
+ try {
497
+ #ifdef OS_WINDOWS
498
+ closesocket(FSocket);
499
+ #else
500
+ close(FSocket);
501
+ #endif
502
+ } catch (...) {
503
+ }
504
+ FSocket=INVALID_SOCKET;
505
+ }
506
+ LastTcpError=0;
507
+ }
508
+ //---------------------------------------------------------------------------
509
+ int TMsgSocket::SckBind()
510
+ {
511
+ int Res;
512
+ int Opt=1;
513
+ SetSin(LocalSin, LocalAddress, LocalPort);
514
+ if (LastTcpError==0)
515
+ {
516
+ CreateSocket();
517
+ if (LastTcpError==0)
518
+ {
519
+ setsockopt(FSocket ,SOL_SOCKET, SO_REUSEADDR, (const char *)&Opt, sizeof(int));
520
+ Res =bind(FSocket, (struct sockaddr*)&LocalSin, sizeof(sockaddr_in));
521
+ SockCheck(Res);
522
+ if (Res==0)
523
+ {
524
+ LocalBind=LocalSin.sin_addr.s_addr;
525
+ }
526
+ }
527
+ }
528
+ else
529
+ LastTcpError=WSAEINVALIDADDRESS;
530
+
531
+ return LastTcpError;
532
+ }
533
+ //---------------------------------------------------------------------------
534
+ int TMsgSocket::SckListen()
535
+ {
536
+ LastTcpError=0;
537
+ SockCheck(listen(FSocket ,SOMAXCONN));
538
+ return LastTcpError;
539
+ }
540
+ //---------------------------------------------------------------------------
541
+ bool TMsgSocket::Ping(char *Host)
542
+ {
543
+ return Pinger->Ping(Host, PingTimeout);
544
+ }
545
+ //---------------------------------------------------------------------------
546
+ bool TMsgSocket::Ping(sockaddr_in Addr)
547
+ {
548
+ if (PingTimeout == 0)
549
+ return true;
550
+ else
551
+ return Pinger->Ping(Addr.sin_addr.s_addr, PingTimeout);
552
+ }
553
+ //---------------------------------------------------------------------------
554
+ socket_t TMsgSocket::SckAccept()
555
+ {
556
+ socket_t result;
557
+ LastTcpError=0;
558
+ result = accept(FSocket, NULL, NULL);
559
+ if(result==INVALID_SOCKET)
560
+ LastTcpError =GetLastSocketError();
561
+ return result;
562
+ }
563
+ //---------------------------------------------------------------------------
564
+ int TMsgSocket::SendPacket(void *Data, int Size)
565
+ {
566
+ int Result;
567
+
568
+ LastTcpError=0;
569
+ if (SendTimeout>0)
570
+ {
571
+ if (!CanWrite(SendTimeout))
572
+ {
573
+ LastTcpError = WSAETIMEDOUT;
574
+ return LastTcpError;
575
+ }
576
+ }
577
+ if (send(FSocket, (char*)Data, Size, MSG_NOSIGNAL)==Size)
578
+ return 0;
579
+ else
580
+ Result =SOCKET_ERROR;
581
+
582
+ SockCheck(Result);
583
+ return Result;
584
+ }
585
+ //---------------------------------------------------------------------------
586
+ bool TMsgSocket::PacketReady(int Size)
587
+ {
588
+ return (WaitingData()>=Size);
589
+ }
590
+ //---------------------------------------------------------------------------
591
+ int TMsgSocket::Receive(void *Data, int BufSize, int &SizeRecvd)
592
+ {
593
+ LastTcpError=0;
594
+ if (CanRead(RecvTimeout))
595
+ {
596
+ SizeRecvd=recv(FSocket ,(char*)Data ,BufSize ,MSG_NOSIGNAL );
597
+
598
+ if (SizeRecvd>0) // something read (default case)
599
+ LastTcpError=0;
600
+ else
601
+ if (SizeRecvd==0)
602
+ LastTcpError = WSAECONNRESET; // Connection reset by Peer
603
+ else
604
+ LastTcpError=GetLastSocketError(); // we need to know what happened
605
+ }
606
+ else
607
+ LastTcpError = WSAETIMEDOUT;
608
+
609
+ if (LastTcpError==WSAECONNRESET)
610
+ Connected = false;
611
+
612
+ return LastTcpError;
613
+ }
614
+ //---------------------------------------------------------------------------
615
+ int TMsgSocket::RecvPacket(void *Data, int Size)
616
+ {
617
+ int BytesRead;
618
+ WaitForData(Size, RecvTimeout);
619
+ if (LastTcpError==0)
620
+ {
621
+ BytesRead=recv(FSocket, (char*)Data, Size, MSG_NOSIGNAL);
622
+ if (BytesRead==0)
623
+ LastTcpError = WSAECONNRESET; // Connection reset by Peer
624
+ else
625
+ if (BytesRead<0)
626
+ LastTcpError = GetLastSocketError();
627
+ }
628
+ else // After the timeout the bytes waiting were less then we expected
629
+ if (LastTcpError==WSAETIMEDOUT)
630
+ Purge();
631
+
632
+ if (LastTcpError==WSAECONNRESET)
633
+ Connected =false;
634
+
635
+ return LastTcpError;
636
+ }
637
+ //---------------------------------------------------------------------------
638
+ int TMsgSocket::PeekPacket(void *Data, int Size)
639
+ {
640
+ int BytesRead;
641
+ WaitForData(Size, RecvTimeout);
642
+ if (LastTcpError==0)
643
+ {
644
+ BytesRead=recv(FSocket, (char*)Data, Size, MSG_PEEK | MSG_NOSIGNAL );
645
+ if (BytesRead==0)
646
+ LastTcpError = WSAECONNRESET; // Connection reset by Peer
647
+ else
648
+ if (BytesRead<0)
649
+ LastTcpError = GetLastSocketError();
650
+ }
651
+ else // After the timeout the bytes waiting were less then we expected
652
+ if (LastTcpError==WSAETIMEDOUT)
653
+ Purge();
654
+
655
+ if (LastTcpError==WSAECONNRESET)
656
+ Connected =false;
657
+
658
+ return LastTcpError;
659
+ }
660
+ //---------------------------------------------------------------------------
661
+ bool TMsgSocket::Execute()
662
+ {
663
+ return true;
664
+ }
665
+ //==============================================================================
666
+ // PING
667
+ //==============================================================================
668
+
669
+ static int PingKind;
670
+
671
+ #ifdef OS_WINDOWS
672
+ // iphlpapi, is loaded dinamically because if this fails we can still try
673
+ // to use raw sockets
674
+
675
+ static char const *iphlpapi = "\\iphlpapi.dll";
676
+ #pragma pack(1)
677
+
678
+ //typedef byte TTxBuffer[40];
679
+ typedef byte TTxBuffer[32];
680
+ #pragma pack()
681
+
682
+ typedef HANDLE (__stdcall *pfn_IcmpCreateFile)();
683
+ typedef bool (__stdcall *pfn_IcmpCloseHandle)(HANDLE PingHandle);
684
+
685
+ typedef int (__stdcall *pfn_IcmpSendEcho2)(
686
+ HANDLE PingHandle,
687
+ void *Event,
688
+ void *AcpRoutine,
689
+ void *AcpContext,
690
+ unsigned long DestinationAddress,
691
+ void *RequestData,
692
+ int RequestSize,
693
+ void *not_used, //should be *IP_OPTION_INFORMATION but we don't use it
694
+ void *ReplyBuffer,
695
+ int ReplySize,
696
+ int Timeout
697
+ );
698
+
699
+ static pfn_IcmpCreateFile IcmpCreateFile;
700
+ static pfn_IcmpCloseHandle IcmpCloseHandle;
701
+ static pfn_IcmpSendEcho2 IcmpSendEcho2;
702
+ static HINSTANCE IcmpDllHandle = 0;
703
+ static bool IcmpAvail = false;
704
+
705
+ bool IcmpInit()
706
+ {
707
+ char iphlppath[MAX_PATH+12];
708
+
709
+ int PathLen = GetSystemDirectoryA(iphlppath, MAX_PATH);
710
+ if (PathLen != 0)
711
+ {
712
+ strcat(iphlppath, iphlpapi);
713
+ IcmpDllHandle = LoadLibraryA(iphlppath);
714
+ }
715
+ else
716
+ IcmpDllHandle = 0;
717
+
718
+ if (IcmpDllHandle != 0)
719
+ {
720
+ IcmpCreateFile=(pfn_IcmpCreateFile)GetProcAddress(IcmpDllHandle,"IcmpCreateFile");
721
+ IcmpCloseHandle=(pfn_IcmpCloseHandle)GetProcAddress(IcmpDllHandle,"IcmpCloseHandle");
722
+ IcmpSendEcho2=(pfn_IcmpSendEcho2)GetProcAddress(IcmpDllHandle,"IcmpSendEcho2");
723
+ return (IcmpCreateFile!=NULL) && (IcmpCloseHandle!=NULL) && (IcmpSendEcho2!=NULL);
724
+ }
725
+ else
726
+ return false;
727
+ }
728
+
729
+ void IcmpDone()
730
+ {
731
+ if (IcmpDllHandle!=0)
732
+ FreeLibrary(IcmpDllHandle);
733
+ IcmpAvail=false;
734
+ }
735
+ #endif
736
+
737
+ //---------------------------------------------------------------------------
738
+ // RAW Socket Pinger
739
+ //---------------------------------------------------------------------------
740
+ TRawSocketPinger::TRawSocketPinger()
741
+ {
742
+ FSocket =socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
743
+ FId =word(size_t(this));
744
+ FSeq =0;
745
+ }
746
+ //---------------------------------------------------------------------------
747
+ TRawSocketPinger::~TRawSocketPinger()
748
+ {
749
+ if (FSocket!=INVALID_SOCKET)
750
+ {
751
+ #ifdef OS_WINDOWS
752
+ closesocket(FSocket);
753
+ #else
754
+ close(FSocket);
755
+ #endif
756
+ FSocket=INVALID_SOCKET;
757
+ };
758
+ }
759
+ //---------------------------------------------------------------------------
760
+ void TRawSocketPinger::InitPacket()
761
+ {
762
+ memset(&IcmpBuffer,0,ICmpBufferSize);
763
+ FSeq++;
764
+
765
+ SendPacket=PIcmpPacket(pbyte(&IcmpBuffer)+sizeof(TIPHeader));
766
+ SendPacket->Header.ic_type=ICMP_ECHORQ;
767
+ SendPacket->Header.ic_code=0;
768
+ SendPacket->Header.ic_cksum=0;
769
+ SendPacket->Header.ic_id=FId;
770
+ SendPacket->Header.ic_seq=FSeq;
771
+
772
+ memset(&SendPacket->Data,0,sizeof(SendPacket->Data));
773
+ SendPacket->Header.ic_cksum=PacketChecksum();
774
+ }
775
+ //---------------------------------------------------------------------------
776
+ word TRawSocketPinger::PacketChecksum()
777
+ {
778
+ word *P = (word*)(SendPacket);
779
+ longword Sum = 0;
780
+ int c;
781
+ for (c = 0; c < int(sizeof(TIcmpPacket) / 2); c++) {
782
+ Sum+=*P;
783
+ P++;
784
+ }
785
+ Sum=(Sum >> 16) + (Sum & 0xFFFF);
786
+ Sum=Sum+(Sum >> 16);
787
+ return word(~Sum);
788
+ }
789
+ //---------------------------------------------------------------------------
790
+ bool TRawSocketPinger::CanRead(int Timeout)
791
+ {
792
+ timeval TimeV;
793
+ int64_t x;
794
+ fd_set FDset;
795
+
796
+ TimeV.tv_usec = (Timeout % 1000) * 1000;
797
+ TimeV.tv_sec = Timeout / 1000;
798
+
799
+ FD_ZERO(&FDset);
800
+ FD_SET(FSocket, &FDset);
801
+
802
+ x = select(FSocket + 1, &FDset, NULL, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio
803
+ if (x==(int)(SOCKET_ERROR))
804
+ x=0;
805
+ return (x > 0);
806
+ }
807
+ //---------------------------------------------------------------------------
808
+ bool TRawSocketPinger::Ping(longword ip_addr, int Timeout)
809
+ {
810
+ sockaddr_in LSockAddr;
811
+ sockaddr_in RSockAddr;
812
+ PIcmpReply Reply;
813
+
814
+ if (FSocket==INVALID_SOCKET)
815
+ return true;
816
+
817
+ // Init packet
818
+ InitPacket();
819
+ Reply=PIcmpReply(&IcmpBuffer);
820
+ // Init Remote and Local Addresses struct
821
+ RSockAddr.sin_family=AF_INET;
822
+ RSockAddr.sin_port=0;
823
+ RSockAddr.sin_addr.s_addr=ip_addr;
824
+
825
+ LSockAddr.sin_family=AF_INET;
826
+ LSockAddr.sin_port=0;
827
+ LSockAddr.sin_addr.s_addr=inet_addr("0.0.0.0");
828
+
829
+ // Bind to local
830
+ if (bind(FSocket, (struct sockaddr*)&LSockAddr, sizeof(sockaddr_in))!=0)
831
+ return false;
832
+ // Connect to remote (not a really TCP connection, only to setup the socket)
833
+ if (connect(FSocket, (struct sockaddr*)&RSockAddr, sizeof(sockaddr_in))!=0)
834
+ return false;
835
+ // Send ICMP packet
836
+ if (send(FSocket, (char*)SendPacket, sizeof(TIcmpPacket), MSG_NOSIGNAL)!=int(sizeof(TIcmpPacket)))
837
+ return false;
838
+ // Wait for a reply
839
+ if (!CanRead(Timeout))
840
+ return false;// time expired
841
+ // Get the answer
842
+ if (recv(FSocket, (char*)&IcmpBuffer, ICmpBufferSize, MSG_NOSIGNAL)<int(sizeof(TIcmpReply)))
843
+ return false;
844
+ // Check the answer
845
+ return (Reply->IPH.ip_src==RSockAddr.sin_addr.s_addr) && // the peer is what we are looking for
846
+ (Reply->ICmpReply.Header.ic_type==ICMP_ECHORP); // type = reply
847
+ }
848
+ //---------------------------------------------------------------------------
849
+ // Pinger
850
+ //---------------------------------------------------------------------------
851
+ TPinger::TPinger()
852
+ {
853
+ }
854
+ //---------------------------------------------------------------------------
855
+ TPinger::~TPinger()
856
+ {
857
+ }
858
+ //---------------------------------------------------------------------------
859
+ bool TPinger::RawPing(longword ip_addr, int Timeout)
860
+ {
861
+ PRawSocketPinger RawPinger = new TRawSocketPinger();
862
+ bool Result;
863
+ Result=RawPinger->Ping(ip_addr, Timeout);
864
+ delete RawPinger;
865
+ return Result;
866
+ }
867
+ //---------------------------------------------------------------------------
868
+ #ifdef OS_WINDOWS
869
+ bool TPinger::WinPing(longword ip_addr, int Timeout)
870
+ {
871
+ HANDLE PingHandle;
872
+ TTxBuffer TxBuffer;
873
+ TIcmpBuffer IcmpBuffer;
874
+ bool Result;
875
+
876
+ PingHandle = IcmpCreateFile();
877
+ if (PingHandle != INVALID_HANDLE_VALUE)
878
+ {
879
+ memset(&TxBuffer,'\55',sizeof(TTxBuffer));
880
+ Result=(IcmpSendEcho2(PingHandle, NULL, NULL, NULL, ip_addr,
881
+ &TxBuffer, sizeof(TxBuffer), NULL, &IcmpBuffer, ICmpBufferSize, Timeout))>0;
882
+ IcmpCloseHandle(PingHandle);
883
+ return Result;
884
+ }
885
+ else
886
+ return false;
887
+ }
888
+ #endif
889
+ //---------------------------------------------------------------------------
890
+ bool TPinger::Ping(char *Host, int Timeout)
891
+ {
892
+ longword Addr;
893
+ Addr=inet_addr(Host);
894
+ return Ping(Addr, Timeout);
895
+ }
896
+ //---------------------------------------------------------------------------
897
+ bool TPinger::Ping(longword ip_addr, int Timeout)
898
+ {
899
+ #ifdef OS_WINDOWS
900
+ if (PingKind==pkWinHelper)
901
+ return WinPing(ip_addr, Timeout);
902
+ else
903
+ #endif
904
+ if (PingKind==pkRawSocket)
905
+ return RawPing(ip_addr, Timeout);
906
+ else
907
+ return true; // we still need to continue
908
+ }
909
+ //---------------------------------------------------------------------------
910
+ // Checks if raw sockets are allowed
911
+ //---------------------------------------------------------------------------
912
+ bool RawSocketsCheck()
913
+ {
914
+ socket_t RawSocket;
915
+ bool Result;
916
+ RawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
917
+
918
+ Result=RawSocket != INVALID_SOCKET;
919
+ if (Result)
920
+ #ifdef OS_WINDOWS
921
+ closesocket(RawSocket);
922
+ #else
923
+ close(RawSocket);
924
+ #endif
925
+
926
+ return Result;
927
+ }
928
+ //---------------------------------------------------------------------------
929
+ // Sockets init
930
+ // - Winsock Startup (Windows)
931
+ // - ICMP Helper Load (Windows)
932
+ // - Check for raw socket (Unix or Windows if ICMP load failed)
933
+ //---------------------------------------------------------------------------
934
+ SocketsLayer::SocketsLayer()
935
+ {
936
+ #ifdef OS_WINDOWS
937
+ timeBeginPeriod(1); // it's not strictly related to socket but here is a nice place
938
+ WSAStartup(0x202,&wsaData);
939
+ if (IcmpInit())
940
+ PingKind=pkWinHelper;
941
+ else
942
+ #endif
943
+ if (RawSocketsCheck())
944
+ PingKind=pkRawSocket;
945
+ else
946
+ PingKind=pkCannotPing;
947
+ }
948
+
949
+ SocketsLayer::~SocketsLayer()
950
+ {
951
+ #ifdef OS_WINDOWS
952
+ IcmpDone();
953
+ WSACleanup();
954
+ timeEndPeriod(1);
955
+ #endif
956
+ }
957
+
958
+