| 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 | #include "config_unix.h" |
|---|
| 37 | #include "config_win32.h" |
|---|
| 38 | #include "debug.h" |
|---|
| 39 | #include "memory.h" |
|---|
| 40 | #include "net_udp.h" |
|---|
| 41 | #include "hmac.h" |
|---|
| 42 | #include "qfDES.h" |
|---|
| 43 | #include "base64.h" |
|---|
| 44 | #include "gettimeofday.h" |
|---|
| 45 | #include "mbus.h" |
|---|
| 46 | #include "mbus_config.h" |
|---|
| 47 | |
|---|
| 48 | #define MBUS_BUF_SIZE 1500 |
|---|
| 49 | #define MBUS_ACK_BUF_SIZE 1500 |
|---|
| 50 | #define MBUS_MAX_ADDR 10 |
|---|
| 51 | #define MBUS_MAX_PD 10 |
|---|
| 52 | #define MBUS_MAX_QLEN 50 |
|---|
| 53 | |
|---|
| 54 | #ifdef NEED_VSNPRINTF |
|---|
| 55 | static int vsnprintf(char *s, int buf_size, const char *format, va_list ap) |
|---|
| 56 | { |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | UNUSED(buf_size); |
|---|
| 61 | return vsprintf(s,format,ap); |
|---|
| 62 | } |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | struct mbus_msg { |
|---|
| 66 | struct mbus_msg *next; |
|---|
| 67 | struct timeval time; |
|---|
| 68 | struct timeval ts; |
|---|
| 69 | char *dest; |
|---|
| 70 | int reliable; |
|---|
| 71 | int complete; |
|---|
| 72 | int seqnum; |
|---|
| 73 | int retransmit_count; |
|---|
| 74 | int message_size; |
|---|
| 75 | int num_cmds; |
|---|
| 76 | char *cmd_list[MBUS_MAX_QLEN]; |
|---|
| 77 | char *arg_list[MBUS_MAX_QLEN]; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | struct mbus { |
|---|
| 81 | socket_udp *s; |
|---|
| 82 | int num_addr; |
|---|
| 83 | char *addr[MBUS_MAX_ADDR]; |
|---|
| 84 | int max_other_addr; |
|---|
| 85 | int num_other_addr; |
|---|
| 86 | char **other_addr; |
|---|
| 87 | char *parse_buffer[MBUS_MAX_PD]; |
|---|
| 88 | char *parse_bufend[MBUS_MAX_PD]; |
|---|
| 89 | int parse_depth; |
|---|
| 90 | int seqnum; |
|---|
| 91 | struct mbus_msg *cmd_queue; |
|---|
| 92 | struct mbus_msg *waiting_ack; |
|---|
| 93 | char *hashkey; |
|---|
| 94 | int hashkeylen; |
|---|
| 95 | char *encrkey; |
|---|
| 96 | int encrkeylen; |
|---|
| 97 | struct timeval last_heartbeat; |
|---|
| 98 | struct mbus_config *cfg; |
|---|
| 99 | void (*cmd_handler)(char *src, char *cmd, char *arg, void *dat); |
|---|
| 100 | void (*err_handler)(int seqnum, int reason); |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | static int mbus_addr_match(char *a, char *b) |
|---|
| 104 | { |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | char *y, c; |
|---|
| 112 | |
|---|
| 113 | assert(a != NULL); |
|---|
| 114 | assert(b != NULL); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | while (isspace((unsigned char)*a) || (*a == '(')) a++; |
|---|
| 118 | while (isspace((unsigned char)*b) || (*b == '(')) b++; |
|---|
| 119 | |
|---|
| 120 | while ((*b != '\0') && (*b != ')')) { |
|---|
| 121 | while (isspace((unsigned char)*b)) b++; |
|---|
| 122 | for (y = b; ((*y != ' ') && (*y != ')') && (*y != '\0')); y++) { |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | c = *y; |
|---|
| 126 | *y = '\0'; |
|---|
| 127 | if (strstr(a, b) == NULL) { |
|---|
| 128 | |
|---|
| 129 | *y = c; |
|---|
| 130 | return FALSE; |
|---|
| 131 | } |
|---|
| 132 | *y = c; |
|---|
| 133 | b = y; |
|---|
| 134 | } |
|---|
| 135 | return TRUE; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static void store_other_addr(struct mbus *m, char *a) |
|---|
| 139 | { |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | int i; |
|---|
| 146 | |
|---|
| 147 | for (i = 0; i < m->num_other_addr; i++) { |
|---|
| 148 | if (mbus_addr_match(m->other_addr[i], a)) { |
|---|
| 149 | |
|---|
| 150 | return; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (m->num_other_addr == m->max_other_addr) { |
|---|
| 155 | |
|---|
| 156 | m->max_other_addr *= 2; |
|---|
| 157 | m->other_addr = (char **) xrealloc(m->other_addr, m->max_other_addr * sizeof(char *)); |
|---|
| 158 | } |
|---|
| 159 | m->other_addr[m->num_other_addr++] = xstrdup(a); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | int mbus_addr_valid(struct mbus *m, char *addr) |
|---|
| 163 | { |
|---|
| 164 | int i; |
|---|
| 165 | |
|---|
| 166 | for (i = 0; i < m->num_other_addr; i++) { |
|---|
| 167 | if (mbus_addr_match(m->other_addr[i], addr)) { |
|---|
| 168 | return TRUE; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | return FALSE; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | static char tx_cryptbuf[MBUS_BUF_SIZE]; |
|---|
| 178 | static char tx_buffer[MBUS_BUF_SIZE]; |
|---|
| 179 | static char *tx_bufpos; |
|---|
| 180 | |
|---|
| 181 | #define MBUS_AUTH_LEN 16 |
|---|
| 182 | |
|---|
| 183 | static void tx_header(int seqnum, int ts, char reliable, char *src, char *dst, int ackseq) |
|---|
| 184 | { |
|---|
| 185 | memset(tx_buffer, 0, MBUS_BUF_SIZE); |
|---|
| 186 | memset(tx_buffer, ' ', MBUS_AUTH_LEN); |
|---|
| 187 | tx_bufpos = tx_buffer + MBUS_AUTH_LEN; |
|---|
| 188 | sprintf(tx_bufpos, "\nmbus/1.0 %6d %9d %c (%s) %s ", seqnum, ts, reliable, src, dst); |
|---|
| 189 | tx_bufpos += 33 + strlen(src) + strlen(dst); |
|---|
| 190 | if (ackseq == -1) { |
|---|
| 191 | sprintf(tx_bufpos, "()\n"); |
|---|
| 192 | tx_bufpos += 3; |
|---|
| 193 | } else { |
|---|
| 194 | sprintf(tx_bufpos, "(%6d)\n", ackseq); |
|---|
| 195 | tx_bufpos += 9; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | static void tx_add_command(char *cmnd, char *args) |
|---|
| 200 | { |
|---|
| 201 | sprintf(tx_bufpos, "%s (%s)\n", cmnd, args); |
|---|
| 202 | tx_bufpos += strlen(cmnd) + strlen(args) + 4; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | static void tx_send(struct mbus *m) |
|---|
| 206 | { |
|---|
| 207 | char digest[16]; |
|---|
| 208 | int len; |
|---|
| 209 | unsigned char initVec[8] = {0,0,0,0,0,0,0,0}; |
|---|
| 210 | |
|---|
| 211 | while (((tx_bufpos - tx_buffer) % 8) != 0) { |
|---|
| 212 | |
|---|
| 213 | *(tx_bufpos++) = '\0'; |
|---|
| 214 | } |
|---|
| 215 | *tx_bufpos = '\0'; |
|---|
| 216 | len = tx_bufpos - tx_buffer; |
|---|
| 217 | |
|---|
| 218 | if (m->hashkey != NULL) { |
|---|
| 219 | |
|---|
| 220 | hmac_md5(tx_buffer + MBUS_AUTH_LEN+1, strlen(tx_buffer) - (MBUS_AUTH_LEN+1), m->hashkey, m->hashkeylen, digest); |
|---|
| 221 | base64encode(digest, 12, tx_buffer, MBUS_AUTH_LEN); |
|---|
| 222 | } |
|---|
| 223 | if (m->encrkey != NULL) { |
|---|
| 224 | |
|---|
| 225 | memset(tx_cryptbuf, 0, MBUS_BUF_SIZE); |
|---|
| 226 | memcpy(tx_cryptbuf, tx_buffer, len); |
|---|
| 227 | assert((len % 8) == 0); |
|---|
| 228 | assert(len < MBUS_BUF_SIZE); |
|---|
| 229 | assert(m->encrkeylen == 8); |
|---|
| 230 | qfDES_CBC_e(m->encrkey, tx_cryptbuf, len, initVec); |
|---|
| 231 | memcpy(tx_buffer, tx_cryptbuf, len); |
|---|
| 232 | } |
|---|
| 233 | udp_send(m->s, tx_buffer, len); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | static void resend(struct mbus *m, struct mbus_msg *curr) |
|---|
| 237 | { |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | int i; |
|---|
| 241 | |
|---|
| 242 | tx_header(curr->seqnum, curr->ts.tv_sec, (char)(curr->reliable?'R':'U'), m->addr[0], curr->dest, -1); |
|---|
| 243 | for (i = 0; i < curr->num_cmds; i++) { |
|---|
| 244 | tx_add_command(curr->cmd_list[i], curr->arg_list[i]); |
|---|
| 245 | } |
|---|
| 246 | tx_send(m); |
|---|
| 247 | curr->retransmit_count++; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | void mbus_retransmit(struct mbus *m) |
|---|
| 251 | { |
|---|
| 252 | struct mbus_msg *curr = m->waiting_ack; |
|---|
| 253 | struct timeval time; |
|---|
| 254 | long diff; |
|---|
| 255 | |
|---|
| 256 | if (!mbus_waiting_ack(m)) { |
|---|
| 257 | return; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | gettimeofday(&time, NULL); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | diff = ((time.tv_sec * 1000) + (time.tv_usec / 1000)) - ((curr->time.tv_sec * 1000) + (curr->time.tv_usec / 1000)); |
|---|
| 264 | if (diff > 10000) { |
|---|
| 265 | debug_msg("Reliable mbus message failed!\n"); |
|---|
| 266 | if (m->err_handler == NULL) { |
|---|
| 267 | abort(); |
|---|
| 268 | } |
|---|
| 269 | m->err_handler(curr->seqnum, MBUS_MESSAGE_LOST); |
|---|
| 270 | return; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | if ((diff > 750) && (curr->retransmit_count == 2)) { |
|---|
| 276 | resend(m, curr); |
|---|
| 277 | return; |
|---|
| 278 | } |
|---|
| 279 | if ((diff > 500) && (curr->retransmit_count == 1)) { |
|---|
| 280 | resend(m, curr); |
|---|
| 281 | return; |
|---|
| 282 | } |
|---|
| 283 | if ((diff > 250) && (curr->retransmit_count == 0)) { |
|---|
| 284 | resend(m, curr); |
|---|
| 285 | return; |
|---|
| 286 | } |
|---|
| 287 | curr = curr->next; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | void mbus_heartbeat(struct mbus *m, int interval) |
|---|
| 291 | { |
|---|
| 292 | struct timeval curr_time; |
|---|
| 293 | |
|---|
| 294 | gettimeofday(&curr_time, NULL); |
|---|
| 295 | |
|---|
| 296 | if (curr_time.tv_sec - m->last_heartbeat.tv_sec > interval) { |
|---|
| 297 | mbus_qmsg(m, "()", "mbus.hello", "", FALSE); |
|---|
| 298 | m->last_heartbeat = curr_time; |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | int mbus_waiting_ack(struct mbus *m) |
|---|
| 303 | { |
|---|
| 304 | return m->waiting_ack != NULL; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | int mbus_sent_all(struct mbus *m) |
|---|
| 308 | { |
|---|
| 309 | return (m->cmd_queue == NULL) && (m->waiting_ack == NULL); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | struct mbus *mbus_init(void (*cmd_handler)(char *src, char *cmd, char *arg, void *dat), |
|---|
| 313 | void (*err_handler)(int seqnum, int reason)) |
|---|
| 314 | { |
|---|
| 315 | struct mbus *m; |
|---|
| 316 | struct mbus_key k; |
|---|
| 317 | int i; |
|---|
| 318 | |
|---|
| 319 | m = (struct mbus *) xmalloc(sizeof(struct mbus)); |
|---|
| 320 | if (m == NULL) { |
|---|
| 321 | debug_msg("Unable to allocate memory for mbus\n"); |
|---|
| 322 | return NULL; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | m->cfg = (struct mbus_config *) xmalloc(sizeof(struct mbus_config)); |
|---|
| 326 | mbus_lock_config_file(m->cfg); |
|---|
| 327 | m->s = udp_init("224.255.222.239", (u_int16) 47000, (u_int16) 47000, 0); |
|---|
| 328 | m->seqnum = 0; |
|---|
| 329 | m->cmd_handler = cmd_handler; |
|---|
| 330 | m->err_handler = err_handler; |
|---|
| 331 | m->num_addr = 0; |
|---|
| 332 | m->num_other_addr = 0; |
|---|
| 333 | m->max_other_addr = 10; |
|---|
| 334 | m->other_addr = (char **) xmalloc(sizeof(char *) * 10); |
|---|
| 335 | m->parse_depth = 0; |
|---|
| 336 | m->cmd_queue = NULL; |
|---|
| 337 | m->waiting_ack = NULL; |
|---|
| 338 | |
|---|
| 339 | gettimeofday(&(m->last_heartbeat), NULL); |
|---|
| 340 | |
|---|
| 341 | mbus_get_encrkey(m->cfg, &k); |
|---|
| 342 | m->encrkey = k.key; |
|---|
| 343 | m->encrkeylen = k.key_len; |
|---|
| 344 | |
|---|
| 345 | mbus_get_hashkey(m->cfg, &k); |
|---|
| 346 | m->hashkey = k.key; |
|---|
| 347 | m->hashkeylen = k.key_len; |
|---|
| 348 | |
|---|
| 349 | for (i = 0; i < MBUS_MAX_ADDR; i++) m->addr[i] = NULL; |
|---|
| 350 | for (i = 0; i < MBUS_MAX_PD; i++) m->parse_buffer[i] = NULL; |
|---|
| 351 | for (i = 0; i < MBUS_MAX_PD; i++) m->parse_bufend[i] = NULL; |
|---|
| 352 | mbus_unlock_config_file(m->cfg); |
|---|
| 353 | |
|---|
| 354 | return m; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | void mbus_cmd_handler(struct mbus *m, void (*cmd_handler)(char *src, char *cmd, char *arg, void *dat)) |
|---|
| 358 | { |
|---|
| 359 | m->cmd_handler = cmd_handler; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | static void mbus_flush_msgs(struct mbus_msg *queue) |
|---|
| 363 | { |
|---|
| 364 | struct mbus_msg *curr, *next; |
|---|
| 365 | int i; |
|---|
| 366 | |
|---|
| 367 | curr = queue; |
|---|
| 368 | while(curr) { |
|---|
| 369 | next = curr->next; |
|---|
| 370 | xfree(curr->dest); |
|---|
| 371 | for(i = 0; i < curr->num_cmds; i++) { |
|---|
| 372 | xfree(curr->cmd_list[i]); |
|---|
| 373 | xfree(curr->arg_list[i]); |
|---|
| 374 | } |
|---|
| 375 | curr = next; |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | void mbus_exit(struct mbus *m) |
|---|
| 380 | { |
|---|
| 381 | assert(m != NULL); |
|---|
| 382 | |
|---|
| 383 | while(m->parse_depth--) { |
|---|
| 384 | xfree(m->parse_buffer[m->parse_depth]); |
|---|
| 385 | m->parse_buffer[m->parse_depth] = NULL; |
|---|
| 386 | m->parse_bufend[m->parse_depth] = NULL; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | mbus_flush_msgs(m->cmd_queue); |
|---|
| 390 | mbus_flush_msgs(m->waiting_ack); |
|---|
| 391 | |
|---|
| 392 | if (m->encrkey != NULL) { |
|---|
| 393 | xfree(m->encrkey); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | udp_exit(m->s); |
|---|
| 397 | |
|---|
| 398 | xfree(m->hashkey); |
|---|
| 399 | xfree(m->cfg); |
|---|
| 400 | xfree(m); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | void mbus_addr(struct mbus *m, char *addr) |
|---|
| 404 | { |
|---|
| 405 | assert(m->num_addr < MBUS_MAX_ADDR); |
|---|
| 406 | mbus_parse_init(m, xstrdup(addr)); |
|---|
| 407 | if (mbus_parse_lst(m, &(m->addr[m->num_addr]))) { |
|---|
| 408 | m->num_addr++; |
|---|
| 409 | } |
|---|
| 410 | mbus_parse_done(m); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | void mbus_send(struct mbus *m) |
|---|
| 414 | { |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | struct mbus_msg *curr = m->cmd_queue; |
|---|
| 419 | int i; |
|---|
| 420 | |
|---|
| 421 | if (m->waiting_ack != NULL) { |
|---|
| 422 | return; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | while (curr != NULL) { |
|---|
| 426 | |
|---|
| 427 | tx_header(curr->seqnum, curr->ts.tv_sec, (char)(curr->reliable?'R':'U'), m->addr[0], curr->dest, -1); |
|---|
| 428 | for (i = 0; i < curr->num_cmds; i++) { |
|---|
| 429 | tx_add_command(curr->cmd_list[i], curr->arg_list[i]); |
|---|
| 430 | } |
|---|
| 431 | tx_send(m); |
|---|
| 432 | |
|---|
| 433 | m->cmd_queue = curr->next; |
|---|
| 434 | if (curr->reliable) { |
|---|
| 435 | |
|---|
| 436 | gettimeofday(&(curr->time), NULL); |
|---|
| 437 | m->waiting_ack = curr; |
|---|
| 438 | return; |
|---|
| 439 | } else { |
|---|
| 440 | while (curr->num_cmds > 0) { |
|---|
| 441 | curr->num_cmds--; |
|---|
| 442 | xfree(curr->cmd_list[curr->num_cmds]); |
|---|
| 443 | xfree(curr->arg_list[curr->num_cmds]); |
|---|
| 444 | } |
|---|
| 445 | xfree(curr->dest); |
|---|
| 446 | xfree(curr); |
|---|
| 447 | } |
|---|
| 448 | curr = m->cmd_queue; |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | void mbus_qmsg(struct mbus *m, char *dest, const char *cmnd, const char *args, int reliable) |
|---|
| 453 | { |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | struct mbus_msg *curr = m->cmd_queue; |
|---|
| 457 | struct mbus_msg *prev = NULL; |
|---|
| 458 | int alen = strlen(cmnd) + strlen(args) + 4; |
|---|
| 459 | |
|---|
| 460 | if (reliable && !mbus_addr_valid(m, dest)) { |
|---|
| 461 | debug_msg("Trying to send reliably to an unknown address...\n"); |
|---|
| 462 | #ifdef NDEF |
|---|
| 463 | if (m->err_handler == NULL) { |
|---|
| 464 | abort(); |
|---|
| 465 | } |
|---|
| 466 | m->err_handler(curr->seqnum, MBUS_DESTINATION_UNKNOWN); |
|---|
| 467 | #endif |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | while (curr != NULL) { |
|---|
| 471 | if ((!curr->complete) |
|---|
| 472 | && mbus_addr_match(curr->dest, dest) |
|---|
| 473 | && (curr->num_cmds < MBUS_MAX_QLEN) |
|---|
| 474 | && ((curr->message_size + alen) < (MBUS_BUF_SIZE - 8))) { |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | curr->num_cmds++; |
|---|
| 480 | curr->reliable |= reliable; |
|---|
| 481 | curr->cmd_list[curr->num_cmds-1] = xstrdup(cmnd); |
|---|
| 482 | curr->arg_list[curr->num_cmds-1] = xstrdup(args); |
|---|
| 483 | curr->message_size += alen; |
|---|
| 484 | return; |
|---|
| 485 | } else { |
|---|
| 486 | curr->complete = TRUE; |
|---|
| 487 | } |
|---|
| 488 | prev = curr; |
|---|
| 489 | curr = curr->next; |
|---|
| 490 | } |
|---|
| 491 | curr = (struct mbus_msg *) xmalloc(sizeof(struct mbus_msg)); |
|---|
| 492 | curr->next = NULL; |
|---|
| 493 | curr->dest = xstrdup(dest); |
|---|
| 494 | curr->retransmit_count = 0; |
|---|
| 495 | curr->message_size = alen + 60 + strlen(dest) + strlen(m->addr[0]); |
|---|
| 496 | curr->seqnum = m->seqnum++; |
|---|
| 497 | curr->reliable = reliable; |
|---|
| 498 | curr->complete = FALSE; |
|---|
| 499 | curr->num_cmds = 1; |
|---|
| 500 | curr->cmd_list[0] = xstrdup(cmnd); |
|---|
| 501 | curr->arg_list[0] = xstrdup(args); |
|---|
| 502 | if (prev == NULL) { |
|---|
| 503 | m->cmd_queue = curr; |
|---|
| 504 | } else { |
|---|
| 505 | prev->next = curr; |
|---|
| 506 | } |
|---|
| 507 | gettimeofday(&(curr->time), NULL); |
|---|
| 508 | gettimeofday(&(curr->ts), NULL); |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | void mbus_qmsgf(struct mbus *m, char *dest, int reliable, const char *cmnd, const char *format, ...) |
|---|
| 512 | { |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | char buffer[MBUS_BUF_SIZE]; |
|---|
| 518 | va_list ap; |
|---|
| 519 | |
|---|
| 520 | va_start(ap, format); |
|---|
| 521 | #ifdef WIN32 |
|---|
| 522 | _vsnprintf(buffer, MBUS_BUF_SIZE, format, ap); |
|---|
| 523 | #else |
|---|
| 524 | vsnprintf(buffer, MBUS_BUF_SIZE, format, ap); |
|---|
| 525 | #endif |
|---|
| 526 | va_end(ap); |
|---|
| 527 | mbus_qmsg(m, dest, cmnd, buffer, reliable); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | void mbus_parse_init(struct mbus *m, char *str) |
|---|
| 531 | { |
|---|
| 532 | assert(m->parse_depth < (MBUS_MAX_PD - 1)); |
|---|
| 533 | m->parse_depth++; |
|---|
| 534 | m->parse_buffer[m->parse_depth] = str; |
|---|
| 535 | m->parse_bufend[m->parse_depth] = str + strlen(str); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | void mbus_parse_done(struct mbus *m) |
|---|
| 539 | { |
|---|
| 540 | m->parse_buffer[m->parse_depth] = NULL; |
|---|
| 541 | m->parse_bufend[m->parse_depth] = NULL; |
|---|
| 542 | m->parse_depth--; |
|---|
| 543 | assert(m->parse_depth >= 0); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | #define CHECK_OVERRUN if (m->parse_buffer[m->parse_depth] > m->parse_bufend[m->parse_depth]) {\ |
|---|
| 547 | debug_msg("parse buffer overflow\n");\ |
|---|
| 548 | return FALSE;\ |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | int mbus_parse_lst(struct mbus *m, char **l) |
|---|
| 552 | { |
|---|
| 553 | int instr = FALSE; |
|---|
| 554 | int inlst = FALSE; |
|---|
| 555 | |
|---|
| 556 | *l = m->parse_buffer[m->parse_depth]; |
|---|
| 557 | while (isspace((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 558 | m->parse_buffer[m->parse_depth]++; |
|---|
| 559 | CHECK_OVERRUN; |
|---|
| 560 | } |
|---|
| 561 | if (*m->parse_buffer[m->parse_depth] != '(') { |
|---|
| 562 | return FALSE; |
|---|
| 563 | } |
|---|
| 564 | *(m->parse_buffer[m->parse_depth]) = ' '; |
|---|
| 565 | while (*m->parse_buffer[m->parse_depth] != '\0') { |
|---|
| 566 | if ((*m->parse_buffer[m->parse_depth] == '"') && (*(m->parse_buffer[m->parse_depth]-1) != '\\')) { |
|---|
| 567 | instr = !instr; |
|---|
| 568 | } |
|---|
| 569 | if ((*m->parse_buffer[m->parse_depth] == '(') && (*(m->parse_buffer[m->parse_depth]-1) != '\\') && !instr) { |
|---|
| 570 | inlst = !inlst; |
|---|
| 571 | } |
|---|
| 572 | if ((*m->parse_buffer[m->parse_depth] == ')') && (*(m->parse_buffer[m->parse_depth]-1) != '\\') && !instr) { |
|---|
| 573 | if (inlst) { |
|---|
| 574 | inlst = !inlst; |
|---|
| 575 | } else { |
|---|
| 576 | *m->parse_buffer[m->parse_depth] = '\0'; |
|---|
| 577 | m->parse_buffer[m->parse_depth]++; |
|---|
| 578 | CHECK_OVERRUN; |
|---|
| 579 | return TRUE; |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | m->parse_buffer[m->parse_depth]++; |
|---|
| 583 | CHECK_OVERRUN; |
|---|
| 584 | } |
|---|
| 585 | return FALSE; |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | int mbus_parse_str(struct mbus *m, char **s) |
|---|
| 589 | { |
|---|
| 590 | while (isspace((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 591 | m->parse_buffer[m->parse_depth]++; |
|---|
| 592 | CHECK_OVERRUN; |
|---|
| 593 | } |
|---|
| 594 | if (*m->parse_buffer[m->parse_depth] != '"') { |
|---|
| 595 | return FALSE; |
|---|
| 596 | } |
|---|
| 597 | *s = m->parse_buffer[m->parse_depth]++; |
|---|
| 598 | while (*m->parse_buffer[m->parse_depth] != '\0') { |
|---|
| 599 | if ((*m->parse_buffer[m->parse_depth] == '"') && (*(m->parse_buffer[m->parse_depth]-1) != '\\')) { |
|---|
| 600 | m->parse_buffer[m->parse_depth]++; |
|---|
| 601 | *m->parse_buffer[m->parse_depth] = '\0'; |
|---|
| 602 | m->parse_buffer[m->parse_depth]++; |
|---|
| 603 | return TRUE; |
|---|
| 604 | } |
|---|
| 605 | m->parse_buffer[m->parse_depth]++; |
|---|
| 606 | CHECK_OVERRUN; |
|---|
| 607 | } |
|---|
| 608 | return FALSE; |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | static int mbus_parse_sym(struct mbus *m, char **s) |
|---|
| 612 | { |
|---|
| 613 | while (isspace((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 614 | m->parse_buffer[m->parse_depth]++; |
|---|
| 615 | CHECK_OVERRUN; |
|---|
| 616 | } |
|---|
| 617 | if (!isgraph((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 618 | return FALSE; |
|---|
| 619 | } |
|---|
| 620 | *s = m->parse_buffer[m->parse_depth]++; |
|---|
| 621 | while (!isspace((unsigned char)*m->parse_buffer[m->parse_depth]) && (*m->parse_buffer[m->parse_depth] != '\0')) { |
|---|
| 622 | m->parse_buffer[m->parse_depth]++; |
|---|
| 623 | CHECK_OVERRUN; |
|---|
| 624 | } |
|---|
| 625 | *m->parse_buffer[m->parse_depth] = '\0'; |
|---|
| 626 | m->parse_buffer[m->parse_depth]++; |
|---|
| 627 | CHECK_OVERRUN; |
|---|
| 628 | return TRUE; |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | int mbus_parse_int(struct mbus *m, int *i) |
|---|
| 632 | { |
|---|
| 633 | char *p; |
|---|
| 634 | |
|---|
| 635 | while (isspace((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 636 | m->parse_buffer[m->parse_depth]++; |
|---|
| 637 | CHECK_OVERRUN; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | *i = strtol(m->parse_buffer[m->parse_depth], &p, 10); |
|---|
| 641 | if (((*i == LONG_MAX) || (*i == LONG_MIN)) && (errno == ERANGE)) { |
|---|
| 642 | debug_msg("integer out of range\n"); |
|---|
| 643 | return FALSE; |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | if (p == m->parse_buffer[m->parse_depth]) { |
|---|
| 647 | return FALSE; |
|---|
| 648 | } |
|---|
| 649 | if (!isspace((unsigned char)*p) && (*p != '\0')) { |
|---|
| 650 | return FALSE; |
|---|
| 651 | } |
|---|
| 652 | m->parse_buffer[m->parse_depth] = p; |
|---|
| 653 | CHECK_OVERRUN; |
|---|
| 654 | return TRUE; |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | int mbus_parse_flt(struct mbus *m, double *d) |
|---|
| 658 | { |
|---|
| 659 | char *p; |
|---|
| 660 | while (isspace((unsigned char)*m->parse_buffer[m->parse_depth])) { |
|---|
| 661 | m->parse_buffer[m->parse_depth]++; |
|---|
| 662 | CHECK_OVERRUN; |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | *d = strtod(m->parse_buffer[m->parse_depth], &p); |
|---|
| 666 | if (errno == ERANGE) { |
|---|
| 667 | debug_msg("float out of range\n"); |
|---|
| 668 | return FALSE; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | if (p == m->parse_buffer[m->parse_depth]) { |
|---|
| 672 | return FALSE; |
|---|
| 673 | } |
|---|
| 674 | if (!isspace((unsigned char)*p) && (*p != '\0')) { |
|---|
| 675 | return FALSE; |
|---|
| 676 | } |
|---|
| 677 | m->parse_buffer[m->parse_depth] = p; |
|---|
| 678 | CHECK_OVERRUN; |
|---|
| 679 | return TRUE; |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | char *mbus_decode_str(char *s) |
|---|
| 683 | { |
|---|
| 684 | int l = strlen(s); |
|---|
| 685 | int i, j; |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | assert(s[0] == '\"'); |
|---|
| 689 | assert(s[l-1] == '\"'); |
|---|
| 690 | |
|---|
| 691 | for (i=1,j=0; i < l - 1; i++,j++) { |
|---|
| 692 | if (s[i] == '\\') { |
|---|
| 693 | i++; |
|---|
| 694 | } |
|---|
| 695 | s[j] = s[i]; |
|---|
| 696 | } |
|---|
| 697 | s[j] = '\0'; |
|---|
| 698 | return s; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | char *mbus_encode_str(const char *s) |
|---|
| 702 | { |
|---|
| 703 | int i, j; |
|---|
| 704 | int len = strlen(s); |
|---|
| 705 | char *buf = (char *) xmalloc((len * 2) + 3); |
|---|
| 706 | |
|---|
| 707 | for (i = 0, j = 1; i < len; i++,j++) { |
|---|
| 708 | if (s[i] == ' ') { |
|---|
| 709 | buf[j] = '\\'; |
|---|
| 710 | buf[j+1] = ' '; |
|---|
| 711 | j++; |
|---|
| 712 | } else if (s[i] == '\"') { |
|---|
| 713 | buf[j] = '\\'; |
|---|
| 714 | buf[j+1] = '\"'; |
|---|
| 715 | j++; |
|---|
| 716 | } else { |
|---|
| 717 | buf[j] = s[i]; |
|---|
| 718 | } |
|---|
| 719 | } |
|---|
| 720 | buf[0] = '\"'; |
|---|
| 721 | buf[j] = '\"'; |
|---|
| 722 | buf[j+1] = '\0'; |
|---|
| 723 | return buf; |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | int mbus_recv(struct mbus *m, void *data, struct timeval *timeout) |
|---|
| 727 | { |
|---|
| 728 | char *auth, *ver, *src, *dst, *ack, *r, *cmd, *param, *npos; |
|---|
| 729 | char buffer[MBUS_BUF_SIZE]; |
|---|
| 730 | int buffer_len, seq, i, a, rx, ts, authlen, loop_count; |
|---|
| 731 | char ackbuf[MBUS_ACK_BUF_SIZE]; |
|---|
| 732 | char digest[16]; |
|---|
| 733 | unsigned char initVec[8] = {0,0,0,0,0,0,0,0}; |
|---|
| 734 | |
|---|
| 735 | rx = FALSE; |
|---|
| 736 | loop_count = 0; |
|---|
| 737 | while (loop_count++ < 10) { |
|---|
| 738 | memset(buffer, 0, MBUS_BUF_SIZE); |
|---|
| 739 | assert(m->s != NULL); |
|---|
| 740 | udp_fd_zero(); |
|---|
| 741 | udp_fd_set(m->s); |
|---|
| 742 | if ((udp_select(timeout) > 0) && udp_fd_isset(m->s)) { |
|---|
| 743 | buffer_len = udp_recv(m->s, buffer, MBUS_BUF_SIZE); |
|---|
| 744 | if (buffer_len > 0) { |
|---|
| 745 | rx = TRUE; |
|---|
| 746 | } else { |
|---|
| 747 | return rx; |
|---|
| 748 | } |
|---|
| 749 | } else { |
|---|
| 750 | return FALSE; |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | if (m->encrkey != NULL) { |
|---|
| 754 | |
|---|
| 755 | if ((buffer_len % 8) != 0) { |
|---|
| 756 | debug_msg("Encrypted message not a multiple of 8 bytes in length\n"); |
|---|
| 757 | continue; |
|---|
| 758 | } |
|---|
| 759 | memcpy(tx_cryptbuf, buffer, buffer_len); |
|---|
| 760 | memset(initVec, 0, 8); |
|---|
| 761 | qfDES_CBC_d(m->encrkey, tx_cryptbuf, buffer_len, initVec); |
|---|
| 762 | memcpy(buffer, tx_cryptbuf, buffer_len); |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | if (strncmp(buffer + MBUS_AUTH_LEN + 1, "mbus/1.0", 8) != 0) { |
|---|
| 768 | debug_msg("Message did not correctly decrypt...\n"); |
|---|
| 769 | continue; |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | mbus_parse_init(m, buffer); |
|---|
| 773 | |
|---|
| 774 | npos=strchr(buffer,'\0'); |
|---|
| 775 | if(npos!=NULL) { |
|---|
| 776 | buffer_len=npos-buffer; |
|---|
| 777 | } |
|---|
| 778 | |
|---|
| 779 | if (!mbus_parse_sym(m, &auth)) { |
|---|
| 780 | debug_msg("Failed to parse authentication header\n"); |
|---|
| 781 | mbus_parse_done(m); |
|---|
| 782 | continue; |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | authlen = strlen(auth); |
|---|
| 787 | hmac_md5(buffer + authlen + 1, buffer_len - authlen - 1, m->hashkey, m->hashkeylen, digest); |
|---|
| 788 | base64encode(digest, 12, ackbuf, 16); |
|---|
| 789 | if ((strlen(auth) != 16) || (strncmp(auth, ackbuf, 16) != 0)) { |
|---|
| 790 | debug_msg("Failed to authenticate message...\n"); |
|---|
| 791 | mbus_parse_done(m); |
|---|
| 792 | continue; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | if (!mbus_parse_sym(m, &ver)) { |
|---|
| 797 | mbus_parse_done(m); |
|---|
| 798 | debug_msg("Parser failed version (1): %s\n",ver); |
|---|
| 799 | continue; |
|---|
| 800 | } |
|---|
| 801 | if (strcmp(ver, "mbus/1.0") != 0) { |
|---|
| 802 | mbus_parse_done(m); |
|---|
| 803 | debug_msg("Parser failed version (2): %s\n",ver); |
|---|
| 804 | continue; |
|---|
| 805 | } |
|---|
| 806 | if (!mbus_parse_int(m, &seq)) { |
|---|
| 807 | mbus_parse_done(m); |
|---|
| 808 | debug_msg("Parser failed seq\n"); |
|---|
| 809 | continue; |
|---|
| 810 | } |
|---|
| 811 | if (!mbus_parse_int(m, &ts)) { |
|---|
| 812 | mbus_parse_done(m); |
|---|
| 813 | debug_msg("Parser failed ts\n"); |
|---|
| 814 | continue; |
|---|
| 815 | } |
|---|
| 816 | if (!mbus_parse_sym(m, &r)) { |
|---|
| 817 | mbus_parse_done(m); |
|---|
| 818 | debug_msg("Parser failed reliable\n"); |
|---|
| 819 | continue; |
|---|
| 820 | } |
|---|
| 821 | if (!mbus_parse_lst(m, &src)) { |
|---|
| 822 | mbus_parse_done(m); |
|---|
| 823 | debug_msg("Parser failed src\n"); |
|---|
| 824 | continue; |
|---|
| 825 | } |
|---|
| 826 | if (!mbus_parse_lst(m, &dst)) { |
|---|
| 827 | mbus_parse_done(m); |
|---|
| 828 | debug_msg("Parser failed dst\n"); |
|---|
| 829 | continue; |
|---|
| 830 | } |
|---|
| 831 | if (!mbus_parse_lst(m, &ack)) { |
|---|
| 832 | mbus_parse_done(m); |
|---|
| 833 | debug_msg("Parser failed ack\n"); |
|---|
| 834 | continue; |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | store_other_addr(m, src); |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | for (i = 0; i < m->num_addr; i++) { |
|---|
| 841 | if (mbus_addr_match(m->addr[i], dst)) { |
|---|
| 842 | |
|---|
| 843 | mbus_parse_init(m, ack); |
|---|
| 844 | while (mbus_parse_int(m, &a)) { |
|---|
| 845 | if (mbus_waiting_ack(m) && (m->waiting_ack->seqnum == a)) { |
|---|
| 846 | while (m->waiting_ack->num_cmds > 0) { |
|---|
| 847 | m->waiting_ack->num_cmds--; |
|---|
| 848 | xfree(m->waiting_ack->cmd_list[m->waiting_ack->num_cmds]); |
|---|
| 849 | xfree(m->waiting_ack->arg_list[m->waiting_ack->num_cmds]); |
|---|
| 850 | } |
|---|
| 851 | xfree(m->waiting_ack->dest); |
|---|
| 852 | xfree(m->waiting_ack); |
|---|
| 853 | m->waiting_ack = NULL; |
|---|
| 854 | } |
|---|
| 855 | } |
|---|
| 856 | mbus_parse_done(m); |
|---|
| 857 | |
|---|
| 858 | if (strcmp(r, "R") == 0) { |
|---|
| 859 | char *newsrc = (char *) xmalloc(strlen(src) + 3); |
|---|
| 860 | struct timeval t; |
|---|
| 861 | |
|---|
| 862 | sprintf(newsrc, "(%s)", src); |
|---|
| 863 | gettimeofday(&t, NULL); |
|---|
| 864 | tx_header(++m->seqnum, (int) t.tv_sec, 'U', m->addr[0], newsrc, seq); |
|---|
| 865 | tx_send(m); |
|---|
| 866 | xfree(newsrc); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | while (mbus_parse_sym(m, &cmd)) { |
|---|
| 870 | if (mbus_parse_lst(m, ¶m)) { |
|---|
| 871 | char *newsrc = (char *) xmalloc(strlen(src) + 3); |
|---|
| 872 | sprintf(newsrc, "(%s)", src); |
|---|
| 873 | m->cmd_handler(newsrc, cmd, param, data); |
|---|
| 874 | xfree(newsrc); |
|---|
| 875 | } else { |
|---|
| 876 | debug_msg("Unable to parse mbus command:\n"); |
|---|
| 877 | debug_msg("cmd = %s\n", cmd); |
|---|
| 878 | debug_msg("arg = %s\n", param); |
|---|
| 879 | break; |
|---|
| 880 | } |
|---|
| 881 | } |
|---|
| 882 | } |
|---|
| 883 | } |
|---|
| 884 | mbus_parse_done(m); |
|---|
| 885 | } |
|---|
| 886 | return rx; |
|---|
| 887 | } |
|---|