Operating System - HP-UX
1834178 Members
2092 Online
110064 Solutions
New Discussion

Programmatically or environmentally selecting Network IFace

 
SOLVED
Go to solution
RDMurphyx
New Member

Programmatically or environmentally selecting Network IFace

Having problem finding any information on how to either programmatically or environmentally set the network interface a process uses. My need is to have a client application network request's look like they are comming from distinct IP's depending on certain criteria.

Thanks,
R.
2 REPLIES 2
Matti_Kurkela
Honored Contributor
Solution

Re: Programmatically or environmentally selecting Network IFace

I assume you mean the client application is running on a host that has several IP addresses (either interfaces to different physical networks or more likely IP aliases) and you want to pick just one of them. Am I correct?

When setting up a network socket for incoming connections, you normally do a set of system calls: socket() to create the socket, bind() to select a port, listen() to wait for connections and ultimately accept() to receive the incoming connection. With outgoing connections, it is normal to just create the socket and use connect() to set up the connection.

It is possible to use the bind() system call in the outgoing case too. It is not done very often, because usually the client software does not care which local address and/or local port number it will get. If bind() is not used before connect(), the operating system will allocate a free port.

The bind() system call will require a sockaddr_in structure as input. It should contain both the desired local IP address and local port number. To select the IP address but allow the OS to pick the local port number, just set the port number (sin_port) to zero. See "man 2 bind".

If an application has the option to make it bind() its outgoing connections to a specific IP address, it is very useful when packaging the application for use with ServiceGuard.
MK
RDMurphyx
New Member

Re: Programmatically or environmentally selecting Network IFace

Thanks for the response - it answers my question precisely. Will give it a try.

R.