LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Cannot receive UDP packets even on local loopback (https://www.linuxquestions.org/questions/linux-networking-3/cannot-receive-udp-packets-even-on-local-loopback-4175539962/)

BobInWonderland 04-17-2015 01:52 AM

Cannot receive UDP packets even on local loopback
 
This question is not specific to the OpenWRT platform, so please read on even if you're not familiar with OpenWRT.

I compiled a simple C UDP server and client for the OpenWrt router. They work as expected when on my pc. The client, if on the router, seems to successfully send the packets (port 6115) since my pc acknowledges them if send them to the pc (with the same server code). But the server does not receive them if on the router, whether the client is run on the router or on the pc.

On the router, no other programs were using port 6115.

I've checked the firewall configuration and it seems to allow port 6115:

Code:

config rule
        option input 'ACCEPT'
        option output 'ACCEPT'
        option forward 'ACCEPT'
        option target 'ACCEPT'
        option proto 'tcp udp'
        option src_port '6115'
        option dest_port '6115'
        option name 'Allow-Noxiallis'
        option src '*'
        option family 'ipv4'

I've tried to disable the firewall but nothing changes.

So now for the question: Can the firewall even interfere if I send the packets at 127.0.0.1 or should I be trying something other than messing with the firewall?

I've heard the problem can be caused because my router device is big-endian, if that can cause the problem what can I do about it?

If relevant, here are the client and server codes (for local loopback):

Server:

Code:


#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>

int main() {

    int udpSocket, ndat;
    struct sockaddr_in serverAddr;
    struct sockaddr_storage serverStorage;
    socklen_t addr_size;
    char buf[1024];

    udpSocket=socket(PF_INET,SOCK_DGRAM,0);

    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(6115);
    memset(serverAddr.sin_zero,'\0',sizeof serverAddr.sin_zero);

    bind(udpSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr));

    addr_size=sizeof serverStorage;

    while (1) {

        ndat=recvfrom(udpSocket,buf,1024,0,(struct sockaddr*)&serverStorage,&addr_size);
        printf("DATA RECEIVED WITH %u BYTES\n",ndat);

    }

    return 0;

}

Client:

Code:


#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main() {

    int udpSocket;
    char buffer[1024]="Hello [home]";
    struct sockaddr_in serverAddr;
    socklen_t addr_size=sizeof serverAddr;

    udpSocket=socket(PF_INET,SOCK_DGRAM,0);

    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(6115);
    serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    memset(serverAddr.sin_zero,'\0',sizeof serverAddr.sin_zero);

    bind(udpSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr));

    sendto(udpSocket,buffer,1024,0,(struct sockaddr*)&serverAddr,addr_size);

    printf("Sent...\n");

    return 0;

}

Thanks in advance.

BobInWonderland 04-17-2015 07:26 AM

My bad, I had ignored the server code because for some reason it was working on the PC. Anyway, adding serverAddr.sin_addr.s_addr=htonl(INADDR_ANY); before the call to bind(); solved the problem.


All times are GMT -5. The time now is 11:50 PM.