Example #1
0
void vv_bind(const char *line, tcp_node_t tcp_node){
	
	int socket;
	char* addr_str = malloc(sizeof(char)*INET_ADDRSTRLEN);
	int port;
	
	int ret = sscanf(line, "v_bind %d %s %d", &socket, addr_str, &port);
	if (ret != 3){
		fprintf(stderr, "syntax error (usage: v_bind [socket] [address] [port])\n");
		free(addr_str);
		return;
	} 	
	
		
	/* convert the given string into an address structure */
	struct in_addr* addr = malloc(sizeof(struct in_addr));
	inet_pton(AF_INET, addr_str, addr);
	free(addr_str);

	ret = tcp_api_bind(tcp_node, socket, addr, port);
	if(ret < 0)
		printf("v_bind returned error: %d\n", ret);
	else
		printf("v_bind returned: %d\n", ret);

	/* free it, hopefully tcp_api_bind didn't want to hang onto it */
	free(addr);
}
Example #2
0
int
main (int argc, char *argv[]) {
    int soc = -1, acc;
    uint8_t buf[65536];
    ssize_t len;

    if (microps_init(&param) == -1) {
        goto ERROR;
    }
    soc = tcp_api_open();
    if (soc == -1) {
        goto ERROR;
	}
    if (tcp_api_bind(soc, hton16(7)) == -1) {
        goto ERROR;
    }
    tcp_api_listen(soc);
    acc = tcp_api_accept(soc);
    if (acc == -1) {
        goto ERROR;
    }
fprintf(stderr, "accept success, soc=%d, acc=%d\n", soc, acc);
	while (1) {
		len = tcp_api_recv(acc, buf, sizeof(buf));
		if (len <= 0) {
			break;
		}
		hexdump(stderr, buf, len);
		tcp_api_send(acc, buf, len);
	}
	tcp_api_close(acc);
/*
    ip_addr_t peer_addr;
    uint16_t peer_port;
    ip_addr_pton("72.21.215.232", &peer_addr);
    peer_port = hton16(80);
    tcp_api_connect(soc, &peer_addr, peer_port);
    strcpy(buf, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: Close\r\n\r\n");
    tcp_api_send(soc, (uint8_t *)buf, strlen(buf));
    while (1) {
        len = tcp_api_recv(soc, (uint8_t *)buf, sizeof(buf));
        //fprintf(stderr, "len: %ld\n", len);
        if (len <= 0) {
            break;
        }
        //hexdump(stderr, buf, len);
    }
*/
	tcp_api_close(soc);
	microps_cleanup();
    return  0;
ERROR:
    if (soc != -1) {
        tcp_api_close(soc);
    }
    microps_cleanup();
    return -1;
}
Example #3
0
void accept_cmd(const char *line, tcp_node_t tcp_node){
	int ret, port, socket;
	
	ret = sscanf(line, "accept %d", &port);
	if((ret != 1)&&(sscanf(line, "a %d", &port)!=1)){
		fprintf(stderr, "syntax error (usage: accept [port])\n");
		return;
	}
	// create new socket
	if((socket = tcp_api_socket(tcp_node))<0){
		printf("Error: v_socket() returned: %s\n", strerror(-socket));
		return;
	}
	// now bind
	struct in_addr addr;	
	ret = tcp_api_bind(tcp_node, socket, &addr, port);
	if(ret < 0){
		printf("Error: v_bind returned: %s\n", strerror(-ret));
		return;
	}
	// now listen
	ret = tcp_api_listen(tcp_node, socket);	
	if(ret < 0){
		printf("Error: v_listen returned: %s\n", strerror(-ret));
		return;
	}
	// now accept in a loop - keep accepting	
	/* pack the args */
	tcp_api_args_t args = tcp_api_args_init();
	args->node		 = tcp_node;
	args->socket  	 = socket;
	args->function_call = "v_accept()";

	/* So we need to call tcp_api_accept in a loop without blocking.... so here's my solution that I've implemented:
		We call thread the call tcp_driver_accept_entry which then goes to call tcp_api_accept_entry in a loop.
		This means we're creating threads within the while loop thread, but this way we can pretty print the result
		of each individual tcp_api_accept call.  the thread for tcp_driver_accept_entry has return value 0
		because the actual accept calls that returned sockets will have already been printed.
	*/
	tcp_node_thread(tcp_node, tcp_driver_accept_entry, args);
	
	return;
}
Example #4
0
/* recvfile filename port 
	Listen for a connection on the given port. Once established, write every-
	thing you can read from the socket to the given file. Once the other side closes the connection,
	close the connection as well. Your driver must continue to accept other commands. */
void recvfile_cmd(const char* line, tcp_node_t tcp_node){
	int ret, socket, port;
	char* filename_buffer = (char*)malloc(sizeof(char)*FILE_BUF_SIZE);
	
	ret = sscanf(line, "recvfile %s %d", filename_buffer, &port);
	if(ret != 2){
		fprintf(stderr, "syntax error (usage: recvfile [filename] [port])\n");
		return;
	}	
	// create new socket
	if((socket = tcp_api_socket(tcp_node))<0){
		printf("Error: v_socket() returned: %s\n", strerror(-socket));
		free(filename_buffer);
		return;
	}
	// now bind
	struct in_addr addr;	
	ret = tcp_api_bind(tcp_node, socket, &addr, port);
	if(ret < 0){
		printf("Error: v_bind returned: %s\n", strerror(-ret));
		tcp_node_remove_connection_kernal(tcp_node, tcp_node_get_connection_by_socket(tcp_node, socket));
		free(filename_buffer);
		return;
	}
	// now listen
	ret = tcp_api_listen(tcp_node, socket);	
	if(ret < 0){
		printf("Error: v_listen returned: %s\n", strerror(-ret));
		free(filename_buffer);
		return;
	}
	// now accept and write	
	tcp_api_args_t args = tcp_api_args_init();
	args->node = tcp_node;
	args->socket = socket;
	args->buffer = filename_buffer;
	args->port = port;
	args->function_call = "recvfile()";
	
	tcp_node_thread(tcp_node, tcp_api_recvfile_entry, args);	
	return;
}