Пример #1
0
/**
 * Start a "discovery rpc" sequence.
 *
 * @param np		existing NAT-PMP gateway (NULL if unknown yet)
 * @param retries	amount of retries before timeouting
 * @param cb		callback to invoke on completion / timeout
 * @param arg		user-defined callback argument
 */
static void
natpmp_rpc_discover(natpmp_t *np, unsigned retries,
	natpmp_discover_cb_t cb, void *arg)
{
	struct natpmp_rpc *rd;
	host_addr_t addr;
	pmsg_t *mb;

	if (np != NULL) {
		natpmp_check(np);
		addr = np->gateway;
	} else {
		/*
		 * If we can't determine the default gateway, we can't go much further.
		 * We notify of the discovery failure synchronously.
		 */

		if (0 != getgateway(&addr)) {
			if (GNET_PROPERTY(natpmp_debug))
				g_warning("NATPMP cannot find default gateway");
			(*cb)(FALSE, NULL, arg);
			return;
		} else {
			if (GNET_PROPERTY(natpmp_debug)) {
				g_info("NATPMP gateway is %s", host_addr_to_string(addr));
			}
		}
	}

	/*
	 * Build the discovery request:
	 *
     *    0                   1
     *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
     *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     *   | Vers = 0      | OP = 0        |
     *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 */

	mb = pmsg_new(PMSG_P_DATA, NULL, 2);
	pmsg_write_u8(mb, NATPMP_VERSION);
	pmsg_write_u8(mb, NATPMP_OP_DISCOVERY);

	/*
	 * Initiate asynchronous iteration discovery.
	 */

	rd = natpmp_rpc_alloc(np, addr, NATPMP_OP_DISCOVERY, mb);
	rd->cb.discovery = cb;
	rd->arg = arg;
	if (retries != 0)
		rd->retries = MIN(retries, rd->retries);

	cq_main_insert(1, natpmp_rpc_iterate, rd);
}
Пример #2
0
/**
 * Collect entropy from current IP gateway.
 */
static void
entropy_collect_gateway(SHA1Context *ctx)
{
	host_addr_t addr;

	ZERO(&addr);

	if (-1 == getgateway(&addr))
		sha1_feed_ulong(ctx, errno);

	SHA1Input(ctx, &addr, sizeof addr);
}
Пример #3
0
int main(int argc, char *argv[])
{
	
	struct ifaddrs *ifaddr, *ifa;
	int family, s;
	char host[NI_MAXHOST], mask4[NI_MAXHOST], mask6[NI_MAXHOST];
	
	if (getifaddrs(&ifaddr) == -1) {
		perror("getifaddrs");
		exit(EXIT_FAILURE);
	}
	
	struct iface *ifl_data = malloc(sizeof(struct iface));
	ifl_data->if_name = NULL;
	
	struct iface_list *ifl = malloc(sizeof(struct iface_list));
	ifl->ifl_next = NULL;
	ifl->ifl_data = NULL;
	
	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr == NULL) {
			continue;
		}
		
		ifl->ifl_data = NULL;
		ifl_data->if_addr_ipv4 = NULL;
		ifl_data->if_addr_ipv6 = NULL;
		ifl_data->if_addr_mask4 = NULL;
		ifl_data->if_addr_mask6 = NULL;
		
		//printf(" ==== for ifa_name:: %s\n", ifa->ifa_name);
		
		ifl_data->if_name = ifa->ifa_name;
		
		family = ifa->ifa_addr->sa_family;
		
		if (family == AF_INET) {
			getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
			ifl_data->if_addr_ipv4 = host;
			getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in), mask4, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
			ifl_data->if_addr_mask4 = mask4;
		}
		
		if (family == AF_INET6) {
			void *addr = NULL;
			addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
			char addrs[INET6_ADDRSTRLEN];
			inet_ntop(AF_INET6, addr, addrs, INET6_ADDRSTRLEN);
			ifl_data->if_addr_ipv6 = addrs;
			
			void *mask = NULL;
			mask = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
			char masks[INET6_ADDRSTRLEN];
			inet_ntop(AF_INET6, mask, masks, INET6_ADDRSTRLEN);
			ifl_data->if_addr_mask6 = masks;
			
//			getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in), mask6, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
			printf("mask6: %s\n", masks);
//			ifl_data->if_addr_mask6 = mask6;
		}
		
		//ifl_add_once(ifl, ifl_data);
		ifl_add_once_ng(ifl, ifl_data);
		
	}
	
	printf(" ==== \n");
	ifl_enum(ifl);
	printf(" ==== \n");
	

	struct iface_list *ifc;
	ifc = NULL;
//	int in = 0;
	for (ifc = ifl->ifl_next; ifc; ifc = ifc->ifl_next) {
//		if (ifc->ifl_data && ifc->ifl_data->if_name) {
			ifc->ifl_data->if_addr_gateway = NULL;
//		printf("if_name: %x\n", &ifc->ifl_data);
//		printf("if_name: %s\n", ifc->ifl_data->if_name);
		getgateway(ifc->ifl_data);
		if (!ifc->ifl_data->if_addr_gateway) {
				ifc->ifl_data->if_addr_gateway = malloc(3);
				strncpy(ifc->ifl_data->if_addr_gateway, "*\0", 2);
//			printf("%s\n", ifc->ifl_data->if_addr_gateway);
//		}
//		}
	}
	}
	
	//struct iface_list *ifc;
	for (ifc = ifl; ifc; ifc = ifc->ifl_next) {
		if (ifc->ifl_data && ifc->ifl_data->if_name) {
			printf("name: %s\n", ifc->ifl_data->if_name);
			printf("\tipv4: %s\n", ifc->ifl_data->if_addr_ipv4);
			printf("\tipv6: %s", ifc->ifl_data->if_addr_ipv6);
			if (ifc->ifl_data->if_addr_mask6) {
				printf("/%d", get_ipv6_mask(ifc->ifl_data->if_addr_mask6));
			}
			printf("\n");
			printf("\tmask4: %s\n", ifc->ifl_data->if_addr_mask4);
			printf("\tmask6: %s\n", ifc->ifl_data->if_addr_mask6);
			if (ifc->ifl_data->if_addr_gateway)
				printf("\tgateway: %s\n", ifc->ifl_data->if_addr_gateway);
		}
	}
	
}