Exemplo n.º 1
0
/* init_conn:
 *  This just chains to the channel initialiser, basically.
 */
static int init_conn (NET_CONN *conn, const char *addr)
{
	struct conn_data_t *data;
	
	conn->peer_addr[0] = '\0';	
	conn->data = data = malloc (sizeof *data);
	if (!data) return 1;
	
	if (create_queues (conn)) {
		free (data);
		return 2;
	}
	
	data->conns = NULL;
	data->referer = NULL;
	
	data->chan = net_openchannel (conn->type, addr);
	if (!data->chan) {
		destroy_queues (conn);
		free (data);
		return 3;
	}

	return 0; /* success */
}
Exemplo n.º 2
0
void *pv_messclient_receive(){

NET_CHANNEL *chan = NULL;
message_buf  rbuf;
struct str_user_data * p_user_data;

  chan = net_openchannel (DRIVER,user_data.local_IP_udp_port);

   
    if (!chan) {
        fprintf (stderr,"Error opening channel.\n");
        exit(ERROR);
    }


 while(1){
   while (net_query (chan)){
       /* If so, receive them and print them out.  */

         if (net_receive (chan,&rbuf,sizeof(rbuf), NULL) > 0){
              p_user_data = (struct str_user_data *)( rbuf.mtext + 1 );
              /* Print the message */
              printf("\n# Received from IP : %s PID : %d : %s\n", p_user_data->local_IP,p_user_data->pid,rbuf.mtext+1+sizeof(struct str_user_data));  
              /*Write the init massage again */ 
              printf("# IP : %s PID :%d :", user_data.local_IP, user_data.pid );
              fflush(stdout);
        }/*if*/
    }/*while*/ 
 }/*while (1)*/

   net_closechannel (chan);

return OK;
}
Exemplo n.º 3
0
void init() {
	char temp[1024], nick[1024], addr[1024], newaddr[NET_MAX_ADDRESS_LENGTH];
	NET_DRIVERLIST drv;
	drv = net_driverlist_create();
	net_driverlist_clear (drv);
	net_driverlist_add (drv, netdriver);

	if (!net_initdrivers (drv)) {
		printf("Error initialising driver.\n");
		exit (1);
	}

	printf ("Enter target address: ");
	fgets (addr, 1024, stdin);
	while (strchr(addr,'\n')) *strchr(addr,'\n')=0;

	printf ("Enter nickname: ");
	fgets (nick, 10, stdin);
	while (strchr(nick,'\n')) *strchr(nick,'\n')=0;

	if (!(chan = net_openchannel (netdriver, NULL))) {
		printf ("Unable to open channel.\n");
		exit (2);
	}

	printf ("Connecting to %s...\n", addr);

	net_assigntarget (chan, addr);
	sprintf (temp, "%c%s", CHAT_MAGIC, nick);
	net_send (chan, temp, strlen (temp));

	while ((!net_query (chan))/* && !conio_kbhit()*/);

	if (0/*conio_kbhit()*/) {
		conio_getch();
		printf ("Aborted.\n");
		exit (3);
	}

	{
		int x = net_receive (chan, temp, 1024, newaddr);
		if (x == -1) {
			printf ("Receive error.\n");
			exit (5);
		}
		temp[x] = 0;
	}

	if (strcmp (temp, "OK")) {
		printf ("Connection refused.\n");
		exit (4);
	}

	printf ("Connection accepted, redirecting... ");
	fflush (stdout);
	net_assigntarget (chan, newaddr);
	printf ("done.\n");
}
Exemplo n.º 4
0
/* get_channel:
 *  Scans the list of connections for one matching this connector, and 
 *  fills it in if found, returning positive.  Otherwise, adds a new entry
 *  to the list, fills it in, and returns negative.  On error, returns zero.
 */
static int get_channel (struct conns_list *conns, const char *addr, int conn_id, int type, const char *bind, NET_CHANNEL **chan, struct conn_data_t *condat)
{
#if 0
	/* Before we do anything else, time out entries which have been here 
	 * too long */
	struct conns_list *ptr = conns;
	while (ptr->next) {
		if ((unsigned)(__libnet_timer_func() - ptr->last_access_time) > 10000) {
			struct conns_list *ptr2 = ptr->next;
			ptr->next = ptr2->next;
			free (ptr2->addr);
			free (ptr2);
		}
	}
#endif

	while (conns->next) {
		conns = conns->next;
		if ((conn_id == conns->client_conn_id) && !strcmp (addr, conns->addr)) {
			*chan = conns->chan;
			conns->last_access_time = __libnet_timer_func();
			return 1;
		}
	}
	conns->next = malloc (sizeof *conns->next);
	if (conns->next) {
		conns->next->next = NULL;
		conns->next->addr = strdup (addr);
		conns->next->client_conn_id = conn_id;
		conns->next->ref = condat;
		conns->next->last_access_time = __libnet_timer_func();
		if (conns->next->addr) {
			conns->next->chan = net_openchannel (type, bind);
			if (conns->next->chan) {
				*chan = conns->next->chan;
				return -1;
			}

			free (conns->next->addr);
		}
		free (conns->next);
		conns->next = NULL;
	}
	return 0;
}
Exemplo n.º 5
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;
}