"Fossies" - the Fresh Open Source Software Archive

Member "libping-1.15/lib/smtp.c" (28 Mar 2002, 3639 Bytes) of package /linux/privat/old/libping-1.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "smtp.c" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * SMTP module
    3  *
    4  * Copyright (C) 2001 Jeffrey Fulmer <jdfulmer@armstrong.com>
    5  * This file is part of LIBPING
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; either version 2 of the License, or
   10  * (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20  *
   21  */
   22 #ifdef HAVE_CONFIG_H
   23 # include <config.h>
   24 #endif/*HAVE_CONFIG_H*/ 
   25 
   26 #include <setup.h>
   27 #include <sock.h>
   28 #include <smtp.h>
   29 #include <util.h> 
   30 #include <stdarg.h>
   31 #include <stdlib.h>
   32 #include <stdio.h>
   33 
   34 #ifdef HAVE_SYS_TIMES_H
   35 # include <sys/times.h>
   36 #endif /*HAVE_SYS_TIMES_H*/
   37 #if TIME_WITH_SYS_TIME
   38 # include <sys/time.h>
   39 # include <time.h>
   40 #else
   41 # if HAVE_SYS_TIME_H
   42 #  include <sys/time.h>
   43 # else
   44 #  include <time.h>
   45 # endif
   46 #endif /*TIME_WITH_SYS_TIME*/
   47  
   48 #define  MSGBUF 2048 
   49 
   50 int smtp_cmd( CONN *C, char *format, ... ); 
   51 
   52 int
   53 mysmtp( SMTPDATA *S )
   54 {
   55   CONN *C;
   56   char buf[MSGBUF];
   57   int ret  = 0;
   58   struct timeval mytime;
   59 
   60   C = (CONN*)xmalloc( sizeof( CONN ));
   61   C->port    =   25;
   62   C->timeout = ( S->timeout == 0 )?60:S->timeout; 
   63  
   64   /* set the rtt timer */
   65   (void) gettimeofday( &mytime, (struct timezone *)NULL); 
   66 
   67   if(( C->sock=JOEsocket( C, S->hostname ))    < 0 ){ JOEclose( C ); return -1;  } 
   68   if(( ret = JOEreadline( C, buf, MSGBUF ))    < 0 ){ JOEclose( C ); return ret; }
   69   if(( ret = smtp_cmd( C, "%s", "HELO dude" )) < 0 ){ JOEclose( C ); return ret; }
   70   if(( ret = smtp_cmd( C, "%s", "QUIT"   ))    < 0 ){ JOEclose( C ); return ret; }
   71   JOEclose( C );
   72 
   73   S->rtt = elapsed_time( &mytime );
   74   
   75   return 1;
   76 }
   77 
   78 int
   79 smtp_cmd( CONN *C, char *format, ... )
   80 {
   81   va_list args;
   82   char buf[MSGBUF];
   83   char rec[MSGBUF];
   84   int  len;
   85   int  ret;
   86   
   87   bzero( buf, sizeof( buf ));
   88   bzero( rec, sizeof( rec));  
   89 
   90   va_start( args, format );
   91   if(( vsnprintf( buf, MSGBUF-3, format, args )) < 0 ){
   92     perror( "message too large" );
   93     exit( 1 );
   94   }
   95   
   96   (void)strcat( buf, "\015\012" );
   97   
   98   if( JOEsocket_write( C, buf, sizeof( buf )) < 0 )
   99     return -1;
  100 
  101   if(( ret = JOEreadline( C, rec, MSGBUF )) <= 0 ){ 
  102     return -1; 
  103   }
  104   *rec='\0';
  105 
  106   va_end( args );
  107   if(( ret = atoi( rec )) > 400 ){ 
  108     return -1; 
  109   } else return 1;
  110 }
  111 
  112 int 
  113 pingsmtp( const char *hostname )
  114 {
  115   SMTPDATA *S;
  116   int ret;
  117  
  118   S = (SMTPDATA*)xmalloc( sizeof( SMTPDATA ));
  119   S->hostname = (char*)strdup( hostname );
  120   S->timeout = 0;
  121  
  122   return mysmtp( S );
  123 }
  124 
  125 int 
  126 pingtsmtp( const char *hostname, int t )
  127 {
  128   SMTPDATA *S;
  129   int ret;
  130  
  131   S = (SMTPDATA*)xmalloc( sizeof( SMTPDATA ));
  132   S->hostname = (char*)strdup( hostname );
  133   S->timeout = t;
  134  
  135   return mysmtp( S );
  136 }
  137 
  138 int
  139 tpingsmtp( const char *hostname )
  140 {
  141   SMTPDATA *S;
  142   int ret;
  143  
  144   S = (SMTPDATA*)xmalloc( sizeof( SMTPDATA ));
  145   S->hostname = (char*)strdup( hostname );
  146   S->timeout = 0;
  147  
  148   ret = mysmtp( S ); 
  149  
  150   if( ret > 0 ){ return S->rtt; }
  151   else         { return ret; }
  152 }
  153  
  154 int
  155 tpingtsmtp( const char *hostname, int t )
  156 {
  157   SMTPDATA *S;
  158   int ret;
  159  
  160   S = (SMTPDATA*)xmalloc( sizeof( SMTPDATA ));
  161   S->hostname = (char*)strdup( hostname );
  162   S->timeout = t;
  163  
  164   ret = mysmtp( S ); 
  165  
  166   if( ret > 0 ){ return S->rtt; }
  167   else         { return ret; }
  168  
  169 }