Copied missing files from the subversion repository.
[collectd.git] / src / libping / smtp.c
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 #include <string.h>
34
35 #ifdef HAVE_SYS_TIMES_H
36 # include <sys/times.h>
37 #endif /*HAVE_SYS_TIMES_H*/
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif /*TIME_WITH_SYS_TIME*/
48
49 #include "memory.h"
50  
51 #define  MSGBUF 2048 
52
53 int smtp_cmd( CONN *C, char *format, ... ); 
54
55 int
56 mysmtp( SMTPDATA *S )
57 {
58   CONN *C;
59   char buf[MSGBUF];
60   int ret  = 0;
61   struct timeval mytime;
62
63   C = (CONN*)xmalloc( sizeof( CONN ));
64   C->port    =   25;
65   C->timeout = ( S->timeout == 0 )?60:S->timeout; 
66  
67   /* set the rtt timer */
68   (void) gettimeofday( &mytime, (struct timezone *)NULL); 
69
70   if(( C->sock=JOEsocket( C, S->hostname ))    < 0 ){ JOEclose( C ); return -1;  } 
71   if(( ret = JOEreadline( C, buf, MSGBUF ))    < 0 ){ JOEclose( C ); return ret; }
72   if(( ret = smtp_cmd( C, "%s", "HELO dude" )) < 0 ){ JOEclose( C ); return ret; }
73   if(( ret = smtp_cmd( C, "%s", "QUIT"   ))    < 0 ){ JOEclose( C ); return ret; }
74   JOEclose( C );
75
76   S->rtt = elapsed_time( &mytime );
77   
78   return 1;
79 }
80
81 int
82 smtp_cmd( CONN *C, char *format, ... )
83 {
84   va_list args;
85   char buf[MSGBUF];
86   char rec[MSGBUF];
87   int  ret;
88   
89   bzero( buf, sizeof( buf ));
90   bzero( rec, sizeof( rec));  
91
92   va_start( args, format );
93   if(( vsnprintf( buf, MSGBUF-3, format, args )) < 0 ){
94     perror( "message too large" );
95     exit( 1 );
96   }
97   
98   (void)strcat( buf, "\015\012" );
99   
100   if( JOEsocket_write( C, buf, sizeof( buf )) < 0 )
101     return -1;
102
103   if(( ret = JOEreadline( C, rec, MSGBUF )) <= 0 ){ 
104     return -1; 
105   }
106   *rec='\0';
107
108   va_end( args );
109   if(( ret = atoi( rec )) > 400 ){ 
110     return -1; 
111   } else return 1;
112 }
113
114 int 
115 pingsmtp( const char *hostname )
116 {
117   SMTPDATA *S;
118  
119   S = (SMTPDATA*)xmalloc( sizeof( SMTPDATA ));
120   S->hostname = (char*)strdup( hostname );
121   S->timeout = 0;
122  
123   return mysmtp( S );
124 }
125
126 int 
127 pingtsmtp( const char *hostname, int t )
128 {
129   SMTPDATA *S;
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