예제 #1
0
파일: netif.c 프로젝트: xskali12/canshark
/**
 * Add a network interface to the list of lwIP netifs.
 *
 * @param netif a pre-allocated netif structure
 * @param ipaddr IP address for the new netif
 * @param netmask network mask for the new netif
 * @param gw default gateway IP address for the new netif
 * @param state opaque data passed to the new netif
 * @param init callback function that initializes the interface
 * @param input callback function that is called to pass
 * ingress packets up in the protocol layer stack.
 *
 * @return netif, or NULL if failed.
 */
struct netif *netif_add(struct netif *netif, ip_addr_t *ipaddr,
			ip_addr_t *netmask, ip_addr_t *gw, void *state,
			netif_init_fn init, netif_input_fn input)
{
	/* reset new interface configuration state */
	ip_addr_set_zero(&netif->ip_addr);
	ip_addr_set_zero(&netif->netmask);
	ip_addr_set_zero(&netif->gw);
	netif->flags = 0;
#if LWIP_DHCP
	/* netif not under DHCP control by default */
	netif->dhcp = NULL;
#endif				/* LWIP_DHCP */
#if LWIP_AUTOIP
	/* netif not under AutoIP control by default */
	netif->autoip = NULL;
#endif				/* LWIP_AUTOIP */
#if LWIP_NETIF_STATUS_CALLBACK
	netif->status_callback = NULL;
#endif				/* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
	netif->link_callback = NULL;
#endif				/* LWIP_NETIF_LINK_CALLBACK */
#if LWIP_IGMP
	netif->igmp_mac_filter = NULL;
#endif				/* LWIP_IGMP */
#if ENABLE_LOOPBACK
	netif->loop_first = NULL;
	netif->loop_last = NULL;
#endif				/* ENABLE_LOOPBACK */

	/* remember netif specific state information data */
	netif->state = state;
	netif->num = netif_num++;
	netif->input = input;
	NETIF_SET_HWADDRHINT(netif, NULL);
#if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
	netif->loop_cnt_current = 0;
#endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */

	netif_set_addr(netif, ipaddr, netmask, gw);

	/* call user specified initialization function for netif */
	if (init(netif) != ERR_OK) {
		return NULL;
	}

	/* add this netif to the list */
	netif->next = netif_list;
	netif_list = netif;
	snmp_inc_iflist();

#if LWIP_IGMP
	/* start IGMP processing */
	if (netif->flags & NETIF_FLAG_IGMP) {
		igmp_start(netif);
	}
#endif /* LWIP_IGMP */

	LWIP_DEBUGF(NETIF_DEBUG,
		("netif: added interface %c%c IP addr " IP4_F " netmask " IP4_F " gw " IP4_F "\n",
		 netif->name[0], netif->name[1], IP4_FV(ipaddr), IP4_FV(netmask), IP4_FV(gw)));
	return netif;
}
예제 #2
0
파일: netif.c 프로젝트: AjeyBohare/minix
/**
 * Add a network interface to the list of lwIP netifs.
 *
 * @param netif a pre-allocated netif structure
 * @param ipaddr IP address for the new netif
 * @param netmask network mask for the new netif
 * @param gw default gateway IP address for the new netif
 * @param state opaque data passed to the new netif
 * @param init callback function that initializes the interface
 * @param input callback function that is called to pass
 * ingress packets up in the protocol layer stack.
 *
 * @return netif, or NULL if failed.
 */
struct netif *
netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
  ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
{
#if LWIP_IPV6
  u32_t i;
#endif

  LWIP_ASSERT("No init function given", init != NULL);

  /* reset new interface configuration state */
  ip_addr_set_zero(&netif->ip_addr);
  ip_addr_set_zero(&netif->netmask);
  ip_addr_set_zero(&netif->gw);
#if LWIP_IPV6
  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
    ip6_addr_set_zero(&netif->ip6_addr[i]);
    netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
  }
  netif->output_ip6 = netif_null_output_ip6;
#endif /* LWIP_IPV6 */
  netif->flags = 0;
#if LWIP_DHCP
  /* netif not under DHCP control by default */
  netif->dhcp = NULL;
#endif /* LWIP_DHCP */
#if LWIP_AUTOIP
  /* netif not under AutoIP control by default */
  netif->autoip = NULL;
#endif /* LWIP_AUTOIP */
#if LWIP_IPV6_AUTOCONFIG
  /* IPv6 address autoconfiguration not enabled by default */
  netif->ip6_autoconfig_enabled = 0;
#endif /* LWIP_IPV6_AUTOCONFIG */
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
  netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
#endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
#if LWIP_IPV6_DHCP6
  /* netif not under DHCPv6 control by default */
  netif->dhcp6 = NULL;
#endif /* LWIP_IPV6_DHCP6 */
#if LWIP_NETIF_STATUS_CALLBACK
  netif->status_callback = NULL;
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
  netif->link_callback = NULL;
