Joelbu
Joelbu

Reputation: 92

InetPton() implies "127.0.0.1" is not a valid IP. Why?

I am trying to create a C program that opens a connection via TCP, but when calling InetPton with "127.0.0.1" as the IP, it returns 0, meaning the passed string is not a valid IP address. Since InetPton requires a string formatted like this : ddd.ddd.ddd.ddd, I have also tried passing 127.000.000.001, which didn't work either.

The program is meant to run on x64 Windows.

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <winerror.h>

int initWinsock() {
    WSADATA wsaData;
    int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (result != 0) {
        char buffer[1024];
        sprintf_s(buffer, sizeof(buffer), "WSAStartup fehlgeschlagen: %d\n", result);
        OutputDebugStringA(buffer);
        return 1;
    }
    OutputDebugStringA("[1/3] WSAStartup successful\n");
    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        if (initWinsock() != 0) {
            return 1;
        }
    
        const char* serverAddress = "127.0.0.1";
        int serverPort = 64263;
        char errorMessage[256];
    
        SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (clientSocket == INVALID_SOCKET) {
            sprintf_s(errorMessage, sizeof(errorMessage), "Socket-Error: %d\n", WSAGetLastError());
            OutputDebugStringA(errorMessage);
            WSACleanup();
            return 1;
        }
        else {
            OutputDebugStringA("[2/3] Socket created successfully!\n");
        }
    
        //Server-Adress
        struct sockaddr_in serverAddr;
        ZeroMemory(&serverAddr, sizeof(serverAddr));
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(serverPort);
    
        int result = InetPton(AF_INET, serverAddress, &serverAddr.sin_addr);
        if (result == -1) {
            char errorMessage[256];
            sprintf_s(errorMessage, sizeof(errorMessage), "InetPton-Error: %d\n", WSAGetLastError());
            OutputDebugStringA(errorMessage);
            closesocket(clientSocket);
            WSACleanup();
            return 1;
        }
        else if (result == 0) {
            sprintf_s(errorMessage, sizeof(errorMessage), "IP could not be parsed!\nLast WSAError: %d\n", WSAGetLastError());
            OutputDebugStringA(errorMessage);
            closesocket(clientSocket);
            WSACleanup();
            return 1;
        }
        else if(result == 1) {
            OutputDebugStringA("[3/3] Server defined successfully\n");
        }
    }

Upvotes: 1

Views: 91

Answers (1)

Joelbu
Joelbu

Reputation: 92

Turns out InetPton expects a UTF-16 string by default, so I just called InetPtonA, which expects a "normal" ASCII char*, so instead of calling

int result = InetPton(AF_INET, serverAddress, &serverAddr.sin_addr.S_un.S_addr); which redirects to InetPtonW(),

I used int result = InetPtonA(AF_INET, serverAddress, &serverAddr.sin_addr.S_un.S_addr);.

Upvotes: 0

Related Questions