How can I get the interface name/index associated with a TCP socket?

前端 未结 9 560
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 04:17

I\'m writing a TCP server that needs to know which interface each connection arrived from. I cannot use the address/subnet to deduce which interface was used, since there mi

9条回答
  •  情歌与酒
    2021-01-12 05:04

    Kieron's suggestion to write a netfilter module is probably one way to try, but I would like to refrain from writing my very first kernel module for this solution.

    I've come up with another option of using source NAT and translating the source port of the connection to correlate with the connection source. I can assign port ranges for each network and check it in the server. The only problem is that source NAT in iptables is done in the POSTROUTING chain, and I'm not sure it's used for connections that are accepted by that host, so I might need to use another server.

    No easy solutions here, too bad I can't get the interface name/index from the socket...

提交回复
热议问题