By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

BSD Sockets - Selecting Source Address

P: 158
Hello Bytes,

I am using UDP BSD sockets in my program, all is working just fine until my computer has two nics or 1 nic and one virtual nic. I am running Linux x86 Debian Squeeze.

I have one nic pointed at my 10.0.0. network and another one at 10.13.0. network

My outbound packets are making it to the destination on my 10.13.0 network.

The problem is that the computer on the 10.13.0 network that im sending to tries to send an ACK back but it sees my address as being my 10.0.0. address but packets cannot route from the 10.13.0. to the 10.0.0. network. If it saw my address as being from the 10.13.0. network it would work fine.

When creating the udp sockets can I specify what source address to use?

Thanks in advance,

Here is how I am implementing the sending so far:

Expand|Select|Wrap|Line Numbers
  1.     memset(&hints, 0, sizeof hints);
  2.     hints.ai_family = AF_UNSPEC;
  3.     hints.ai_socktype = SOCK_DGRAM;
  5.     if ((rv = getaddrinfo(host.c_str(), po, &hints, &servinfo)) != 0) {
  6.         throw "getaddrinfo failed";
  7.     }
  9.     // loop through all the results and make a socket
  10.     for (p = servinfo; p != NULL; p = p->ai_next) {
  12.         cout << "**Addr:" << p->ai_addr->sa_data << endl;
  14.         if ((sockfd = socket(p->ai_family, p->ai_socktype,
  15.                 p->ai_protocol)) == -1) {
  16.             continue;
  17.         }
  19.         break;
  20.     }
  22.     if (p == NULL) {
  23.         throw "failed to bind to socket";
  24.     }
  26.     if ((numbytes = sendto(sockfd, msg.c_str(), msg.length(), 0,
  27.             p->ai_addr, p->ai_addrlen)) == -1) {
  28.         return numbytes;
  29.     }
  31.     freeaddrinfo(servinfo);
  32.     close(sockfd);
Oct 22 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 542
As a first step, lets forget about 10.0.0.x nic/network.
Say,your machine has ip assigned to other nic.
Your target machine,to which you are sending the UDP packets,has ip Now, if my assumptions are right then check if both these machines are able to ping each other or not? If that works well then, I don't think any good reason about why it's not receiving ack packet unless firewall is ok with udp packets.
Oct 23 '10 #2

P: 158
Thanks for the reply ashitpro, but I don't you understand the problem I am having.

My machine can send packets to the target machine on the 10.13.0 network without a problem and the target machine can send packets/ping to my machine if it uses my 10.13.0 address. But if it uses my 10.0.0. address my network will not allow it to go.

My problem is probably due to how my network is setup but I do not have much control over that.

So my problem is that the target machine only knows my 10.0.0 address because that is what the first packet is sent by. Is there a way to make the packet I am sending have a source address of my 10.13.0. address?
Oct 25 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.