From owner-freebsd-questions Thu Aug 10 13:34:36 1995 Return-Path: questions-owner Received: (from majordom@localhost) by freefall.FreeBSD.org (8.6.11/8.6.6) id NAA22655 for questions-outgoing; Thu, 10 Aug 1995 13:34:36 -0700 Received: from handset.laa.com (laa.com [204.7.172.201]) by freefall.FreeBSD.org (8.6.11/8.6.6) with SMTP id NAA22648 for ; Thu, 10 Aug 1995 13:34:32 -0700 Received: from tdf_ltd by handset.laa.com (NX5.67e/NX3.0M) id AA01308; Thu, 10 Aug 95 16:34:18 -0400 Message-Id: <9508102034.AA01308@handset.laa.com> Received: by tdf_ltd.laa.com (NX5.67e/NX3.0X) id AA26920; Thu, 10 Aug 95 16:34:14 -0400 Content-Type: text/plain Mime-Version: 1.0 (NeXT Mail 3.3 v118.2) Received: by NeXT.Mailer (1.118.2) From: Gary Stanny Date: Thu, 10 Aug 95 16:34:12 -0400 To: hackers@FreeBSD.ORG Subject: Telnet can connect to a port but I can't Cc: questions@freefall.FreeBSD.org.gary Sender: questions-owner@FreeBSD.ORG Precedence: bulk I'm not sure it this question should go to questions or hackers so I am posting it to both. So network wizards from both areas please feel free to enlighten me. I am trying to use FreeBSD to write a simple protocol converter - using code straight from Stevens' book (Unix Network Programming) but I am get- ting a couple of errors. Can anyone see what I am doing wrong ? The port is listening on the remote machine (netstat says the state of the port is LISTEN) & telnet can connect to it, but my program fails to connect it. tdf_ltd >udp2tcp -i23456 -o12345 -d204.7.172.60 -a1 udp2tcp converter V1.0 udp2tcp: Can't bind TCP local address : Can't assign requested address The port is listening on the local machine & telnet can connect to it, but my program fails to connect it and I get a different error then the remote host tests. tdf_ltd >udp2tcp -i23456 -o12345 -d127.0.0.1 -a1 udp2tcp converter V1.0 udp2tcp: Can't bind TCP local address : Address already in use What does telnet do to the socket&|port that I'm not ??? /*-----------------------------------------------------------------*/ /* udp2tcp.c This program grabs a UDP packet off a target port, converts it to TCP & slams it out another port. */ #include #include #include #include #include #include #include #include /*-----------------------------------------------------------------*/ int debug_flag , inputPort , outputPort , auditorNumber ; char *destAddress ; int sock_in , sock_out ; struct sockaddr_in destAddr , sourceAddr ; ARG arg_table[] = { { 'a' , INTEGER , &auditorNumber , "Auditor log file number" } , { 'd' , STRING , ( int * ) &destAddress , "Destination IP address" } , { 'i' , INTEGER , &inputPort , "Input port number" } , { 'o' , INTEGER , &outputPort , "Output port number" } , { 'D' , BOOLEAN , &debug_flag , "Runtime debug flag" } , } ; #define ARG_TABLE_SIZE ( sizeof( arg_table ) / sizeof( ARG ) ) /*-----------------------------------------------------------------*/ int main( int argc , char *argv[] ) { int new_argc , i ; new_argc = getargs( argc , argv , arg_table , ARG_TABLE_SIZE ) ; printf( "udp2tcp converter V%s\n" , VERSION_NUMBER ) ; /* open the TCP output socket */ if( ( sock_out = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 ) { perror( "udp2tcp: Can't open TCP socket " ) ; return ERR_SOCKET_OPEN_FAILURE ; } bzero( ( char * ) &destAddr , sizeof( destAddr ) ) ; destAddr.sin_family = AF_INET ; destAddr.sin_addr.s_addr = inet_addr( destAddress ) ; destAddr.sin_port = htons( outputPort ) ; if( bind( sock_out , (struct sockaddr *) &destAddr , sizeof( destAddr ) ) < 0 ) { perror( "udp2tcp: Can't bind TCP local address " ) ; return ERR_BIND_FAILURE ; } /* open the UDP input socket */ if( ( sock_in = socket( AF_INET , SOCK_DGRAM , 0 ) ) < 0 ) { perror( "udp2tcp: Can't open UDP socket " ) ; return ERR_SOCKET_OPEN_FAILURE ; } bzero( ( char * ) &sourceAddr , sizeof( sourceAddr ) ) ; sourceAddr.sin_family = AF_INET ; sourceAddr.sin_addr.s_addr = htonl( INADDR_ANY ) ; sourceAddr.sin_port = htons( inputPort ) ; if( bind( sock_in , (struct sockaddr *) &sourceAddr , sizeof( sourceAddr ) ) < 0 ) { perror( "udp2tcp: Can't bind UDP local address " ) ; return ERR_BIND_FAILURE ; } echoThePackets( sock_in ) ; return SS_NORMAL ; } /*-----------------------------------------------------------------*/ void echoThePackets( int input_sock ) { int n , n2 ; char buff[ MAX_PACKET ] , temp[ MAX_PACKET + 25 ] ; while( TRUE ) { n = recvfrom( input_sock , buff , MAX_PACKET , 0 , (struct sockaddr *) 0 , (int *) 0 ) ; if( n < 0 ) { perror( "udp2tcp: Input - recvfrom error " ) ; localExit( ERR_SOCKET_READ_ERROR ) ; } if( n == 0 ) return ; n2 = sendto( sock_out , buff , n , 0 , (struct sockaddr *) &destAddr , sizeof( destAddr) ) ; if( n2 < 0 ) { perror( "udp2tcp: Output - sendto error " ) ; localExit( ERR_SOCKET_WRITE_ERROR ) ; } } } /*-----------------------------------------------------------------*/ thanks a bunch in advance cheers gary Gary Stanny Lynn-Arthur Associates, Inc. +1 313 995 5590 stanny@laa.com Operations Support Systems +1 313 995 5989 (fax) 2350 Green Road Suite 160 Ann Arbor, MI, 48105 USA cheers gary Gary Stanny Lynn-Arthur Associates, Inc. +1 313 995 5590 stanny@laa.com Operations Support Systems +1 313 995 5989 (fax) 2350 Green Road Suite 160 Ann Arbor, MI, 48105 USA