Example #1
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;
}
Example #2
0
void do_client() {
	char obuffer[1024] = {0}, ibuffer[1024] = {0};
	int obuffer_offset = 0, stop = 0;

	fflush (stdout);  /* get anything out of the buffer */
	conio_init();
	show_buffer (obuffer, obuffer_offset);

	do {

		if (net_query (chan)) {
			int col = 10;
			int x = net_receive (chan, ibuffer, 1024, NULL);

			if (x<0)
				strcpy (ibuffer, "!!! (local) error reading packet");
			else
				ibuffer[x] = 0;

			switch (ibuffer[0]) {
				case '*': col = 9; break;
				case '+':
				case '-': col = 11; break;
				case '!': col = 12; break;
			}

			write_to_window (ibuffer, col);
			show_buffer (obuffer, obuffer_offset);

			if (!strcmp (ibuffer, "*** go away")) stop = 1;
			if (!strcmp (ibuffer, "*** server shutting down")) stop = 1;
		}

		if (conio_kbhit()) {
			char ch = conio_getch();
			switch (ch) {
			    	case 7:
				case 8:
					if (obuffer_offset) obuffer[--obuffer_offset] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
				case 13:
					net_send (chan, obuffer, strlen (obuffer));
					obuffer[obuffer_offset = 0] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
				default:
					obuffer[obuffer_offset] = ch;
					obuffer[++obuffer_offset] = 0;
					show_buffer (obuffer, obuffer_offset);
					break;
			}
		}

	} while (!stop);

	erase_buffer();
	conio_exit();
}
Example #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");
}
Example #4
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;
}
Example #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;
}