#include-once ; Some socket functions ; Author: funkey ; Project start 2013.03.12 ; Actual project date 2014.07.24 ; Actual project date 2014.11.08 ; _TCPRecv tested and changed Global Const $tagIN_ADDR = "ulong S_addr;" Global Const $tagIN6_ADDR = "USHORT Word[8];" Global Const $tagSockAddr = "USHORT sa_family;char sa_data[14]" Global Const $tagSockAddr_In = "short sin_family;USHORT sin_port;ULONG sin_addr;char sin_zero[8]" Global Const $tagSockAddr_In6 = "short sin6_family;USHORT sin6_port;ULONG sin6_flowinfo;byte sin6_addr[16];ULONG sin6_scope_id" Global Const $tagSockAddr_Storage = "short ss_family;char ss_pad1[6];INT64 ss_align;char ss_pad1[112]" Global Const $tagTimeVal = "long tv_sec;long tv_usec" Global Const $tagAddrInfo = "int ai_flags;int ai_family;int ai_socktype;int ai_protocol;uint ai_addrlen;ptr ai_canonname;ptr ai_addr;ptr ai_next" Global Const $FD_SETSIZE = 64 Global Const $tagFd_set = "UINT fd_count;UINT fd_array[" & $FD_SETSIZE & "]" Global Const $INADDR_ANY = 0x0 Global Const $INADDR_NONE = 0xffffffff Global Const $INADDR_LOOPBACK = 0x7f000001 Global Const $AF_UNSPEC = 0 Global Const $AF_UNIX = 1 Global Const $AF_INET = 2 Global Const $AF_IMPLINK = 3 Global Const $AF_PUP = 4 Global Const $AF_CHAOS = 5 Global Const $AF_IPX = 6 Global Const $AF_NS = 6 Global Const $AF_ISO = 7 Global Const $AF_OSI = $AF_ISO Global Const $AF_ECMA = 8 Global Const $AF_DATAKIT = 9 Global Const $AF_CCITT = 10 Global Const $AF_SNA = 11 Global Const $AF_DECnet = 12 Global Const $AF_DLI = 13 Global Const $AF_LAT = 14 Global Const $AF_HYLINK = 15 Global Const $AF_APPLETALK = 16 Global Const $AF_NETBIOS = 17 Global Const $AF_VOICEVIEW = 18 Global Const $AF_FIREFOX = 19 Global Const $AF_UNKNOWN1 = 20 Global Const $AF_BAN = 21 Global Const $AF_ATM = 22 Global Const $AF_INET6 = 23 Global Const $SOCKET_ERROR = -1 Global Const $INVALID_SOCKET = -1 Global Const $SOCK_STREAM = 1 Global Const $SOCK_DGRAM = 2 Global Const $SOCK_RAW = 3 Global Const $SOCK_RDM = 4 Global Const $SOCK_SEQPACKET = 5 Global Const $IPPROTO_IP = 0 Global Const $IPPROTO_ICMP = 1 Global Const $IPPROTO_IGMP = 2 Global Const $IPPROTO_GGP = 3 Global Const $IPPROTO_TCP = 6 Global Const $IPPROTO_PUP = 12 Global Const $IPPROTO_UDP = 17 Global Const $IPPROTO_IDP = 22 Global Const $IPPROTO_ND = 77 Global Const $IPPROTO_RAW = 255 Global Const $SOL_SOCKET = 0xFFFF ;SOL_SOCKET Socket Options ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx Global Const $IP_OPTIONS = 1 Global Const $SO_DEBUG = 1 Global Const $SO_ACCEPTCONN = 2 Global Const $SO_REUSEADDR = 4 ; BOOLEAN Global Const $SO_KEEPALIVE = 8 Global Const $SO_DONTROUTE = 16 Global Const $SO_BROADCAST = 32 ;INT Global Const $SO_USELOOPBACK = 64 Global Const $SO_LINGER = 128 Global Const $SO_OOBINLINE = 256 Global Const $SO_EXCLUSIVEADDRUSE = -5 ; BOOLEAN (~SO_REUSEADDR) Global Const $SO_DONTLINGER = -129 ; (~SO_LINGER) Global Const $SO_SNDBUF = 0x1001 Global Const $SO_RCVBUF = 0x1002 Global Const $SO_SNDLOWAT = 0x1003 Global Const $SO_RCVLOWAT = 0x1004 Global Const $SO_SNDTIMEO = 0x1005 ; DWORD Global Const $SO_RCVTIMEO = 0x1006 ; DWORD Global Const $SO_ERROR = 0x1007 Global Const $SO_TYPE = 0x1008 Global Const $FIONBIO = 0x8004667E Global Const $FIONREAD = 0x4004667F Global Const $FIOASYNC = 0x8004667D Global Const $SIOCSHIWAT = 0x80047300 Global Const $SIOCGHIWAT = 0x80047301 Global Const $SIOCSLOWAT = 0x80047302 Global Const $SIOCGLOWAT = 0x80047303 Global Const $SIOCATMARK = 0x40047307 ;max backlog for listen() Global Const $SOMAXCONN = 0x7fffffff ;flags for send(to) Global Const $MSG_DONTROUTE = 4 Global Const $MSG_OOB = 1 Global Const $WSABASEERR = 10000 Global Const $WSAEINTR = ($WSABASEERR + 4) Global Const $WSAEBADF = ($WSABASEERR + 9) Global Const $WSAEACCES = ($WSABASEERR + 13) Global Const $WSAEFAULT = ($WSABASEERR + 14) Global Const $WSAEINVAL = ($WSABASEERR + 22) Global Const $WSAEMFILE = ($WSABASEERR + 24) Global Const $WSAEWOULDBLOCK = ($WSABASEERR + 35) Global Const $WSAEINPROGRESS = ($WSABASEERR + 36) ; deprecated on WinSock2 Global Const $WSAEALREADY = ($WSABASEERR + 37) Global Const $WSAENOTSOCK = ($WSABASEERR + 38) Global Const $WSAEDESTADDRREQ = ($WSABASEERR + 39) Global Const $WSAEMSGSIZE = ($WSABASEERR + 40) Global Const $WSAEPROTOTYPE = ($WSABASEERR + 41) Global Const $WSAENOPROTOOPT = ($WSABASEERR + 42) Global Const $WSAEPROTONOSUPPORT = ($WSABASEERR + 43) Global Const $WSAESOCKTNOSUPPORT = ($WSABASEERR + 44) Global Const $WSAEOPNOTSUPP = ($WSABASEERR + 45) Global Const $WSAEPFNOSUPPORT = ($WSABASEERR + 46) Global Const $WSAEAFNOSUPPORT = ($WSABASEERR + 47) Global Const $WSAEADDRINUSE = ($WSABASEERR + 48) Global Const $WSAEADDRNOTAVAIL = ($WSABASEERR + 49) Global Const $WSAENETDOWN = ($WSABASEERR + 50) Global Const $WSAENETUNREACH = ($WSABASEERR + 51) Global Const $WSAENETRESET = ($WSABASEERR + 52) Global Const $WSAECONNABORTED = ($WSABASEERR + 53) Global Const $WSAECONNRESET = ($WSABASEERR + 54) Global Const $WSAENOBUFS = ($WSABASEERR + 55) Global Const $WSAEISCONN = ($WSABASEERR + 56) Global Const $WSAENOTCONN = ($WSABASEERR + 57) Global Const $WSAESHUTDOWN = ($WSABASEERR + 58) Global Const $WSAETOOMANYREFS = ($WSABASEERR + 59) Global Const $WSAETIMEDOUT = ($WSABASEERR + 60) Global Const $WSAECONNREFUSED = ($WSABASEERR + 61) Global Const $WSAELOOP = ($WSABASEERR + 62) Global Const $WSAENAMETOOLONG = ($WSABASEERR + 63) Global Const $WSAEHOSTDOWN = ($WSABASEERR + 64) Global Const $WSAEHOSTUNREACH = ($WSABASEERR + 65) Global Const $WSAENOTEMPTY = ($WSABASEERR + 66) Global Const $WSAEPROCLIM = ($WSABASEERR + 67) Global Const $WSAEUSERS = ($WSABASEERR + 68) Global Const $WSAEDQUOT = ($WSABASEERR + 69) Global Const $WSAESTALE = ($WSABASEERR + 70) Global Const $WSAEREMOTE = ($WSABASEERR + 71) Global Const $WSAEDISCON = ($WSABASEERR + 101) Global Const $WSASYSNOTREADY = ($WSABASEERR + 91) Global Const $WSAVERNOTSUPPORTED = ($WSABASEERR + 92) Global Const $WSANOTINITIALISED = ($WSABASEERR + 93) Global Const $WSAHOST_NOT_FOUND = ($WSABASEERR + 1001) Global Const $WSATRY_AGAIN = ($WSABASEERR + 1002) Global Const $WSANO_RECOVERY = ($WSABASEERR + 1003) Global Const $WSANO_DATA = ($WSABASEERR + 1004) ; WinSock2 specific error codes Global Const $WSAENOMORE = ($WSABASEERR + 102) Global Const $WSAECANCELLED = ($WSABASEERR + 103) Global Const $WSAEINVALIDPROCTABLE = ($WSABASEERR + 104) Global Const $WSAEINVALIDPROVIDER = ($WSABASEERR + 105) Global Const $WSAEPROVIDERFAILEDINIT = ($WSABASEERR + 106) Global Const $WSASYSCALLFAILURE = ($WSABASEERR + 107) Global Const $WSASERVICE_NOT_FOUND = ($WSABASEERR + 108) Global Const $WSATYPE_NOT_FOUND = ($WSABASEERR + 109) Global Const $WSA_E_NO_MORE = ($WSABASEERR + 110) Global Const $WSA_E_CANCELLED = ($WSABASEERR + 111) Global Const $WSAEREFUSED = ($WSABASEERR + 112) ; WS QualityofService errors Global Const $WSA_QOS_RECEIVERS = ($WSABASEERR + 1005) Global Const $WSA_QOS_SENDERS = ($WSABASEERR + 1006) Global Const $WSA_QOS_NO_SENDERS = ($WSABASEERR + 1007) Global Const $WSA_QOS_NO_RECEIVERS = ($WSABASEERR + 1008) Global Const $WSA_QOS_REQUEST_CONFIRMED = ($WSABASEERR + 1009) Global Const $WSA_QOS_ADMISSION_FAILURE = ($WSABASEERR + 1010) Global Const $WSA_QOS_POLICY_FAILURE = ($WSABASEERR + 1011) Global Const $WSA_QOS_BAD_STYLE = ($WSABASEERR + 1012) Global Const $WSA_QOS_BAD_OBJECT = ($WSABASEERR + 1013) Global Const $WSA_QOS_TRAFFIC_CTRL_ERROR = ($WSABASEERR + 1014) Global Const $WSA_QOS_GENERIC_ERROR = ($WSABASEERR + 1015) Global Const $WSA_QOS_ESERVICETYPE = ($WSABASEERR + 1016) Global Const $WSA_QOS_EFLOWSPEC = ($WSABASEERR + 1017) Global Const $WSA_QOS_EPROVSPECBUF = ($WSABASEERR + 1018) Global Const $WSA_QOS_EFILTERSTYLE = ($WSABASEERR + 1019) Global Const $WSA_QOS_EFILTERTYPE = ($WSABASEERR + 1020) Global Const $WSA_QOS_EFILTERCOUNT = ($WSABASEERR + 1021) Global Const $WSA_QOS_EOBJLENGTH = ($WSABASEERR + 1022) Global Const $WSA_QOS_EFLOWCOUNT = ($WSABASEERR + 1023) Global Const $WSA_QOS_EUNKOWNPSOBJ = ($WSABASEERR + 1024) Global Const $WSA_QOS_EPOLICYOBJ = ($WSABASEERR + 1025) Global Const $WSA_QOS_EFLOWDESC = ($WSABASEERR + 1026) Global Const $WSA_QOS_EPSFLOWSPEC = ($WSABASEERR + 1027) Global Const $WSA_QOS_EPSFILTERSPEC = ($WSABASEERR + 1028) Global Const $WSA_QOS_ESDMODEOBJ = ($WSABASEERR + 1029) Global Const $WSA_QOS_ESHAPERATEOBJ = ($WSABASEERR + 1030) Global Const $WSA_QOS_RESERVED_PETYPE = ($WSABASEERR + 1031) Global $hDLL_WS2_32 = 0 Func _WSAStartup() If $hDLL_WS2_32 = 0 Then $hDLL_WS2_32 = DllOpen("ws2_32.dll") If $hDLL_WS2_32 = -1 Then $hDLL_WS2_32 = 0 Return 0 EndIf If TCPStartup() = 0 Then ;TCPStartup() is equal to WSAStartup function DllClose($hDLL_WS2_32) $hDLL_WS2_32 = 0 Return 0 EndIf EndIf Return 1 EndFunc ;==>_WSAStartup Func _WSACleanup() If $hDLL_WS2_32 <> 0 Then TCPShutdown() ;TCPShutdown() is equal to WSACleanup function DllClose($hDLL_WS2_32) $hDLL_WS2_32 = 0 EndIf Return 1 EndFunc ;==>_WSACleanup Func _setsockopt($iSocket, $iLevel, $iOptName, $tOptVal) Local $iOptLen = DllStructGetSize($tOptVal) Local $aRet = DllCall($hDLL_WS2_32, "int", "setsockopt", "uint", $iSocket, "int", $iLevel, "int", $iOptName, "struct*", $tOptVal, "int", $iOptLen) Return $aRet[0] EndFunc ;==>_setsockopt Func _getsockopt($iSocket, $iLevel, $iOptName, ByRef $tOptVal) Local $iOptLen = DllStructGetSize($tOptVal) Local $aRet = DllCall($hDLL_WS2_32, "int", "getsockopt", "int", $iSocket, "int", $iLevel, "int", $iOptName, "struct*", $tOptVal, "int*", $iOptLen) Return $aRet[0] EndFunc ;==>_getsockopt Func _socket($AF = $AF_INET, $SocketType = $SOCK_STREAM, $Protocol = $IPPROTO_TCP) ; AF_INET, SOCK_STREAM, IPPROTO_TCP Local $aRet = DllCall($hDLL_WS2_32, "int", "socket", "int", $AF, "int", $SocketType, "int", $Protocol) Return $aRet[0] EndFunc ;==>_socket Func _closesocket($iSocket) Local $aRet = DllCall($hDLL_WS2_32, "int", "closesocket", "UINT", $iSocket) Return $aRet[0] EndFunc ;==>_closesocket ;shutdown() allows you to cut off communication in a certain direction, or both ways (just like closesocket() does.) ;0 Further receives are disallowed ;1 Further sends are disallowed ;2 Further sends and receives are disallowed (like closesocket()) ;It's important to note that shutdown() doesn't actually close the file descriptor — it just changes ;its usability. To free a socket descriptor, you need to use closesocket(). Func _shutdown($iSocket, $iHow) Local $aRet = DllCall($hDLL_WS2_32, "int", "shutdown", "UINT", $iSocket, "int", $iHow) Return $aRet[0] EndFunc ;==>_shutdown Func _bind($iSocket, $sIP, $iPort) Local $tSockAddr_In = DllStructCreate($tagSockAddr_In) Local $pSockAddr_In = DllStructGetPtr($tSockAddr_In) Local $iSizeSockAddr_In = DllStructGetSize($tSockAddr_In) DllStructSetData($tSockAddr_In, "sin_family", $AF_INET) DllStructSetData($tSockAddr_In, "sin_port", _htons($iPort)) DllStructSetData($tSockAddr_In, "sin_addr", _inet_addr($sIP)) Local $aRet = DllCall($hDLL_WS2_32, "int", "bind", "UINT", $iSocket, "struct*", $pSockAddr_In, "int", $iSizeSockAddr_In) Return $aRet[0] EndFunc ;==>_bind Func _connect($iSocket, $sIP, $iPort) Local $tSockAddr_In = DllStructCreate($tagSockAddr_In) Local $iSizeSockAddr_In = DllStructGetSize($tSockAddr_In) DllStructSetData($tSockAddr_In, "sin_family", $AF_INET) DllStructSetData($tSockAddr_In, "sin_port", _htons($iPort)) DllStructSetData($tSockAddr_In, "sin_addr", _inet_addr($sIP)) Local $aRet = DllCall($hDLL_WS2_32, "int", "connect", "UINT", $iSocket, "struct*", $tSockAddr_In, "int", $iSizeSockAddr_In) Return $aRet[0] EndFunc ;==>_connect Func _listen($iSocket, $iBacklog = $SOMAXCONN) Local $aRet = DllCall($hDLL_WS2_32, "int", "listen", "UINT", $iSocket, "int", $iBacklog) Return $aRet[0] EndFunc ;==>_listen Func _accept($iSocket, ByRef $sIP, ByRef $iPort) Local $tAddr = DllStructCreate($tagSockAddr_In) Local $aRet = DllCall($hDLL_WS2_32, "int", "accept", "UINT", $iSocket, "struct*", $tAddr, "int*", DllStructGetSize($tAddr)) $iPort = _ntohs(DllStructGetData($tAddr, 2)) $sIP = _inet_ntoa(DllStructGetData($tAddr, 3)) Return $aRet[0] EndFunc ;==>_accept Func _select(ByRef $tReadFds, $iTimeout) Local $tTimeout = DllStructCreate($tagTimeVal) DllStructSetData($tTimeout, "tv_sec", Floor($iTimeout / 1000)) DllStructSetData($tTimeout, "tv_usec", ($iTimeout - DllStructGetData($tTimeout, "tv_sec") * 1000) * 1000) Local $aRet = DllCall($hDLL_WS2_32, "int", "select", "int", 0, "struct*", $tReadFds, "ptr", 0, "ptr", 0, "struct*", $tTimeout) Return $aRet[0] ;-1 = error; 0 = timeout; else data! EndFunc ;==>_select Func _selectEx($pReadFds, $pWriteFds, $pExceptFds, $iTimeout) Local $tTimeout = DllStructCreate($tagTimeVal) DllStructSetData($tTimeout, "tv_sec", Floor($iTimeout / 1000)) DllStructSetData($tTimeout, "tv_usec", ($iTimeout - DllStructGetData($tTimeout, "tv_sec") * 1000) * 1000) Local $aRet = DllCall($hDLL_WS2_32, "int", "select", "int", 0, "ptr", $pReadFds, "ptr", $pWriteFds, "ptr", $pExceptFds, "struct*", $tTimeout) Return $aRet[0] ;-1 = error; 0 = timeout; else data! EndFunc ;==>_selectEx Func _FD_ZERO(ByRef $tFd_set) DllStructSetData($tFd_set, "fd_count", 0) EndFunc ;==>_FD_ZERO Func _FD_SET($iSock, ByRef $tFd_set) Local $iCount = DllStructGetData($tFd_set, "fd_count") For $i = 0 To $iCount - 1 If DllStructGetData($tFd_set, "fd_Array", $i + 1) = $iSock Then ExitLoop EndIf Next If $i = $iCount Then If $iCount < $FD_SETSIZE Then DllStructSetData($tFd_set, "fd_array", $iSock, $i + 1) DllStructSetData($tFd_set, "fd_count", $iCount + 1) EndIf EndIf EndFunc ;==>_FD_SET Func _FD_CLR($iSock, ByRef $tFd_set) Local $iSockBehind Local $iCount = DllStructGetData($tFd_set, "fd_count") For $i = 1 To $iCount If DllStructGetData($tFd_set, "fd_array", $i) = $iSock Then While ($i < $iCount) $iSockBehind = DllStructGetData($tFd_set, "fd_array", $i + 1) DllStructSetData($tFd_set, "fd_array", $iSockBehind, $i) $i += 1 WEnd DllStructSetData($tFd_set, "fd_count", $iCount - 1) ExitLoop EndIf Next EndFunc ;==>_FD_CLR Func _FD_ISSET($iSock, ByRef $tFd_set) Local $aRet = DllCall($hDLL_WS2_32, "int", "__WSAFDIsSet", "UINT", $iSock, "struct*", $tFd_set) Return $aRet[0] EndFunc ;==>_FD_ISSET Func _send($iSocket, ByRef $tBuffer, $iFlags = 0) Local $iLen = DllStructGetSize($tBuffer) Local $aRet = DllCall($hDLL_WS2_32, "int", "send", "int", $iSocket, "struct*", $tBuffer, "int", $iLen, "int", $iFlags) Return $aRet[0] EndFunc ;==>_send Func _sendto($iSocket, ByRef $tBuffer, ByRef $tTo, $iFlags = 0) Local $iLen = DllStructGetSize($tBuffer) Local $iLenTo = DllStructGetSize($tTo) Local $aRet = DllCall($hDLL_WS2_32, "int", "sendto", "int", $iSocket, "struct*", $tBuffer, "int", $iLen, "int", $iFlags, "struct*", $tTo, "int", $iLenTo) Return $aRet[0] EndFunc ;==>_sendto Func _recv($iSocket, ByRef $tBuffer, $iFlags = 0) Local $iLen = DllStructGetSize($tBuffer) Local $aRet = DllCall($hDLL_WS2_32, "int", "recv", "uint", $iSocket, "struct*", $tBuffer, "int", $iLen, "int", $iFlags) Return $aRet[0] EndFunc ;==>_recv Func _recvfrom($iSocket, ByRef $tBuffer, ByRef $tFrom, $iFlags = 0) Local $iLen = DllStructGetSize($tBuffer) Local $iLenFrom = DllStructGetSize($tFrom) Local $aRet = DllCall($hDLL_WS2_32, "int", "recvfrom", "uint", $iSocket, "struct*", $tBuffer, "int", $iLen, "int", $iFlags, "struct*", $tFrom, "int*", $iLenFrom) Return $aRet[0] EndFunc ;==>_recvfrom Func _inet_addr($sIP) Local $aRet = DllCall($hDLL_WS2_32, "ULONG", "inet_addr", "str", $sIP) Return $aRet[0] EndFunc ;==>_inet_addr Func _inet_ntoa($In_Addr) ;only IPv4 (deprecated) --> use _inet_ntop() instead Local $aRet = DllCall($hDLL_WS2_32, "str", "inet_ntoa", "long", $In_Addr) Return $aRet[0] EndFunc ;==>_inet_ntoa Func _inet_ntop($tIn_Addr, $iFamily = $AF_INET) ;IPv4 and IPv6 --> Vista and above Local $tBuffer = DllStructCreate("char[46]") ;16 for IPv4, 46 for IPv6 Local $aRet = DllCall($hDLL_WS2_32, "str", "inet_ntop", "int", $iFamily, "struct*", $tIn_Addr, "struct*", $tBuffer, "int", DllStructGetSize($tBuffer)) Return DllStructGetData($tBuffer, 1) EndFunc ;==>_inet_ntop Func _InetNtop($tIN_ADDR, $iFamily = $AF_INET) ;IPv4 and IPv6 --> Vista and above Local $tBuffer = DllStructCreate("wchar[46]") ;16 for IPv4, 46 for IPv6 Local $aRet = DllCall($hDLL_WS2_32, "wstr", "InetNtopW", "int", $iFamily, "struct*", $tIN_ADDR, "struct*", $tBuffer, "int", DllStructGetSize($tBuffer) / 2) Return DllStructGetData($tBuffer, 1) EndFunc ;==>_InetNtop Func _inet_pton($sIP, ByRef $tIN_ADDR, $iFamily = $AF_INET) ;IPv4 and IPv6 --> Vista and above Local $aRet = DllCall($hDLL_WS2_32, "int", "inet_pton", "int", $iFamily, "str", $sIP, "struct*", $tIN_ADDR) Switch $aRet[0] Case -1 Return SetError(1, _WSAGetLastError(), $aRet[0]) ; error Case 0 Return SetError(2, 0, $aRet[0]) ; wrong IP address EndSwitch Return $aRet[0] EndFunc ;==>_inet_pton Func _InetPton($sIP, ByRef $tIN_ADDR, $iFamily = $AF_INET) ;IPv4 and IPv6 --> Vista and above Local $aRet = DllCall($hDLL_WS2_32, "int", "InetPtonW", "int", $iFamily, "wstr", $sIP, "struct*", $tIN_ADDR) Switch $aRet[0] Case -1 Return SetError(1, _WSAGetLastError(), $aRet[0]) ; error Case 0 Return SetError(2, 0, $aRet[0]) ; wrong IP address EndSwitch Return $aRet[0] EndFunc ;==>_InetPton ;host byte order to network byte order (double) Func _htond($value) Local $aRet = DllCall($hDLL_WS2_32, "UINT64", "htond", "double", $value) Return $aRet[0] EndFunc ;==>_htond ;host byte order to network byte order (float) Func _htonf($value) Local $aRet = DllCall($hDLL_WS2_32, "UINT", "htonf", "float", $value) Return $aRet[0] EndFunc ;==>_htonf ;host byte order to network byte order (long) Func _htonl($value) Local $aRet = DllCall($hDLL_WS2_32, "ULONG", "htonl", "ULONG", $value) Return $aRet[0] EndFunc ;==>_htonl ;host byte order to network byte order (long long) Func _htonll($value) Local $aRet = DllCall($hDLL_WS2_32, "UINT64", "htonll", "UINT64", $value) Return $aRet[0] EndFunc ;==>_htonll ;host byte order to network byte order (short) Func _htons($iPort) Local $aRet = DllCall($hDLL_WS2_32, "USHORT", "htons", "USHORT", $iPort) Return $aRet[0] EndFunc ;==>_htons ;network byte order to host byte order (double) Func _ntohd($value) Local $aRet = DllCall($hDLL_WS2_32, "double", "ntohd", "UINT64", $value) Return $aRet[0] EndFunc ;==>_ntohd ;network byte order to host byte order (float) Func _ntohf($value) Local $aRet = DllCall($hDLL_WS2_32, "float", "ntohf", "UINT", $value) Return $aRet[0] EndFunc ;==>_ntohf ;network byte order to host byte order (long) Func _ntohl($iNetLong) Local $aRet = DllCall($hDLL_WS2_32, "ULONG", "ntohl", "ULONG", $iNetLong) Return $aRet[0] EndFunc ;==>_ntohl ;network byte order to host byte order (long long) Func _ntohll($value) Local $aRet = DllCall($hDLL_WS2_32, "UINT64", "ntohll", "UINT64", $value) Return $aRet[0] EndFunc ;==>_ntohll ;network byte order to host byte order (short) Func _ntohs($iNetShort) Local $aRet = DllCall($hDLL_WS2_32, "USHORT", "ntohs", "USHORT", $iNetShort) Return $aRet[0] EndFunc ;==>_ntohs Func _WSAGetLastError() Local $aRet = DllCall($hDLL_WS2_32, "int", "WSAGetLastError") Return $aRet[0] EndFunc ;==>_WSAGetLastError Func _GetLocalPort($iSocket) ;funkey Local $tIn_Addr = DllStructCreate($tagIN_ADDR) Local $tName = DllStructCreate($tagSockAddr_In) DllStructSetData($tName, "sin_addr", DllStructGetPtr($tIn_Addr)) Local $aRes = DllCall($hDLL_WS2_32, "int", "getsockname", "uint", $iSocket, "struct*", $tName, "int*", 512) Local $aRes2 = DllCall($hDLL_WS2_32, "ushort", "ntohs", "ushort", DllStructGetData($tName, 2)) Return $aRes2[0] EndFunc ;==>_GetLocalPort Func _getsockname($iSocket) Local $tIn_Addr = DllStructCreate($tagIN_ADDR) Local $tName = DllStructCreate($tagSockAddr_In) DllStructSetData($tName, "sin_addr", DllStructGetPtr($tIn_Addr)) Local $aRes[2] Local $aRet = DllCall($hDLL_WS2_32, "int", "getsockname", "uint", $iSocket, "struct*", $tName, "int*", 512) If Not @error And $aRet[0] = 0 Then $aRes[0] = _inet_ntoa(DllStructGetData($tName, "sin_addr")) ;IP address $aRes[1] = _ntohs(DllStructGetData($tName, "sin_port")) ;IP port ;~ $aRes[2] = DllStructGetData($tName, "sin_family") ; always AF_INET !? Return $aRes EndIf Return SetError(1, 0, 0) EndFunc ;==>_getsockname ;More than TCPNameToIP(), returns an array of IP addresses ;IPv6 and IPv4 addresses if wanted Func _getaddrinfo($sNodeName = "..localmachine", $AF = $AF_UNSPEC) Local $pResult, $tAddrInfo, $sIP, $tName Local $aRet = DllCall($hDLL_WS2_32, "int", "getaddrinfo", "str", $sNodeName, "ptr", 0, "ptr", 0, "ptr*", 0) $pResult = $aRet[4] Do $tAddrInfo = DllStructCreate($tagAddrInfo, $pResult) Switch DllStructGetData($tAddrInfo, "ai_family") Case $AF_INET If $AF = $AF_UNSPEC Or $AF = $AF_INET Then $tName = DllStructCreate($tagSockAddr_In, DllStructGetData($tAddrInfo, "ai_addr")) $sIP &= _inet_ntoa(DllStructGetData($tName, "sin_addr")) & ";" EndIf Case $AF_INET6 If $AF = $AF_UNSPEC Or $AF = $AF_INET6 Then $tName = DllStructCreate($tagSockAddr_In6, DllStructGetData($tAddrInfo, "ai_addr")) $sIP &= _inet_ntop(DllStructGetPtr($tName, "sin6_addr"), $AF_INET6) & ";" EndIf EndSwitch $pResult = DllStructGetData($tAddrInfo, "ai_next") Until $pResult = 0 _freeaddrinfo($aRet[4]) Return StringSplit(StringTrimRight($sIP, 1), ";", 2) EndFunc ;==>_getaddrinfo ;More than TCPNameToIP(), returns an array of IP addresses ;IPv4 addresses only Func _getaddrinfoIPv4($sNodeName = "..localmachine") Local $pResult, $tAddrInfo, $sIP Local $aRet = DllCall($hDLL_WS2_32, "int", "getaddrinfo", "str", $sNodeName, "ptr", 0, "ptr", 0, "ptr*", 0) $pResult = $aRet[4] Do $tAddrInfo = DllStructCreate($tagAddrInfo, $pResult) Local $tName = DllStructCreate($tagSockAddr_In, DllStructGetData($tAddrInfo, "ai_addr")) $sIP &= _inet_ntoa(DllStructGetData($tName, "sin_addr")) & ";" $pResult = DllStructGetData($tAddrInfo, "ai_next") Until $pResult = 0 _freeaddrinfo($aRet[4]) Return StringSplit(StringTrimRight($sIP, 1), ";", 2) EndFunc ;==>_getaddrinfoIPv4 Func _freeaddrinfo($pAddrInfo) DllCall($hDLL_WS2_32, "none", "freeaddrinfo", "ptr", $pAddrInfo) EndFunc ;==>_freeaddrinfo Func _ioctlsocket($iSocket, $iCmd, $iArg) Local $aRet = DllCall($hDLL_WS2_32, "int", "ioctlsocket", "uint", $iSocket, "long", $iCmd, "ULONG*", $iArg) Return $aRet[0] EndFunc ;==>_ioctlsocket Func _TCPConnectEx($sIP, $iRemPort, $iRcvTimeOut = 0, $iSendTimeOut = 0, $iLocPort = -1) Local $tTimeVal, $iSockError, $iBind, $iConnect, $aIP[1] Local $iSock = _socket($AF_INET, $SOCK_STREAM, $IPPROTO_TCP) If $iRcvTimeOut > 0 Then $tTimeVal = DllStructCreate("DWORD timeout") DllStructSetData($tTimeVal, "timeout", $iRcvTimeOut) $iSockError = _setsockopt($iSock, $SOL_SOCKET, $SO_RCVTIMEO, $tTimeVal) If $iSockError Then _closesocket($iSock) Return SetError(1, _WSAGetLastError(), 0) EndIf EndIf If $iSendTimeOut > 0 Then $tTimeVal = DllStructCreate("DWORD timeout") DllStructSetData($tTimeVal, "timeout", $iSendTimeOut) $iSockError = _setsockopt($iSock, $SOL_SOCKET, $SO_SNDTIMEO, $tTimeVal) If $iSockError Then _closesocket($iSock) Return SetError(2, _WSAGetLastError(), 0) EndIf EndIf If $iLocPort >= 0 Then $iBind = _bind($iSock, @IPAddress1, $iLocPort) If $iBind Then _closesocket($iSock) Return SetError(3, _WSAGetLastError(), 0) EndIf EndIf If _IsValidIP($sIP) Then $aIP[0] = $sIP Else $aIP = _getaddrinfo($sIP) EndIf $iConnect = _connect($iSock, $aIP[0], $iRemPort) If $iConnect Then _closesocket($iSock) Return SetError(4, _WSAGetLastError(), 0) EndIf Return $iSock EndFunc ;==>_TCPConnectEx Func _TCPConnect($IP, $Port, $Timeout = 0, $LocalPort = Default, $LocalIP = Default) Local $iSocket, $iBind, $iResult, $tTimeout, $tWrite, $iLastError, $aIP[1] $iSocket = _socket() If $iSocket = $SOCKET_ERROR Then Return SetError(1, _WSAGetLastError(), $SOCKET_ERROR) If $LocalIP = Default Then $LocalIP = @IPAddress1 If $LocalPort <> Default Then $iBind = _bind($iSocket, $LocalIP, $LocalPort) If $iBind Then $iLastError = _WSAGetLastError() _closesocket($iSocket) Return SetError(2, $iLastError, $SOCKET_ERROR) EndIf EndIf If $Timeout > 0 Then $iResult = _ioctlsocket($iSocket, $FIONBIO, 1) ; nonblocking socket If $iResult <> 0 Then $iLastError = _WSAGetLastError() _closesocket($iSocket) Return SetError(3, $iLastError, $SOCKET_ERROR) ;ioctlsocket() error EndIf EndIf If _IsValidIP($IP) Then $aIP[0] = $IP Else $aIP = _getaddrinfo($IP) EndIf $iResult = _connect($iSocket, $aIP[0], $Port) If $iResult = $SOCKET_ERROR Then $iLastError = _WSAGetLastError() If $iLastError = $WSAEWOULDBLOCK Then $tWrite = DllStructCreate($tagFd_set) _FD_SET($iSocket, $tWrite) $iResult = _selectEx(0, DllStructGetPtr($tWrite), 0, $Timeout) If $iResult = $SOCKET_ERROR Then $iLastError = _WSAGetLastError() _closesocket($iSocket) Return SetError(4, $iLastError, $SOCKET_ERROR) ;select error ElseIf $iResult = 0 Then $iLastError = $WSAETIMEDOUT _closesocket($iSocket) Return SetError(5, $iLastError, $SOCKET_ERROR) ;timeout Else If Not _FD_ISSET($iSocket, $tWrite) Then $iLastError = _WSAGetLastError() _closesocket($iSocket) Return SetError(6, $iLastError, $SOCKET_ERROR) ; what the hell happened here EndIf EndIf Else _closesocket($iSocket) If $iLastError = 0 Then $iLastError = $WSAECONNREFUSED Return SetError(7, $iLastError, $SOCKET_ERROR) ; another WSA error EndIf EndIf If $Timeout > 0 Then $iResult = _ioctlsocket($iSocket, $FIONBIO, 0) ; blocking socket again If $iResult <> 0 Then $iLastError = _WSAGetLastError() _closesocket($iSocket) Return SetError(8, $iLastError, $SOCKET_ERROR) ;ioctlsocket() error EndIf EndIf Return $iSocket EndFunc ;==>_TCPConnect Func _TCPRecv($iSocket, $iBufLen = 2048, $iTimeout = 2000) Local $iRetSelect, $iRetRecv;, $sRet = "", $sTemp = "" Local $tReadFds = DllStructCreate($tagFd_set) Local $tReadFds_Copy = DllStructCreate($tagFd_set) Local $tBuffer = DllStructCreate("byte buffer[" & $iBufLen & "]") _FD_ZERO($tReadFds) _FD_SET($iSocket, $tReadFds) While 1 DllCall('ntdll.dll', 'none', 'RtlMoveMemory', 'struct*', $tReadFds_Copy, 'struct*', $tReadFds, 'ULONG_PTR', DllStructGetSize($tReadFds)) $iRetSelect = _select($tReadFds_Copy, $iTimeout) Switch $iRetSelect Case $SOCKET_ERROR Return SetError(1, _WSAGetLastError(), "") Case 0 ; Timeout (no or no more data) Return SetExtended(0, "") Case Else If _FD_ISSET($iSocket, $tReadFds_Copy) Then DllCall('ntdll.dll', 'none', 'RtlZeroMemory', 'struct*', $tBuffer, 'ULONG_PTR', DllStructGetSize($tBuffer)) $iRetRecv = _recv($iSocket, $tBuffer) Switch $iRetRecv Case $SOCKET_ERROR Return SetError(2, _WSAGetLastError(), "") Case 0 ; Connection closed or no more data Return SetError(3, 0, "") Case Else Return SetExtended($iRetRecv, DllStructGetData($tBuffer, 1)) EndSwitch Else ExitLoop EndIf EndSwitch WEnd EndFunc ;==>_TCPRecv Func _IsValidIP($sIP) Local $aIP = StringSplit($sIP, ".") If $aIP[0] <> 4 Then Return SetError(1, 0, 0) For $x = 1 To $aIP[0] If $aIP[$x] < 0 Or $aIP[$x] > 255 Then Return SetError(2, $x, 0) Next Return 1 EndFunc ;==>_IsValidIP Func _SendARP($DestIP) Local $DestAddress = _inet_addr($DestIP) If $DestAddress = $INADDR_NONE Then Return SetError(1, 0, "INVALID_IP_ADDRESS") Local $tMacAddr = DllStructCreate("BYTE[8]") Local $aRet = DllCall("Iphlpapi.dll", "DWORD", "SendARP", "ULONG", $DestAddress, "ULONG", 0, "struct*", $tMacAddr, "ULONG*", 8) Local $sMAC = "" Switch $aRet[0] Case 0 For $i = 1 To $aRet[4] - 1 $sMAC &= Hex(DllStructGetData($tMacAddr, 1, $i), 2) & "-" Next $sMAC &= Hex(DllStructGetData($tMacAddr, 1, $i), 2) Return $sMAC Case 31 Return SetError(2, $aRet[0], "ERROR_GEN_FAILURE") Case 87 Return SetError(3, $aRet[0], "ERROR_INVALID_PARAMETER") Case 1784 Return SetError(4, $aRet[0], "ERROR_INVALID_USER_BUFFER") Case 67 Return SetError(5, $aRet[0], "ERROR_BAD_NET_NAME") Case 1168 Return SetError(6, $aRet[0], "ERROR_NOT_FOUND") EndSwitch EndFunc ;==>_SendARP