cso2-launcher icon indicating copy to clipboard operation
cso2-launcher copied to clipboard

Resolve master server IP through DNS

Open gnexia2013 opened this issue 5 years ago • 2 comments

Server creates domain name, client connects domain name

gnexia2013 avatar Mar 12 '19 01:03 gnexia2013

uint32_t ipAddr[4];
if (sscanf(info.szIpAddress.c_str(), "%d.%d.%d.%d", & ipAddr[0], & ipAddr[1], & ipAddr[2], & ipAddr[3]) == 4) {
    if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255)
        return; // A vaild ip address, good to go.
}

// Not a ip address, resolve dns name
auto pHost = gethostbyname(info.szIpAddress.c_str());

if (pHost == NULL) {
    auto error = WSAGetLastError();
    switch (error) {
        case WSAHOST_NOT_FOUND:
            printf("Host %s not found\n", info.szIpAddress.c_str());
            break;
        case WSANO_DATA:
            printf("No data record found\n");
            break;
        default:
            printf("Function failed with error: %ld\n", error);
            break;
    }

    info.szIpAddress = MASTER_SERVER_ADDRESS;
} else {
    int i = 0;
    if (pHost -> h_addrtype == AF_INET) {
        in_addr addr;
        while (pHost -> h_addr_list[i] != 0) {
            addr.s_addr = * (u_long *)pHost -> h_addr_list[i++];
            info.szIpAddress = inet_ntoa(addr);

            return;
        }
    } else {
        printf("%s is not a vaild internet master server", info.szIpAddress.c_str());
        info.szIpAddress = MASTER_SERVER_ADDRESS;
    }
}

This should working, needs test out @Ochii @gnexia2013

GEEKiDoS avatar May 17 '19 15:05 GEEKiDoS

uint32_t ipAddr[4];
if (sscanf(info.szIpAddress.c_str(), "%d.%d.%d.%d", & ipAddr[0], & ipAddr[1], & ipAddr[2], & ipAddr[3]) == 4) {
    if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255)
        return; // A vaild ip address, good to go.
}

// Not a ip address, resolve dns name
auto pHost = gethostbyname(info.szIpAddress.c_str());

if (pHost == NULL) {
    auto error = WSAGetLastError();
    switch (error) {
        case WSAHOST_NOT_FOUND:
            printf("Host %s not found\n", info.szIpAddress.c_str());
            break;
        case WSANO_DATA:
            printf("No data record found\n");
            break;
        default:
            printf("Function failed with error: %ld\n", error);
            break;
    }

    info.szIpAddress = MASTER_SERVER_ADDRESS;
} else {
    int i = 0;
    if (pHost -> h_addrtype == AF_INET) {
        in_addr addr;
        while (pHost -> h_addr_list[i] != 0) {
            addr.s_addr = * (u_long *)pHost -> h_addr_list[i++];
            info.szIpAddress = inet_ntoa(addr);

            return;
        }
    } else {
        printf("%s is not a vaild internet master server", info.szIpAddress.c_str());
        info.szIpAddress = MASTER_SERVER_ADDRESS;
    }
}

This should working, needs test out @Ochii @gnexia2013

Good job, do you want to open a pull request?

EDIT: I haven't tested it yet

L-Leite avatar May 17 '19 17:05 L-Leite