#endif /* LWIP_NETIF_LINK_CALLBACK */
#if LWIP_IGMP
  netif->igmp_mac_filter = NULL;
#endif /* LWIP_IGMP */
#if LWIP_IPV6 && LWIP_IPV6_MLD
  netif->mld_mac_filter = NULL;
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
#if ENABLE_LOOPBACK
  netif->loop_first = NULL;
  netif->loop_last = NULL;
#endif /* ENABLE_LOOPBACK */

  /* remember netif specific state information data */
  netif->state = state;
  netif->num = netif_num++;
  netif->input = input;
  NETIF_SET_HWADDRHINT(netif, NULL);
#if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  netif->loop_cnt_current = 0;
#endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */

  netif_set_addr(netif, ipaddr, netmask, gw);

  /* call user specified initialization function for netif */
  if (init(netif) != ERR_OK) {
    return NULL;
  }

  /* add this netif to the list */
  netif->next = netif_list;
  netif_list = netif;
  snmp_inc_iflist();

#if LWIP_IGMP
  /* start IGMP processing */
  if (netif->flags & NETIF_FLAG_IGMP) {
    igmp_start(netif);
  }
#endif /* LWIP_IGMP */

  LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
    netif->name[0], netif->name[1]));
  ip_addr_debug_print(NETIF_DEBUG, ipaddr);
  LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  ip_addr_debug_print(NETIF_DEBUG, netmask);
  LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  ip_addr_debug_print(NETIF_DEBUG, gw);
  LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  return netif;
}
예제 #3
0
파일: if.c 프로젝트: balajig/Layer3Switch
void interface_init (struct interface *netif, void *state, netif_input_fn input)
{
    static u8_t         netifnum = 0;

#if LWIP_IPV6
  u32_t i;
#endif

  /* reset new interface configuration state */
  ip_addr_set_zero(&netif->ip_addr);
  ip_addr_set_zero(&netif->netmask);
  ip_addr_set_zero(&netif->gw);
#if LWIP_IPV6
  for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
    ip6_addr_set_zero(&netif->ip6_addr[i]);
    netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
  }
#endif /* LWIP_IPV6 */

    netif->flags = 0;
#if LWIP_DHCP
    /* netif not under DHCP control by default */
    netif->dhcp = NULL;
#endif /* LWIP_DHCP */
#if LWIP_AUTOIP
    /* netif not under AutoIP control by default */
    netif->autoip = NULL;
#endif /* LWIP_AUTOIP */
#if LWIP_IPV6_AUTOCONFIG
  /* IPv6 address autoconfiguration not enabled by default */
  netif->ip6_autoconfig_enabled = 0;
#endif /* LWIP_IPV6_AUTOCONFIG */
#if LWIP_IPV6_SEND_ROUTER_SOLICIT
  netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
#endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
#if LWIP_IPV6_DHCP6
  /* netif not under DHCPv6 control by default */
  netif->dhcp6 = NULL;
#endif /* LWIP_IPV6_DHCP6 */
#if LWIP_NETIF_STATUS_CALLBACK
    netif->status_callback = NULL;
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
    netif->link_callback = NULL;
#endif /* LWIP_NETIF_LINK_CALLBACK */
#if LWIP_IGMP
    netif->igmp_mac_filter = NULL;
#endif /* LWIP_IGMP */
#if LWIP_IPV6 && LWIP_IPV6_MLD
  netif->mld_mac_filter = NULL;
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
#if ENABLE_LOOPBACK
    netif->loop_first = NULL;
    netif->loop_last = NULL;
#endif /* ENABLE_LOOPBACK */

    /* remember netif specific state information data */
    netif->state = state;
    netif->num = netifnum++;
    netif->input = input;
#if LWIP_NETIF_HWADDRHINT
    netif->addr_hint = NULL;
#endif /* LWIP_NETIF_HWADDRHINT */
#if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
    netif->loop_cnt_current = 0;
#endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */

#ifdef CONFIG_OPENSWITCH_TCP_IP
    ethernetif_init (netif);
#else
    netif->linkoutput = low_level_output;
#endif

    if_zebra_new_hook (netif);

#if 0
    /* call user specified initialization function for netif */
    if (init (netif) != ERR_OK)
    {
        return NULL;
    }
#endif

    /* add this netif to the list */
#ifdef CONFIG_OPENSWITCH_TCP_IP
    snmp_inc_iflist ();
#endif

#if 0
    /* start IGMP processing */
    if (netif->flags & NETIF_FLAG_IGMP)
    {
        igmp_start (netif);
    }
