netlib.c
int udp_session(struct vtun_host *host) 
{
     struct sockaddr_in saddr; 
     short port;
     int s,opt;
     extern int is_rmt_fd_connected;

     if( (s=socket(AF_INET,SOCK_DGRAM,0))== -1 ){
        vtun_syslog(LOG_ERR,"Can't create socket");
        return -1;
     }

     opt=1;
     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 
    
     /* Set local address and port */
     local_addr(&saddr, host, 1);
     if( bind(s,(struct sockaddr *)&saddr,sizeof(saddr)) ){
        vtun_syslog(LOG_ERR,"Can't bind to the socket");
        return -1;
     }

     opt = sizeof(saddr);
     if( getsockname(s,(struct sockaddr *)&saddr,&opt) ){
        vtun_syslog(LOG_ERR,"Can't get socket name");
        return -1;
     }

     /* Write port of the new UDP socket */
     port = saddr.sin_port;
     if( write_n(host->rmt_fd,(char *)&port,sizeof(short)) < 0 ){
        vtun_syslog(LOG_ERR,"Can't write port number");
        return -1;
     }
     host->sopt.lport = htons(port);

     /* Read port of the other's end UDP socket */
     if( readn_t(host->rmt_fd,&port,sizeof(short),host->timeout) < 0 ){
        vtun_syslog(LOG_ERR,"Can't read port number %s", strerror(errno));
        return -1;
     }

     opt = sizeof(saddr);
     if( getpeername(host->rmt_fd,(struct sockaddr *)&saddr,&opt) ){
        vtun_syslog(LOG_ERR,"Can't get peer name");
        return -1;
     }

     saddr.sin_port = port;

     /* if the config says to delay the UDP connection, we wait for an
	incoming packet and then force a connection back.  We need to
	put this here because we need to keep that incoming triggering
	packet and pass it back up the chain. */

     if (VTUN_USE_NAT_HACK(host))
     	is_rmt_fd_connected=0;
	else {
     if( connect(s,(struct sockaddr *)&saddr,sizeof(saddr)) ){
        vtun_syslog(LOG_ERR,"Can't connect socket");
        return -1;
     }
     is_rmt_fd_connected=1;
	}
     
     host->sopt.rport = htons(port);

     /* Close TCP socket and replace with UDP socket */	
     close(host->rmt_fd); 
     host->rmt_fd = s;	

     vtun_syslog(LOG_INFO,"UDP connection initialized");
     return s;
}
/* 
 * Establish UDP session with host connected to fd(socket).
 * Returns connected UDP socket or -1 on error.
 */
Establish UDP session with host connected to fd(socket).
Returns connected UDP socket or -1 on error.
Establish UDP session with host connected to fd ( socket ) . Returns connected UDP socket or - 1 on error .
int udp_session(struct vtun_host *host) 
{
     struct sockaddr_in saddr; 
     short port;
     int s,opt;
     extern int is_rmt_fd_connected;
     if( (s=socket(AF_INET,SOCK_DGRAM,0))== -1 ){
        vtun_syslog(LOG_ERR,"Can't create socket");
        return -1;
     }
     opt=1;
     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 
     local_addr(&saddr, host, 1);
     if( bind(s,(struct sockaddr *)&saddr,sizeof(saddr)) ){
        vtun_syslog(LOG_ERR,"Can't bind to the socket");
        return -1;
     }
     opt = sizeof(saddr);
     if( getsockname(s,(struct sockaddr *)&saddr,&opt) ){
        vtun_syslog(LOG_ERR,"Can't get socket name");
        return -1;
     }
     port = saddr.sin_port;
     if( write_n(host->rmt_fd,(char *)&port,sizeof(short)) < 0 ){
        vtun_syslog(LOG_ERR,"Can't write port number");
        return -1;
     }
     host->sopt.lport = htons(port);
     if( readn_t(host->rmt_fd,&port,sizeof(short),host->timeout) < 0 ){
        vtun_syslog(LOG_ERR,"Can't read port number %s", strerror(errno));
        return -1;
     }
     opt = sizeof(saddr);
     if( getpeername(host->rmt_fd,(struct sockaddr *)&saddr,&opt) ){
        vtun_syslog(LOG_ERR,"Can't get peer name");
        return -1;
     }
     saddr.sin_port = port;
     if (VTUN_USE_NAT_HACK(host))
     	is_rmt_fd_connected=0;
	else {
     if( connect(s,(struct sockaddr *)&saddr,sizeof(saddr)) ){
        vtun_syslog(LOG_ERR,"Can't connect socket");
        return -1;
     }
     is_rmt_fd_connected=1;
	}
     host->sopt.rport = htons(port);
     close(host->rmt_fd); 
     host->rmt_fd = s;	
     vtun_syslog(LOG_INFO,"UDP connection initialized");
     return s;
}
int udp_session ( struct vtun_host * host ) { struct sockaddr_in saddr ; short port ; int s , opt ; extern int is_rmt_fd_connected ; if ( ( s = socket ( AF_INET , SOCK_DGRAM , 0 ) ) == -1 ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } opt = 1 ; setsockopt ( s , SOL_SOCKET , SO_REUSEADDR , & opt , sizeof ( opt ) ) ; local_addr ( & saddr , host , 1 ) ; if ( bind ( s , ( struct sockaddr * ) & saddr , sizeof ( saddr ) ) ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } opt = sizeof ( saddr ) ; if ( getsockname ( s , ( struct sockaddr * ) & saddr , & opt ) ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } port = saddr . sin_port ; if ( write_n ( host -> rmt_fd , ( char * ) & port , sizeof ( short ) ) < 0 ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } host -> sopt . lport = htons ( port ) ; if ( readn_t ( host -> rmt_fd , & port , sizeof ( short ) , host -> timeout ) < 0 ) { vtun_syslog ( LOG_ERR , " " , strerror ( errno ) ) ; return -1 ; } opt = sizeof ( saddr ) ; if ( getpeername ( host -> rmt_fd , ( struct sockaddr * ) & saddr , & opt ) ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } saddr . sin_port = port ; if ( VTUN_USE_NAT_HACK ( host ) ) is_rmt_fd_connected = 0 ; else { if ( connect ( s , ( struct sockaddr * ) & saddr , sizeof ( saddr ) ) ) { vtun_syslog ( LOG_ERR , " " ) ; return -1 ; } is_rmt_fd_connected = 1 ; } host -> sopt . rport = htons ( port ) ; close ( host -> rmt_fd ) ; host -> rmt_fd = s ; vtun_syslog ( LOG_INFO , " " ) ; return s ; }
Establish UDP session with host connected to fd(socket).
Establish UDP session with host connected to fd ( socket ) .
/* Set local address and port */ /* Write port of the new UDP socket */ /* Read port of the other's end UDP socket */ /* if the config says to delay the UDP connection, we wait for an
	incoming packet and then force a connection back.  We need to
	put this here because we need to keep that incoming triggering
	packet and pass it back up the chain. */ /* Close TCP socket and replace with UDP socket */