Esempio n. 1
0
static int
connect_tcp(fence_req_t *req, fence_auth_type_t auth,
	    void *key, size_t key_len)
{
	int fd = -1;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	char buf[128];

	switch(req->family) {
	case PF_INET:
		memset(&sin, 0, sizeof(sin));
		memcpy(&sin.sin_addr, req->address,
		       sizeof(sin.sin_addr));
		sin.sin_family = PF_INET;
		fd = ipv4_connect(&sin.sin_addr, req->port,
				  5);
		if (fd < 0) {
			printf("Failed to call back\n");
			return -1;
		}
		break;
	case PF_INET6:
		memset(&sin6, 0, sizeof(sin6));
		memcpy(&sin6.sin6_addr, req->address,
		       sizeof(sin6.sin6_addr));
		sin.sin_family = PF_INET6;
		fd = ipv6_connect(&sin6.sin6_addr, req->port,
				  5);

		memset(buf,0,sizeof(buf));
		inet_ntop(PF_INET6, &sin6.sin6_addr, buf, sizeof(buf));

		if (fd < 0) {
			printf("Failed to call back %s\n", buf);
			return -1;
		}
		break;
	default:
		printf("Family = %d\n", req->family);
		return -1;
	}

	/* Noops if auth == AUTH_NONE */
	if (tcp_response(fd, auth, key, key_len, 10) <= 0) {
		printf("Failed to respond to challenge\n");
		close(fd);
		return -1;
	}

	if (tcp_challenge(fd, auth, key, key_len, 10) <= 0) {
		printf("Remote failed challenge\n");
		close(fd);
		return -1;
	}
	return fd;
}
Esempio n. 2
0
int
serial_fence_virt(fence_virt_args_t *args)
{
	struct in_addr ina;
	struct in6_addr in6a;
	serial_req_t req;
	int fd, ret;
	char speed[32], *flags = NULL;
	struct timeval tv;
	serial_resp_t resp;

	if (args->serial.device) {
		strncpy(speed, args->serial.speed, sizeof(speed));

		//printf("Port: %s Speed: %s\n", args->serial.device, speed);

		if ((flags = strchr(speed, ','))) {
			*flags = 0;
			flags++;
		}
	
		fd = open_port(args->serial.device, speed, flags);
		if (fd == -1) {
			perror("open_port");
			return -1;
		}

		hangup(fd, 300000);
	} else {
		fd = -1;
		if (inet_pton(PF_INET, args->serial.address, &ina)) {
			fd = ipv4_connect(&ina, args->net.port, 3);
		} else if (inet_pton(PF_INET6, args->serial.address, &in6a)) {
			fd = ipv6_connect(&in6a, args->net.port, 3);
		}

		if (fd < 0) {
			perror("vmchannel connect");
			printf("Failed to connect to %s:%d\n", args->serial.address,
			       args->net.port);
		}
	}


	memset(&req, 0, sizeof(req));
	req.magic = SERIAL_MAGIC;
	req.request = (uint8_t)args->op;
	gettimeofday(&tv, NULL);
	req.seqno = (int)tv.tv_usec;

	if (args->domain) 
		strncpy((char *)req.domain, args->domain, sizeof(req.domain));
	
	tv.tv_sec = 3;
	tv.tv_usec = 0;
	swab_serial_req_t(&req);
	ret = _write_retry(fd, &req, sizeof(req), &tv);
	if (ret < sizeof(req)) {
		if (ret < 0)
			return ret;
		printf("Failed to send request\n");
	}

	tv.tv_sec = args->timeout;
	tv.tv_usec = 0;
	resp.magic = SERIAL_MAGIC;
	do {
		if (wait_for(fd, (const char *)&resp.magic,
			     sizeof(resp.magic), &tv) == 0) {
			ret = _read_retry(fd, &resp.response, sizeof(resp.response), &tv);
		}

		swab_serial_resp_t(&resp);
	} while(resp.magic != SERIAL_MAGIC && (tv.tv_sec || tv.tv_usec));

	if (resp.magic != SERIAL_MAGIC)
		return -1;
	ret = resp.response;
	if (resp.response == RESP_HOSTLIST) /* hostlist */ {
		/* ok read hostlist */
		do_read_hostlist(fd, args->timeout);
		ret = 0;
	}

	close(fd);

	return ret;
}