예제 #1
0
static int
uinet_demo_base_start(struct uinet_demo_config *cfg, uinet_instance_t uinst,
		      struct ev_loop *loop)
{
	uinet_if_t uif;
	
	cfg->uinst = uinst;
	cfg->loop = loop;

	if (cfg->copy_to != NULL) {
		cfg->copy_uif = uinet_iffind_byname(cfg->uinst, cfg->copy_to);
		if (cfg->copy_uif == NULL)
			return (UINET_EINVAL);
		if (cfg->copy_every) {
			cfg->copy_mode = UINET_IP_COPY_MODE_RX;
			if (cfg->copy_every > 1)
				cfg->copy_mode |= UINET_IP_COPY_MODE_MAYBE;
			else
				cfg->copy_mode |= UINET_IP_COPY_MODE_ON;
		}
	} else
		cfg->copy_uif = NULL;
		
	return (0);
}
예제 #2
0
int
uinet_getifstat(const char *name, struct uinet_ifstat *stat)
{
	struct uinet_config_if *ifcfg;
	struct ifnet *ifp;

	ifcfg = uinet_iffind_byname(name);
	if (NULL == ifcfg) {
		printf("could not find interface %s\n", name);
		return (EINVAL);
	}

	ifp = ifnet_byindex_ref(ifcfg->ifindex);
	if (NULL == ifp) {
		printf("could not find interface %s by index\n", name);
		return (EINVAL);
	}
	
	stat->ifi_ipackets   = ifp->if_data.ifi_ipackets;
	stat->ifi_ierrors    = ifp->if_data.ifi_ierrors;
	stat->ifi_opackets   = ifp->if_data.ifi_opackets;
	stat->ifi_oerrors    = ifp->if_data.ifi_oerrors;
	stat->ifi_collisions = ifp->if_data.ifi_collisions;
	stat->ifi_ibytes     = ifp->if_data.ifi_ibytes;
	stat->ifi_obytes     = ifp->if_data.ifi_obytes;
	stat->ifi_imcasts    = ifp->if_data.ifi_imcasts;
	stat->ifi_omcasts    = ifp->if_data.ifi_omcasts;
	stat->ifi_iqdrops    = ifp->if_data.ifi_iqdrops;
	stat->ifi_noproto    = ifp->if_data.ifi_noproto;
	stat->ifi_hwassist   = ifp->if_data.ifi_hwassist;
	stat->ifi_epoch      = ifp->if_data.ifi_epoch;
	stat->ifi_icopies    = ifp->if_data.ifi_icopies;
	stat->ifi_izcopies   = ifp->if_data.ifi_izcopies;
	stat->ifi_ocopies    = ifp->if_data.ifi_ocopies;
	stat->ifi_ozcopies   = ifp->if_data.ifi_ozcopies;

	if_rele(ifp);

	return (0);
}
예제 #3
0
static int
uinet_ifconfig_begin(struct socket **so, struct ifreq *ifr, const char *name)
{
	struct thread *td = curthread;
	struct uinet_config_if *ifcfg;
	int error;

	ifcfg = uinet_iffind_byname(name);
	if (NULL == ifcfg) {
		printf("could not find interface %s\n", name);
		return (EINVAL);
	}

	error = socreate(PF_INET, so, SOCK_DGRAM, 0, td->td_ucred, td);
	if (0 != error) {
		printf("ifconfig socket creation failed (%d)\n", error);
		return (error);
	}

	snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), "%s", ifcfg->name);
	
	return (0);
}
예제 #4
0
static int
nproxy_start(struct uinet_demo_config *cfg, uinet_instance_t uinst, struct ev_loop *loop)
{
	struct uinet_demo_nproxy *nproxy = (struct uinet_demo_nproxy *)cfg;
	struct uinet_socket *listen_socket = NULL;
	struct ev_uinet_ctx *soctx = NULL;
	struct uinet_in_addr addr;
	int optlen, optval;
	int error;
	struct uinet_sockaddr_in sin;

	if (uinet_inet_pton(UINET_AF_INET, nproxy->listen_addr, &addr) <= 0) {
		printf("%s: Malformed address %s\n", nproxy->cfg.name, nproxy->listen_addr);
		error = UINET_EINVAL;
		goto fail;
	}

	nproxy->outbound_if = uinet_iffind_byname(nproxy->cfg.uinst, nproxy->outbound_if_name);
	if (nproxy->outbound_if == NULL) {
		printf("%s: Unknown outbound interface %s\n", nproxy->cfg.name,
		       nproxy->outbound_if_name);
		error = UINET_EINVAL;
		goto fail;
	}
	
	error = uinet_socreate(nproxy->cfg.uinst, UINET_PF_INET, &listen_socket, UINET_SOCK_STREAM, 0);
	if (0 != error) {
		printf("%s: Listen socket creation failed (%d)\n", nproxy->cfg.name, error);
		goto fail;
	}

	soctx = ev_uinet_attach(listen_socket);
	if (NULL == soctx) {
		printf("%s: Failed to alloc libev socket context\n", nproxy->cfg.name);
		error = UINET_ENOMEM;
		goto fail;
	}

	if (nproxy->promisc) {
		if ((error = uinet_make_socket_promiscuous(listen_socket, NULL))) {
			printf("%s: Failed to make listen socket promiscuous (%d)\n",
			       nproxy->cfg.name, error);
			goto fail;
		}
	}

	if (cfg->copy_mode) {
		if ((error = uinet_sosetcopymode(listen_socket, cfg->copy_mode,
						 cfg->copy_limit, cfg->copy_uif))) {
			printf("%s: Failed to set copy mode (%d)\n",
			       nproxy->cfg.name, error);
			goto fail;
		}
	}
	
	/*
	 * Socket needs to be non-blocking to work with the event system
	 */
	uinet_sosetnonblocking(listen_socket, 1);

	/* Set NODELAY on the listen socket so it will be set on all
	 * accepted sockets via inheritance.
	 */
	optlen = sizeof(optval);
	optval = 1;
	if ((error = uinet_sosetsockopt(listen_socket, UINET_IPPROTO_TCP, UINET_TCP_NODELAY,
					&optval, optlen))) {
		printf("%s: Failed to set TCP_NODELAY (%d)\n", nproxy->cfg.name, error);
		goto fail;
	}

	/* Listen on all VLANs */
	if ((error = uinet_setl2info2(listen_socket, NULL, NULL,
				      UINET_INL2I_TAG_ANY, NULL))) {
		printf("%s: Listen socket L2 info set failed (%d)\n",
		       nproxy->cfg.name, error);
		goto fail;
	}

	nproxy->listen_socket = listen_socket;

	memset(&sin, 0, sizeof(struct uinet_sockaddr_in));
	sin.sin_len = sizeof(struct uinet_sockaddr_in);
	sin.sin_family = UINET_AF_INET;
	sin.sin_addr = addr;
	sin.sin_port = htons(nproxy->listen_port);
	error = uinet_sobind(listen_socket, (struct uinet_sockaddr *)&sin);
	if (0 != error) {
		printf("%s: Bind to %s:%u failed\n", nproxy->cfg.name,
		       nproxy->listen_addr, nproxy->listen_port);
		goto fail;
	}
	
	error = uinet_solisten(nproxy->listen_socket, -1);
	if (0 != error) {
		printf("%s: Listen on %s:%u failed\n", nproxy->cfg.name,
		       nproxy->listen_addr, nproxy->listen_port);
		goto fail;
	}

	if (nproxy->cfg.verbose)
		printf("%s: Listening on %s:%u\n", nproxy->cfg.name,
		       nproxy->listen_addr, nproxy->listen_port);

	/*
	 * Set up a read watcher to accept new connections
	 */
	ev_init(&nproxy->listen_watcher, nproxy_accept_cb);
	ev_uinet_set(&nproxy->listen_watcher, soctx, EV_READ);
	nproxy->listen_watcher.data = nproxy;
	ev_uinet_start(nproxy->cfg.loop, &nproxy->listen_watcher);

	return (0);

fail:
	if (soctx) ev_uinet_detach(soctx);
	if (listen_socket) uinet_soclose(listen_socket);

	return (error);
}