tags:

views:

114

answers:

3

Is their a way (except looping on all open FDs) to obtain the FD for a given IP addr & port? I have a bunch of open UDP sockets each bound to an IP address & port. The application, in some instances, acts as a forward application. Is their a getfdbyname system call?

Specifically, my UDP application(C) sits between nodes A and B .

1) A sends a message to C using source Port 2000, destination port 3000 which is received by C

2) C then has to forward this to port 3000 of node B using port 2000.

At step 1, the open socket bound to port 3000 receives a message. However, at this point, I need to obtain the FD for the socket bound of port 2000 to forward the message.

Any ideas except looping over all configured sockets?

+3  A: 

You could keep a hash of address => socket mappings.

What I usually do is have struct consisting of the address and the socket FD, representing a host with which I communicate. Then I have a lookup function which finds that struct by address.

Borealid
This is the right answer - "keep track of it when you create the sockets".
caf
Yes - if your application wants to do this, it needs to keep track of it. You could use a STL container (e.g. map) with the address / port pair as a key.
MarkR
A: 

No there's no call like that, but with UDP you don't need a separate socket for each destination host/port - that's what sendto(3) is for (unless you actually connect(2) your UDP sockets).

Nikolai N Fetissov
i am more interested in changing the source IP/port of each datagram I sent out
Aditya Sehgal
Hmm, if you send it out source IP/port **will change automatically** since it's sent from different host/port. I'm talking about your app C here - just use **one** outbound socket and change the destination IP/port to whatever you need.
Nikolai N Fetissov
i want the source IP/port to change to what I want not what kernel fills in. If i receive the message from srcPort 2000 to my listening port on 5000, i need to forward this message from srcPort 2000 to the peer's dest port 5000
Aditya Sehgal
As far as I remember, you can re-`bind(2)` UDP socket to different port. That would give you what you want.
Nikolai N Fetissov
@Aditya: I suggest to open another question then! But to answer, in short: You may be looking for a raw socket, which you can use to send raw ip packets the way you see fit (including the IP+UDP header you want), using `socket(PF_INET,SOCK_RAW,IPPROTO_IP)`.
mvds
Aditya Sehgal
+1  A: 

You could have a look at strace netstat -anlp which shows that you can look in /proc/net/udp to find a list of all sockets, like so:

sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode  
53: 0100007F:0035 00000000:0000 07 00000000:00000000 00:00000000 00000000   103        0 11075 2 ffff1231230f90c0 

This shows how "some process" is listening at UDP port 53 (for the slow reader, 0x0035 == 53 and 0x0100007F is localhost)

The inode (11075) is the link to the fd. Looking in /proc/<bindpid>/fd/ we see:

...
lrwx------ 1 bind bind 64 2010-07-20 06:26 513 -> socket:[11075]
...

So the fd is 513. I'm not suggesting that you follow this route, but I think it is at least one way to obtain what you asked for.

mvds