Thread: connect two interfaces of the same host

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    7

    connect two interfaces of the same host

    Hi,

    I have following situation:
    my Linux host has two interfaces eth0 (192.168.254.254) and eth1 (192.168.253.254). For the test purpose I want connect these two port with a cable and make some data exchange. For both the server and client socket I make bind(). The IP addresses seem to be assigned properly. When I start the test and lo interface is up everything runs successfully but not over the cable. When I make lo interface down I don't get any connection. I also tried to set route for these hosts but with the same result. What am I doing wrong?

    Code:
    // server init code
    int open_server(char *interface, char *server_ip, struct _tester *tester)
    {
      int sockfd, i;
      struct sockaddr_in *server;
      struct ifreq ifr;
    
      printf("Server:\nInterface: %s\nIP: %s\n", interface, server_ip);
      memset(&ifr, 0, sizeof(struct ifreq));
    
      if((sockfd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
      {
        perror("Socket");
        return TST_ERROR;
      }
    
      i = 1;
      if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
      {
        perror("setsockopt");
        return TST_ERROR;
      }
    
      server = (struct sockaddr_in *)&(ifr.ifr_addr);
      server->sin_family = AF_INET;
      server->sin_port = htons(SERVER_PORT);
      inet_aton(server_ip, &server->sin_addr);
    
      strncpy(ifr.ifr_name, interface, IFNAMSIZ);
      if(ioctl(sockfd, SIOCSIFADDR, &ifr) < 0)
      {
        perror("IOCTL (SIOCSIFADDR)");
        return TST_ERROR;
      }
    
      // bring interface up
      ifr.ifr_flags = IFF_UP;
    
      if(ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
      {
        perror("IOCTL (SIOCSIFADDR)");
        return TST_ERROR;
      }
    
      if(bind(sockfd, (struct sockaddr *)server, sizeof(struct sockaddr_in)) < 0)
      {
        perror("Bind");
        return TST_ERROR;
      }
    
      if(listen(sockfd, 5) < 0)
      {
        perror("Listen");
        return TST_ERROR;
      }
    
      printf("IP assigned %s\n", inet_ntoa(server->sin_addr));
      return sockfd;
    }
    Code:
    // client init code
    int open_client(char *interface, char *client_ip, struct _tester *tester)
    {
      int sockfd, i;
      struct sockaddr_in *client;
      struct ifreq ifr;
    
      printf("Client:\nInterface: %s\nIP: %s\n", interface, client_ip);
      memset(&ifr, 0, sizeof(struct ifreq));
    
      if((sockfd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
        perror("Socket");
    
      i = 1;
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
    
      
      client = (struct sockaddr_in *)&(ifr.ifr_addr);
      client->sin_family = AF_INET;
      client->sin_port = htons(SERVER_PORT + 1);
      inet_aton(client_ip, &client->sin_addr);
    
      strncpy(ifr.ifr_name, interface, IFNAMSIZ);
      if(ioctl(sockfd, SIOCSIFADDR, &ifr) < 0)
        perror("IOCTL (SIOCSIFADDR)");
    
      // bring interface up
      ifr.ifr_flags = IFF_UP;
    
      if(ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
        perror("IOCTL (SIOCSIFADDR)");
    
      if(bind(sockfd, (struct sockaddr *)client, sizeof(struct sockaddr_in)) < 0)
      {
        perror("Bind");
        return TST_ERROR;
      }
    
      return sockfd;
    }
    Thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do you use a cross cable? For direct connection of 2 network cards you need a cross not a regular cable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    to make a cross cable , you want to swap the twisted pair going to pin 1/2 with the one going to 3/5 one one end of the cable only. This way 1/2 on end A goes to 3/5 on end B and vice versa. either that or you could just get 2 cables and plug them both into a router

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you can just buy a crossover cable . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by dwks View Post
    Or you can just buy a crossover cable . . .
    oh sure, throw money at the problem

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, well, do you think that someone who doesn't know that they even need a crossover cable will be able to "swap the twisted pair going to pin 1/2 with the one going to 3/5 one one end of the cable only. This way 1/2 on end A goes to 3/5 on end B and vice versa." ?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And to put the connector manually to this custom build cable - you need a tool - that will cost you even more than the cable itself
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    7

    Lightbulb

    I have two interfaces with Auto-MDI (When switch UTP port or LAN card supports auto MDI/MDI-X function, the connection is independent of cable’s crossover or non-crossover type and allows using a straight cable to make a switch-to-switch connection.)

    I have solved the problem by putting the interfaces in promiscuous mode and sending raw ethernet broadcast packet. If I unplug the cable I still can receive the packet but the RX bytes on the interface won't be incremented. So checking RX bytes (read /proc/net/dev) I can recognize if the communication was successfully gone over the cable.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    How new is your system? People are talking about crossover cables -- on newer network cards, the device is able to throw a switch and "cross over" by itself, using a regular cable -- this is autodetected and handled automatically. But if your system is pretty old, you may in fact need a crossover cable.

    It could be that your routing table is not set up right. Simply configuring the IP addresses isn't quite enough to get communication going. And it would probably help if you configured the interfaces in "point-to-point" mode.

    Is this Linux? Can you post the ifconfig and route information?

    EDIT: I see your post saying that it works in promiscuous mode. That proves the problem isn't crossover. You probably just don't have your routes set up properly.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by vart View Post
    And to put the connector manually to this custom build cable - you need a tool - that will cost you even more than the cable itself
    but then you will have the tool and you can never have enough tools, unless they are the kind that walk around and say things like 'we need to collateralize the synergy into a proactive mission oriented solution'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if a host is alive
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 05-05-2009, 11:10 AM
  2. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  3. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  4. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM