598 * fragment lengths SDNV |
598 * fragment lengths SDNV |
599 * |
599 * |
600 * @return The number of bytes or -1 on error |
600 * @return The number of bytes or -1 on error |
601 *******************************************************************************/ |
601 *******************************************************************************/ |
602 int |
602 int |
603 bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq, char* buf, size_t buf_len) |
603 bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq, |
|
604 char* buf, |
|
605 size_t buf_len, |
|
606 int verbose) |
604 { |
607 { |
605 int i=0, j=0, k=0; |
608 int i=0, j=0, k=0; |
606 int encoding_len; |
609 int q_encoding_len, f_encoding_len, encoding_len; |
607 char encoding[PATH_MAX]; |
610 char encoding[PATH_MAX]; |
608 |
611 |
609 memset(buf, 0, buf_len); |
612 memset(buf, 0, buf_len); |
610 |
613 |
611 // BPQ-kind 1-byte |
614 // BPQ-kind 1-byte |
613 |
616 |
614 // matching rule type 1-byte |
617 // matching rule type 1-byte |
615 if (i < buf_len) buf[i++] = (char) bpq->matching_rule; |
618 if (i < buf_len) buf[i++] = (char) bpq->matching_rule; |
616 |
619 |
617 // BPQ-value-length SDNV |
620 // BPQ-value-length SDNV |
618 if ( (encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 ) |
621 if ( (q_encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 ) |
619 return -1; |
622 return -1; |
620 for (j=0; i<buf_len && j<encoding_len; ++j) |
623 for (j=0; i<buf_len && j<q_encoding_len; ++j) |
621 buf[i++] = encoding[j]; |
624 buf[i++] = encoding[j]; |
622 |
625 |
623 // BPQ-value n-bytes |
626 // BPQ-value n-bytes |
624 for (j=0; i<buf_len && j<bpq->query.query_len; ++j) |
627 for (j=0; i<buf_len && j<bpq->query.query_len; ++j) |
625 buf[i++] = bpq->query.query_val[j]; |
628 buf[i++] = bpq->query.query_val[j]; |
626 |
629 |
627 // number of fragments SDNV |
630 // number of fragments SDNV |
628 if ( (encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 ) |
631 if ( (f_encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 ) |
629 return -1; |
632 return -1; |
630 for (j=0; i<buf_len && j<encoding_len; ++j) |
633 for (j=0; i<buf_len && j<f_encoding_len; ++j) |
631 buf[i++] = encoding[j]; |
634 buf[i++] = encoding[j]; |
632 |
|
633 |
635 |
634 for (k=0; k<bpq->fragments.num_frag_returned; ++k) { |
636 for (k=0; k<bpq->fragments.num_frag_returned; ++k) { |
635 |
637 |
636 // fragment offsets SDNV |
638 // fragment offsets SDNV |
637 if ( (encoding_len = sdnv_encode (bpq->fragments.frag_offsets[k], encoding, PATH_MAX)) == -1 ) |
639 if ( (encoding_len = sdnv_encode (bpq->fragments.frag_offsets[k], encoding, PATH_MAX)) == -1 ) |
644 return -1; |
646 return -1; |
645 for (j=0; i<buf_len && j<encoding_len; ++j) |
647 for (j=0; i<buf_len && j<encoding_len; ++j) |
646 buf[i++] = encoding[j]; |
648 buf[i++] = encoding[j]; |
647 } |
649 } |
648 |
650 |
|
651 if (verbose) { |
|
652 fprintf (stdout, "\nbpq_to_char_array (buf_len:%d, i:%d):\n",buf_len,i); |
|
653 fprintf (stdout, " kind: %d\n", (int) buf[0]); |
|
654 fprintf (stdout, " matching rule: %d\n", (int) buf[1]); |
|
655 fprintf (stdout, " query len: %d\n", bpq->query.query_len); |
|
656 fprintf (stdout, " q_encoding_len: %d\n", q_encoding_len); |
|
657 fprintf (stdout, " query val: %s\n", bpq->query.query_val); |
|
658 fprintf (stdout, " fragment len: %d\n", bpq->fragments.num_frag_returned); |
|
659 fprintf (stdout, " f_encoding_len: %d\n\n", f_encoding_len); |
|
660 } |
649 |
661 |
650 return i; |
662 return i; |
651 } |
663 } |
652 /******************************************************************************* |
664 /******************************************************************************* |
653 * char array to bpq: |
665 * char array to bpq: |
661 * fragment lengths SDNV |
673 * fragment lengths SDNV |
662 * |
674 * |
663 * @return DTN_SUCCESS or -1 on error |
675 * @return DTN_SUCCESS or -1 on error |
664 *******************************************************************************/ |
676 *******************************************************************************/ |
665 int |
677 int |
666 char_array_to_bpq(const char* buf, size_t buf_len, dtn_bpq_extension_block_data_t * bpq) |
678 char_array_to_bpq(const char* buf, |
|
679 size_t buf_len, |
|
680 dtn_bpq_extension_block_data_t * bpq, |
|
681 int verbose) |
667 { |
682 { |
668 int i=0, j=0; |
683 int i=0, j=0; |
669 int decoding_len=0; |
684 int q_decoding_len, f_decoding_len, decoding_len; |
670 |
685 |
671 // BPQ-kind 1-byte |
686 // BPQ-kind 1-byte |
672 if (i<buf_len) bpq->kind = (u_int) buf[i++]; |
687 if (i<buf_len) bpq->kind = (u_int) buf[i++]; |
673 |
688 |
674 // matching rule type 1-byte |
689 // matching rule type 1-byte |
675 if (i<buf_len) bpq->matching_rule = (u_int) buf[i++]; |
690 if (i<buf_len) bpq->matching_rule = (u_int) buf[i++]; |
676 |
691 |
677 // BPQ-value-length SDNV |
692 // BPQ-value-length SDNV |
678 if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->query.query_len))) == -1 ) |
693 if ( (q_decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->query.query_len))) == -1 ) |
679 return -1; |
694 return -1; |
680 i += decoding_len; |
695 i += q_decoding_len; |
681 |
696 |
682 // BPQ-value n-bytes |
697 // BPQ-value n-bytes |
683 if (i<buf_len) bpq->query.query_val = &(buf[i]); |
698 if (i<buf_len) bpq->query.query_val = &(buf[i]); |
684 i += bpq->query.query_len; |
699 i += bpq->query.query_len; |
685 |
700 |
686 // number of fragments SDNV |
701 // number of fragments SDNV |
687 if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 ) |
702 if ( (f_decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 ) |
688 return -1; |
703 return -1; |
689 i += decoding_len; |
704 i += f_decoding_len; |
690 |
705 |
691 for (j=0; i<buf_len && j<bpq->fragments.num_frag_returned; ++j) { |
706 for (j=0; i<buf_len && j<bpq->fragments.num_frag_returned; ++j) { |
692 |
707 |
693 // fragment offsets SDNV |
708 // fragment offsets SDNV |
694 if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.frag_offsets[j]))) == -1 ) |
709 if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.frag_offsets[j]))) == -1 ) |
701 i += decoding_len; |
716 i += decoding_len; |
702 } |
717 } |
703 |
718 |
704 if (i != buf_len) |
719 if (i != buf_len) |
705 return -1; |
720 return -1; |
|
721 |
|
722 if (verbose) { |
|
723 fprintf (stdout, "\nchar_array_to_bpq (buf_len:%d, i:%d):\n",buf_len, i); |
|
724 fprintf (stdout, " kind: %d\n", (int) buf[0]); |
|
725 fprintf (stdout, " matching rule: %d\n", (int) buf[1]); |
|
726 fprintf (stdout, " query len: %d\n", bpq->query.query_len); |
|
727 fprintf (stdout, " q_decoding_len: %d\n", q_decoding_len); |
|
728 fprintf (stdout, " query val: %s\n", bpq->query.query_val); |
|
729 fprintf (stdout, " fragment len: %d\n", bpq->fragments.num_frag_returned); |
|
730 fprintf (stdout, " f_decoding_len: %d\n\n", f_decoding_len); |
|
731 } |
706 |
732 |
707 return DTN_SUCCESS; |
733 return DTN_SUCCESS; |
708 } |
734 } |
709 |
735 |
710 /******************************************************************************* |
736 /******************************************************************************* |
747 bpq_block_data.query.query_val = query; |
773 bpq_block_data.query.query_val = query; |
748 bpq_block_data.fragments.num_frag_returned = 0; |
774 bpq_block_data.fragments.num_frag_returned = 0; |
749 bpq_block_data.fragments.frag_offsets = NULL; |
775 bpq_block_data.fragments.frag_offsets = NULL; |
750 bpq_block_data.fragments.frag_lenghts = NULL; |
776 bpq_block_data.fragments.frag_lenghts = NULL; |
751 |
777 |
752 buf_len = bpq_to_char_array(&bpq_block_data, buf, PATH_MAX); |
778 buf_len = bpq_to_char_array(&bpq_block_data, buf, PATH_MAX, verbose); |
753 |
779 |
754 // set the bpq block |
780 // set the bpq block |
755 bpq_block.type = DTN_BPQ_BLOCK_TYPE; |
781 bpq_block.type = DTN_BPQ_BLOCK_TYPE; |
756 bpq_block.flags = DTN_BPQ_BLOCK_FLAGS; |
782 bpq_block.flags = DTN_BPQ_BLOCK_FLAGS; |
757 bpq_block.data.data_len = buf_len; |
783 bpq_block.data.data_len = buf_len; |
800 recv_bpq(dtn_handle_t handle, |
826 recv_bpq(dtn_handle_t handle, |
801 dtn_timeval_t timeout, |
827 dtn_timeval_t timeout, |
802 const char * filename, |
828 const char * filename, |
803 int verbose) |
829 int verbose) |
804 { |
830 { |
805 int ret = 0; |
831 int ret = 0, num_blocks, i; |
806 dtn_bundle_spec_t bundle_spec; |
832 dtn_bundle_spec_t bundle_spec; |
807 dtn_bundle_payload_t payload; |
833 dtn_extension_block_t* bpq_blocks; |
|
834 dtn_bpq_extension_block_data_t bpq_block_data; |
|
835 dtn_bundle_payload_t payload; |
808 |
836 |
809 memset(&bundle_spec, 0, sizeof(bundle_spec)); |
837 memset(&bundle_spec, 0, sizeof(bundle_spec)); |
|
838 memset(&bpq_block_data, 0, sizeof(dtn_bpq_extension_block_data_t)); |
810 memset(&payload, 0, sizeof(payload)); |
839 memset(&payload, 0, sizeof(payload)); |
811 |
840 |
812 // recv the bpq bundle |
841 // recv the bpq bundle |
813 if (verbose) fprintf(stdout, "blocking waiting for dtn_recv\n"); |
842 if (verbose) fprintf(stdout, "blocking waiting for dtn_recv\n"); |
814 ret = dtn_recv(handle, &bundle_spec, DTN_PAYLOAD_FILE, &payload, timeout); |
843 ret = dtn_recv(handle, &bundle_spec, DTN_PAYLOAD_FILE, &payload, timeout); |
820 fprintf(stdout, "bundle received successfully: id %s,%llu.%llu\n", |
849 fprintf(stdout, "bundle received successfully: id %s,%llu.%llu\n", |
821 bundle_spec.source.uri, |
850 bundle_spec.source.uri, |
822 bundle_spec.creation_ts.secs, |
851 bundle_spec.creation_ts.secs, |
823 bundle_spec.creation_ts.seqno); |
852 bundle_spec.creation_ts.seqno); |
824 } |
853 } |
|
854 |
|
855 // extract the bpq |
|
856 num_blocks = bundle_spec.blocks.blocks_len; |
|
857 bpq_blocks = bundle_spec.blocks.blocks_val; |
|
858 |
|
859 for (i = 0; i < num_blocks; ++i) { |
|
860 if (bpq_blocks[i].type == DTN_BPQ_BLOCK_TYPE) { |
|
861 |
|
862 if (verbose) fprintf(stdout, "bundle contains a " |
|
863 "BPQ extension block\n"); |
|
864 |
|
865 if (verbose) fprintf(stdout, "BPQ query_len: %d)\n", (int) bpq_blocks[i].data.data_len); |
|
866 if (verbose) fprintf(stdout, "BPQ query_val: %s)\n", (char*)bpq_blocks[i].data.data_val); |
|
867 |
|
868 if ( bpq_blocks[i].data.data_len <= 0 || bpq_blocks[i].data.data_val == NULL) { |
|
869 fprintf(stderr, "error decoding query bundle: %d\n", ret); |
|
870 return -1; |
|
871 } |
|
872 |
|
873 ret = char_array_to_bpq(bpq_blocks[i].data.data_val, |
|
874 bpq_blocks[i].data.data_len, |
|
875 &bpq_block_data, |
|
876 verbose); |
|
877 if (ret != DTN_SUCCESS) { |
|
878 fprintf(stderr, "error decoding query bundle: %d\n", ret); |
|
879 return ret; |
|
880 } |
|
881 |
|
882 // if (verbose) fprintf(stdout, "BPQ query(%s)\n", bpq_block_data.query.query_val); |
|
883 |
|
884 break; |
|
885 } |
|
886 } |
|
887 |
825 |
888 |
826 // handle the payload file |
889 // handle the payload file |
827 ret = handle_file_transfer(bundle_spec, payload, filename, verbose); |
890 ret = handle_file_transfer(bundle_spec, payload, filename, verbose); |
828 if (ret != DTN_SUCCESS) { |
891 if (ret != DTN_SUCCESS) { |
829 fprintf(stderr, "error handling file transfer: %d\n", ret); |
892 fprintf(stderr, "error handling file transfer: %d\n", ret); |