Exemplo n.º 1
0
Arquivo: main.c Projeto: azhgul/AODV-1
void host_init(char *ifname)
{
    struct sockaddr_in *ina;
    char buf[1024], tmp_ifname[IFNAMSIZ],
	ifnames[(IFNAMSIZ + 1) * MAX_NR_INTERFACES], *iface;
    struct ifconf ifc;
    struct ifreq ifreq, *ifr;
    int i, iw_sock, if_sock = 0;

    memset(&this_host, 0, sizeof(struct host_info));
    memset(dev_indices, 0, sizeof(unsigned int) * MAX_NR_INTERFACES);

    if (!ifname) {
	/* No interface was given... search for first wireless. */
	iw_sock = socket(PF_INET, SOCK_DGRAM, 0);
	ifc.ifc_len = sizeof(buf);
	ifc.ifc_buf = buf;
	if (ioctl(iw_sock, SIOCGIFCONF, &ifc) < 0) {
	    fprintf(stderr, "Could not get wireless info\n");
	    exit(-1);
	}
	ifr = ifc.ifc_req;
	for (i = ifc.ifc_len / sizeof(struct ifreq); i >= 0; i--, ifr++) {
	    struct iwreq req;

	    strcpy(req.ifr_name, ifr->ifr_name);
	    if (ioctl(iw_sock, SIOCGIWNAME, &req) >= 0) {
		strcpy(tmp_ifname, ifr->ifr_name);
		break;
	    }
	}
	/* Did we find a wireless interface? */
	if (!strlen(tmp_ifname)) {
	    fprintf(stderr, "\nCould not find a wireless interface!\n");
	    fprintf(stderr, "Use -i <interface> to override...\n\n");
	    exit(-1);
	}
	strcpy(ifreq.ifr_name, tmp_ifname);
	if (ioctl(iw_sock, SIOCGIFINDEX, &ifreq) < 0) {
	    alog(LOG_ERR, errno, __FUNCTION__,
		 "Could not get index of %s", tmp_ifname);
	    close(if_sock);
	    exit(-1);
	}
	close(iw_sock);

	ifname = tmp_ifname;

	alog(LOG_NOTICE, 0, __FUNCTION__,
	     "Attaching to %s, override with -i <if1,if2,...>.", tmp_ifname);
    }

    strcpy(ifnames, ifname);

    /* Intitialize the local sequence number an rreq_id to zero */
    this_host.seqno = 1;
    this_host.rreq_id = 0;

    /* Zero interfaces enabled so far... */
    this_host.nif = 0;

    gettimeofday(&this_host.bcast_time, NULL);

    /* Find the indices of all interfaces to broadcast on... */
    if_sock = socket(AF_INET, SOCK_DGRAM, 0);

    iface = strtok(ifname, ",");

    /* OK, now lookup interface information, and store it... */
    do {
	strcpy(ifreq.ifr_name, iface);
	if (ioctl(if_sock, SIOCGIFINDEX, &ifreq) < 0) {
	    alog(LOG_ERR, errno, __FUNCTION__, "Could not get index of %s",
		 iface);
	    close(if_sock);
	    exit(-1);
	}
	this_host.devs[this_host.nif].ifindex = ifreq.ifr_ifindex;

	dev_indices[this_host.nif++] = ifreq.ifr_ifindex;

	strcpy(DEV_IFINDEX(ifreq.ifr_ifindex).ifname, iface);

	/* Get IP-address of interface... */
	ina = get_if_info(iface, SIOCGIFADDR);

	if (ina == NULL)
	    exit(-1);

	DEV_IFINDEX(ifreq.ifr_ifindex).ipaddr = ina->sin_addr;

	/* Get netmask of interface... */
	ina = get_if_info(iface, SIOCGIFNETMASK);

	if (ina == NULL)
	    exit(-1);

	DEV_IFINDEX(ifreq.ifr_ifindex).netmask = ina->sin_addr;

	ina = get_if_info(iface, SIOCGIFBRDADDR);

	if (ina == NULL)
	    exit(-1);

	DEV_IFINDEX(ifreq.ifr_ifindex).broadcast = ina->sin_addr;

	DEV_IFINDEX(ifreq.ifr_ifindex).enabled = 1;

	if (this_host.nif >= MAX_NR_INTERFACES)
	    break;

    } while ((iface = strtok(NULL, ",")));

    close(if_sock);

    /* Load kernel modules */
    load_modules(ifnames);

    /* Enable IP forwarding and set other kernel options... */
    if (set_kernel_options() < 0) {
	fprintf(stderr, "Could not set kernel options!\n");
	exit(-1);
    }
}
Exemplo n.º 2
0
void host_init(char *ifname) {
  static struct local_host_info host_info;
  struct sockaddr_in *ina;
  char buf[1024];
  struct ifconf ifc;
  struct ifreq *ifr;
  int i, iw_sock;

  /* Make sure the global "this_host" pointer points to the info
     structure so that the information can be accessed from
     outside this function... */
  memset(&host_info, '\0', sizeof(struct local_host_info));
  this_host = &host_info;
  
  /* Find the first wireless interface */
  if(ifname != NULL) 
    strcpy(host_info.ifname, ifname);
  else {
    iw_sock= socket(AF_INET, SOCK_DGRAM, 0);
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(iw_sock, SIOCGIFCONF, &ifc) < 0) {
      fprintf(stderr, "Could not get wireless info\n");
      exit(-1);
    }
    ifr = ifc.ifc_req;
    for(i = ifc.ifc_len / sizeof(struct ifreq); i >= 0; i--, ifr++) {
      struct iwreq req;
	strcpy(req.ifr_name, ifr->ifr_name);
	if (ioctl(iw_sock, SIOCGIWNAME, &req) >= 0) {
	  strcpy(host_info.ifname, ifr->ifr_name);
	  break;
	}
    }
    close(iw_sock);
    
    /* Did we find a wireless interface? */
    if(host_info.ifname[0] == '\0') {
      fprintf(stderr, "Could not find a wireless interface!\n");
      fprintf(stderr, "Use -i <interface> to override...\n");
      exit(-1);
    }
  }
  
  /* Load required kernel modules */
  load_modules(host_info.ifname);
  
  /* Get IP-address of interface... */
  ina = get_if_info(host_info.ifname, SIOCGIFADDR);
  if(ina == NULL)
    exit(-1);
  
  /* Remember our host IP address... */
  host_info.ipaddr = ntohl(ina->sin_addr.s_addr);

  /* printf("Address of interface %s is %s\n", host_info.ifname,  */
  /* 	 ip_to_str(host_info.ipaddr)); */

  /* Get netmask of interface... */
  ina = get_if_info(host_info.ifname, SIOCGIFNETMASK);
  if(ina == NULL)
    exit(-1);
  
  /* Remember the netmask */
  host_info.netmask = ntohl(ina->sin_addr.s_addr);

  /* printf("Netmask of interface %s is %s\n", host_info.ifname,  */
  /* 	 ip_to_str(host_info.netmask)); */
  
  ina = get_if_info(host_info.ifname, SIOCGIFBRDADDR);
  if(ina == NULL)
    exit(-1);

  host_info.broadcast = ntohl(ina->sin_addr.s_addr);
  /* printf("Broadcast address is %s\n", ip_to_str(host_info.broadcast)); */

  /* Enable IP forwarding and set other kernel options... */
  if(set_kernel_options(host_info.ifname) < 0) {
    fprintf(stderr, "Could not set kernel options!\n");
    exit(-1);
  }
 
  /* Add broadcast route (255.255.255.255) */
  k_add_rte(AODV_BROADCAST, 0, 0, 0); 
  
  /* Intitialize the local sequence number an flood_id to zero */
  host_info.seqno = 0;
  host_info.flood_id = 0;
 
}