Ping-Funktion



  • hallo,
    ich wollte mir mittels raw-sockets mein eigenes kleines ping-programm schreiben, doch leider gibt mir die funktion recvfrom immer -1 zurück. 😞 hier mal der code meiner ping-funktion:

    int icmpProto::ping( char *host, int size, int timeout, int count, int pause )
    {
    	SOCKET sock;
    	SOCKADDR_IN dest;
    	SOCKADDR_IN from;
    
    	int fromlen;
    	int recv_timeout;
    
    	HOSTENT *h;
    
    	unsigned long data_size;
    
    	char *dest_ip;
    	char *icmp_data;
    	char *recved;
    
    	unsigned int addr = 0;
    	unsigned short seq_no = 0;
    
    	int recv_bytes;
    	int sent_bytes;
    
    	fromlen = sizeof( from );
    
    	recv_timeout = timeout;
    
    	sock = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
    	if( sock == INVALID_SOCKET )
    	{
    		cerr << "error: can't create socket: " << WSAGetLastError( ) << endl;
    		return SOCKET_ERROR;
    	}
    
    	setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, ( char * ) &recv_timeout, sizeof( recv_timeout ) );
    
    	h = gethostbyname( host );
    
    	if ( !h )
    		addr = inet_addr( host );
    
    	if( ( !h ) && ( addr == INADDR_NONE ) )
    	{
    		cerr << "error: can't resolve host: " << WSAGetLastError( ) << endl;
    		return SOCKET_ERROR;
    	}
    
    	if( h != NULL )
    		memcpy( &( dest.sin_addr ), h->h_addr, h->h_length );
    	else
    		dest.sin_addr.s_addr = addr;
    
    	if( h )
    		dest.sin_family = h->h_addrtype;
    	else
    		dest.sin_family = AF_INET;
    
    	dest_ip = inet_ntoa( dest.sin_addr );
    
    	cout << dest_ip << endl;
    
    	data_size = size;
    	if( ( data_size < minsize ) || ( data_size > maxsize ) )
    	{
    		data_size = defsize;
    
    		cout << "Data size too large / small!" << endl
    			 << "Setting to default: " << defsize << endl;
    	}
    
    	data_size += sizeof( icmp_header );
    
    	icmp_data = ( char * ) malloc( maxsize );
    	recved = ( char * ) malloc( maxsize + sizeof( ip_header ) + sizeof( icmp_header ) );
    
    	if( !icmp_data || !recved )
    	{
    		cerr << "error: not enough memory: " << GetLastError( ) << endl;
    		return -1;
    	}
    
    	createICMPHeader( icmp_data, data_size );
    
    	for( int i=0; i<count; i++ )
    	{
    		( ( icmp_header * ) icmp_data )->i_cksum = 0;
    		( ( icmp_header * ) icmp_data )->timestamp = GetTickCount( );
    
    		( ( icmp_header * ) icmp_data )->i_seq = seq_no++;
    		( ( icmp_header * ) icmp_data )->i_cksum = checksum( ( unsigned short * ) icmp_data, data_size );
    
    		sent_bytes = sendto( sock, icmp_data, data_size, 0, ( SOCKADDR * ) &dest, sizeof( dest ) );
    
    		if( sent_bytes == SOCKET_ERROR )
    		{
    			if( WSAGetLastError( ) == WSAETIMEDOUT )
    			{
    				cout << "send: connection timed out" << endl;
    				continue;
    			}
    			cout << "sendto failed: " << WSAGetLastError( ) << endl;
    			return -1;
    		}
    
    		if( sent_bytes < data_size )
    			cout << "wrote " << sent_bytes << " bytes" << endl;
    
    		recv_bytes = recvfrom( sock, recved, maxsize+sizeof( ip_header )+sizeof( icmp_header ), 0, ( SOCKADDR * ) &from, &fromlen );
    
    		if( recv_bytes == SOCKET_ERROR )
    		{
    			if( WSAGetLastError( ) == WSAETIMEDOUT )
    			{
    				cout << "recv: connection timed out" << endl;
    				continue;
    			}
    			cout << "recvfrom failed: " << WSAGetLastError( ) << endl;
    			return -1;
    		}
    
    		decodeResponse( recved, recv_bytes, &from );
    		Sleep( pause );		
    	}
    
    	return 0;
    }
    

    wenn der fehler hier nicht zu finden ist, dann kann ich auch noch die anderen funktionen posten. ich denke aber, die haben eher nichts damit zu tun...

    falls das hier das falsche forum ist, bitte nicht böse sein, ich wusste nicht wohin damit... 🙄

    ciao, cypoc



  • Eine falsch berechnete Checksum des ICMP-headers war der Grund dafür,
    dass der Kernel des Betriebssystems keine Antworten verschickt hat. Die Funktion selbst, läuft einwandfrei. 🙂

    cypoc


Anmelden zum Antworten