Ejemplo n.º 1
0
/* poll_listen:
 *  Here we check for an incoming connection, and if there is one we
 *  fill in `newconn' with our data pointer for it and the addresses,
 *  and return nonzero.  Otherwise return 0.
 */
static int poll_listen (NET_CONN *conn, NET_CONN *newconn)
{
	struct conn_data_t *data;
	char buffer[12], buffer2[8+NET_MAX_ADDRESS_LENGTH] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char addr[NET_MAX_ADDRESS_LENGTH];
	int x;

	int count = 32; /* maximum number of packets to process */
	while (net_query (((struct conn_data_t *)conn->data)->chan) && count-- > 0) {
		if ((net_receive (((struct conn_data_t *)conn->data)->chan, buffer, 12, addr) == 12) && !memcmp (buffer, "connect", 8)) {
			newconn->data = data = malloc (sizeof *data);
			if (!data) continue;

			if (create_queues (newconn)) {
				free (data);
				continue;
			}

			data->conns = NULL;
			
			x = get_channel (
				((struct conn_data_t *)conn->data)->conns, addr,
				(buffer[8] << 24) + (buffer[9] << 16) +
				(buffer[10] << 8) + buffer[11],
				conn->type, NULL, &data->chan, data
			);
			
			if (x) {
				data->referer = conn->data;

				/* tell new channel where to send in future */
				net_assigntarget (data->chan, addr);

				/* send reply now with address of new channel, through
				 * listening conn so it can get through NATs */
				net_assigntarget (((struct conn_data_t *)conn->data)->chan, addr);
				strcpy (buffer2+8, net_getlocaladdress (data->chan));
				net_send (((struct conn_data_t *)conn->data)->chan, buffer2, 8+NET_MAX_ADDRESS_LENGTH);
			}
			
			if (x >= 0) {
				destroy_queues (newconn);
				free (data);
				continue;
			}

			strcpy (newconn->peer_addr, addr);

			return 1;
		}
	}

	return 0;
}
Ejemplo n.º 2
0
int main (int argc, char **argv)
{
    NET_CONN *listen, *conn = NULL;
    NET_CHANNEL *chan;
    char remote[NET_MAX_ADDRESS_LENGTH], buf[NET_MAX_ADDRESS_LENGTH];
    char *p, c;
    int server = -1;

    if (argc > 1) {
        if (!strcmp (argv[1], "server")) server = 1;
        else if (!strcmp (argv[1], "client")) server = 0;
    }
    if (server == -1) {
        puts ("Pass `server' or `client' on the command line.");
        return 1;
    }

    net_init();
    net_detectdrivers(net_drivers_all);
    net_initdrivers(net_drivers_all);

    if (server) {
        listen = net_openconn(DRIVER, "");
        net_listen(listen);
        while (!conn) 
            conn = net_poll_listen(listen);
    } else {
        conn = net_openconn(DRIVER, NULL);
        if (net_connect_wait_time(conn, ADDRESS, 5) != 0) {
            printf("Error connecting\n");
            return 1;
        }
    }

    chan = net_openchannel(DRIVER, NULL);
    p = net_getlocaladdress(chan);
    net_send_rdm(conn, p, strlen(p) + 1);

    while (!net_query_rdm(conn)) ;
    net_receive_rdm(conn, remote, sizeof remote);

    printf ("Address before fixing: %s\n", remote);

    if (net_fixupaddress_conn(conn, remote, buf) != 0) {
	printf("Didn't work\n");
	return 1;
    }

    printf ("Address after fixing: %s\n", buf);

    printf("assigning target: %s\n", buf);
    net_assigntarget(chan, buf);

    do {
	c = fgetc(stdin);
	if (c == '1') 
	    net_send(chan, "** Channel", 11);
	else if (c == '2')
	    net_send_rdm(conn, "** Conn", 8);

	while (net_query(chan)) {
	    net_receive(chan, buf, sizeof buf, NULL);
	    printf("%s\n", buf);
	}

	while (net_query_rdm(conn)) {
	    net_receive_rdm(conn, buf, sizeof buf);
	    printf("%s\n", buf);
	}
    } while (c != 'q');

    return 0;
}