tp_as.c

Go to the documentation of this file.
00001 /* vim: set tabstop=4: */
00002 /*
00003  * This file is part of TraceProto.
00004  * Copyright 2004-2005 Eric Hope and others; see the AUTHORS file for details.
00005  *
00006  * TraceProto is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * TraceProto is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with TraceProto; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include <sys/types.h>
00022 #include <sys/time.h>
00023 #include <sys/socket.h>
00024 #include <netdb.h>
00025 #include <arpa/inet.h>
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <unistd.h>
00029 #include <stdlib.h>
00030 #include <assert.h>
00031 
00032 #include "config.h"
00033 #include "tp_as.h"
00034 
00035 #ifdef OS_APPLE
00036 #include <netinet/in.h>
00037 #endif /* OS_APPLE */
00038 
00039 #ifdef OS_FREEBSD
00040 #include <netinet/in.h>
00041 #endif /* OS_FREEBSD */
00042 
00043 
00044 #ifdef USE_DMALLOC
00045 #include <dmalloc.h>
00046 #endif /* USE_DMALLOC */
00047 
00048 static char * ra_server;
00049 static char * ra_service;
00050 static struct hostent * ra_hostent;
00051 static struct servent * ra_servent;
00052 
00053 
00054 int setup_as ( void )
00055 {
00056         char * ra_env;
00057         size_t ra_env_len;
00058 
00059         /* look up the server name if present */
00060         ra_env = getenv ( "RA_SERVER" );
00061 
00062         if ( ra_env == NULL )
00063         {
00064                 ra_server = ( char * ) malloc ( sizeof ( char ) * 13 );
00065                 assert(ra_server != NULL);
00066                 memset ( ra_server, '\0', sizeof ( char ) * 13 );
00067                 strncpy ( ra_server, "whois.ra.net", 12 );
00068         } else {
00069                 ra_env_len = strlen ( ra_env );
00070                 ra_server = ( char * ) malloc ( sizeof ( char ) * ( ra_env_len + 1 ) );
00071                 assert(ra_server != NULL);
00072                 memset ( ra_server, '\0', sizeof ( char ) * ( ra_env_len + 1 ) );
00073                 strncpy ( ra_server, ra_env, ra_env_len );
00074         }
00075 
00076         if ( ( ra_hostent = gethostbyname ( ra_server ) ) == NULL )
00077         {
00078                 strcpy ( as_string, "unable to lookup ra_server IP" );
00079                 return -1;
00080         }
00081 
00082         ra_env = NULL;
00083         ra_env_len = 0;
00084 
00085         /* look up the port */
00086         ra_env = getenv ( "RA_SERVICE" );
00087 
00088         if ( ra_env == NULL )
00089         {
00090                 ra_service = ( char * ) malloc ( sizeof ( char ) * 6 );
00091                 assert(ra_service != NULL);
00092                 memset ( ra_service, '\0', sizeof ( char ) * 6 );
00093                 strncpy ( ra_service, "whois", 5 );
00094         } else {
00095                 ra_env_len = strlen ( ra_env );
00096                 ra_service = ( char * ) malloc ( sizeof ( char ) * ( ra_env_len + 1 ) );
00097                 assert(ra_service != NULL);
00098                 memset ( ra_service, '\0', strlen ( ra_env ) + 1 );
00099                 strncpy ( ra_service, ra_env, ra_env_len );
00100         }
00101 
00102         if ( ( ra_servent = getservbyname ( ra_service, "tcp" ) ) == NULL )
00103         {
00104                 strcpy ( as_string, "unable to lookup ra_server port" );
00105                 return -1;
00106         }
00107 
00108         return 0;
00109 }
00110 
00111 
00112 int find_as ( const char * dotted_quad )
00113 {
00114         int as_socket;
00115         struct protoent * protoent;
00116         struct sockaddr_in as_server_addr;
00117 
00118         char send_buf [ 18 ];
00119         char recv_buf [ 512 ];
00120         char const * sub_string;
00121         char * end_string;
00122 
00123         fd_set as_select;
00124         struct timeval timeout;
00125 
00126         protoent = getprotobyname ( "tcp" );
00127 
00128         if ( ( as_socket = socket ( PF_INET, SOCK_STREAM, protoent->p_proto ) ) == -1 )
00129         {
00130                 strcpy ( as_string, "as socket error" );
00131                 return -1;
00132         }
00133 
00134         as_server_addr.sin_family = PF_INET;
00135         /* as_server_addr.sin_port = htons ( ra_servent->s_port ); */
00136         as_server_addr.sin_port = ra_servent->s_port;
00137         bcopy ( ra_hostent->h_addr_list[0], & as_server_addr.sin_addr, (size_t) (ra_hostent->h_length) );
00138 
00139         if ( connect ( as_socket,
00140                 ( struct sockaddr * ) & as_server_addr,
00141                 sizeof ( struct sockaddr_in ) ) == -1 )
00142         {
00143                 /* FIX: parse connect errors more fully here */
00144                 strcpy ( as_string, "unable to connect to server" );
00145                 return -1;
00146         }
00147 
00148         FD_ZERO ( & as_select );
00149         FD_SET ( as_socket, & as_select );
00150 
00151         timeout.tv_sec = 10;
00152         timeout.tv_usec = 0;
00153 
00154         memset ( send_buf, '\0', sizeof ( send_buf ) );
00155         memset ( recv_buf, '\0', sizeof ( recv_buf ) );
00156         snprintf ( send_buf, sizeof ( send_buf ), "%s\r\n", dotted_quad );
00157 
00158         if ( send ( as_socket, send_buf, sizeof ( send_buf ), 0 ) == -1 )
00159         {
00160                 /* FIX: parse send errors more fully here */
00161                 strcpy ( as_string, "error sending" );
00162                 return -1;
00163         }
00164 
00165         if ( select ( as_socket + 1, & as_select, NULL, NULL, & timeout ) == 0 )
00166         {
00167                 strcpy ( as_string, "UNKNOWN" );
00168                 return -1;
00169         }
00170 
00171         if ( recv ( as_socket, recv_buf, sizeof ( recv_buf ) - 1, 0 ) == -1 )
00172         {
00173                 /* FIX: blah blah blah */
00174                 strcpy ( as_string, "error receiving" );
00175                 return -1;
00176         }
00177 
00178         sub_string = strstr ( recv_buf, "AS" );
00179         if ( sub_string == NULL )
00180         {
00181                 sub_string = "UNKNOWN";
00182         } else {
00183                 end_string = strchr ( sub_string, '\n' );
00184                 * end_string = '\0';
00185         }
00186 
00187         bcopy ( sub_string, as_string, sizeof ( as_string ) - 1 );
00188 
00189         return 0;
00190 }

Generated on Wed Sep 16 11:08:43 2009 for traceproto by  doxygen 1.5.4