/
hpfeeds.c
196 lines (127 loc) · 3.9 KB
/
hpfeeds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
hpfeeds.c
Copyright (C) 2011 The Honeynet Project
Copyright (C) 2011 Tillmann Werner, tillmann.werner@gmx.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <hpfeeds.h>
#include <stdlib.h>
#include <string.h>
#include "sha1.h"
hpf_msg_t *hpf_msg_new(void) {
hpf_msg_t *msg;
msg = calloc(1, sizeof(hpf_msg_t));
msg->hdr.msglen = htonl(sizeof(msg->hdr));
return msg;
}
void hpf_msg_delete(hpf_msg_t *m) {
if (m) free(m);
return;
}
hpf_msg_t *hpf_msg_getmsg(u_char *data) {
return (hpf_msg_t *) data;
}
u_int32_t hpf_msg_getsize(hpf_msg_t *m) {
return ntohl(m->hdr.msglen);
}
u_int32_t hpf_msg_gettype(hpf_msg_t *m) {
return m->hdr.opcode;
}
hpf_msg_t *hpf_msg_add_chunk(hpf_msg_t **m, const u_char *data, size_t len) {
hpf_msg_t *msg = *m;
u_char l;
if (!m || !data || !len)
return NULL;
l = len < 0xff ? len : 0xff;
*m = msg = realloc(msg, ntohl(msg->hdr.msglen) + l + 1);
if (msg == NULL)
return NULL;
((u_char *) msg)[ntohl(msg->hdr.msglen)] = l;
memcpy(((u_char *) msg) + ntohl(msg->hdr.msglen) + 1, data, l);
msg->hdr.msglen += htonl(1 + l);
return msg;
}
hpf_chunk_t *hpf_msg_get_chunk(u_char *data, size_t len) {
hpf_chunk_t *c;
if (!data || !len) return NULL;
c = (hpf_chunk_t *) data;
// incomplete chunk?
if (c->len > len + 1) return NULL;
return c;
}
hpf_msg_t *hpf_msg_add_payload(hpf_msg_t **m, const u_char *data, size_t len) {
hpf_msg_t *msg = *m;
if (!m || !data || !len)
return NULL;
*m = msg = realloc(msg, ntohl(msg->hdr.msglen) + len);
if (msg == NULL)
return NULL;
memcpy(((u_char *) msg) + ntohl(msg->hdr.msglen), data, len);
msg->hdr.msglen += htonl(len);
return msg;
}
hpf_msg_t *hpf_msg_error(u_char *err, size_t err_size) {
hpf_msg_t *msg;
msg = hpf_msg_new();
if (msg == NULL)
return NULL;
msg->hdr.opcode = OP_ERROR;
hpf_msg_add_payload(&msg, err, err_size);
return msg;
}
hpf_msg_t *hpf_msg_info(u_int32_t nonce, u_char *fbname, size_t fbname_len) {
hpf_msg_t *msg;
msg = hpf_msg_new();
if (msg == NULL)
return NULL;
msg->hdr.opcode = OP_INFO;
hpf_msg_add_chunk(&msg, fbname, fbname_len);
hpf_msg_add_payload(&msg, (u_char *) &nonce, sizeof(u_int32_t));
return msg;
}
hpf_msg_t *hpf_msg_auth(u_int32_t nonce, u_char *ident, size_t ident_len, u_char *secret, size_t secret_len) {
hpf_msg_t *msg;
SHA1Context ctx;
u_char hash[SHA1HashSize];
msg = hpf_msg_new();
if (msg == NULL)
return NULL;
msg->hdr.opcode = OP_AUTH;
SHA1Reset(&ctx);
SHA1Input(&ctx, (u_int8_t *) &nonce, sizeof(nonce));
SHA1Input(&ctx, (u_int8_t *) secret, secret_len);
SHA1Result(&ctx, hash);
hpf_msg_add_chunk(&msg, ident, ident_len);
hpf_msg_add_payload(&msg, hash, SHA1HashSize);
return msg;
}
hpf_msg_t *hpf_msg_publish(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len, u_char *data, size_t data_len) {
hpf_msg_t *msg;
msg = hpf_msg_new();
if (msg == NULL)
return NULL;
msg->hdr.opcode = OP_PUBLISH;
hpf_msg_add_chunk(&msg, ident, ident_len);
hpf_msg_add_chunk(&msg, channel, channel_len);
hpf_msg_add_payload(&msg, data, data_len);
return msg;
}
hpf_msg_t *hpf_msg_subscribe(u_char *ident, size_t ident_len, u_char *channel, size_t channel_len) {
hpf_msg_t *msg;
msg = hpf_msg_new();
if (msg == NULL)
return NULL;
msg->hdr.opcode = OP_SUBSCRIBE;
hpf_msg_add_chunk(&msg, ident, ident_len);
hpf_msg_add_payload(&msg, channel, channel_len);
return msg;
}