socket question

socket question

Post by Guillaume Malle » Wed, 16 Dec 1998 04:00:00



Hi everyone,

I'm encoutering the following problem running a socket UDP
server under Solaris2. The server listens for incoming
packets on a socket that was bound (ie <bind>ed) using
INADDR_ANY as the value of the sin_addr.s_addr field. The
server runs on a mutliple-address machine, and this option
enables you to leave unspecified the adress to which you
want to bind the socket, and say that any of the machine's
addresses will do (well, i suspect those who'll be able to
answer know that already, but anyway).

So, my question is, once i've received a packet on that
socket (via recvfrom()), how do i determine the IP address
it was sent to ? That is to say, how can i tell which of the
several valid IP addresses for my machine the sender used ?

Thanks,

GM -

 
 
 

socket question

Post by Bas de Bakke » Wed, 16 Dec 1998 04:00:00


    Guillaume> So, my question is, once i've received a packet on that
    Guillaume> socket (via recvfrom()), how do i determine the IP
    Guillaume> address it was sent to ? That is to say, how can i tell
    Guillaume> which of the several valid IP addresses for my machine
    Guillaume> the sender used ?

You should really rush out and buy "Unix Network Programming, 2nd
edition, volume 1" by W. Richard Stevens.  See his web page at
http://www.kohala.com/~rstevens/

If you don't want to do that, you could download the source of the
book at said web page and look in the advio/recvfromflags.c source for
an example on how to do this.  But if you're doing advanced network
programming (and you obviously do), I really recommend you read the
book.  I found it very useful.

Regards,
Bas.

 
 
 

socket question

Post by pe.. » Wed, 16 Dec 1998 04:00:00



> Hi everyone,

I would say "You can't" (Barry, correct me if i'm wrong:-)

If you are interested in which of your local addresses
a datagram is addressed to, open one socket on each
(and possibly select() on each filediscriptor).

Why do you care ? You should be able to return it
using the "from-address" anyway ?

Regards
Peter h

Quote:> I'm encoutering the following problem running a socket UDP
> server under Solaris2. The server listens for incoming
> packets on a socket that was bound (ie <bind>ed) using
> INADDR_ANY as the value of the sin_addr.s_addr field. The
> server runs on a mutliple-address machine, and this option
> enables you to leave unspecified the adress to which you
> want to bind the socket, and say that any of the machine's
> addresses will do (well, i suspect those who'll be able to
> answer know that already, but anyway).
> So, my question is, once i've received a packet on that
> socket (via recvfrom()), how do i determine the IP address
> it was sent to ? That is to say, how can i tell which of the
> several valid IP addresses for my machine the sender used ?
> Thanks,
> GM -

--
--
Peter H?kanson            Phone +46 0708 39 23 04
Network Management AB     Fax   +46 031 779 7844
 
 
 

socket question

Post by Barry Margoli » Wed, 16 Dec 1998 04:00:00





>> Hi everyone,

>I would say "You can't" (Barry, correct me if i'm wrong:-)

>If you are interested in which of your local addresses
>a datagram is addressed to, open one socket on each
>(and possibly select() on each filediscriptor).

>Why do you care ? You should be able to return it
>using the "from-address" anyway ?

RFC 1122 says that UDP-based applications SHOULD set the source address of
replies to the destination address of the query.  Modern DNS resolver and
recursive server implementations, for instance, will ignore responses that
come from a different address than the query was sent to, to catch some
attempts at spoofing responses.

Quote:

>> I'm encoutering the following problem running a socket UDP
>> server under Solaris2. The server listens for incoming
>> packets on a socket that was bound (ie <bind>ed) using
>> INADDR_ANY as the value of the sin_addr.s_addr field. The
>> server runs on a mutliple-address machine, and this option
>> enables you to leave unspecified the adress to which you
>> want to bind the socket, and say that any of the machine's
>> addresses will do (well, i suspect those who'll be able to
>> answer know that already, but anyway).

>> So, my question is, once i've received a packet on that
>> socket (via recvfrom()), how do i determine the IP address
>> it was sent to ? That is to say, how can i tell which of the
>> several valid IP addresses for my machine the sender used ?

>> Thanks,

>> GM -

>--
>--
>Peter H?kanson            Phone +46 0708 39 23 04
>Network Management AB     Fax   +46 031 779 7844

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
 
 
 

socket question

Post by Solomon Red » Fri, 18 Dec 1998 04:00:00


You can use the getsockname() system call to get the local IP address of
given a socket. A similar function, getpeername() returns the IP address of
the remote client given a socket.

SR


>Hi everyone,

>I'm encoutering the following problem running a socket UDP
>server under Solaris2. The server listens for incoming
>packets on a socket that was bound (ie <bind>ed) using
>INADDR_ANY as the value of the sin_addr.s_addr field. The
>server runs on a mutliple-address machine, and this option
>enables you to leave unspecified the adress to which you
>want to bind the socket, and say that any of the machine's
>addresses will do (well, i suspect those who'll be able to
>answer know that already, but anyway).

>So, my question is, once i've received a packet on that
>socket (via recvfrom()), how do i determine the IP address
>it was sent to ? That is to say, how can i tell which of the
>several valid IP addresses for my machine the sender used ?

>Thanks,

>GM -

 
 
 

socket question

Post by Barry Margoli » Sat, 19 Dec 1998 04:00:00




>You can use the getsockname() system call to get the local IP address of
>given a socket. A similar function, getpeername() returns the IP address of
>the remote client given a socket.

That only works with a connected socket.  If he's using recvfrom(), he's
presumably not using a connected socket.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.