Skip to contentSkip to conversation

Problem with a UDP server implements

DISCUSSION jiangtao.jit 3 participants 11 messages Oct 14, 2011 – Oct 21, 2011

lore
Share & tools
lore
#0 lore raw

Hi:

I'm writing a UDP server and meet a problem
the UDP server program is run on a box with single card but has multi ip:
like eth0 10.0.0.1

        eth0:1     10.0.0.2
		eth0:2		11.0.0.1

I bind the server socket with INADDR_ANY

when a client send a packet to 10.0.0.2(the eth0:1 's ip)
the server can recieve the packet and reply, but the client can't recieve the server's response I use wireshark to capture the response packet
and found that the server use 10.0.0.1(the eth0 's ip) as saddr in the response packet I expected it to be 10.0.0.2 but it's not
my question : is there any way to let the UDP server relpy just use the ip it receieved the request ? It's :
when a client send a request to 10.0.0.1(eth0); the server use 10.0.0.1 to response and another client send a request to 10.0.0.2(eth0:1); the server use 10.0.0.2 to response

Thanks.

2011-10-15
jiangtao.jit

#1 lore raw

Hi!

Quoted 9 lines · jiangtao.jit

Hi:

I'm writing a UDP server and meet a problem
the UDP server program is run on a box with single card but has multi ip:
like eth0 10.0.0.1

        eth0:1     10.0.0.2
		eth0:2		11.0.0.1

I bind the server socket with INADDR_ANY

...

Quoted 2 lines

when a client send a request to 10.0.0.1(eth0); the server use 10.0.0.1 to response and another client send a request to 10.0.0.2(eth0:1); the server use 10.0.0.2 to response

Well, is seems there is neither a variant of recvfrom() which tells you your local address nor a variant of sendto() which allows you to specify your local address. I guess you have to call getaddrinfo() and create a socket for every address you want to bind to. If you want to stay single threaded you can switch them to nonblocking and use epoll to wait for packets.

-Michi

Signature · 2 lines
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com
author
#2 lore raw

michi:

1.
Is that to create two sockets
and one bind to 10.0.0.1 the other bind to 10.0.0.2 then use epoll wait these two sockets ?

2.
Is there any way to find out the turple like TCP
then the UDP server can reply with exactly the ip it recieved the client's request ?

Thanks.

2011/10/15 <[email protected]>:

Quoted 27 lines

Hi!

Quoted 9 lines · jiangtao.jit

Hi:

I'm writing a UDP server and meet a problem
the UDP server program is run on a box with single card but has multi ip:
like ? ?eth0 ? ? ? ?10.0.0.1
? ? ? ? eth0:1 ? ? 10.0.0.2
? ? ? ? ? ? ? eth0:2 ? ? ? ? ?11.0.0.1

I bind the server socket with INADDR_ANY

...

Quoted 2 lines

when a client send a request to 10.0.0.1(eth0); the server use 10.0.0.1 to response and another client send a request to 10.0.0.2(eth0:1); the server use 10.0.0.2 to response

Well, is seems there is neither a variant of recvfrom() which tells you your local address nor a variant of sendto() which allows you to specify your local address. I guess you have to call getaddrinfo() and create a socket for every address you want to bind to. If you want to stay single threaded you can switch them to nonblocking and use epoll to wait for packets.

? ? ? ?-Michi
--
programing a layer 3+4 network protocol for mesh networks see http://michaelblizek.twilightparadox.com

#3 lore raw

Hi!

Quoted 6 lines · tao jiang

michi:

1.
Is that to create two sockets
and one bind to 10.0.0.1 the other bind to 10.0.0.2 then use epoll wait these two sockets ?

Yes, exactly.

Quoted 4 lines

2.
Is there any way to find out the turple like TCP
then the UDP server can reply with exactly the ip it recieved the client's request ?

You should receive the packet only on the socket which is bound to the address. You can save the bind address somewhere, so you know the local address. You will know the client address because it is passed back by recvfrom.

-Michi

Signature · 2 lines
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com
author
#4 lore raw

michi:

Thanks for your help.
Much appreciated.

2011/10/15 <[email protected]>:

Quoted 27 lines

Hi!

Quoted 6 lines · tao jiang

michi:

1.
Is that to create two sockets
and one bind to 10.0.0.1 the other bind to 10.0.0.2 then use epoll wait these two sockets ?

Yes, exactly.

Quoted 4 lines

2.
Is there any way to find out the turple like TCP
then the UDP server can reply with exactly the ip it recieved the client's request ?

You should receive the packet only on the socket which is bound to the address. You can save the bind address somewhere, so you know the local address. You will know the client address because it is passed back by recvfrom.

? ? ? ?-Michi
--
programing a layer 3+4 network protocol for mesh networks see http://michaelblizek.twilightparadox.com

#5 lore raw
In reply to jiangtao.jit
Quoted 10 lines · jiangtao.jit

Hi:

I'm writing a UDP server and meet a problem
the UDP server program is run on a box with single card but has multi ip:
like eth0 10.0.0.1

        eth0:1     10.0.0.2
		eth0:2		11.0.0.1

I bind the server socket with INADDR_ANY

Quoted 9 lines

when a client send a packet to 10.0.0.2(the eth0:1 's ip)
the server can recieve the packet and reply, but the client can't recieve the server's response I use wireshark to capture the response packet
and found that the server use 10.0.0.1(the eth0 's ip) as saddr in the response packet I expected it to be 10.0.0.2 but it's not
my question : is there any way to let the UDP server relpy just use the ip it receieved the request ? It's :
when a client send a request to 10.0.0.1(eth0); the server use 10.0.0.1 to response and another client send a request to 10.0.0.2(eth0:1); the server use 10.0.0.2 to response

You can always build the complete raw packet (including the IP header) yourself.

Bernd

Signature · 2 lines
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at
author
#6 lore raw

Bernd:

Is it to create a SOCK_RAW socket for receieve and send? Then is it means I have to parse the IP header and options if any? Will it cause performance problem, compare to SOCK_DGRAM?

Thanks.

2011/10/15 Bernd Petrovitsch <[email protected]>:

Quoted 30 lines · jiangtao.jit
Quoted 10 lines

Hi:

I'm writing a UDP server and meet a problem
the UDP server program is run on a box with single card but has multi ip:
like ? ?eth0 ? ? ? ?10.0.0.1
? ? ? ? eth0:1 ? ? 10.0.0.2
? ? ? ? ? ? ? eth0:2 ? ? ? ? ?11.0.0.1

I bind the server socket with INADDR_ANY

Quoted 9 lines

when a client send a packet to 10.0.0.2(the eth0:1 's ip)
the server can recieve the packet and reply, but the client can't recieve the server's response I use wireshark to capture the response packet
and found that the server use 10.0.0.1(the eth0 's ip) as saddr in the response packet I expected it to be 10.0.0.2 but it's not
my question : is there any way to let the UDP server relpy just use the ip it receieved the request ? It's :
when a client send a request to 10.0.0.1(eth0); the server use 10.0.0.1 to response and another client send a request to 10.0.0.2(eth0:1); the server use 10.0.0.2 to response

You can always build the complete raw packet (including the IP header) yourself.

? ? ? ?Bernd
--
Bernd Petrovitsch ? ? ? ? ? ? ? ? ?Email : bernd at petrovitsch.priv.at ? ? ? ? ? ? ? ? ? ? LUGA : http://www.luga.at

#7 lore raw

On Die, 2011-10-18 at 07:26 +0800, tao jiang wrote:
[....]

Quoted 1 line

Is it to create a SOCK_RAW socket for receieve and send?

Only for sending. Search for sourcecode for ping - there are several around that show how to do it.

You can use recvfrom() to get the senders address for each packet.

[...]

Quoted 1 line

Will it cause performance problem, compare to SOCK_DGRAM?

IMHO that won't measurably - either user-space constructs the UDP and IP header or the kernel does. And we talk about a few bytes only.

[ Don't top-post. ]

Bernd

Signature · 2 lines
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at
author
#8 lore raw

Bernd:

Thanks for your guide.
But there is still a question
if recvfrom() used, how to figure out the client's dest ip for reply ? i.e. on which ip the server receieved the request
when construct the IP header, i don't know which saddr to fill in

Thanks.

2011/10/18 Bernd Petrovitsch <[email protected]>:

Quoted 23 lines · tao jiang

[....]

Quoted 1 line

Is it to create a SOCK_RAW socket for receieve and send?

Only for sending. Search for sourcecode for ping - there are several around that show how to do it.

You can use recvfrom() to get the senders address for each packet.

[...]

Quoted 1 line

Will it cause performance problem, compare to SOCK_DGRAM?

IMHO that won't measurably - either user-space constructs the UDP and IP header or the kernel does. And we talk about a few bytes only.

[ Don't top-post. ]

? ? ? ?Bernd
--
Bernd Petrovitsch ? ? ? ? ? ? ? ? ?Email : bernd at petrovitsch.priv.at ? ? ? ? ? ? ? ? ? ? LUGA : http://www.luga.at

#9 lore raw

Hi!

On Die, 2011-10-18 at 22:48 +0800, tao jiang wrote:
[...]

Quoted 5 lines

Thanks for your guide.
But there is still a question
if recvfrom() used, how to figure out the client's dest ip for reply ? i.e. on which ip the server receieved the request
when construct the IP header, i don't know which saddr to fill in

Ooops, forgot that we need the destination address of the received packet too to construct the answer for it.
Then yes, it seems that you would need the raw packet on the receiving side too.

[ Full quote deelted ]

Bernd

Signature · 2 lines
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at
author
#10 lore raw

Bernd:

Thanks again.

2011/10/19 Bernd Petrovitsch <[email protected]>:

Quoted 23 lines

Hi!

On Die, 2011-10-18 at 22:48 +0800, tao jiang wrote:
[...]

Quoted 5 lines

Thanks for your guide.
But there is still a question
if recvfrom() used, how to figure out the client's dest ip for reply ? i.e. on which ip the server receieved the request
when construct the IP header, i don't know which saddr to fill in

Ooops, forgot that we need the destination address of the received packet too to construct the answer for it.
Then yes, it seems that you would need the raw packet on the receiving side too.

[ Full quote deelted ]

? ? ? ?Bernd
--
Bernd Petrovitsch ? ? ? ? ? ? ? ? ?Email : bernd at petrovitsch.priv.at ? ? ? ? ? ? ? ? ? ? LUGA : http://www.luga.at