Rurorita Posted May 11, 2021 Share Posted May 11, 2021 (edited) Hi everyone, so im working on a ipv4 and ipv6 server - client communication UDF and with the focus of making it really performant and somewhat secure. Besides that i also addin a couple of more features for convenience. For example a relay and a proxy. I tested both quite alot and think that it is somewhat fast alread. My provider can deliver me a maximum of 200 mbps and the proxy can fully utilize it. Spoiler But while testing the relay and proxy i encountered an issue with connecting to some IP's. I wont post the code of either of these as the issue seems to be easy to describe on an example. expandcollapse popupGlobal $__net_hWs2_32 = -1 Local $hSocket = __netcode_TCPConnect("108.174.11.65", 443) MsgBox(0, "", $hSocket) Func __netcode_TCPConnect($sIP, $sPort, $nAdressFamily = 2) if $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll') ; create socket Local $arGen = DllCall($__net_hWs2_32, "uint", "socket", "int", $nAdressFamily, "int", 1, "int", 6) Local $hSocket = $arGen[0] ; create ip and port struct ; ~ todo IPv6 support here Local $tDataBuffer = DllStructCreate("short; ushort; uint; char[8]") DllStructSetData($tDataBuffer, 1, 2) $arGen = DllCall($__net_hWs2_32, "ushort", "htons", "ushort", $sPort) DllStructSetData($tDataBuffer, 2, $arGen[0]) $arGen = DllCall($__net_hWs2_32, "uint", "inet_addr", "str", $sIP) DllStructSetData($tDataBuffer, 3, $arGen[0]) ; connect $arGen = DllCall($__net_hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($tDataBuffer), "int", DllStructGetSize($tDataBuffer)) if $arGen[0] <> 0 Then Return SetError(__netcode_WSAGetLastError(), 0, -1) Return $hSocket EndFunc Func __netcode_WSAGetLastError() If $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll') Local $iRet = DllCall($__net_hWs2_32, "int", "WSAGetLastError") If @error Then SetExtended(1) Return 0 EndIf Return $iRet[0] EndFunc The IP belongs to LinkedIn. I can clearly connect to it in the browser. But not in Autoit. I researched the problem but dont find an explanation of the issue. I recoded all mayor TCP functions already, so i think i need todo some changes there, but i just dont seem to find which. Maybe someone has some ideas? Edited May 11, 2021 by Rurorita Amateur Coder - UDF's _storageS-UDF , _netcode-UDF (_netcode_Core-UDF, _netcode_AddonCore-UDF, _netcode_Proxy-UDF, _netcode_Relay-UDF, _netcode_Router-UDF) Link to comment Share on other sites More sharing options...
TheXman Posted May 11, 2021 Share Posted May 11, 2021 (edited) Are you saying that the example, as provided above, works with other IP addresses? If so, I seriously doubt that. 😉 Your example does not initialize Winsock before trying to make the connection. If you would've added error checking after the line that executes your __netcode_TCPConnect() function, you would've seen that you received a 10093 Winsock error. That error basically means that Winsock was not initialized. If you run the modified example below, as is, you will see the error. If you uncomment the TCPStartup() and TCPShutdown functions and run it again, it returns a socket handle -- at least it does for me. TCPStartup() is equivalent to calling the wsastartup Winsock API. expandcollapse popupGlobal $__net_hWs2_32 = -1 ;~ TCPStartup() ;added by TheXman Global $hSocket = __netcode_TCPConnect("108.174.11.65", 443) If @error Then Exit MsgBox(0, "ERROR", "__netcode_TCPConnect failed with @error = " & @error) ;added by TheXman MsgBox(0, "", $hSocket) ;~ TCPShutdown() ;added by TheXman Func __netcode_TCPConnect($sIP, $sPort, $nAdressFamily = 2) if $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll') ; create socket Local $arGen = DllCall($__net_hWs2_32, "uint", "socket", "int", $nAdressFamily, "int", 1, "int", 6) Local $hSocket = $arGen[0] ; create ip and port struct ; ~ todo IPv6 support here Local $tDataBuffer = DllStructCreate("short; ushort; uint; char[8]") DllStructSetData($tDataBuffer, 1, 2) $arGen = DllCall($__net_hWs2_32, "ushort", "htons", "ushort", $sPort) DllStructSetData($tDataBuffer, 2, $arGen[0]) $arGen = DllCall($__net_hWs2_32, "uint", "inet_addr", "str", $sIP) DllStructSetData($tDataBuffer, 3, $arGen[0]) ; connect $arGen = DllCall($__net_hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($tDataBuffer), "int", DllStructGetSize($tDataBuffer)) if $arGen[0] <> 0 Then Return SetError(__netcode_WSAGetLastError(), 0, -1) Return $hSocket EndFunc Func __netcode_WSAGetLastError() If $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll') Local $iRet = DllCall($__net_hWs2_32, "int", "WSAGetLastError") If @error Then SetExtended(1) Return 0 EndIf Return $iRet[0] EndFunc Edited May 11, 2021 by TheXman Corrected typos in the text of the reply. Rurorita 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Rurorita Posted May 12, 2021 Author Share Posted May 12, 2021 I looked into the proxy and i actually never called TCPStartup once. And id never thought id need to because it was working on most sites even if it doesnt in this example. But after initing tcp now, yet sites load up that never loaded before. It is weird to me. But thanks this solution was much easier then i thought it would be. Now i have to make all sockets async so that autoit stops hanging on connection attempts that take longer and to make the proxy less noticable. Amateur Coder - UDF's _storageS-UDF , _netcode-UDF (_netcode_Core-UDF, _netcode_AddonCore-UDF, _netcode_Proxy-UDF, _netcode_Relay-UDF, _netcode_Router-UDF) Link to comment Share on other sites More sharing options...
JockoDundee Posted May 12, 2021 Share Posted May 12, 2021 39 minutes ago, Rurorita said: And id never thought id need to because it was working on most sites even if it doesnt in this example. Do you have an example of a public IP that works without TCPStartup() ? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Rurorita Posted May 12, 2021 Author Share Posted May 12, 2021 12 minutes ago, JockoDundee said: Do you have an example of a public IP that works without TCPStartup() ? No i couldnt reproduce it in this example Amateur Coder - UDF's _storageS-UDF , _netcode-UDF (_netcode_Core-UDF, _netcode_AddonCore-UDF, _netcode_Proxy-UDF, _netcode_Relay-UDF, _netcode_Router-UDF) Link to comment Share on other sites More sharing options...
JockoDundee Posted May 12, 2021 Share Posted May 12, 2021 2 hours ago, Rurorita said: No i couldnt reproduce it in this example So to be clear you are saying that, now when you try to connect to any of the previously working IPs with the original code you posted (that did not include TCPStartup()), it gives an error? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now