1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
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. *
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. *
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 *======================================================================*/
32 #include <netinet/in.h>
35 void header_add(void* buf
, int count
, unsigned long addr
, unsigned short port
)
37 unsigned char* cp
= (unsigned char*)buf
;
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); */
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);
70 if ( toknam_eq((unsigned char *)buf
+ HEADER_LENGTH
,
71 (unsigned char *)JOURNALLER_PING_EVENT_TYPE
) )
73 LOG_PROG("System::Ping message received.\n");
80 void header_fingerprint(void* buf
, struct packet_check
* pc
)
82 unsigned char* cp
= (unsigned char*)buf
;
84 unsigned long long tm
;
87 /* Extract the timestamp. */
88 unmarshal_short(cp
, length
);
89 unmarshal_ulong_long(cp
, tm
);
91 /* Add phony timestamp of zero. */
93 marshal_long_long(cp
, zero
);
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
++ )
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 ;
122 if ( toknam_eq(buf
+ HEADER_LENGTH
,(unsigned char *)JOURNALLER_DM_SERVE
) )
124 st
.hurryup_discards
[1] += 1 ;
127 if ( toknam_eq(buf
+ HEADER_LENGTH
,(unsigned char *)JOURNALLER_SS_SERVE
) )
129 st
.hurryup_discards
[2] += 1 ;
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
)
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");
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
;
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
)
190 dtmp
= (struct lwes_event_deserialize_tmp
*)
191 malloc(sizeof(struct lwes_event_deserialize_tmp
));
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
,
211 if ( lwes_event_get_IP_ADDR(ping_event
,
212 JOURNALLER_PING_SENDER_IP_FIELD
,
214 { // none-of-the-above, so use header's sender-ip
215 char* xxx
= (char*)&return_ip
;
223 if( lwes_event_get_U_INT_16(ping_event
,
224 JOURNALLER_PING_RETURN_PORT_FIELD
,
227 return_port
= JOURNALLER_PING_DEFAULT_PORT
;
230 lwes_event_get_U_INT_16(ping_event
, JOURNALLER_PING_RETURN_PORT_FIELD
,
233 journaller_ping_transport_send_pong(inet_ntoa(return_ip
), return_port
);
235 lwes_event_destroy(ping_event
);