1

I need to use multiple network interfaces in a C program with sockets. My interfaces are wlan0 and eth0. When I connect eth0, the ping through wlan0 returns "Host unreachable", and consequently the socket who connects through wlan0 returns "connection timed out". If I run ifconfig, it returns 2 interfaces both UP and with an IP address. How can I connect both eth0 and wlan0?

EDIT:

My basic questions is: why i can't reach internet with wlan0 when eth0 is connected?

My routing tables:

 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 eth0
 10.0.0.0        0.0.0.0         255.255.255.0   U     9      0        0 wlan0
 192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0

My iptables :

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination  

ifconfig:

 eth0      Link encap:Ethernet  HWaddr 74:d0:2b:07:3a:dd  
           inet addr:192.168.1.129  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: fe80::76d0:2bff:fe07:3add/64 Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:33194 errors:0 dropped:0 overruns:0 frame:0
           TX packets:23759 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:31065847 (31.0 MB)  TX bytes:3057118 (3.0 MB)

 lo        Link encap:Local Loopback  
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1/128 Scope:Host
           UP LOOPBACK RUNNING  MTU:65536  Metric:1
           RX packets:21018 errors:0 dropped:0 overruns:0 frame:0
           TX packets:21018 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:10519343 (10.5 MB)  TX bytes:10519343 (10.5 MB)

 wlan0     Link encap:Ethernet  HWaddr 6c:71:d9:5f:6b:7f  
           inet addr:10.0.0.6  Bcast:10.0.0.255  Mask:255.255.255.0
           inet6 addr: fe80::6e71:d9ff:fe5f:6b7f/64 Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:34286 errors:0 dropped:0 overruns:0 frame:0
           TX packets:26667 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:25986919 (25.9 MB)  TX bytes:3409031 (3.4 MB)
2
  • As asked, I think this is too broad. That said, have a look at your IP routes and compare them at a time when things are working and a time when things are not working. It's certainly plausible that routing is the cause of your difficulties.
    – user
    Commented Nov 6, 2015 at 18:06
  • I have posted more details in the answer to the comment below. Sorry but English is not my first language ...
    – Sceik
    Commented Nov 6, 2015 at 21:50

1 Answer 1

0

The IP protocol, and protocols that lie directly above it like UDP and TCP, are intended to be global (NAT notwithstanding), intended to be hardware agnostic, and intended to not care about security. This means:

  • by design, any IP address is supposed to be able to communicate with any other IP address, irrespective of the media type (Ethernet, token-ring, wireless Ethernet, SONET, whatever)
    • therefore, by design, there is only one top-level network, aka. one "Internet".
  • by design, no security is considered - an IP packet's job is to get from its source to a host with its destination IP and that's it.
    • therefore, by design, an IP packet doesn't care what it's traveling over as long as it's headed towards its destination
    • also therefore, by design, an IP packet doesn't care what network interface (or other mechanism - e.g. PPP) it's leaving from. A router will care, since a router knows what IP addresses are behind a network interface.
    • when a router or system is confronted with multiple paths to the same IP, it assumes any one will work the same, and that it can load balance between them or fail over to the other one.

Because of these, the standard C sockets API doesn't support sending traffic out of a specific interface, you are only supposed to be concerned with the destination IP and leave the routing/physical layer up to infrastructure and the OS.

So you will have to do things like adjust your OS routing tables or use other features specific to your OS to accomplish what you want.

3
  • I'm not sure if I have understood very well - what I am doing is that I am creating two sockets, and binding each one to the IP address corresponding to the network interface (one to eth0, the other to wlan0). However, by doing this, when I call the connect() function on the wlan0 socket I get a "connection timed out" error. When I tried to execute "ping -I wlan0 www.google.it" it says "host unreachable". This is why I thought it was a problem of OS routing.
    – Sceik
    Commented Nov 6, 2015 at 21:48
  • What's the IPs/netmasks on these interfaces?
    – LawrenceC
    Commented Nov 7, 2015 at 13:10
  • The netmask of wlan0 is '255.255.255.0' .I added ifconfig to my question.
    – Sceik
    Commented Nov 7, 2015 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .