Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ IP long conversion problem

  • 04-08-2008 7:37pm
    #1
    Registered Users, Registered Users 2 Posts: 250 ✭✭


    Hi

    I am trying to extract the long local and peer IP address from a socket connected to a remote peer. Below is my test code:

    [PHP]
    struct sockaddr_in addr_local={0};
    int nlSize = sizeof(struct sockaddr);
    int lres = getsockname(s, (struct sockaddr *) &addr_local, &nlSize);

    in_addr lin = addr_local.sin_addr;

    struct sockaddr_in addr_peer={0};
    int npSize = sizeof(struct sockaddr);
    int nres = getpeername(s, (struct sockaddr *) &addr_peer, &npSize);

    in_addr pin = addr_peer.sin_addr;

    u_long lip = lin.S_un.S_addr;
    u_long pip = pin.S_un.S_addr;

    u_short lpo = ntohs(addr_local.sin_port);
    u_short ppo = ntohs(addr_peer.sin_port);

    dbgprint("lip=(%d) %d:%d, pip=(%d) %d:%d",
    lin.S_un.S_addr,lip, lpo, pin.S_un.S_addr, pip, ppo);[/PHP]

    My problem is that, if I surf to say Microsoft.com, and look at all the socket connections my local machine is making to the remote server, some of the long ip's returns negative numbers. I don't understand why this happen. I thought it's a conversion problem from S_un.S_addr to u_long, but if I just do a dbgprint on S_un.S_addr, the it also returns a "negative" or signed ip-long address.

    Can somebody explain if and what I am doing wrong?

    TIA


Comments

  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    [php]
    int s;
    struct sockaddr_in server, addr;
    socklen_t len;

    // make a socket
    s = socket(PF_INET, SOCK_STREAM, 0);

    // connect to a server
    server.sin_family = AF_INET;
    inet_aton("63.161.169.137", &server.sin_addr);
    server.sin_port = htons(80);

    connect(s, (struct sockaddr*)&server, sizeof server);

    // get the peer name
    // we know we just connected to 63.161.169.137:80, so this should print:
    // Peer IP address: 63.161.169.137
    // Peer port : 80

    len = sizeof addr;
    getpeername(s, (struct sockaddr*)&addr, &len);
    printf("Peer IP address: %s\n", inet_ntoa(addr.sin_addr));
    printf("Peer port : %d\n", ntohs(addr.sin_port));
    [/php]


  • Registered Users, Registered Users 2 Posts: 250 ✭✭ikoonman


    thanks for the response.

    i think i've found the problem tho, the limitation seems to be in dbgprint. it "wraps" unsigned longs into signed longs. instead of using "%d" for the u_long, i used "%x", to monitor the hex values. when i manually convert the hex back to decimal the ip is the correct long value.

    can anyone confirm this behaviour?


  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    have you tried using %ld for long ints?


  • Registered Users, Registered Users 2 Posts: 250 ✭✭ikoonman


    that did the trick, thank you.

    wasn't dbgprint's fault at all! .... was my lack of knowledge ;)


  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    Your welcome :)


  • Advertisement
Advertisement