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? 开发者_运维问答
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.
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.
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).
精彩评论