Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Aug 95 16:34:12 -0400
From:      Gary Stanny <stanny@laa.com>
To:        hackers@FreeBSD.ORG
Cc:        questions@freefall.FreeBSD.org.gary
Subject:   Telnet can connect to a port but I can't
Message-ID:  <9508102034.AA01308@handset.laa.com>

next in thread | raw e-mail | index | archive | help
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 <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

/*-----------------------------------------------------------------*/

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9508102034.AA01308>