Setting socket outgoing port number in Linux kernel

929 Views Asked by At

I am trying to integrate multipath tcp (https://www.multipath-tcp.org/) into a project, and I would like to modify the source code in order to manually choose the outgoing socket port for a new mptcp subflow so that I can identify the packets going in and out within my application.

The address is created with:

inet_create(sock_net(meta_sk), &sock, IPPROTO_TCP, 1);

and bound:

sock.ops->bind(&sock, (struct sockaddr *)&loc_in, sizeof(struct sockaddr_in));

and then connected:

sock.ops->connect(&sock, (struct sockaddr *)&rem_in,
            sizeof(struct sockaddr_in), O_NONBLOCK);

My question is this: How can I manually set the outgoing port of a socket at kernel level, and/or where is the port set in this sequence of calls, so I can modify it?

1

There are 1 best solutions below

1
On BEST ANSWER

You would set the outgoing port in the loc_in structure that you use to bind() the socket to a local adapter/interface, eg:

struct sockaddr_in loc_in;
loc_in.sin_family = AF_INET;

// desired port...
loc_in.sin_port = htons(...);

// IP of desired adapter to connect() from...
loc_in.sin_addr.s_addr = inet_addr("...");