示例#1
0
/*
 * Test the network write function with a timeout.  We fork off a child
 * process that runs delay_reader, and then we write 64KB to the network in
 * two chunks, once with a timeout and once without, and then try a third time
 * when we should time out.
 */
static void
test_network_write(void)
{
    socket_type fd, c;
    pid_t child;
    struct sockaddr_in sin;
    socklen_t slen;
    char *buffer;

    /* Create the data that we're going to send. */
    buffer = bmalloc(4096 * 1024);
    memset(buffer, 'a', 4096 * 1024);

    /* Create the listening socket. */
    fd = network_bind_ipv4(SOCK_STREAM, "127.0.0.1", 11119);
    if (fd == INVALID_SOCKET)
        sysbail("cannot create or bind socket");
    if (listen(fd, 1) < 0)
        sysbail("cannot listen to socket");

    /* Create the child, which will connect and then read data with delay. */
    child = fork();
    if (child < 0)
        sysbail("cannot fork");
    else if (child == 0) {
        socket_close(fd);
        client_delay_reader("127.0.0.1");
    }

    /* Set an alarm just in case our timeouts don't work. */
    alarm(10);

    /* Accept the client connection. */
    slen = sizeof(struct sockaddr_in);
    c = accept(fd, &sin, &slen);
    if (c == INVALID_SOCKET)
        sysbail("cannot accept on socket");

    /* Test some successful writes with and without a timeout. */
    socket_set_errno(0);
    ok(network_write(c, buffer, 32 * 1024, 0), "network_write");
    ok(network_write(c, buffer, 32 * 1024, 1),
       "network_write with timeout");

    /*
     * A longer write cannot be completely absorbed before the client sleep,
     * so should fail with a timeout.
     */
    ok(!network_write(c, buffer, 4096 * 1024, 1),
       "network_write aborted with timeout");
    is_int(ETIMEDOUT, socket_errno, "...with correct error");
    alarm(0);

    /* Clean up. */
    socket_close(c);
    kill(child, SIGTERM);
    waitpid(child, NULL, 0);
    socket_close(fd);
    free(buffer);
}
示例#2
0
void do_serial(char* msg) {
	if(msg[0] == 'D' && msg[1] == 'H' && msg[2] == 'T') {
        network_write("{ \"type\":\"sensor.senseless.%s\", \"msg\": { \"sensor\":\"DHT\", \"raw\":\"%s\" } }\n", hs_designation(), msg);
    } else if(msg[0] == 'P' && msg[1] == 'I' && msg[2] == 'R') {
        network_write("{ \"type\":\"sensor.senseless.%s\", \"msg\": { \"sensor\":\"motion\", \"status\":\"triggered\" } }\n", hs_designation());
    } else if(msg[0] == 'L' && msg[1] == 'D' && msg[2] == 'R') {
        network_write("{ \"type\":\"sensor.senseless.%s\", \"msg\": { \"sensor\":\"light\", \"raw\":\"%s\" } }\n", hs_designation(), msg);
    }
}
示例#3
0
文件: tokens.c 项目: sigmaris/remctl
/*
 * Send a token to a file descriptor.  Takes the file descriptor, the token,
 * and the flags (a single byte, even though they're passed in as an integer)
 * and writes them to the file descriptor.  Returns TOKEN_OK on success and
 * TOKEN_FAIL_SYSTEM, TOKEN_FAIL_SOCKET, or TOKEN_FAIL_TIMEOUT on an error
 * (including partial writes).
 */
