Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ DGRAM socket get the RECEIVER address

In C++,
how can I get the receiver address of the UDP packet which I have received using recvfrom. I know that it should be the same host on which I am receiving the packet, but I need to extract it from the received packet, in order to verify something. How can I do this?

I found that one way of doing this is:

int r = getsockopt(receiver_sock, SOL_IP, SO_ORIGINAL_DST, (struct sockaddr *) &sender_addr, (socklen_t *)&addr_len);`

But I get the error:

error: ‘SO_ORIGINAL_DST’ was not declared in this scope

I am using the appropriate headers

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/src/linux-headers-2.6.32-21/include/linux/netfilter_ipv4.h>
#include <arpa/inet.h>    
#include <linux/netfilter.h>

Using netfilter_ipv4 gives other errors like INT_MIN not declared. However, I think the mistake is something more fundamental rather than inclusion of proper header.

Please help.

like image 719
SkypeMeSM Avatar asked Sep 18 '25 05:09

SkypeMeSM


1 Answers

On Linux you want to use IP_PKTINFO option, see ip(7), and the recvmsg(2) call.

Stevens has examples of doing this but with IP_RECVDSTADDR and IP_RECVIF options that are not available on Linux.

like image 132
Nikolai Fetissov Avatar answered Sep 19 '25 18:09

Nikolai Fetissov