Must use (void) instead of () for function declarations.
[lwes-journaller.git] / src / header.c
blob1d619a685b0a0d803b33e7c7418b432d42e83090
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
20 #include "config.h"
22 #include "header.h"
24 #include "log.h"
25 #include "opt.h"
26 #include "perror.h"
27 #include "stats.h"
29 #include "lwes.h"
30 #include "marshal.h"
32 #include <netinet/in.h>
33 #include <sys/time.h>
35 void header_add(void* buf, int count, unsigned long addr, unsigned short port)
37 unsigned char* cp = (unsigned char*)buf;
39 struct timeval t;
40 unsigned long long tm;
42 if ( -1 == gettimeofday(&t, 0) ) /* This is where we need to */
43 PERROR("gettimeofday"); /* timestamp the packet. */
45 tm = ((((long long)t.tv_sec) * 1000LL) + (long long)(t.tv_usec/1000));
47 marshal_short(cp, count); /* Size of message body. */
48 marshal_ulong_long(cp, tm); /* Now in msec. */
49 marshal_long(cp, addr); /* Sender IP address. */
50 marshal_short(cp, port); /* Sender port number. */
51 marshal_short(cp, arg_site); /* Site ID number */
52 /* TODO: should be perfectly alright, but un-QA'd marshal_long(cp, 0); */
53 /* reserved */
56 int header_is_rotate (void* buf, time_t* when)
58 if ( toknam_eq((unsigned char *)buf + HEADER_LENGTH,
59 (unsigned char *)ROTATE_COMMAND) )
61 unsigned long long tm;
62 unsigned char* cp = (unsigned char*)buf + 2 ;
63 LOG_PROG("Command::Rotate message received.\n");
64 unmarshal_ulong_long(cp, tm);
65 *when = (time_t)(tm / 1000LL);
66 return 1;
69 // Ping-Pong ?
70 if ( toknam_eq((unsigned char *)buf + HEADER_LENGTH,
71 (unsigned char *)JOURNALLER_PING_EVENT_TYPE) )
73 LOG_PROG("System::Ping message received.\n");
74 return 2;
77 return 0;
80 void header_fingerprint(void* buf, struct packet_check* pc)
82 unsigned char* cp = (unsigned char*)buf;
83 long long zero = 0LL;
84 unsigned long long tm;
85 short length;
87 /* Extract the timestamp. */
88 unmarshal_short(cp, length);
89 unmarshal_ulong_long(cp, tm);
91 /* Add phony timestamp of zero. */
92 cp -= 8;
93 marshal_long_long(cp, zero);
95 pc->received = tm;
96 /* pc->md5 = md5(); */
99 /////////////////////////////////////////////////////////////////////////////
100 // It's a small thing, but toknam_eq() is quicker than memcmp().
101 // * memcmp does a tremendous amount of setup before comparing the first
102 // character (it's designed to compare large strings efficently)
103 // * we don't care if greater-than or less-than, just equal or not
104 // * syntactically simpler (taking advantage of token's length byte)
105 int toknam_eq(const unsigned char* toknam, const unsigned char* nam)
107 unsigned char len = *nam+1 ;
108 while ( *toknam++ == *nam++ )
109 if ( --len == 0 )
110 return 1 ;
111 return 0 ;
114 /////////////////////////////////////////////////////////////////////////////
115 int non_revenue_bearing (const unsigned char* buf)
117 if ( toknam_eq(buf + HEADER_LENGTH,(unsigned char *)JOURNALLER_CM_SERVE) )
119 st.hurryup_discards[0] += 1 ;
120 return 1 ;
122 if ( toknam_eq(buf + HEADER_LENGTH,(unsigned char *)JOURNALLER_DM_SERVE) )
124 st.hurryup_discards[1] += 1 ;
125 return 1 ;
127 if ( toknam_eq(buf + HEADER_LENGTH,(unsigned char *)JOURNALLER_SS_SERVE) )
129 st.hurryup_discards[2] += 1 ;
130 return 1 ;
132 return 0 ;
135 /* globals */
136 static struct lwes_emitter *emitter = NULL ;
137 static struct lwes_event_deserialize_tmp *dtmp = NULL;
138 #define JOURNALLER_PING_SENDER_IP_FIELD "SenderIP"
139 #define JOURNALLER_PING_RETURN_IP_FIELD "ReturnIP"
140 #define JOURNALLER_PING_RETURN_PORT_FIELD "ReturnPort"
141 #define JOURNALLER_PING_DEFAULT_PORT 64646
144 journaller_ping_transport_send_pong (char *address, int port)
146 LOG_PROG("Sending System::Pong to %s:%i\n", address, port);
148 struct lwes_event *pong_event =
149 lwes_event_create( (struct lwes_event_type_db *) NULL,
150 (LWES_SHORT_STRING) JOURNALLER_PONG_EVENT_TYPE);
151 if ( pong_event == NULL )
153 return 1 ;
156 lwes_emitter_emitto(address, NULL/*arg_interface*/, port, emitter, pong_event);
157 lwes_event_destroy(pong_event);
159 LOG_PROG("Sending System::Pong finished\n");
160 return 0;
164 ping (void* buf, size_t bufsiz)
166 unsigned short int return_port ;
167 struct in_addr return_ip ; memset(&return_ip, 0, sizeof(return_ip)) ;
168 struct lwes_event *ping_event ;
169 char* evt = (char*)buf ;
171 if ( arg_nopong )
173 return 1 ;
176 /* setup System::Pong's transport */
177 if ( emitter == NULL )
179 emitter = lwes_emitter_create( (LWES_CONST_SHORT_STRING) arg_ip,
180 (LWES_CONST_SHORT_STRING) NULL, //arg_interface,
181 (LWES_U_INT_32) arg_port, 0, 60 );
183 if ( emitter == NULL )
185 return -1 ;
188 if ( dtmp == NULL )
190 dtmp = (struct lwes_event_deserialize_tmp *)
191 malloc(sizeof(struct lwes_event_deserialize_tmp));
192 if(dtmp == NULL)
194 return -1;
198 ping_event = lwes_event_create_no_name(NULL);
200 if ( ping_event != NULL )
203 lwes_event_from_bytes (ping_event,
204 (LWES_BYTE_P)&evt[HEADER_LENGTH],
205 bufsiz-HEADER_LENGTH, 0, dtmp);
207 if( lwes_event_get_IP_ADDR (ping_event,
208 JOURNALLER_PING_RETURN_IP_FIELD,
209 &return_ip) != 0 )
211 if ( lwes_event_get_IP_ADDR(ping_event,
212 JOURNALLER_PING_SENDER_IP_FIELD,
213 &return_ip) != 0 )
214 { // none-of-the-above, so use header's sender-ip
215 char* xxx = (char*)&return_ip ;
216 xxx[0] = evt[13] ;
217 xxx[1] = evt[12] ;
218 xxx[2] = evt[11] ;
219 xxx[3] = evt[10] ;
223 if( lwes_event_get_U_INT_16(ping_event,
224 JOURNALLER_PING_RETURN_PORT_FIELD,
225 &return_port) != 0 )
227 return_port = JOURNALLER_PING_DEFAULT_PORT;
230 lwes_event_get_U_INT_16(ping_event, JOURNALLER_PING_RETURN_PORT_FIELD,
231 &return_port);
233 journaller_ping_transport_send_pong(inet_ntoa(return_ip), return_port);
234 /* cleanup */
235 lwes_event_destroy(ping_event);
238 /* we're done */
239 return 0;
242 /* end-of-file */