Copied missing files from the subversion repository.
[collectd.git] / src / libping / echo.c
1 /**
2  * ECHO module
3  *
4  * Copyright (C) 2001,2002 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 <sock.h>
27 #include <echo.h>
28 #include <util.h>
29 #include <stdlib.h>
30 #include <stdio.h>  
31 #include <errno.h>
32 #include <setup.h>
33 #include <string.h>
34
35 #include "memory.h"
36
37 #define MAXLINE  81921
38
39 int 
40 send_data( CONN *C, int bytes )
41 {
42   char buf[MAXLINE];
43   char rec[MAXLINE];
44   int  ret;
45   int  x, n;
46
47   bytes = (bytes>MAXLINE-3)?MAXLINE-3:bytes;
48
49   memset( buf, 0, bytes+3 );
50   memset( rec, 0, bytes+3 );
51
52   for( x = 0; x < bytes; x++ )
53     buf[x] = '#';
54
55   (void)strcat( buf, "\015\012" );
56
57   if( JOEsocket_write( C, buf, sizeof( buf )) < 0 ){
58     return -1; 
59   }
60
61   if(( n = JOEreadline( C, rec, sizeof( rec ))) < 0 ){
62     return -1;
63   }
64
65   if(( ret = strlen( rec )) > 0 ){ 
66     return 1;
67   }
68   else
69     return -1;
70 }
71
72 int
73 myecho( ECHODATA *E )
74 {
75   CONN *C;               /* threadsafe connection */
76   int ret;               /* return conditional    */
77   struct timeval mytime;
78  
79   C = (CONN*)xmalloc( sizeof( CONN )); 
80   C->port    = 7; 
81   C->timeout = ( E->timeout == 0 )?60:E->timeout;  
82
83   (void) gettimeofday( &mytime, (struct timezone *)NULL);  
84
85   if(( C->sock = JOEsocket( C, E->hostname )) < 0 ){
86     return -1;
87   }
88
89   ret = send_data( C, E->bytes );
90   
91   E->rtt = elapsed_time( &mytime );
92   return ret;
93 }
94
95 int
96 echohost( const char *hostname, int b )
97 {
98   ECHODATA *E;
99  
100   E = (ECHODATA*)xmalloc( sizeof( ECHODATA ));
101   E->hostname = (char*)strdup( hostname );
102   E->bytes   = b;
103   E->timeout = 0;  
104   return ( myecho( E ));
105 }
106  
107 int
108 echothost( const char *hostname, int b, int t )
109 {
110   ECHODATA *E;
111  
112   E = (ECHODATA*)xmalloc( sizeof( ECHODATA ));
113   E->hostname = (char*)strdup( hostname );
114   E->bytes   = b;
115   E->timeout = t; 
116  
117   return ( myecho( E ));
118 }
119
120 int
121 techohost( const char *hostname, int b )
122 {
123   ECHODATA *E;
124   int ret;
125  
126   E = (ECHODATA*)xmalloc( sizeof( ECHODATA ));
127   E->hostname = (char*)strdup( hostname );
128   E->bytes   = b;
129   E->timeout = 0; 
130  
131   ret = myecho( E );
132  
133   if( ret > 0 ){ return E->rtt; }
134   else         { return ret; }
135 }
136  
137 int
138 techothost( const char *hostname, int b, int t )
139 {
140   ECHODATA *E; 
141   int ret;
142   
143   E = (ECHODATA*)xmalloc( sizeof( ECHODATA )); 
144   E->hostname = (char*)strdup( hostname );
145   E->bytes   = b;
146   E->timeout = t; 
147
148   ret = myecho( E );
149   if( ret > 0 ){ return E->rtt; }
150   else         { return ret; }
151  
152
153