コード例 #1
0
ファイル: server_main.c プロジェクト: SergeAvdeyev/my_project
/* Called by LAPB to transmit data via physical connection */
void lapb_transmit_data(struct lapb_cs * lapb, char *data, int data_size, int extra_before, int extra_after) {
	(void)lapb;
	(void)extra_before;
	(void)extra_after;
	if (!is_server_accepted()) return;
	//lapb_debug(lapb, 0, "[LAPB] data_transmit is called");

	_uchar buffer[1024];
	buffer[0] = 0x7E; /* Open flag */
	buffer[1] = 0x7E; /* Open flag */
	memcpy(&buffer[2], data, data_size);
	buffer[data_size + 2] = 0; /* Good FCS */
	buffer[data_size + 3] = 0; /* Good FCS */
	buffer[data_size + 4] = 0x7E; /* Close flag */
	buffer[data_size + 5] = 0x7E; /* Close flag */
	int n = send(tcp_client_socket(), buffer, data_size + 6, MSG_NOSIGNAL);
	if (n < 0)
		lapb_debug(0, "[LAPB] ERROR writing to socket, %s", strerror(errno));
}
コード例 #2
0
ファイル: server_main.c プロジェクト: SergeAvdeyev/my_project
/* Called by LAPB to transmit data via physical connection */
void transmit_data(struct lapb_cs * lapb, char *data, int data_size, int extra_before, int extra_after) {
	(void)lapb;
	(void)extra_before;
	(void)extra_after;
	if (!is_server_accepted()) return;
	//debug(lapb, 0, "[LAPB] data_transmit is called");

	_uchar buffer[1024];
	buffer[0] = 0x7E; /* Open flag */
	buffer[1] = 0x7E; /* Open flag */
	memcpy(&buffer[2], data, data_size);
	if (error_type == 1) { /* Bad FCS (for I frames) */
		_uchar i_frame = ~(buffer[3] & 0x01);
		if (i_frame && (error_counter == 0)) {
			buffer[data_size + 2] = 1; /* Bad FCS */
			buffer[data_size + 3] = 1; /* Bad FCS */
		} else {
			buffer[data_size + 2] = 0; /* Good FCS */
			buffer[data_size + 3] = 0; /* Good FCS */
		};
	} else if (error_type == 2) { /* Bad N(R) (for I or S frames) */
		_uchar if_nr_present = buffer[3];
		if_nr_present = if_nr_present & 0x03;
		if ((if_nr_present != 0x03) && (error_counter == 0))
			buffer[3] |= 0xE0; /* Bad N(R) */
		buffer[data_size + 2] = 0; /* Good FCS */
		buffer[data_size + 3] = 0; /* Good FCS */
	} else {
		buffer[data_size + 2] = 0; /* Good FCS */
		buffer[data_size + 3] = 0; /* Good FCS */
	};
	buffer[data_size + 4] = 0x7E; /* Close flag */
	buffer[data_size + 5] = 0x7E; /* Close flag */
	int n = send(tcp_client_socket(), buffer, data_size + 6, MSG_NOSIGNAL);
	if (n < 0)
		lapb_debug(0, "[LAPB] ERROR writing to socket, %s", strerror(errno));
}
コード例 #3
0
ファイル: tcpgen.c プロジェクト: carriercomm/flowgen
void *
client_thread (void * param)
{
	int n, ret, fd, port, sknum = 0;
	char buf[9216];
	unsigned long xmitted = 0;

	D ("Start to connect");

	/* create tcp client sockets for each flow */
	for (sknum = 0; sknum < tcpgen.flow_num; sknum++) {
		if (!tcpgen.randomized)
			port = 0;
		else
			port = RANDOM_PORT ();

		fd = tcp_client_socket (tcpgen.dst, tcpgen.src,
					TCPGEN_PORT, port);

		if (!fd) {
			goto err;
		}

		tcpgen.client_sock[sknum] = fd;
	}

	/* initalize flow distribution */
	switch (tcpgen.flow_dist) {
	case FLOWDIST_SAME :
		flow_dist_init_same ();
		break;
	case FLOWDIST_RANDOM :
		flow_dist_init_random ();
		break;
	case FLOWDIST_POWER :
		flow_dist_init_power ();
		break;
	default :
		D ("invalid flow distribution pattern");
		goto err;
	}

	/* send packets */

	while (1) {
		for (n = 0; n < tcpgen.socklistlen; n++) {
			ret = write (tcpgen.socklist[n], buf, tcpgen.data_len);
			if (ret < 0) {
				D ("failed to write %d byte to socket %d",
				   tcpgen.data_len, tcpgen.socklist[n]);
				goto err;
			}

			if (tcpgen.verbose)
				D ("write %d bytes to socket %d", ret,
				   tcpgen.socklist[n]);

			if (tcpgen.interval)
				usleep (tcpgen.interval);

			xmitted++;
			if (tcpgen.count && tcpgen.count < xmitted)
				goto err;
		}			
	}

err:
	for (n = 0; n < sknum; n++)
		close (tcpgen.client_sock[n]);

	return NULL;
}