gethotbyname error continued

Frank Ramsay fjramsay1234 at hotmail.com
Tue Jun 18 14:47:28 EDT 2002


Well using sethostent(0)/endhostent didn't fix the problem... and using 
splint didn't find any errors in the code (any at all, which supprises me) 
and there doesn't seem to be a memory leak (atleast the amount of memory the 
application uses doesn't grow over the 30-40 minutes it takes for 
gethostbyname to start failing.  So I cut the function containing the call 
out into it's own program and ran it... it also fails.  So if there is a bug 
in the code it is here.  I'm examining it myself but I figured I'd post it 
so that anyone else who is bored (or just plain curious) could take a look 
at it.  (sorry about the >'s hotmail added them when I clicked forward :(  )

-fjr


>From: root <root at localhost.localdomain>
>To: fjramsay1234 at hotmail.com
>Date: Tue, 18 Jun 2002 14:25:58 -0400
>
>#include <sys/socket.h>
>
>#include <stdlib.h>
>#include <stdio.h>
>#include <stdarg.h>
>#include <ctype.h>
>#include <sys/types.h>
>#include <sys/socket.h>
>#include <netinet/in.h>
>#include <arpa/inet.h>
>#include <netdb.h>
>#include <stdio.h>
>#include <unistd.h>
>#include <sys/timeb.h>
>#include <time.h>
>
>void getTimeString(char *timestr) {
>   time_t now;
>   int x;
>
>   now = time((time_t*)NULL);
>   sprintf(timestr,"%s",ctime(&now));
>   for (x = 0; x < 100; x++)
>     if (timestr[x] == 10 || timestr[x] == 13)
>       timestr[x] = 0;
>}
>
>void log_error(char *fmt, ...)
>{
>   va_list argp;
>   char timestr[100];
>
>   getTimeString(timestr);
>   fprintf(stderr, "%s - ERROR: ",timestr);
>   va_start(argp, fmt);
>   vfprintf(stderr, fmt, argp);
>   va_end(argp);
>}
>
>void log_debug(char *fmt, ...)
>{
>   va_list argp;
>   char timestr[100];
>
>   getTimeString(timestr);
>   fprintf(stderr, "%s - ERROR: ",timestr);
>   va_start(argp, fmt);
>   vfprintf(stderr, fmt, argp);
>   va_end(argp);
>}
>
>#define DEFAULT_PROTO SOCK_STREAM
>#define CAMPSserverName "S1046970"
>#define CAMPSipport (unsigned short)999999
>#define ERR -1
>#define MAX_SOCKET_BUF 2048
>#define SERVER_HANDSHAKE	"yamon"
>#define swt_link "SW1"
>
>char BufW[MAX_SOCKET_BUF];
>int verbosemode = 1;
>
>int ConnectSocketCAMPS(char *Buffer, int timer)
>{
>   char *server_name = CAMPSserverName;
>   unsigned short port = CAMPSipport;
>   int socket_type = DEFAULT_PROTO;
>   int retval = 0;
>   int on = 1;
>   int bufsize = 2048;
>   unsigned int addr;
>   struct sockaddr_in CAMPSserver;
>   struct hostent *hp;
>   int conn_socket;
>
>   if (verbosemode) {
>     log_debug("SoftSwitch Operating Verbose Message Mode, Detail Level 
>%i\n", verbosemode);
>   } else {
>     log_debug("SoftSwitch Operating in Standard Message Mode\n");
>   }
>
>   sethostent(0);
>   //	 Detect if we should call gethostbyname() or gethostbyaddr()
>   if (isalpha(server_name[0])) {   /* server address is a name */
>     hp = gethostbyname(server_name);
>   } else  { /* Convert nnn.nnn address to a usable one */
>     addr = inet_addr(server_name);
>     hp = gethostbyaddr((char *)&addr,4,AF_INET);
>     hp = gethostbyaddr((char *)&addr,5,AF_INET);
>   }
>   endhostent();
>
>   if (hp == NULL ) {
>     log_error("%s Unknown Host %d - 
>%s\n",server_name,h_errno,hstrerror(h_errno));
>
>     return (ERR);
>   }
>
>
>   // Copy the resolved information into the sockaddr_in structure
>   memset(&CAMPSserver,0,sizeof(CAMPSserver));
>   memcpy(&(CAMPSserver.sin_addr),hp->h_addr,hp->h_length);
>   CAMPSserver.sin_family = hp->h_addrtype;
>   CAMPSserver.sin_port = htons(port);
>   conn_socket = socket(AF_INET, SOCK_STREAM, 0);  /* Open a socket */
>   setsockopt(conn_socket, SOL_SOCKET, SO_SNDBUF, (char*)&bufsize, 
>sizeof(bufsize));
>   setsockopt(conn_socket, SOL_SOCKET, SO_RCVBUF, (char*)&bufsize, 
>sizeof(bufsize));
>   if (conn_socket < 0 ) {
>     return(ERR);
>   } else {
>
>     if (verbosemode)  {
>       printf("SoftSwitch Socket Successfully Opened:\n");
>       printf("SoftSwitch Connected To Host:\n");
>       printf("SoftSwitch connecting to: %s\n",hp->h_name);
>     }
>   }
>
>
>   if (connect(conn_socket,(struct 
>sockaddr*)&CAMPSserver,sizeof(CAMPSserver)) < 0)
>     {
>       return (ERR);
>     }
>
>   if ((retval = setsockopt(conn_socket, SOL_SOCKET, SO_REUSEADDR, (char 
>*)&on,
>			   sizeof(on))) < 0)  {
>     return(ERR);
>   }
>
>
>   if ((retval = setsockopt(conn_socket, SOL_SOCKET, SO_KEEPALIVE, (char 
>*)&on,
>			   sizeof(on))) < 0)  {
>     return(ERR);
>   }
>
>   if ((retval = setsockopt(conn_socket, SOL_SOCKET, SO_SNDBUF, (char 
>*)&Buffer,
>			   sizeof(Buffer))) < 0)  {
>     return(ERR);
>   }
>
>
>   // Send Negotiation packet - specify preferred language.
>   if (timer !=0) {
>     sleep(timer);
>   }
>   memset(Buffer,0,sizeof(Buffer));
>   memset(BufW,0,sizeof(BufW));
>
>   strcpy(BufW, SERVER_HANDSHAKE);
>   strcat(BufW, swt_link );
>   strcpy(Buffer, 	BufW);
>   retval = send(conn_socket,Buffer,MAX_SOCKET_BUF,0);
>
>   if (retval < 0) {
>     return(ERR);
>   }
>
>   if (verbosemode)  {
>     printf("SoftSwitch Searching For CAMPS Server:\n");
>   }
>
>   printf("SoftSwitch Located CAMPS Server, Handshake =\n");
>   return (conn_socket);
>}
>
>
>
>
>int main(void) {
>   char buffer[2048];
>
>   while (1 == 1) {
>     sleep(1);
>     ConnectSocketCAMPS(buffer, 0);
>   }
>}
>
>
>
>
>
>
>
>
>
>
>
>
>
>




_________________________________________________________________
Join the world)B’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com




More information about the Discuss mailing list