简体   繁体   中英

OpenWRT C program not receiving packets

I compiled a simple C UDP server and client for the OpenWrt router. The codes work as expected when both are on my pc. The client, if on the router, seems to successfully send the packets (port 6115) since my pc acknowledges them if I 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 (using local loopback) or on the pc.

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

I've checked the firewall configuration in /etc/config/firewall and it seems to allow packets from port 6115:

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-myudp'

  option src '*'

  option family 'ipv4'

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

Question: Can the firewall even interfere if I send the packets through local loopback (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:

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);

}

Client:

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");

Please give me suggestions on how to solve the problem if you have any. Thanks in advance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM