Beispiel #1
0
static __inline__ int32_t
init_this(network_client *nc, uint32_t factory, void *io)
{
	int32_t fd = (int32_t)io;

	nc->ip[0] = '\0';
	unix_sock_get_peer(fd, nc->ip, MAX_IP4_LEN);

	nc->factory = factory;
	nc->proto_watch = proto_watch_new(io, DEFAULT_TIMEOUT_SEC * DEFAULT_CHECK_TIMES * 1000, 
		&network_client_watch_impl, nc, proto_watch_on_finalize);
	if (!nc->proto_watch)
	{
		TRACE_WARNING("network_client()->proto_watch_new() failed.");
		unix_sock_close((int32_t)io);
		return -EINVAL;
	}

	proto_watch_set_window(nc->proto_watch, DEFAULT_PW_WINSIZE);

    obj_ref(nc); // *****
    
	nc->state = 0;
    
	nc->lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init(nc->lock, NULL);

	return 0;
}
Beispiel #2
0
network_client *
network_client_new(uint32_t size, network_client_ops *ops,
	uint32_t factory, void *io)
{
	init_parm parm;

	if (size < sizeof(network_client))
	{
		unix_sock_close((int32_t)io);
		return NULL;
	}

	parm.opts = ops;
	parm.io = io;
	parm.factory = factory;

	return (network_client*)client_alloc(
		size, &client_ops_impl,&parm,__FUNCTION__);
}
Beispiel #3
0
int alfred_client_set_data(struct globals *globals)
{
	unsigned char buf[MAX_PAYLOAD];
	struct alfred_push_data_v0 *push;
	struct alfred_data *data;
	int ret, len;

	if (unix_sock_open_client(globals, ALFRED_SOCK_PATH))
		return -1;

	push = (struct alfred_push_data_v0 *)buf;
	data = push->data;
	len = sizeof(*push) + sizeof(*data);
	while (!feof(stdin)) {
		ret = fread(&buf[len], 1, sizeof(buf) - len, stdin);
		len += ret;

		if (sizeof(buf) == len)
			break;
	}

	push->header.type = ALFRED_PUSH_DATA;
	push->header.version = ALFRED_VERSION;
	push->header.length = htons(len - sizeof(push->header));
	push->tx.id = get_random_id();
	push->tx.seqno = htons(0);

	/* we leave data->source "empty" */
	memset(data->source, 0, sizeof(data->source));
	data->header.type = globals->clientmode_arg;
	data->header.version = globals->clientmode_version;
	data->header.length = htons(len - sizeof(*push) - sizeof(*data));

	ret = write(globals->unix_sock, buf, len);
	if (ret != len)
		fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
			__func__, ret, len, strerror(errno));

	unix_sock_close(globals);
	return 0;
}
Beispiel #4
0
int alfred_client_modeswitch(struct globals *globals)
{
	unsigned char buf[MAX_PAYLOAD];
	struct alfred_modeswitch_v0 *modeswitch;
	int ret, len;

	if (unix_sock_open_client(globals, ALFRED_SOCK_PATH))
		return -1;

	modeswitch = (struct alfred_modeswitch_v0 *)buf;
	len = sizeof(*modeswitch);

	modeswitch->header.type = ALFRED_MODESWITCH;
	modeswitch->header.version = ALFRED_VERSION;
	modeswitch->header.length = htons(len - sizeof(modeswitch->header));

	switch (globals->opmode) {
	case OPMODE_SLAVE:
		modeswitch->mode = ALFRED_MODESWITCH_SLAVE;
		break;
	case OPMODE_MASTER:
		modeswitch->mode = ALFRED_MODESWITCH_MASTER;
		break;
	default:
		fprintf(stderr, "%s: unknown opmode %u in modeswitch\n",
			__func__, globals->opmode);
		return -1;
	}

	ret = write(globals->unix_sock, buf, len);
	if (ret != len)
		fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
			__func__, ret, len, strerror(errno));

	unix_sock_close(globals);
	return 0;
}
Beispiel #5
0
int alfred_client_request_data(struct globals *globals)
{
	unsigned char buf[MAX_PAYLOAD], *pos;
	struct alfred_request_v0 *request;
	struct alfred_push_data_v0 *push;
	struct alfred_status_v0 *status;
	struct alfred_tlv *tlv;
	struct alfred_data *data;
	int ret, len, data_len, i;
	const size_t buf_data_len = sizeof(buf) - sizeof(*push) - sizeof(*data);

	if (unix_sock_open_client(globals, ALFRED_SOCK_PATH))
		return -1;

	request = (struct alfred_request_v0 *)buf;
	len = sizeof(*request);

	request->header.type = ALFRED_REQUEST;
	request->header.version = ALFRED_VERSION;
	request->header.length = sizeof(*request) - sizeof(request->header);
	request->header.length = htons(request->header.length);
	request->requested_type = globals->clientmode_arg;
	request->tx_id = get_random_id();

	ret = write(globals->unix_sock, buf, len);
	if (ret != len)
		fprintf(stderr, "%s: only wrote %d of %d bytes: %s\n",
			__func__, ret, len, strerror(errno));

	push = (struct alfred_push_data_v0 *)buf;
	tlv = (struct alfred_tlv *)buf;
	while ((ret = read(globals->unix_sock, buf, sizeof(*tlv))) > 0) {
		if (ret < (int)sizeof(*tlv))
			break;

		if (tlv->type == ALFRED_STATUS_ERROR)
			goto recv_err;

		if (tlv->type != ALFRED_PUSH_DATA)
			break;

		/* read the rest of the header */
		ret = read(globals->unix_sock, buf + sizeof(*tlv),
			   sizeof(*push) - sizeof(*tlv));

		/* too short */
		if (ret < (int)(sizeof(*push) - (int)sizeof(*tlv)))
			break;

		/* read the rest of the header */
		ret = read(globals->unix_sock, buf + sizeof(*push),
			   sizeof(*data));

		if (ret < (ssize_t)sizeof(*data))
			break;

		data = push->data;
		data_len = ntohs(data->header.length);

		/* would it fit? it should! */
		if (data_len > (int)buf_data_len)
			break;

		/* read the data */
		ret = read(globals->unix_sock,
			   buf + sizeof(*push) + sizeof(*data), data_len);

		/* again too short */
		if (ret < data_len)
			break;

		pos = data->data;

		printf("{ \"%02x:%02x:%02x:%02x:%02x:%02x\", \"",
		       data->source[0], data->source[1],
		       data->source[2], data->source[3],
		       data->source[4], data->source[5]);
		for (i = 0; i < data_len; i++) {
			if (pos[i] == '"')
				printf("\\\"");
			else if (pos[i] == '\\')
				printf("\\\\");
			else if (!isprint(pos[i]))
				printf("\\x%02x", pos[i]);
			else
				printf("%c", pos[i]);
		}

		printf("\" },\n");
	}

	unix_sock_close(globals);

	return 0;

recv_err:
	/* read the rest of the status message */
	ret = read(globals->unix_sock, buf + sizeof(*tlv),
		   sizeof(*status) - sizeof(*tlv));

	/* too short */
	if (ret < (int)(sizeof(*status) - sizeof(*tlv)))
		return -1;

	status = (struct alfred_status_v0 *)buf;
	fprintf(stderr, "Request failed with %d\n", status->tx.seqno);

	return status->tx.seqno;;
}