00001 /** 00002 * @file ccn_interest.c 00003 * Accessors and mutators for parsed Interest messages 00004 */ 00005 00006 /* 00007 * Part of the CCNx C Library. 00008 * 00009 * Copyright (C) 2010 Palo Alto Research Center, Inc. 00010 * 00011 * This library is free software; you can redistribute it and/or modify it 00012 * under the terms of the GNU Lesser General Public License version 2.1 00013 * as published by the Free Software Foundation. 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. You should have received 00018 * a copy of the GNU Lesser General Public License along with this library; 00019 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, 00020 * Fifth Floor, Boston, MA 02110-1301 USA. 00021 */ 00022 00023 #include <ccn/ccn.h> 00024 #include <ccn/charbuf.h> 00025 #include <ccn/coding.h> 00026 00027 /** 00028 * @returns the lifetime of the interest in units of 2**(-12) seconds 00029 * (the same units as timestamps). 00030 */ 00031 intmax_t 00032 ccn_interest_lifetime(const unsigned char *msg, 00033 const struct ccn_parsed_interest *pi) 00034 { 00035 struct ccn_buf_decoder decoder; 00036 struct ccn_buf_decoder *d = NULL; 00037 unsigned start = pi->offset[CCN_PI_B_InterestLifetime]; 00038 size_t size = pi->offset[CCN_PI_E_InterestLifetime] - start; 00039 uintmax_t val; 00040 if (size == 0) 00041 return(CCN_INTEREST_LIFETIME_SEC << 12); 00042 d = ccn_buf_decoder_start(&decoder, msg + start, size); 00043 val = ccn_parse_optional_tagged_binary_number(d, CCN_DTAG_InterestLifetime, 00044 1, 7, CCN_INTEREST_LIFETIME_SEC << 12); 00045 if (d->decoder.state < 0) 00046 return (d->decoder.state); 00047 return(val); 00048 } 00049 00050 /** 00051 * @returns the lifetime of the interest in units of seconds; 00052 * any fractional part is truncated. 00053 * Not useful for short-lived interests. 00054 */ 00055 int 00056 ccn_interest_lifetime_seconds(const unsigned char *msg, 00057 const struct ccn_parsed_interest *pi) 00058 { 00059 intmax_t val = ccn_interest_lifetime(msg, pi); 00060 if (val < 0) 00061 return(val); 00062 return(val >> 12); 00063 }