Search code examples
csocketsudprecvfrom

Why does recvfrom store an ip address of 0.0.0.0 on UDP message?


I am reading the stored ip address of 0.0.0.0 in the 'from' argument of recvfrom() and I am not sure why. The message comes through just fine and there is some information stored in 'fromSockAddrIn'.

code:

int UDPSock = 0;                    //response from creating sock
int bindRes = 0;                    //response from binding
uint8_t *errCount = calloc(1, sizeof(uint8_t*));    //allocate memory for counting errors
size_t recvBytes = 0;               //n of bytes received from 'recvfrom'
void * buff[MAX_MSG_SIZE] = {0};    //message storage from 'recvfrom'
size_t len = sizeof(buff);          //size of buffer
int afFamily = AF_INET;             
char fromIp[INET_ADDRSTRLEN] = {0};

struct sockaddr_in mySockAddrIn;                //Socket address, internet style
memset(&mySockAddrIn, 0, sizeof(mySockAddrIn)); //allocate memory for socket address
mySockAddrIn.sin_family = afFamily;             //save address family
mySockAddrIn.sin_port = htons(8004);            //convert numeric port number to network byte-order

struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn)); //Socket address, Internet style
socklen_t fromLen = sizeof(fromSockAddrIn);                             //get size of socket address

*errCount = 0;
do{         //get a descripter referencing the socket while checking for errors
    UDPSock = socket(afFamily, SOCK_DGRAM, IPPROTO_UDP);
    Check_Print_Error(UDPSock, __func__, "socket", errCount);
}while(UDPSock == -1);

*errCount = 0;
do{         //Bind while checking for errors
    bindRes = bind(UDPSock, (struct sockaddr *) &mySockAddrIn, sizeof(mySockAddrIn));
    Check_Print_Error(bindRes, __func__, "bind", errCount);
}while(bindRes == -1);

do{         //block until a message is received while checking for errors
    fromLen = sizeof(fromSockAddrIn);
    recvBytes = recvfrom(UDPSock, buff, len, 0,(struct sockaddr *) fromSockAddrIn, &fromLen);
    Check_Print_Error(recvBytes, __func__, "recvfrom", errCount);
    printf("received %d bytes on UDP: %s ", recvBytes, buff);

    inet_ntop(afFamily, &fromSockAddrIn->sin_addr, fromIp, sizeof(fromIp));
    printf("from ip: %s \n", fromIp);
}while(recvBytes > 0);

Check_Print_Error:

void Check_Print_Error(int errNum, const char *func1, char *func2, uint8_t *errCount){
if(errNum == -1){
    errCount += 1;
    fprintf(stderr, "%s, %s error: (%d) %s \n", func1, func2, errno, strerror(errno));
}
if(*errCount >= 10){
    fprintf(stderr, "%s tried %s too many times (%u)\nExiting...", func1, func2, *errCount);
    fflush(stdout);
    fflush(stderr);
    exit(EXIT_FAILURE);
}

return;
}

python program to send message:

import socket
import time
toIPAddr = "192.168.202.128"
port = 8004
message = "ping from python"

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
toAddr = (toIPAddr, port)
print "toAddr:", toAddr

for x in range(0,6):
    print "sending...'",message,"' size of", len(message)
    sock.sendto(message , toAddr)
    time.sleep(1)

my output:

received 16 bytes on UDP: ping from python from ip: 0.0.0.0 

Thanks for any help.


Solution

  • Your use of sizeof(fromSockAddrIn) is wrong everywhere in this code

    struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn));
    

    This just allocates room for a pointer. You need to get the size of a struct sockardd_in, so do

    struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(*fromSockAddrIn));
    

    Similar when you calculate fromLen = sizeof(fromSockAddrIn);, that should be fromLen = sizeof(*fromSockAddrIn);

    It seems you shouldn't need to dynamically allocate your fromSockAddrIn variable. If you just declare it as struct sockaddr_in fromSockAddrIn;, you can use sizeof(mySockAddrIn) though.