application layer routing

 

h1 sends a "*hello world*" message to h2. Then h2 forwards this message to h3.

 

[myudp_c]

#include <sys/types.h>

#include <sys/socket.h>

#include <string.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <arpa/inet.h>

#include <unistd.h>

#define MAXLINE 80

#define SERV_PORT1 8888

 

void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)

{

  int n;

  char sendline[MAXLINE], recvline[MAXLINE + 1];

 

  /* connect to server */

  if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)

  {

    perror("connect error");

    exit(1);

  }

 

  /* read a line and send to server */

   strcpy(sendline,"*hello world*");

   write(sockfd, sendline, strlen(sendline)+1);

}

 

int main(int argc, char **argv)

{

  int sockfd;

  struct sockaddr_in servaddr;

 

  /* check args */

  if(argc != 2)

  {

   printf("usage: udp_c <forwarder IP_address>\n");

   exit(1);

  }

 

  /* init servaddr */

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;

  servaddr.sin_port = htons(SERV_PORT1);

 

  if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)

  {

   printf("[%s] is not a valid IPaddress\n", argv[1]);

   exit(1);

  }

 

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  return 0;

}

 

[myudp_f]

#include <sys/types.h>

#include <sys/socket.h>

#include <string.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#define MAXLINE 80

#define SERV_PORT1 8888

#define SERV_PORT2 9999

 

int main(int argc, char **argv)

{

  int sockfd1, sockfd2;

  struct sockaddr_in forwardaddr, servaddr, cliaddr;

  socklen_t clilen;

  char mesg[MAXLINE];

  int n;

 

  /* check args */

  if(argc != 2)

  {

   printf("usage: udp_f <real server IP_address>\n");

   exit(1);

  }

 

  /* create sockets */

  sockfd1 = socket(AF_INET, SOCK_DGRAM, 0);

  sockfd2 = socket(AF_INET, SOCK_DGRAM, 0);

 

  /* init forwarder */

  bzero(&forwardaddr, sizeof(forwardaddr));

  forwardaddr.sin_family = AF_INET;

  forwardaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  forwardaddr.sin_port = htons(SERV_PORT1);

 

  /* bind address and port to socket */

  if(bind(sockfd1, (struct sockaddr *)&forwardaddr, sizeof(forwardaddr)) == -1)

  {

   perror("bind error");

   exit(1);

  }

 

  /* init servaddr */

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;

  servaddr.sin_port = htons(SERV_PORT2);

 

  if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)

  {

   printf("[%s] is not a valid IPaddress\n", argv[1]);

   exit(1);

  }

 

  /* connect to server */

  if(connect(sockfd2, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)

  {

    perror("connect error");

    exit(1);

  }

 

  for(;;)

  {

    /* waiting for receive data */

    n = recvfrom(sockfd1, mesg, MAXLINE, 0, (struct sockaddr *)&cliaddr, &clilen);

    printf("The forwarder has received a message: %s\n", mesg);

    /* forward data to real server */

    sendto(sockfd2, mesg, strlen(mesg)+1, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));

  }

 

  return 0;

 

}

 

[myudp_s]

#include <sys/types.h>

#include <sys/socket.h>

#include <string.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#define MAXLINE 80

#define SERV_PORT2 9999

 

void do_server(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)

{

  int n;

  socklen_t len;

  char mesg[MAXLINE];

 

  for(;;)

  {

    len = clilen;

 

    /* waiting for receive data */

    n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);

    printf("The server has received a message: %s\n", mesg);

  }

 

}

 

int main(void)

{

  int sockfd;

  struct sockaddr_in servaddr, cliaddr;

 

  /* create a socket */

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

 

  /* init servaddr */

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;

  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  servaddr.sin_port = htons(SERV_PORT2);

 

  /* bind address and port to socket */

  if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)

  {

   perror("bind error");

   exit(1);

  }

 

  do_server(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

  return 0;

}

 

[3hosts.py]

#!/usr/bin/python

"""

   Experiment to connect three hosts together without switch.     H1---H2---H3

"""

from mininet.net import Mininet

from mininet.node import Controller, RemoteController, OVSKernelSwitch, OVSLegacyKernelSwitch, UserSwitch

from mininet.cli import CLI

from mininet.log import setLogLevel

from mininet.link import Link, TCLink

 

def topology():

    "Create a network."

    net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )

    print "*** Creating nodes"

    h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='192.168.10.1/24' )

    h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='192.168.10.2/24' )

    h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='192.168.20.2/24' )

    c7 = net.addController( 'c7', controller=RemoteController, ip='127.0.0.1', port=6633 )

    print "*** Creating links"

    Link(h1, h2)

    Link(h2, h3, intfName1='h2-eth1')

    net.build()

    h2.cmd('ifconfig h2-eth1 192.168.20.1 netmask 255.255.255.0')

    h1.cmd("sudo echo 1 > /proc/sys/net/ipv4/ip_forward")

    h2.cmd("sudo echo 1 > /proc/sys/net/ipv4/ip_forward")

    h3.cmd("sudo echo 1 > /proc/sys/net/ipv4/ip_forward")

    h1.cmd("sudo ip route add 192.168.20.0/24 via 192.168.10.2 dev h1-eth0")

    h3.cmd("sudo ip route add 192.168.10.0/24 via 192.168.20.1 dev h3-eth0")

    c7.start()

    print "*** Running CLI"

    CLI( net )

    print "*** Stopping network"

    net.stop()

 

if __name__ == '__main__':

    setLogLevel( 'info' )

    topology()

 

 

[Execution]

 

 

Dr. Chih-Heng Ke (smallko@gmail.com)

Department of Computer Science and Information Engineering,

National Quemoy University, Kinmen, Taiwan