# How do I get the destination IP of a received UDP packet?

**URL:** <https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719>\
**Category:** Ruby\
**Created:** [February 7, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719 "2009-02-07T15:11:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gavinyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@gavinyap](https://www.ruby-forum.com/u/gavinyap)\
**Post date:** [February 7, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719/1 "2009-02-07T15:11:04Z")

</div>

Hi There,

I need to write a server which is able to identify client by  
destinations.

My machine is listening to “0.0.0.0”, any IP addrs configured to it.  
Take for instance, the machine is configured with “192.168.1.10,  
172.16.0.10”, also listening to “0.0.0.0”, allow listening to Broadcast  
queries, which is also important to my application

Hence, if one of the clients send a UDP packet to it, the server program  
would check the destination address of the client request and reply the  
client with different “answers”.

I’ve not been lucky enough to google how to get the destination address  
of a incoming packet.

Hope someone here could shed some light. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![gavinyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@gavinyap](https://www.ruby-forum.com/u/gavinyap)\
**Post date:** [February 7, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719/2 "2009-02-07T15:49:18Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Gavin Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I need to write a server which is able to identify client by  
> destinations.
> 
> My machine is listening to “0.0.0.0”, any IP addrs configured to it.  
> Take for instance, the machine is configured with “192.168.1.10,  
> 172.16.0.10”, also listening to “0.0.0.0”, allow listening to Broadcast  
> queries, which is also important to my application

How about using sockets for each IP address and “0.0.0.0”?

require ‘socket’

s0 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
s0.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
s0.bind(Socket.sockaddr\_in(9999, “0.0.0.0”))

s1 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
s1.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
s1.bind(Socket.sockaddr\_in(9999, “192.168.1.10”))

s2 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
s2.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
s2.bind(Socket.sockaddr\_in(9999, “172.16.0.10”))

while true  
rs, = IO.select([s0, s1, s2])  
rs.each {|s|  
p [Socket.unpack\_sockaddr\_in(s.getsockname)[1], s.recv(100)]  
}  
end

I’m not certain about the portability of the broadcast  
handling, though.

---

<div class="post-metadata">

**Author:** ![gavinyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@gavinyap](https://www.ruby-forum.com/u/gavinyap)\
**Post date:** [February 7, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719/3 "2009-02-07T18:01:34Z")

</div>

Tanaka A. wrote:

> In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
> Gavin Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > I need to write a server which is able to identify client by  
> > destinations.
> > 
> > My machine is listening to “0.0.0.0”, any IP addrs configured to it.  
> > Take for instance, the machine is configured with “192.168.1.10,  
> > 172.16.0.10”, also listening to “0.0.0.0”, allow listening to Broadcast  
> > queries, which is also important to my application
> 
> How about using sockets for each IP address and “0.0.0.0”?
> 
> require ‘socket’
> 
> s0 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
> s0.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
> s0.bind(Socket.sockaddr\_in(9999, “0.0.0.0”))
> 
> s1 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
> s1.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
> s1.bind(Socket.sockaddr\_in(9999, “192.168.1.10”))
> 
> s2 = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)  
> s2.setsockopt(Socket::SOL\_SOCKET, Socket::SO\_REUSEADDR, 1)  
> s2.bind(Socket.sockaddr\_in(9999, “172.16.0.10”))
> 
> while true  
> rs, = IO.select([s0, s1, s2])  
> rs.each {|s|  
> p [Socket.unpack\_sockaddr\_in(s.getsockname)[1], s.recv(100)]  
> }  
> end
> 
> I’m not certain about the portability of the broadcast  
> handling, though.

Hi Tanaka A. san,

Thanks for the tip.

I guess this is the only way get my destination address.  
I wonder if there’s a way to recieve the packet, with layer 3(IP header)  
and layer 4(UDP header) info.  
I dun mind to write a wrapper to strip off or add, the IP header and UDP  
details.

---

<div class="post-metadata">

**Author:** ![gavinyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@gavinyap](https://www.ruby-forum.com/u/gavinyap)\
**Post date:** [February 7, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-destination-ip-of-a-received-udp-packet/159719/4 "2009-02-07T18:16:36Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Gavin Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I guess this is the only way get my destination address.  
> I wonder if there’s a way to recieve the packet, with layer 3(IP header)  
> and layer 4(UDP header) info.

Your OS may have another (non-portable) way.

For example, IP\_PKTINFO of GNU/Linux, IP\_RECVDSTADDR of 4.4BSD.

However they needs recvmsg which is not supported by Ruby, yet.
