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 *======================================================================*/
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x >> 32)) ) )
37 void header_add(void* buf
, int count
, unsigned long addr
, unsigned short port
)
39 unsigned char* cp
= (unsigned char*)buf
;
40 unsigned long long tm
= time_in_milliseconds();
42 marshal_short(cp
, count
); /* Size of message body. */
43 marshal_ulong_long(cp
, tm
); /* Now in msec. */
44 marshal_long(cp
, addr
); /* Sender IP address. */
45 marshal_short(cp
, port
); /* Sender port number. */
46 marshal_short(cp
, arg_site
); /* Site ID number */
47 /* TODO: should be perfectly alright, but un-QA'd marshal_long(cp, 0); */
51 int header_is_rotate (void* buf
)
53 return toknam_eq((unsigned char *)buf
+ HEADER_LENGTH
,
54 (unsigned char *)ROTATE_COMMAND
);
55 LOG_PROG("Command::Rotate message received.\n");
58 void header_fingerprint(void* buf
, struct packet_check
* pc
)
60 unsigned char* cp
= (unsigned char*)buf
;
62 unsigned long long tm
;
65 /* Extract the timestamp. */
66 unmarshal_short(cp
, length
);
67 unmarshal_ulong_long(cp
, tm
);
69 /* Add phony timestamp of zero. */
71 marshal_long_long(cp
, zero
);
74 /* pc->md5 = md5(); */
77 /////////////////////////////////////////////////////////////////////////////
78 // It's a small thing, but toknam_eq() is quicker than memcmp().
79 // * memcmp does a tremendous amount of setup before comparing the first
80 // character (it's designed to compare large strings efficently)
81 // * we don't care if greater-than or less-than, just equal or not
82 // * syntactically simpler (taking advantage of token's length byte)
83 int toknam_eq(const unsigned char* toknam
, const unsigned char* nam
)
85 unsigned char len
= *nam
+1 ;
86 while ( *toknam
++ == *nam
++ )
92 /////////////////////////////////////////////////////////////////////////////
94 void header_print (const char* buf
)
96 LOG_INF("header payload length: %u\n", header_payload_length(buf
));
97 LOG_INF("receipt time: %llu\n", header_receipt_time(buf
));
98 LOG_INF("now: %llu\n", time(NULL
)*1000L);
99 LOG_INF("sender IP text: %s\n", header_sender_ip_formatted(buf
));
100 LOG_INF("sender port: %u\n", header_sender_port(buf
));
101 LOG_INF("site id: %u\n", header_site_id(buf
));
104 static uint16_t header_uint16(const char* bytes
) {
105 return ntohs(*((uint16_t*) bytes
));
108 static uint32_t header_uint32(const char* bytes
) {
109 return ntohl(*((uint32_t*) bytes
));
112 static uint64_t header_uint64(const char* bytes
) {
113 return (((uint64_t)header_uint32(bytes
))<<32) | header_uint32(bytes
+4);
116 uint16_t header_payload_length(const char* header
)
118 return header_uint16(header
);
121 uint64_t header_receipt_time(const char* header
) {
122 return header_uint64(header
+RECEIPT_TIME_OFFSET
);
125 const char* header_sender_ip_formatted(const char* header
) {
126 struct in_addr addr
= { header_uint32(header
+SENDER_IP_OFFSET
) };
127 return inet_ntoa(addr
);
130 uint16_t header_sender_port(const char* header
) {
131 return header_uint16(header
+SENDER_PORT_OFFSET
);
134 uint16_t header_site_id(const char* header
) {
135 return header_uint16(header
+SITE_ID_OFFSET
);