#endif /* LWIP_IGMP */

    LWIP_DEBUGF (NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
                               netif->ifDescr[0], netif->ifDescr[1]));
    LWIP_DEBUGF (NETIF_DEBUG, ("\n"));
    return;
}
예제 #4
0
파일: main.c 프로젝트: ztuowen/tunsocks
int main(int argc, char *argv[])
{
	int c;
	int keep_alive;
	int non_local;
	struct netif *netif;
	int dns_count;
	char *str;
	char *endptr;
	int mtu;
	int fd_in;
	int fd_out;
	ip4_addr_t ipaddr;
	ip4_addr_t netmask;
	ip4_addr_t gateway;
	ip4_addr_t dns;
	struct event_base *base;
	char *pcap_file;
	struct conn_info *local;
	struct conn_info *remote;
	struct conn_info *socks;
	struct conn_info *info;

	ip_addr_set_zero(&ipaddr);
	ip_addr_set_zero(&netmask);
	ip_addr_set_zero(&gateway);

	local = remote = socks = NULL;
	dns_count = 0;
	keep_alive = 0;
	fd_in = 0;
	fd_out = 1;
	mtu = 0;
	pcap_file = NULL;
	non_local = 0;

	signal(SIGPIPE, SIG_IGN);

	base = event_base_new();
	lwip_init();
	libevent_timeouts_init(base);

	if ((str = getenv("INTERNAL_IP4_ADDRESS")))
		ip4addr_aton(str, &ipaddr);

	if ((str = getenv("INTERNAL_IP4_MTU")))
		mtu = strtoul(str, NULL, 0);

	if ((str = getenv("VPNFD")))
		fd_in = fd_out = strtoul(str, NULL, 0);

	if ((str = getenv("CISCO_DEF_DOMAIN"))) {
		endptr = str;
		while ((str = tokenize(endptr, ", ", &endptr)))
			host_add_search(str);
	}

	if ((str = getenv("INTERNAL_IP4_DNS")))	{
		endptr = str;
		while ((str = tokenize(endptr, ", ", &endptr))) {
			ip4addr_aton(str, &dns);
			dns_setserver(dns_count++, &dns);
			free(str);
		}
	}

	while ((c = getopt(argc, argv, "L:D:R:k:m:s:d:i:n:G:p:gh")) != -1) {

		switch (c) {
		case 'L':
			info = parse_conn_info(optarg, 4);
			if (!info)
				print_usage(argv[0]);

			info->next = local;
			local = info;
			break;
		case 'D':
			info = parse_conn_info(optarg, 2);
			if (!info)
				print_usage(argv[0]);

			info->next = socks;
			socks = info;
			break;
		case 'R':
			info = parse_conn_info(optarg, 4);
			if (!info)
				print_usage(argv[0]);

			info->next = remote;
			remote = info;
			break;
		case 'k':
			keep_alive = strtoul(optarg, &endptr, 0);
			if (*endptr)
				print_usage(argv[0]);
			keep_alive *= 1000;
			break;
		case 'm':
			mtu = strtoul(optarg, &endptr, 0);
			if (*endptr)
				print_usage(argv[0]);
			break;
		case 's':
			while ((str = tokenize(optarg, ", ", &optarg)))
				host_add_search(str);
			break;
		case 'd':
			while ((str = tokenize(optarg, ", ", &optarg))) {
				ip4addr_aton(str, &dns);
				dns_setserver(dns_count++, &dns);
				free(str);
			}
			break;
		case 'i':
			ip4addr_aton(optarg, &ipaddr);
			break;
		case 'n':
			ip4addr_aton(optarg, &netmask);
			break;
		case 'G':
			ip4addr_aton(optarg, &gateway);
			break;
#ifdef USE_PCAP
		case 'p':
			pcap_file = strdup(optarg);
			break;
#endif
		case 'g':
			non_local = 1;
			break;
		default:
			print_usage(argv[0]);
		}
	}

	while (local) {
		info = local;
		str = info->bind && info->bind[0] ? info->bind : NULL;
		if (!non_local)
			str = str ? : "localhost";

		if (forward_local(base, str, info->bind_port,
			info->host, info->host_port, keep_alive) < 0)
			return -1;

		local = info->next;
		free_conn_info(info);
	}

	while (socks) {
		info = socks;
		str = info->bind && info->bind[0] ? info->bind : NULL;
		if (!non_local)
			str = str ? : "localhost";
		if (socks_listen(base, str, info->bind_port, keep_alive) < 0)
			return -1;
		socks = socks->next;
		free_conn_info(info);
	}

	while (remote) {
		info = remote;
		if (forward_remote(base, info->bind_port, info->host,
					info->host_port, keep_alive) < 0)
			return -1;
		remote = info->next;
		free_conn_info(info);
	}

	netif = tunif_add(base, fd_in, fd_out, pcap_file);

	netif_set_ipaddr(netif, &ipaddr);
	netif_set_netmask(netif, &netmask);
	netif_set_gw(netif, &gateway);
	if (mtu)
		netif->mtu = mtu;
	netif_set_up(netif);

	event_base_dispatch(base);

	return 0;
}