Getting ip address from udp packet. - Unix

This is a discussion on Getting ip address from udp packet. - Unix ; So I'm trying to get an IP address from an incoming udp packet but having problems. I seem to just get a static IP from any machine sending it. I think I'm doing the conversion wrong or something. One other ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Getting ip address from udp packet.

  1. Getting ip address from udp packet.

    So I'm trying to get an IP address from an incoming udp packet but
    having problems. I seem to just get a static IP from any machine sending
    it. I think I'm doing the conversion wrong or something.

    One other problem I'm having is I can't seem to cast (struct sockaddr_in
    *) within bind or recvfrom without getting a warning from gcc. I can
    bind (struct sockaddr *) okay though. I'm worried though binding just
    (struct sockaddr *) will lose the ip address member since it's not in
    the generic sockaddr. Where as (struct sockaddr_in*) has the ip address
    member in it. Which is making me think maybe that's the reason I"m not
    getting the correct ip. My code it below.


    int main()
    {
    int lnd;
    struct sockaddr_in servaddr, cliaddr;
    memset(&servaddr, 0, sizeof(servaddr));

    lnd = socket(AF_INET, SOCK_DGRAM, 0);

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(PORT);

    bind(lnd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    listen(lnd, 1);

    while(1)
    {
    char mesg[1024];
    char ip[1024];
    socklen_t len;
    uint port;

    recvfrom(lnd, mesg, 1024, 0, (struct sockaddr *) &cliaddr, &len);
    inet_ntop(AF_INET, &(struct sockaddr*)&cliaddr)->sin_addr.s_addr,
    ip, sizeof(ip));


    printf("%s\n%s\n", mesg, ip);
    }
    }

    Thanks for your help!

    Ryan Knopp

  2. Re: Getting ip address from udp packet.

    Ryan Knopp wrote:
    > So I'm trying to get an IP address from an incoming udp packet but
    > having problems. I seem to just get a static IP from any machine sending
    > it. I think I'm doing the conversion wrong or something.




    > while(1)
    > {
    > char mesg[1024];
    > char ip[1024];
    > socklen_t len;
    > uint port;
    >
    > recvfrom(lnd, mesg, 1024, 0, (struct sockaddr *) &cliaddr, &len);


    Check your manpage for recvfrom(). "len" is a value-result parameter
    that needs to be initialized to sizeof(cliaddr).

    Also, you should nearly always verify the return codes.

    Chris

  3. Re: Getting ip address from udp packet.

    Chris Friesen wrote:
    > Ryan Knopp wrote:
    >> So I'm trying to get an IP address from an incoming udp packet but
    >> having problems. I seem to just get a static IP from any machine
    >> sending it. I think I'm doing the conversion wrong or something.

    >
    >
    >
    >> while(1)
    >> {
    >> char mesg[1024];
    >> char ip[1024];
    >> socklen_t len;
    >> uint port;
    >>
    >> recvfrom(lnd, mesg, 1024, 0, (struct sockaddr *) &cliaddr, &len);

    >
    > Check your manpage for recvfrom(). "len" is a value-result parameter
    > that needs to be initialized to sizeof(cliaddr).
    >
    > Also, you should nearly always verify the return codes.
    >
    > Chris


    Wow, I didn't notice that I had to put the size. Thought it gave me
    back the size of sockaddr. That fixed it, Thanks.

    I also do have return codes I left them out for simplicity of showing
    the code. I was getting back the correct lengh from recvfrom.

    Thanks Chris
    Ryan

  4. Re: Getting ip address from udp packet.

    Ryan Knopp wrote:
    > Chris Friesen wrote:


    >> Check your manpage for recvfrom(). "len" is a value-result parameter
    >> that needs to be initialized to sizeof(cliaddr).


    > Wow, I didn't notice that I had to put the size. Thought it gave me
    > back the size of sockaddr. That fixed it, Thanks.


    It does give you back the size of sockaddr. But you have to tell the
    kernel how much space you've got first, so it doesn't trample random
    memory by copying a struct that is overly large into the buffer you
    provided.

    Chris

+ Reply to Thread