219 |
219 |
220 log_block_info(block); |
220 log_block_info(block); |
221 TRY (extract_kind(buf, &buf_index, buf_length)); |
221 TRY (extract_kind(buf, &buf_index, buf_length)); |
222 TRY (extract_matching_rule(buf, &buf_index, buf_length)); |
222 TRY (extract_matching_rule(buf, &buf_index, buf_length)); |
223 |
223 |
224 if (created_locally) { |
224 TRY (extract_creation_ts(buf, &buf_index, buf_length, created_locally, bundle)); |
225 creation_ts_.seconds_ = bundle->creation_ts().seconds_; |
225 TRY (extract_source(buf, &buf_index, buf_length, created_locally, bundle)); |
226 creation_ts_.seqno_ = bundle->creation_ts().seqno_; |
|
227 source_.assign(bundle->source()); |
|
228 } else { |
|
229 TRY (extract_creation_ts(buf, &buf_index, buf_length)); |
|
230 TRY (extract_source(buf, &buf_index, buf_length)); |
|
231 } |
|
232 |
226 |
233 TRY (extract_query(buf, &buf_index, buf_length)); |
227 TRY (extract_query(buf, &buf_index, buf_length)); |
234 TRY (extract_fragments(buf, &buf_index, buf_length)); |
228 TRY (extract_fragments(buf, &buf_index, buf_length)); |
235 |
229 |
236 return BP_SUCCESS; |
230 return BP_SUCCESS; |
274 log_err("BPQBlock: buf_len() < full_len"); |
268 log_err("BPQBlock: buf_len() < full_len"); |
275 log_err("BPQBlock: buf_len() = %zu", |
269 log_err("BPQBlock: buf_len() = %zu", |
276 block->writable_contents()->buf_len()); |
270 block->writable_contents()->buf_len()); |
277 } |
271 } |
278 |
272 |
279 if ( *(block->data()) != KIND_QUERY || |
273 if ( *(block->data()) != KIND_QUERY && |
280 *(block->data()) != KIND_RESPONSE || |
274 *(block->data()) != KIND_RESPONSE && |
281 *(block->data()) != KIND_RESPONSE_DO_NOT_CACHE_FRAG ) { |
275 *(block->data()) != KIND_RESPONSE_DO_NOT_CACHE_FRAG ) { |
282 log_err("BPQBlock: block->data() = %c (should be 0|1|2)", |
276 log_err("BPQBlock: block->data() = %d (should be 0|1|2)", |
283 *(block->data())); |
277 *(block->data())); |
284 } |
278 } |
285 } |
279 } |
286 |
280 |
287 //---------------------------------------------------------------------- |
281 //---------------------------------------------------------------------- |
288 int |
282 int |
289 BPQBlock::extract_kind (const u_char* buf, u_int* buf_index, u_int buf_length) |
283 BPQBlock::extract_kind (const u_char* buf, u_int* buf_index, u_int buf_length) |
290 { |
284 { |
291 if ( *buf_index < buf_length ) { |
285 if ( *buf_index < buf_length ) { |
292 kind_ = (kind_t) buf[*buf_index++]; |
286 kind_ = (kind_t) buf[(*buf_index)++]; |
293 log_debug("BPQBlock::extract_kind: kind = %d", kind_); |
287 log_debug("BPQBlock::extract_kind: kind = %d", kind_); |
294 return BP_SUCCESS; |
288 return BP_SUCCESS; |
295 |
289 |
296 } else { |
290 } else { |
297 log_err("Error decoding kind"); |
291 log_err("Error decoding kind"); |
302 //---------------------------------------------------------------------- |
296 //---------------------------------------------------------------------- |
303 int |
297 int |
304 BPQBlock::extract_matching_rule (const u_char* buf, u_int* buf_index, u_int buf_length) |
298 BPQBlock::extract_matching_rule (const u_char* buf, u_int* buf_index, u_int buf_length) |
305 { |
299 { |
306 if ( *buf_index < buf_length ) { |
300 if ( *buf_index < buf_length ) { |
307 matching_rule_ = (kind_t) buf[*buf_index++]; |
301 matching_rule_ = (kind_t) buf[(*buf_index)++]; |
308 log_debug("BPQBlock::extract_matching_rule: matching rule = %d", matching_rule_); |
302 log_debug("BPQBlock::extract_matching_rule: matching rule = %d", matching_rule_); |
309 return BP_SUCCESS; |
303 return BP_SUCCESS; |
310 |
304 |
311 } else { |
305 } else { |
312 log_err("Error decoding matching rule"); |
306 log_err("Error decoding matching rule"); |
314 } |
308 } |
315 } |
309 } |
316 |
310 |
317 //---------------------------------------------------------------------- |
311 //---------------------------------------------------------------------- |
318 int |
312 int |
319 BPQBlock::extract_creation_ts (const u_char* buf, u_int* buf_index, u_int buf_length) |
313 BPQBlock::extract_creation_ts ( const u_char* buf, |
|
314 u_int* buf_index, |
|
315 u_int buf_length, |
|
316 bool local, |
|
317 const Bundle* bundle) |
320 { |
318 { |
321 int decoding_len = 0; |
319 int decoding_len = 0; |
|
320 |
|
321 if (local) { |
|
322 u_int temp; |
|
323 |
|
324 creation_ts_.seconds_ = bundle->creation_ts().seconds_; |
|
325 creation_ts_.seqno_ = bundle->creation_ts().seqno_; |
|
326 |
|
327 log_debug("BPQBlock::extract_creation_ts: bundle was locally created"); |
|
328 log_debug("\t timestamp seconds = %llu", creation_ts_.seconds_); |
|
329 log_debug("\t timestamp sequence number = %llu", creation_ts_.seqno_); |
|
330 |
|
331 // increment buf_index accordingly |
|
332 if (*buf_index < buf_length && |
|
333 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
|
334 buf_length - (*buf_index), |
|
335 &temp)) >= 0) { |
|
336 (*buf_index) += decoding_len; |
|
337 } else { |
|
338 log_err("Error decoding timestamp seconds"); |
|
339 return BP_FAIL; |
|
340 } |
|
341 |
|
342 |
|
343 // increment buf_index accordingly |
|
344 if (*buf_index < buf_length && |
|
345 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
|
346 buf_length - (*buf_index), |
|
347 &temp)) >= 0) { |
|
348 (*buf_index) += decoding_len; |
|
349 } else { |
|
350 log_err("Error decoding timestamp sequence number"); |
|
351 return BP_FAIL; |
|
352 } |
|
353 |
|
354 return BP_SUCCESS; |
|
355 } |
|
356 |
322 if (*buf_index < buf_length && |
357 if (*buf_index < buf_length && |
323 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
358 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
324 buf_length - *buf_index, |
359 buf_length - (*buf_index), |
325 &(creation_ts_.seconds_)) ) >= 0 ) { |
360 &(creation_ts_.seconds_)) ) >= 0 ) { |
326 *buf_index += decoding_len; |
361 *buf_index += decoding_len; |
327 log_debug("BPQBlock::extract_creation_ts: timestamp seconds = %llu", |
362 log_debug("BPQBlock::extract_creation_ts: timestamp seconds = %llu", |
328 creation_ts_.seconds_); |
363 creation_ts_.seconds_); |
329 } else { |
364 } else { |
332 } |
367 } |
333 |
368 |
334 |
369 |
335 if (*buf_index < buf_length && |
370 if (*buf_index < buf_length && |
336 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
371 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
337 buf_length - *buf_index, |
372 buf_length - (*buf_index), |
338 &(creation_ts_.seqno_)) ) >= 0 ) { |
373 &(creation_ts_.seqno_)) ) >= 0 ) { |
339 *buf_index += decoding_len; |
374 *buf_index += decoding_len; |
340 log_debug("BPQBlock::extract_creation_ts: timestamp sequence number = %llu", |
375 log_debug("BPQBlock::extract_creation_ts: timestamp sequence number = %llu", |
341 creation_ts_.seqno_); |
376 creation_ts_.seqno_); |
342 } else { |
377 } else { |
347 return BP_SUCCESS; |
382 return BP_SUCCESS; |
348 } |
383 } |
349 |
384 |
350 //---------------------------------------------------------------------- |
385 //---------------------------------------------------------------------- |
351 int |
386 int |
352 BPQBlock::extract_source (const u_char* buf, u_int* buf_index, u_int buf_length) |
387 BPQBlock::extract_source ( const u_char* buf, |
|
388 u_int* buf_index, |
|
389 u_int buf_length, |
|
390 bool local, |
|
391 const Bundle* bundle) |
353 { |
392 { |
354 int decoding_len = 0; |
393 int decoding_len = 0; |
355 u_int src_eid_len = 0; |
394 u_int src_eid_len = 0; |
356 |
395 |
|
396 if (local) { |
|
397 source_.assign(bundle->source()); |
|
398 |
|
399 log_debug("BPQBlock::extract_source: bundle was locally created"); |
|
400 log_debug("\t Source EID length = %u", source_.length()); |
|
401 log_debug("\t Source EID = %s", source_.c_str()); |
|
402 |
|
403 // increment buf_index accordingly |
|
404 if (*buf_index < buf_length && |
|
405 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
|
406 buf_length - (*buf_index), |
|
407 &src_eid_len)) >= 0) { |
|
408 (*buf_index) += decoding_len; |
|
409 (*buf_index) += src_eid_len; |
|
410 } else { |
|
411 log_err("Error decoding Source EID length"); |
|
412 return BP_FAIL; |
|
413 } |
|
414 |
|
415 return BP_SUCCESS; |
|
416 } |
|
417 |
357 if (*buf_index < buf_length && |
418 if (*buf_index < buf_length && |
358 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
419 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
359 buf_length - *buf_index, |
420 buf_length - (*buf_index), |
360 &src_eid_len)) >= 0 ) { |
421 &src_eid_len)) >= 0 ) { |
361 *buf_index += decoding_len; |
422 (*buf_index) += decoding_len; |
362 log_debug("BPQBlock::extract_source: Source EID length = %u", src_eid_len); |
423 log_debug("BPQBlock::extract_source: Source EID length = %u", src_eid_len); |
363 } else { |
424 } else { |
364 log_err("Error decoding Source EID length"); |
425 log_err("Error decoding Source EID length"); |
365 return BP_FAIL; |
426 return BP_FAIL; |
366 } |
427 } |
367 |
428 |
368 if ((*buf_index + src_eid_len) < buf_length && |
429 if (((*buf_index) + src_eid_len) < buf_length && |
369 source_.assign((const char*) &(buf[*buf_index]), src_eid_len) ) { |
430 source_.assign((const char*) &(buf[*buf_index]), src_eid_len) ) { |
370 |
431 |
371 *buf_index += src_eid_len; |
432 (*buf_index) += src_eid_len; |
372 log_debug("BPQBlock::extract_source: Source EID = %s", source_.c_str()); |
433 log_debug("BPQBlock::extract_source: Source EID = %s", source_.c_str()); |
373 |
434 |
374 } else { |
435 } else { |
375 log_err("Error extracting Source EID"); |
436 log_err("Error extracting Source EID"); |
376 return BP_FAIL; |
437 return BP_FAIL; |
385 { |
446 { |
386 int decoding_len = 0; |
447 int decoding_len = 0; |
387 |
448 |
388 if (*buf_index < buf_length && |
449 if (*buf_index < buf_length && |
389 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
450 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
390 buf_length - *buf_index, |
451 buf_length - (*buf_index), |
391 &query_len_)) >= 0 ) { |
452 &query_len_)) >= 0 ) { |
392 *buf_index += decoding_len; |
453 (*buf_index) += decoding_len; |
393 log_debug("BPQBlock::extract_query: query length = %u", query_len_); |
454 log_debug("BPQBlock::extract_query: query length = %u", query_len_); |
394 } else { |
455 } else { |
395 log_err("Error decoding BPQ query length"); |
456 log_err("Error decoding BPQ query length"); |
396 return BP_FAIL; |
457 return BP_FAIL; |
397 } |
458 } |
398 |
459 |
399 if ((*buf_index + query_len_) < buf_length) { |
460 if (query_len_ <= 0) { |
|
461 log_warn("BPQBlock::extract_query: No query found in block"); |
|
462 } |
|
463 |
|
464 if (((*buf_index) + query_len_) < buf_length) { |
|
465 |
400 query_val_ = (u_char*) malloc ( sizeof(u_char) * query_len_ ); |
466 query_val_ = (u_char*) malloc ( sizeof(u_char) * query_len_ ); |
401 |
467 |
402 memcpy(query_val_, &(buf[*buf_index]), query_len_); |
468 memcpy(query_val_, &(buf[*buf_index]), query_len_); |
403 |
469 |
404 *buf_index += query_len_; |
470 (*buf_index) += query_len_; |
405 log_debug("BPQBlock::extract_query: query value = %s", query_val_); |
471 log_debug("BPQBlock::extract_query: query value = %s", query_val_); |
406 |
472 |
407 } else { |
473 } else { |
408 log_err("Error extracting query value"); |
474 log_err("Error extracting query value"); |
409 return BP_FAIL; |
475 return BP_FAIL; |
422 u_int frag_off = 0; |
488 u_int frag_off = 0; |
423 u_int frag_len = 0; |
489 u_int frag_len = 0; |
424 |
490 |
425 if (*buf_index < buf_length && |
491 if (*buf_index < buf_length && |
426 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
492 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
427 buf_length - *buf_index, |
493 buf_length - (*buf_index), |
428 &frag_count)) >= 0 ) { |
494 &frag_count)) >= 0 ) { |
429 *buf_index += decoding_len; |
495 (*buf_index) += decoding_len; |
430 log_debug("BPQBlock::extract_fragments: number of fragments = %u", frag_count); |
496 log_debug("BPQBlock::extract_fragments: number of fragments = %u", frag_count); |
431 } else { |
497 } else { |
432 log_err("Error decoding number of fragments"); |
498 log_err("Error decoding number of fragments"); |
433 return BP_FAIL; |
499 return BP_FAIL; |
434 } |
500 } |
435 |
501 |
436 for (i=0; i < frag_count; i++) { |
502 for (i=0; i < frag_count; i++) { |
437 |
503 |
438 if (*buf_index < buf_length && |
504 if (*buf_index < buf_length && |
439 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
505 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
440 buf_length - *buf_index, |
506 buf_length - (*buf_index), |
441 &frag_off)) >= 0 ) { |
507 &frag_off)) >= 0 ) { |
442 *buf_index += decoding_len; |
508 (*buf_index) += decoding_len; |
443 log_debug("BPQBlock::extract_fragments: fragment [%u] offset = %u", i, frag_off); |
509 log_debug("BPQBlock::extract_fragments: fragment [%u] offset = %u", i, frag_off); |
444 } else { |
510 } else { |
445 log_err("Error decoding fragment [%u] offset", i); |
511 log_err("Error decoding fragment [%u] offset", i); |
446 return BP_FAIL; |
512 return BP_FAIL; |
447 } |
513 } |
448 |
514 |
449 if (*buf_index < buf_length && |
515 if (*buf_index < buf_length && |
450 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
516 (decoding_len = SDNV::decode( &(buf[*buf_index]), |
451 buf_length - *buf_index, |
517 buf_length - (*buf_index), |
452 &frag_len)) >= 0 ) { |
518 &frag_len)) >= 0 ) { |
453 *buf_index += decoding_len; |
519 (*buf_index) += decoding_len; |
454 log_debug("BPQBlock::extract_fragments: fragment [%u] length = %u", i, frag_len); |
520 log_debug("BPQBlock::extract_fragments: fragment [%u] length = %u", i, frag_len); |
455 } else { |
521 } else { |
456 log_err("Error decoding fragment [%u] length", i); |
522 log_err("Error decoding fragment [%u] length", i); |
457 return BP_FAIL; |
523 return BP_FAIL; |
458 } |
524 } |