enum token_status
token_send(socket_type fd, int flags, gss_buffer_t tok, time_t timeout)
{
    size_t buflen;
    char *buffer;
    bool okay;
    unsigned char char_flags = (unsigned char) flags;
    OM_uint32 len = htonl(tok->length);

    /* Send out the whole message in a single write. */
    if (tok->length > SIZE_MAX - 1 - sizeof(OM_uint32)) {
        errno = ENOMEM;
        return TOKEN_FAIL_SYSTEM;
    }
    buflen = 1 + sizeof(OM_uint32) + tok->length;
    buffer = malloc(buflen);
    if (buffer == NULL)
        return TOKEN_FAIL_SYSTEM;
    memcpy(buffer, &char_flags, 1);
    memcpy(buffer + 1, &len, sizeof(OM_uint32));
    memcpy(buffer + 1 + sizeof(OM_uint32), tok->value, tok->length);
    okay = network_write(fd, buffer, buflen, timeout);
    free(buffer);
    return okay ? TOKEN_OK : map_socket_error(socket_errno);
}
示例#4
0
//----------------------------------------------------------------------
// NETWORK
//----------------------------------------------------------------------
int network(char *arg)
{
    int ix;
    ix = argvindex(network_funcs, arg, strcmp);
    switch(ix) {
	case NETWORK_OPTION:
	    network_option();
	    break;
	case NETWORK_MENU:
	    network_menu();
	    break;
	case NETWORK_WRITE:
	    network_write();
	    break;
    }    
}
示例#5
0
/*
 * The write_socket() function writes the command (char *command) and
 * payload (char *payload) data to the socket (int s). If a write failure
 * occurs, it returns a negative integer. Otherwise, it returns the
 * number of bytes written.
 */
int write_socket(int s, char *command, char *payload) {
	int r, l;
	char *lstr;
	char *str;
	
	l = strlen(command);
	lstr = malloc(20);
	sprintf(lstr, "%i", l);
	
	str = malloc(strlen(lstr) + 1 + strlen(command) + strlen(payload) + 1);
	sprintf(str, "%s:%s%s", lstr, command, payload);
	r = network_write(s, str);
	free(str);
	free(lstr);
	
	return r;
}
示例#6
0
文件: main.c 项目: emaste/deebe
int main_forward()
{
	int ret = 1;

	/* reuse the normal network setup */
	if (network_init()) {
		/* Now accept from gdb */
		if (network_accept()) {
			/* Now connect to remote deebe */
			if (network_connect()) {
				while (true) {
					network_read();
					if (network_in_buffer_total > 0) {
						_forward_packet(&network_out_buffer[0],
								&network_in_buffer[0],
								&network_out_buffer_total,
								&network_in_buffer_total);

						network_write_fwd();
					}
					network_read_fwd();
					if (network_in_buffer_total > 0) {
						_forward_packet(&network_out_buffer[0],
								&network_in_buffer[0],
								&network_out_buffer_total,
								&network_in_buffer_total);

						network_write();
					}
				}
			}
		}
		network_cleanup();
	}

	return ret;
}
示例#7
0
文件: telnet.c 项目: chazu/btmux
static void telnet_send_subop(DESC * client, int option, int how)
{
	unsigned char buffer[16] =
		{ TELNET_IAC, TELNET_SB, option, how, TELNET_IAC, TELNET_SE };
	network_write(client, (void *) buffer, 6);
}
示例#8
0
文件: telnet.c 项目: chazu/btmux
/* Client Ring Buffer Handling */
static void telnet_send_req(DESC * client, int how, int request)
{
	unsigned char buffer[16] = { TELNET_IAC, how, request };
	network_write(client, (void *) buffer, 3);
}
示例#9
0
文件: telnet.c 项目: chazu/btmux
void telnet_write(DESC * client, char *buffer, int length)
{
	dprintk("telnet_write");
	// Color translation should occur here.
	network_write(client, buffer, length);
}
示例#10
0
void udp_send( void ) {
  network_write(network, udp_buffer, udp_buffer_id);
  udp_buffer_id =0;
}
示例#11
0
void do_ping() {
    network_write("{ \"type\":\"io.tcp.pong\", \"msg\":{\"name\":\"%s\"} }", hs_designation());
}
示例#12
0
void do_serial(char* msg) {
    if(strlen(msg) > 0) {
        network_write("{ \"type\":\"sensor.heating.central\", \"msg\": { \"temperature\":\"%s\" } }\n", msg);
    }
}