diff --git a/src/win32shim.c b/src/win32shim.c new file mode 100644 index 0000000..131d387 --- /dev/null +++ b/src/win32shim.c @@ -0,0 +1,40 @@ +/* Custom InetPton implementation */ + +#include +#include + +int __stdcall +InetPton(int Family, + const char *pszAddrString, + void *pAddrBuf) +{ + IN_ADDR *addr = pAddrBuf; + unsigned long ulTmp; + + if (Family != AF_INET) { + // Oops! We don't support that addr family + WSASetLastError(WSAEAFNOSUPPORT); + return -1; + } + + if (!pAddrBuf || !pszAddrString || + pszAddrString[0] == '\0') { + WSASetLastError(WSAEFAULT); + return -1; + } + + // Technically we could just put the return val directly + // into addr->S_un.S_addr, but I don't like writing bad + // data unless absolutely necessary. So store it in a temp + // variable, first. + ulTmp = inet_addr(pszAddrString); + if (ulTmp == INADDR_NONE) { + // Not an IP address + WSASetLastError(WSAEFAULT); + return -1; + } + + // S_un.S_addr is an unsigned long. Easy! + addr->S_un.S_addr = ulTmp; + return 0; +}