c - Query the IP of the interface that will be used for a connection to a certain target -
i have linux server multiple interfaces. each interface has ip , network. there default route , other routes in routing table connections networks go on 1 or other interface. of course, connections initiated programm going ip in same network 1 of intefaces of server go on interface.
given target ip programm on server going connect to: there api retrieve ip address of local interface used?
or alternatively: once connection has been initiated ip, how can local ip address of server appropriate? available?
i don't want write routing logic again.
example: eth0: 192.168.1.10/24 eth1: 10.1.1.20/24 default gw: 192.168.1.1 additional static route: 10.2.0.0/16 via 10.1.1.1
now if programm initiates udp connection 1.2.3.4, use eth0 , appropritate ip want query 192.168.10. if programm initiates udp connection 10.2.55.66, use eth1 , appropritate ip want query 10.1.1.20. if programm initiates udp connection 10.1.1.9, use eth1 , appropritate ip want query 10.1.1.20.
the reason question want modify snmp-trap sending programm put right address in agentfield
nfsv4 mount
similar when local ip address not specified via clientaddr
parameter. has obtain local ip address remote nfs server can connect to, when there multiple interfaces on local machine.
it creating udp datagram socket, binding local addresses, connect()
ing remote machine, querying local ip address used outgoing packets using getsockname()
. because udp connectionless protocol, no data sent via wire @ all; uses local routing tables determine local ip address bound socket.
see utils/mount/network.c:nfs_callback_address() @ linux-nfs git repository implementation. uses utils/mount/network.c:nfs_ca_sockname() obtain address, , utils/mount/network.c:nfs_ca_gai() check if address family supported on local machine (so won't return local address (callback address in nfs terms) using address family, ipv6, not supported on local machine).
yes, fail if machine-internal routing changes after above done -- is, modify routing table hand use interface destination ip address. not problem, because break (hang until timeout) existing connections anyway, because bound wrong local ip address, , kernel cannot switch under processes. in other words, if that, must expect connection breakage anyway.
routing changes outside local machine not affected. should obvious why: original udp connection test did not reach outside machine, local routing tables. routing outside machine (including next hop, or gateway addresses external machine, in local routing table) not cause breakage, , not change results obtained using above procedure.
i hope find useful.
Comments
Post a Comment