Solved

How do I specify/bind a socket to a specific NIC?

Posted on 2010-08-26
4
1,444 Views
Last Modified: 2013-11-29
I'm trying to configure multiple sockets in one app and have them bound to specific addresses & NICs.  I have one TCP socket setup on one address, but I need to set up a separate UDP socket for broadcasting to use a completely different address configured on the other NIC.  I can't seem to specify the UDP socket to send FROM the second NIC.  I've done the....

send_struct.sin_family = AF_INET;
send_struct.sin_addr.s_addr = inet_addr("164.217.14.10");
bind(my_udp_socket, (SOCKADDR*) &send_struct, sizeof(send_struct));

...where NIC 1 is 192.168.1.10 and NIC 2 is 164.217.14.10 (mask: 255.255.0.0)

But, that doesn't seem to do it.  When I call my sendto() function.....

send_struct.sin_port = htons("3000");
send_struct.sin_addr.s_addr = ("164.217.255.255");
sendto(my_udp_socket, (int8 *)pkt, len, 0, (const struct sockaddr *)&send_struct, sizeof(send_struct));

...I don't see it show up on the wire (and the return value from sendto() returns the number of bytes I'm sending and I'm connected to an actual hub, not a switch...thus Wireshark shows everything).  I've searched for an example on someone setting up multiple socket connections bound to different NICs/subnets and haven't found a solution that works yet.  Any ideas?
0
Comment
Question by:JeepGuy
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:vanillasprinkles
ID: 33536979
you will probably be better off setting up two sin_addr's and the corresponding ports and binds for each address:port you want to have in your program (yes you can use send_struct for each unique ip:port you want but it can get messy and hard to keep track of)

I dont think it matters much, but in code i've written, my sendto does not use (const struct sockaddr *), but rather, (struct sockaddr *)
0
 

Author Comment

by:JeepGuy
ID: 33537182
I have already set up two different sockaddr_in structs for the different sockets.  I even tried using one for the bind and another for the sendto() func for the same socket.
0
 
LVL 3

Expert Comment

by:vanillasprinkles
ID: 33538473
sounds awful

oh, i did notice (forgot to add it to my first comment)

sin_addr.s_addr = inet_addr("IP")

your second sin_addr.s_addr=  does not have the inet_addr - not sure if you wanted it like that or not.


I have referenced this page (http://www.linuxhowtos.org/C_C++/socket.htm) many times in the past; what I would to is start a test project for yourself, with binding to two unique IP:Port's and exporting that working code to your project, so that you dont overwhelm yourself in your full sized project.
0
 

Accepted Solution

by:
JeepGuy earned 0 total points
ID: 33559995
yeah, that second sin_addr.s_addr= statement was a typo in my post.  I have it right in my code.

I have abandoned this effort after some more research.  From what I've gathered it's not possible to specify the interface in code.  I was able disable the second interface, multi-home the other interface, and get it working that way.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

SSL is a very common protocol used these days when browsing the web.  The purpose is to provide security to communication, but how does it do it?  There are several pieces at work that have to be setup before SSL will even work and it requires both …
#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now