Fixed for hostemu on win32
authorAndreas Tonnesen <andreto@olsr.org>
Mon, 30 May 2005 13:50:27 +0000 (13:50 +0000)
committerAndreas Tonnesen <andreto@olsr.org>
Mon, 30 May 2005 13:50:27 +0000 (13:50 +0000)
src/win32/ifnet.c
src/win32/net.c

index 10ed54a..f1014ae 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: ifnet.c,v 1.26 2005/05/30 13:13:48 kattemat Exp $
+ * $Id: ifnet.c,v 1.27 2005/05/30 13:50:27 kattemat Exp $
  */
 
 #include "interfaces.h"
@@ -437,6 +437,185 @@ void RemoveInterface(struct olsr_if *IntConf)
   }
 }
 
+int add_hemu_if(struct olsr_if *iface)
+{
+  struct interface *ifp;
+  union olsr_ip_addr null_addr;
+  olsr_u32_t addr[4];
+
+  if(!iface->host_emul)
+    return -1;
+
+  ifp = olsr_malloc(sizeof (struct interface), "Interface update 2");
+
+  memset(ifp, 0, sizeof (struct interface));
+
+  iface->configured = OLSR_TRUE;
+  iface->interf = ifp;
+
+  ifp->is_hcif = OLSR_TRUE;
+  ifp->int_name = olsr_malloc(strlen("hcif01") + 1, "Interface update 3");
+  ifp->int_metric = 0;
+
+  strcpy(ifp->int_name, "hcif01");
+
+  OLSR_PRINTF(1, "Adding %s(host emulation):\n", ifp->int_name)
+
+  OLSR_PRINTF(1, "       Address:%s\n", olsr_ip_to_string(&iface->hemu_ip));
+
+  OLSR_PRINTF(1, "       Index:%d\n", iface->index);
+
+  OLSR_PRINTF(1, "       NB! This is a emulated interface\n       that does not exist in the kernel!\n");
+
+  ifp->int_next = ifnet;
+  ifnet = ifp;
+
+  memset(&null_addr, 0, ipsize);
+  if(COMP_IP(&null_addr, &main_addr))
+    {
+      COPY_IP(&main_addr, &iface->hemu_ip);
+      OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&main_addr))
+    }
+
+  /* setting the interfaces number*/
+  ifp->if_nr = iface->index;
+
+  ifp->int_mtu = OLSR_DEFAULT_MTU;
+
+  ifp->int_mtu -= (olsr_cnf->ip_version == AF_INET6) ? UDP_IPV6_HDRSIZE : UDP_IPV4_HDRSIZE;
+
+  /* Set up buffer */
+  net_add_buffer(ifp);
+
+
+  if(olsr_cnf->ip_version == AF_INET)
+    {
+      struct sockaddr_in sin;
+
+      memset(&sin, 0, sizeof(sin));
+
+      sin.sin_family = AF_INET;
+      sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+      sin.sin_port = htons(10150);
+     /* IP version 4 */
+      ifp->ip_addr.v4 = iface->hemu_ip.v4;
+
+      memcpy(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, &iface->hemu_ip, ipsize);
+      
+      /*
+       *We create one socket for each interface and bind
+       *the socket to it. This to ensure that we can control
+       *on what interface the message is transmitted
+       */
+      
+      ifp->olsr_socket = gethemusocket(&sin);
+      
+      if (ifp->olsr_socket < 0)
+       {
+         fprintf(stderr, "Could not initialize socket... exiting!\n\n");
+         exit(1);
+       }
+
+    }
+  else
+    {
+      /* IP version 6 */
+      memcpy(&ifp->ip_addr, &iface->hemu_ip, ipsize);
+
+#if 0      
+      /*
+       *We create one socket for each interface and bind
+       *the socket to it. This to ensure that we can control
+       *on what interface the message is transmitted
+       */
+      
+      ifp->olsr_socket = gethcsocket6(&addrsock6, bufspace, ifp->int_name);
+      
+      join_mcast(ifp, ifp->olsr_socket);
+      
+      if (ifp->olsr_socket < 0)
+       {
+         fprintf(stderr, "Could not initialize socket... exiting!\n\n");
+         exit(1);
+       }
+      
+#endif
+    }
+
+  /* Send IP as first 4/16 bytes on socket */
+  memcpy(addr, iface->hemu_ip.v6.s6_addr, ipsize);
+  addr[0] = htonl(addr[0]);
+  addr[1] = htonl(addr[1]);
+  addr[2] = htonl(addr[2]);
+  addr[3] = htonl(addr[3]);
+
+  if(send(ifp->olsr_socket, addr , ipsize, 0) != (int)ipsize)
+    {
+      fprintf(stderr, "Error sending IP!");
+    }  
+  
+  /* Register socket */
+  add_olsr_socket(ifp->olsr_socket, &olsr_input_hostemu);
+
+
+  if (olsr_cnf->lq_level == 0)
+    {
+      olsr_register_scheduler_event(&generate_hello, 
+                                    ifp, 
+                                    iface->cnf->hello_params.emission_interval, 
+                                    0, 
+                                    NULL);
+      olsr_register_scheduler_event(&generate_tc, 
+                                    ifp, 
+                                    iface->cnf->tc_params.emission_interval,
+                                    0, 
+                                    NULL);
+    }
+
+  else
+    {
+      olsr_register_scheduler_event(&olsr_output_lq_hello, 
+                                    ifp, 
+                                    iface->cnf->hello_params.emission_interval, 
+                                    0, 
+                                    NULL);
+      olsr_register_scheduler_event(&olsr_output_lq_tc, 
+                                    ifp, 
+                                    iface->cnf->tc_params.emission_interval,
+                                    0, 
+                                    NULL);
+    }
+
+  olsr_register_scheduler_event(&generate_mid, 
+                               ifp, 
+                               iface->cnf->mid_params.emission_interval,
+                               0, 
+                               NULL);
+  olsr_register_scheduler_event(&generate_hna, 
+                               ifp, 
+                               iface->cnf->hna_params.emission_interval,
+                               0, 
+                               NULL);
+
+  /* Recalculate max jitter */
+
+  if((max_jitter == 0) || ((iface->cnf->hello_params.emission_interval / 4) < max_jitter))
+    max_jitter = iface->cnf->hello_params.emission_interval / 4;
+
+  /* Recalculate max topology hold time */
+  if(max_tc_vtime < iface->cnf->tc_params.emission_interval)
+    max_tc_vtime = iface->cnf->tc_params.emission_interval;
+
+  ifp->hello_etime = iface->cnf->hello_params.emission_interval;
+  ifp->valtimes.hello = double_to_me(iface->cnf->hello_params.validity_time);
+  ifp->valtimes.tc = double_to_me(iface->cnf->tc_params.validity_time);
+  ifp->valtimes.mid = double_to_me(iface->cnf->mid_params.validity_time);
+  ifp->valtimes.hna = double_to_me(iface->cnf->hna_params.validity_time);
+
+  return 1;
+}
+
 int chk_if_changed(struct olsr_if *IntConf)
 {
   struct interface *Int;
index c721c26..ff8380d 100644 (file)
@@ -36,7 +36,7 @@
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: net.c,v 1.18 2005/03/21 02:17:37 tlopatic Exp $
+ * $Id: net.c,v 1.19 2005/05/30 13:50:27 kattemat Exp $
  */
 
 #if defined WINCE
@@ -67,6 +67,35 @@ void PError(char *);
 void DisableIcmpRedirects(void);
 int disable_ip_forwarding(int Ver);
 
+
+int
+gethemusocket(struct sockaddr_in *pin)
+{
+  int sock;
+
+  OLSR_PRINTF(1, "       Connecting to switch daemon port 10150...");
+
+  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
+    {
+      perror("hcsocket");
+      return (-1);
+    }
+
+  /* connect to PORT on HOST */
+  if (connect(sock,(struct sockaddr *) pin, sizeof(*pin)) < 0) 
+    {
+      printf("FAILED\n");
+      fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
+      printf("connection refused\n");
+      return (-1);
+    }
+
+  printf("OK\n");
+
+  /* Keep TCP socket blocking */  
+  return (sock);
+}
+
 int getsocket(struct sockaddr *Addr, int BuffSize, char *Int)
 {
   int Sock;