示例#1
0
static void argv_to_hosts(int argc, char *argv[])
{
	int err, i;
	struct sockaddr_in *address;
	struct knet_host *host;
	struct knet_listener *listener;

	listener = malloc(sizeof(struct knet_listener));

	if (listener == NULL) {
		printf("Unable to create listener\n");
		exit(EXIT_FAILURE);
	}

	memset(listener, 0, sizeof(struct knet_listener));

	address = (struct sockaddr_in *) &listener->address;

	address->sin_family = AF_INET;
	err = tok_inaddrport(argv[1], address);

	if (err < 0) {
		printf("Unable to convert ip address: %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	err = knet_listener_add(knet_h, listener);

	if (err != 0) {
		printf("Unable to start knet listener\n");
		exit(EXIT_FAILURE);
	}

	for (i = 2; i < argc; i++) {
		if (knet_host_add(knet_h, i - 1) != 0) {
			printf("Unable to add new knet_host\n");
			exit(EXIT_FAILURE);
		}

		knet_host_get(knet_h, i - 1, &host);

		host->link[0].sock = listener->sock;
		host->link[0].address.ss_family = AF_INET;

		knet_link_timeout(&host->link[0], 1000, 5000, 2048);

		host->link[0].ready = 1;

		err = tok_inaddrport(argv[i],
				(struct sockaddr_in *) &host->link[0].address);

		if (err < 0) {
			printf("Unable to convert ip address: %s", argv[i]);
			exit(EXIT_FAILURE);
		}

		knet_host_release(knet_h, &host);
	}
}
示例#2
0
static void argv_to_hosts(int argc, char *argv[])
{
	int err, i;
	struct knet_host *host;
	struct knet_host *tmp_host;

	for (i = 2; i < argc; i++) {
		if (!strncmp(argv[i], "crypto", 6))
			continue;
		if (!strncmp(argv[i], "debug", 5))
			continue;

		if (knet_host_add(knet_h, i - 1) != 0) {
			printf("Unable to add new knet_host\n");
			exit(EXIT_FAILURE);
		}

		knet_host_get(knet_h, i - 1, &tmp_host);
		host = tmp_host;
		knet_host_release(knet_h, &tmp_host);

		err = tok_inaddrport(argv[1], (struct sockaddr_in *) &host->link[0].src_addr);
		if (err < 0) {
			printf("Unable to convert ip address: %s", argv[i]);
			exit(EXIT_FAILURE);
		}
		strncpy(host->link[0].src_ipaddr, src_host, KNET_MAX_HOST_LEN - 1);
		strncpy(host->link[0].src_port, src_port, 5);
		err = tok_inaddrport(argv[i],
				(struct sockaddr_in *) &host->link[0].dst_addr);
		if (err < 0) {
			printf("Unable to convert ip address: %s", argv[i]);
			exit(EXIT_FAILURE);
		}

		knet_link_timeout(knet_h, host->node_id, &host->link[0], 1000, 5000, 2048);
		knet_link_enable(knet_h, host->node_id, &host->link[0], 1);
	}
}