Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 29c369053a983cc5d8c880dc6e0a2237 > files > 1724

libdtn-devel-2.6.0-2mdv2009.1.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>DTN Reference Implementation: dtncpd.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.8 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
<h1>dtncpd.c</h1><a href="dtncpd_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> *    Copyright 2004-2006 Intel Corporation</span>
<a name="l00003"></a>00003 <span class="comment"> * </span>
<a name="l00004"></a>00004 <span class="comment"> *    Licensed under the Apache License, Version 2.0 (the "License");</span>
<a name="l00005"></a>00005 <span class="comment"> *    you may not use this file except in compliance with the License.</span>
<a name="l00006"></a>00006 <span class="comment"> *    You may obtain a copy of the License at</span>
<a name="l00007"></a>00007 <span class="comment"> * </span>
<a name="l00008"></a>00008 <span class="comment"> *        http://www.apache.org/licenses/LICENSE-2.0</span>
<a name="l00009"></a>00009 <span class="comment"> * </span>
<a name="l00010"></a>00010 <span class="comment"> *    Unless required by applicable law or agreed to in writing, software</span>
<a name="l00011"></a>00011 <span class="comment"> *    distributed under the License is distributed on an "AS IS" BASIS,</span>
<a name="l00012"></a>00012 <span class="comment"> *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<a name="l00013"></a>00013 <span class="comment"> *    See the License for the specific language governing permissions and</span>
<a name="l00014"></a>00014 <span class="comment"> *    limitations under the License.</span>
<a name="l00015"></a>00015 <span class="comment"> */</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
<a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#  include &lt;<a class="code" href="dtn-config_8h.html">dtn-config.h</a>&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#endif</span>
<a name="l00020"></a>00020 <span class="preprocessor"></span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;strings.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;time.h&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &lt;sys/time.h&gt;</span>
<a name="l00031"></a>00031 <span class="preprocessor">#include "<a class="code" href="dtn__api_8h.html">dtn_api.h</a>"</span>
<a name="l00032"></a>00032 
<a name="l00033"></a><a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">00033</a> <span class="preprocessor">#define BUFSIZE 16</span>
<a name="l00034"></a><a class="code" href="dtncpd_8c.html#e15a995a7a30a0d14d0db7362b90ce27">00034</a> <span class="preprocessor"></span><span class="preprocessor">#define BUNDLE_DIR_DEFAULT INSTALL_LOCALSTATEDIR "/dtn/dtncpd-incoming"</span>
<a name="l00035"></a>00035 <span class="preprocessor"></span>
<a name="l00036"></a><a class="code" href="dtncpd_8c.html#d785d8a8f8ff378aa21eb1fa7a6085df">00036</a> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a class="code" href="dtncat_8c.html#b9e1449fd00c98428516f0b41eddcb10">progname</a>;
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="keywordtype">void</span>
<a name="l00039"></a><a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">00039</a> <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>()
<a name="l00040"></a>00040 {
<a name="l00041"></a>00041     fprintf(stderr, <span class="stringliteral">"usage: %s [ directory ]\n"</span>, <a class="code" href="dtncat_8c.html#b9e1449fd00c98428516f0b41eddcb10">progname</a>);
<a name="l00042"></a>00042     fprintf(stderr, <span class="stringliteral">"    optional directory parameter is where incoming "</span>
<a name="l00043"></a>00043                     <span class="stringliteral">"files will get put\n"</span>);
<a name="l00044"></a>00044     fprintf(stderr, <span class="stringliteral">"    (defaults to: %s)\n"</span>, <a class="code" href="dtncpd_8c.html#e15a995a7a30a0d14d0db7362b90ce27">BUNDLE_DIR_DEFAULT</a>);
<a name="l00045"></a>00045     exit(1);
<a name="l00046"></a>00046 }
<a name="l00047"></a>00047 
<a name="l00048"></a>00048 <span class="keywordtype">int</span>
<a name="l00049"></a><a class="code" href="dtncpd_8c.html#217dbf8b442f20279ea00b898af96f52">00049</a> <a class="code" href="dtncat_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span>** argv)
<a name="l00050"></a>00050 {
<a name="l00051"></a>00051     <span class="keywordtype">int</span> i;
<a name="l00052"></a>00052     <span class="keywordtype">int</span> ret;
<a name="l00053"></a>00053     <a class="code" href="dtn__api_8h.html#566289d829b69370d7635c21364bfe18" title="The basic handle for communication with the dtn router.">dtn_handle_t</a> <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>;
<a name="l00054"></a>00054     <a class="code" href="structdtn__endpoint__id__t.html" title="Specification of a dtn endpoint id, i.e.">dtn_endpoint_id_t</a> local_eid;
<a name="l00055"></a>00055     <a class="code" href="structdtn__reg__info__t.html" title="Registration state.">dtn_reg_info_t</a> reginfo;
<a name="l00056"></a>00056     <a class="code" href="dtn__types_8h.html#d9378ff6ab7ee699568e037a8105f9b6" title="A registration cookie.">dtn_reg_id_t</a> <a class="code" href="dtncat_8c.html#8eb0c3e35c1feb7baf9802f03bf42e41">regid</a>;
<a name="l00057"></a>00057     <a class="code" href="structdtn__bundle__spec__t.html" title="Bundle metadata.">dtn_bundle_spec_t</a> bundle;
<a name="l00058"></a>00058     <a class="code" href="structdtn__bundle__payload__t.html">dtn_bundle_payload_t</a> payload;
<a name="l00059"></a>00059     <span class="keyword">const</span> <span class="keywordtype">char</span>* endpoint_demux;
<a name="l00060"></a>00060     <span class="keywordtype">int</span> <a class="code" href="dtnmoteproxy_8c.html#ae6a9b084de32dbd181692c52c5e97de">debug</a> = 1;
<a name="l00061"></a>00061 
<a name="l00062"></a>00062     <span class="keywordtype">char</span> * <a class="code" href="dtnperf-server_8c.html#1bc5fad21b936109b622e4992ad1e0c2">bundle_dir</a> = 0;
<a name="l00063"></a>00063 
<a name="l00064"></a>00064     <span class="keywordtype">char</span> host[<a class="code" href="dtnrecv_8c.html#e688d728e1acdfe5988c7db45d6f0166">PATH_MAX</a>];
<a name="l00065"></a>00065     <span class="keywordtype">int</span> host_len;
<a name="l00066"></a>00066     <span class="keywordtype">char</span> * dirpath;
<a name="l00067"></a>00067     <span class="keywordtype">char</span> * <a class="code" href="dtnrecv_8c.html#b49244970ed5ca30989c2dab2129de34">filename</a>;
<a name="l00068"></a>00068     <span class="keywordtype">char</span> filepath[<a class="code" href="dtnrecv_8c.html#e688d728e1acdfe5988c7db45d6f0166">PATH_MAX</a>];
<a name="l00069"></a>00069     time_t current;
<a name="l00070"></a>00070 
<a name="l00071"></a>00071     <span class="keywordtype">char</span> * buffer;
<a name="l00072"></a>00072     <span class="keywordtype">char</span> s_buffer[<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> + 1];
<a name="l00073"></a>00073 
<a name="l00074"></a>00074     <span class="keywordtype">int</span> bufsize, marker, maxwrite;
<a name="l00075"></a>00075 
<a name="l00076"></a>00076     FILE * target;
<a name="l00077"></a>00077 
<a name="l00078"></a>00078     <span class="comment">// force stdout to always be line buffered, even if output is</span>
<a name="l00079"></a>00079     <span class="comment">// redirected to a pipe or file</span>
<a name="l00080"></a>00080     setvbuf(stdout, (<span class="keywordtype">char</span> *)NULL, _IOLBF, 0);
<a name="l00081"></a>00081     
<a name="l00082"></a>00082     s_buffer[<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>] = <span class="charliteral">'\0'</span>;
<a name="l00083"></a>00083 
<a name="l00084"></a>00084     <span class="keyword">struct </span>stat st;
<a name="l00085"></a>00085 
<a name="l00086"></a>00086     <a class="code" href="dtncat_8c.html#b9e1449fd00c98428516f0b41eddcb10">progname</a> = argv[0];
<a name="l00087"></a>00087     
<a name="l00088"></a>00088     <span class="keywordflow">if</span> (argc &gt; 2) {
<a name="l00089"></a>00089         <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>();
<a name="l00090"></a>00090     }
<a name="l00091"></a>00091     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 2)
<a name="l00092"></a>00092     {
<a name="l00093"></a>00093         <span class="keywordflow">if</span> (argv[1][0] == <span class="charliteral">'-'</span>) {
<a name="l00094"></a>00094             <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>();
<a name="l00095"></a>00095         }
<a name="l00096"></a>00096         bundle_dir = (<span class="keywordtype">char</span> *) argv[1];
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098     <span class="keywordflow">else</span>
<a name="l00099"></a>00099     {
<a name="l00100"></a>00100         bundle_dir = <a class="code" href="dtncpd_8c.html#e15a995a7a30a0d14d0db7362b90ce27">BUNDLE_DIR_DEFAULT</a>;
<a name="l00101"></a>00101     }
<a name="l00102"></a>00102 
<a name="l00103"></a>00103     <span class="keywordflow">if</span> (access(bundle_dir, W_OK | X_OK) == -1) {
<a name="l00104"></a>00104         fprintf(stderr, <span class="stringliteral">"can't access directory '%s': %s\n"</span>,
<a name="l00105"></a>00105                 bundle_dir, strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>));
<a name="l00106"></a>00106         <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>();
<a name="l00107"></a>00107     }
<a name="l00108"></a>00108 
<a name="l00109"></a>00109     <span class="keywordflow">if</span> (stat(bundle_dir, &amp;st) == -1) { 
<a name="l00110"></a>00110         fprintf(stderr, <span class="stringliteral">"can't stat directory '%s': %s\n"</span>,
<a name="l00111"></a>00111                 bundle_dir, strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>));
<a name="l00112"></a>00112         <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>();
<a name="l00113"></a>00113     }
<a name="l00114"></a>00114 
<a name="l00115"></a>00115     <span class="keywordflow">if</span> (!S_ISDIR(st.st_mode)) {
<a name="l00116"></a>00116         fprintf(stderr, <span class="stringliteral">"path '%s' is not a directory\n"</span>,
<a name="l00117"></a>00117                 bundle_dir);
<a name="l00118"></a>00118         <a class="code" href="dtncpd_8c.html#2ef30c42cbc289d899a8be5d2d8f77d0">usage</a>();
<a name="l00119"></a>00119     }
<a name="l00120"></a>00120 
<a name="l00121"></a>00121     <span class="comment">// open the ipc handle</span>
<a name="l00122"></a>00122     <span class="keywordflow">if</span> (debug) printf(<span class="stringliteral">"opening connection to dtn router...\n"</span>);
<a name="l00123"></a>00123     <span class="keywordtype">int</span> err = <a class="code" href="dtn__api_8c.html#3afc66b2e2c75fae07a45467df9f74e9" title="Open a new connection to the router.">dtn_open</a>(&amp;handle);
<a name="l00124"></a>00124     <span class="keywordflow">if</span> (err != <a class="code" href="dtn__errno_8h.html#3723251cb5d056b6ace3e22561da1e11" title="DTN API error codes.">DTN_SUCCESS</a>) {
<a name="l00125"></a>00125         fprintf(stderr, <span class="stringliteral">"fatal error opening dtn handle: %s\n"</span>,
<a name="l00126"></a>00126                 <a class="code" href="dtn__errno_8c.html#1b23d549407e59e3ce2a1c32e3885b1d" title="Get a string value associated with the dtn error code.">dtn_strerror</a>(err));
<a name="l00127"></a>00127         exit(1);
<a name="l00128"></a>00128     }
<a name="l00129"></a>00129 
<a name="l00130"></a>00130     <span class="comment">// build a local eid based on the configuration of our dtn</span>
<a name="l00131"></a>00131     <span class="comment">// router plus the demux string</span>
<a name="l00132"></a>00132     endpoint_demux = <span class="stringliteral">"/dtncp/recv?file=*"</span>;
<a name="l00133"></a>00133     <a class="code" href="dtn__api_8c.html#0947b9119c69984edf25df43ead8858b" title="Build an appropriate local endpoint id by appending the specified service tag to...">dtn_build_local_eid</a>(handle, &amp;local_eid, endpoint_demux);
<a name="l00134"></a>00134     <span class="keywordflow">if</span> (debug) printf(<span class="stringliteral">"local_eid [%s]\n"</span>, local_eid.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>);
<a name="l00135"></a>00135 
<a name="l00136"></a>00136     <span class="comment">// try to find an existin registration, or create a new</span>
<a name="l00137"></a>00137     <span class="comment">// registration based on this eid</span>
<a name="l00138"></a>00138     ret = <a class="code" href="dtn__api_8c.html#add060907ae0beceeebb75ac61a8f552" title="Check for an existing registration on the given endpoint id, returning DTN_SUCCSS...">dtn_find_registration</a>(handle, &amp;local_eid, &amp;regid);
<a name="l00139"></a>00139     <span class="keywordflow">if</span> (ret == 0) {
<a name="l00140"></a>00140         <span class="keywordflow">if</span> (debug) printf(<span class="stringliteral">"dtn_find_registration succeeded, regid 0x%x\n"</span>,
<a name="l00141"></a>00141                           regid);
<a name="l00142"></a>00142         
<a name="l00143"></a>00143         <span class="comment">// bind the current handle to the new registration</span>
<a name="l00144"></a>00144         <a class="code" href="dtn__api_8c.html#ab949d76f4c34a202aea5bac853b310b" title="Associate a registration with the current ipc channel.">dtn_bind</a>(handle, regid);
<a name="l00145"></a>00145         
<a name="l00146"></a>00146     } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="dtn__api_8c.html#6183d81d5f742d13e1fdee26423d94b4" title="Get the error associated with the given handle.">dtn_errno</a>(handle) == <a class="code" href="dtn__errno_8h.html#58a067b3ff3eba74fc55d6d7e7866d5a">DTN_ENOTFOUND</a>) {
<a name="l00147"></a>00147         memset(&amp;reginfo, 0, <span class="keyword">sizeof</span>(reginfo));
<a name="l00148"></a>00148         <a class="code" href="dtn__api_8c.html#b2935a9a7f4ade630f7d2ad1fec5c615" title="Copy the contents of one eid into another.">dtn_copy_eid</a>(&amp;reginfo.<a class="code" href="structdtn__reg__info__t.html#07df1822e2808b2127710a8b54acd07f">endpoint</a>, &amp;local_eid);
<a name="l00149"></a>00149         reginfo.<a class="code" href="structdtn__reg__info__t.html#7bf2e33b713b585bfe1792c40fa8cd6c">flags</a> = <a class="code" href="dtn__types_8h.html#a6d0071f396dd4ef85f6ad6332e37f00d21fdf39ccc69d6790aec0c70ce5f972">DTN_REG_DEFER</a>;
<a name="l00150"></a>00150         reginfo.<a class="code" href="structdtn__reg__info__t.html#6909a69cc63e748899cf4f9fe8c8901a">regid</a> = <a class="code" href="dtn__types_8h.html#f96cd9cebfff24e37ccececf05ebb4e6" title="Value for an unspecified registration cookie (i.e.">DTN_REGID_NONE</a>;
<a name="l00151"></a>00151         reginfo.<a class="code" href="structdtn__reg__info__t.html#dd4b3b0c9f27a0375369f35e33be686d">expiration</a> = 60 * 60;
<a name="l00152"></a>00152         <span class="keywordflow">if</span> ((ret = <a class="code" href="dtn__api_8c.html#7384bf592d4aff0892ff948aafa98b0c" title="Create a dtn registration.">dtn_register</a>(handle, &amp;reginfo, &amp;regid)) != 0) {
<a name="l00153"></a>00153             fprintf(stderr, <span class="stringliteral">"error creating registration: %d (%s)\n"</span>,
<a name="l00154"></a>00154                     ret, <a class="code" href="dtn__errno_8c.html#1b23d549407e59e3ce2a1c32e3885b1d" title="Get a string value associated with the dtn error code.">dtn_strerror</a>(<a class="code" href="dtn__api_8c.html#6183d81d5f742d13e1fdee26423d94b4" title="Get the error associated with the given handle.">dtn_errno</a>(handle)));
<a name="l00155"></a>00155             exit(1);
<a name="l00156"></a>00156         }
<a name="l00157"></a>00157     
<a name="l00158"></a>00158         <span class="keywordflow">if</span> (debug) printf(<span class="stringliteral">"dtn_register succeeded, regid 0x%x\n"</span>, regid);
<a name="l00159"></a>00159 
<a name="l00160"></a>00160     } <span class="keywordflow">else</span> {
<a name="l00161"></a>00161         fprintf(stderr, <span class="stringliteral">"error in dtn_find_registration: %s"</span>,
<a name="l00162"></a>00162                 <a class="code" href="dtn__errno_8c.html#1b23d549407e59e3ce2a1c32e3885b1d" title="Get a string value associated with the dtn error code.">dtn_strerror</a>(<a class="code" href="dtn__api_8c.html#6183d81d5f742d13e1fdee26423d94b4" title="Get the error associated with the given handle.">dtn_errno</a>(handle)));
<a name="l00163"></a>00163         exit(1);
<a name="l00164"></a>00164     }
<a name="l00165"></a>00165     
<a name="l00166"></a>00166     <span class="comment">// loop waiting for bundles</span>
<a name="l00167"></a>00167     <span class="keywordflow">while</span>(1)
<a name="l00168"></a>00168     {
<a name="l00169"></a>00169         <span class="comment">// change this to _MEM here to receive into memory then write the file</span>
<a name="l00170"></a>00170         <span class="comment">// ourselves. (So this code shows both ways to do it.)</span>
<a name="l00171"></a>00171         <span class="comment">// XXX/demmer better would be to have a cmd line option</span>
<a name="l00172"></a>00172         <a class="code" href="dtn__types_8h.html#3dcfddc1b850a71a18d8a0868f67e762" title="The payload of a bundle can be sent or received either in a file, in which case the...">dtn_bundle_payload_location_t</a> file_or_mem = <a class="code" href="dtn__types_8h.html#3dcfddc1b850a71a18d8a0868f67e7621ff9272291ffe7d9f8083e7c5a548909">DTN_PAYLOAD_FILE</a>;
<a name="l00173"></a>00173 
<a name="l00174"></a>00174         memset(&amp;bundle, 0, <span class="keyword">sizeof</span>(bundle));
<a name="l00175"></a>00175         memset(&amp;payload, 0, <span class="keyword">sizeof</span>(payload));
<a name="l00176"></a>00176         memset(&amp;dirpath, 0, <span class="keyword">sizeof</span>(dirpath));
<a name="l00177"></a>00177         memset(&amp;filename, 0, <span class="keyword">sizeof</span>(filename));
<a name="l00178"></a>00178         memset(&amp;filepath, 0, <span class="keyword">sizeof</span>(filepath));
<a name="l00179"></a>00179         memset(&amp;host, 0, <span class="keyword">sizeof</span>(host));
<a name="l00180"></a>00180         memset(&amp;st, 0, <span class="keyword">sizeof</span>(st));
<a name="l00181"></a>00181         
<a name="l00182"></a>00182         printf(<span class="stringliteral">"dtn_recv [%s]...\n"</span>, local_eid.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>);
<a name="l00183"></a>00183     
<a name="l00184"></a>00184         <span class="keywordflow">if</span> ((ret = <a class="code" href="dtn__api_8c.html#7005346bb34aabb5b6332690871fe3f4" title="Blocking receive for a bundle, filling in the spec and payload structures with the...">dtn_recv</a>(handle, &amp;bundle,
<a name="l00185"></a>00185                             file_or_mem, &amp;payload, -1)) &lt; 0)
<a name="l00186"></a>00186         {
<a name="l00187"></a>00187             fprintf(stderr, <span class="stringliteral">"error getting recv reply: %d (%s)\n"</span>,
<a name="l00188"></a>00188                     ret, <a class="code" href="dtn__errno_8c.html#1b23d549407e59e3ce2a1c32e3885b1d" title="Get a string value associated with the dtn error code.">dtn_strerror</a>(<a class="code" href="dtn__api_8c.html#6183d81d5f742d13e1fdee26423d94b4" title="Get the error associated with the given handle.">dtn_errno</a>(handle)));
<a name="l00189"></a>00189             exit(1);
<a name="l00190"></a>00190         }
<a name="l00191"></a>00191 
<a name="l00192"></a>00192         <span class="comment">// mark time received</span>
<a name="l00193"></a>00193         current = time(NULL);
<a name="l00194"></a>00194 
<a name="l00195"></a>00195         <span class="keywordflow">if</span> (strncmp(bundle.<a class="code" href="structdtn__bundle__spec__t.html#3e0a3530457d11bea38b06d00dea30fa">source</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>, <span class="stringliteral">"dtn://"</span>, 6) != 0)
<a name="l00196"></a>00196         {
<a name="l00197"></a>00197             fprintf(stderr, <span class="stringliteral">"bundle source uri '%s' must be a dtn:// uri\n"</span>,
<a name="l00198"></a>00198                     bundle.<a class="code" href="structdtn__bundle__spec__t.html#3e0a3530457d11bea38b06d00dea30fa">source</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>);
<a name="l00199"></a>00199             exit(1);
<a name="l00200"></a>00200         }
<a name="l00201"></a>00201 
<a name="l00202"></a>00202         <span class="comment">// grab the sending authority and service tag (i.e. the path)</span>
<a name="l00203"></a>00203         host_len = strchr(&amp;bundle.<a class="code" href="structdtn__bundle__spec__t.html#3e0a3530457d11bea38b06d00dea30fa">source</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>[6], <span class="charliteral">'/'</span>) - &amp;bundle.<a class="code" href="structdtn__bundle__spec__t.html#3e0a3530457d11bea38b06d00dea30fa">source</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>[6];
<a name="l00204"></a>00204         strncpy(host, &amp;bundle.<a class="code" href="structdtn__bundle__spec__t.html#3e0a3530457d11bea38b06d00dea30fa">source</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>[6], host_len);
<a name="l00205"></a>00205         
<a name="l00206"></a>00206         <span class="comment">// extract directory from destination path (everything</span>
<a name="l00207"></a>00207         <span class="comment">// following std demux, except the '*')</span>
<a name="l00208"></a>00208         endpoint_demux = <span class="stringliteral">"/dtncp/recv?file="</span>;
<a name="l00209"></a>00209         dirpath = strstr(bundle.<a class="code" href="structdtn__bundle__spec__t.html#8c2816b3ce9b92489b3b34c325a4cb4c">dest</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>, endpoint_demux);
<a name="l00210"></a>00210         <span class="keywordflow">if</span> (!dirpath) {
<a name="l00211"></a>00211             fprintf(stderr, <span class="stringliteral">"can't find %s demux in uri '%s'\n"</span>,
<a name="l00212"></a>00212                     endpoint_demux, bundle.<a class="code" href="structdtn__bundle__spec__t.html#8c2816b3ce9b92489b3b34c325a4cb4c">dest</a>.<a class="code" href="structdtn__endpoint__id__t.html#d2e7d5788ae778c77b4041f1f8b1dc80">uri</a>);
<a name="l00213"></a>00213             exit(1);
<a name="l00214"></a>00214         }
<a name="l00215"></a>00215         
<a name="l00216"></a>00216         dirpath += strlen(endpoint_demux); <span class="comment">// skip std demux</span>
<a name="l00217"></a>00217         <span class="keywordflow">if</span> (dirpath[0] == <span class="charliteral">'/'</span>) dirpath++; <span class="comment">// skip leading slash</span>
<a name="l00218"></a>00218 
<a name="l00219"></a>00219         <span class="comment">// filename is everything following last /</span>
<a name="l00220"></a>00220         filename = strrchr(dirpath, <span class="charliteral">'/'</span>);
<a name="l00221"></a>00221         <span class="keywordflow">if</span> (filename == 0)
<a name="l00222"></a>00222         {
<a name="l00223"></a>00223             filename = dirpath;
<a name="l00224"></a>00224             dirpath = <span class="stringliteral">""</span>;
<a name="l00225"></a>00225         }
<a name="l00226"></a>00226         <span class="keywordflow">else</span>
<a name="l00227"></a>00227         {
<a name="l00228"></a>00228             filename[0] = <span class="charliteral">'\0'</span>; <span class="comment">// null terminate path</span>
<a name="l00229"></a>00229             filename++; <span class="comment">// next char;</span>
<a name="l00230"></a>00230         }
<a name="l00231"></a>00231 
<a name="l00232"></a>00232         <span class="comment">// recursively create full directory path</span>
<a name="l00233"></a>00233         <span class="comment">// XXX/demmer system -- yuck!</span>
<a name="l00234"></a>00234         sprintf(filepath, <span class="stringliteral">"mkdir -p %s/%s/%s"</span>, bundle_dir, host, dirpath);
<a name="l00235"></a>00235         system(filepath);
<a name="l00236"></a>00236         
<a name="l00237"></a>00237         <span class="comment">// create file name</span>
<a name="l00238"></a>00238         sprintf(filepath, <span class="stringliteral">"%s/%s/%s/%s"</span>, bundle_dir, host, dirpath, filename);
<a name="l00239"></a>00239         
<a name="l00240"></a>00240         <span class="comment">// bundle name is the name of the bundle payload file</span>
<a name="l00241"></a>00241         buffer = payload.<a class="code" href="structdtn__bundle__payload__t.html#f3a33fd2ae2b3b8eae2e675f1a038cec">filename</a>.<a class="code" href="structdtn__bundle__payload__t.html#0d824d13c3225cb59fa2e34623608b2a">filename_val</a>;
<a name="l00242"></a>00242         <span class="comment">// bufsize is the length of the payload file (not what we want to print)</span>
<a name="l00243"></a>00243         bufsize = payload.<a class="code" href="structdtn__bundle__payload__t.html#f3a33fd2ae2b3b8eae2e675f1a038cec">filename</a>.<a class="code" href="structdtn__bundle__payload__t.html#19a9fb6ab21fdc57d29fbdb0e61d0bbf">filename_len</a>;
<a name="l00244"></a>00244         <span class="comment">// st contains (among other things) size of payload file</span>
<a name="l00245"></a>00245         ret = stat(buffer, &amp;st);
<a name="l00246"></a>00246 
<a name="l00247"></a>00247         printf (<span class="stringliteral">"======================================\n"</span>);
<a name="l00248"></a>00248         printf (<span class="stringliteral">" File Received at %s\n"</span>, ctime(&amp;current));
<a name="l00249"></a>00249         printf (<span class="stringliteral">"   host   : %s\n"</span>, host);
<a name="l00250"></a>00250         printf (<span class="stringliteral">"   path   : %s\n"</span>, dirpath);
<a name="l00251"></a>00251         printf (<span class="stringliteral">"   file   : %s\n"</span>, filename);
<a name="l00252"></a>00252         printf (<span class="stringliteral">"   loc    : %s\n"</span>, filepath);
<a name="l00253"></a>00253         printf (<span class="stringliteral">"   size   : %d bytes\n"</span>, (<span class="keywordtype">int</span>) st.st_size);
<a name="l00254"></a>00254         
<a name="l00255"></a>00255 
<a name="l00256"></a>00256         <span class="keywordflow">if</span> (file_or_mem == <a class="code" href="dtn__types_8h.html#3dcfddc1b850a71a18d8a0868f67e7621ff9272291ffe7d9f8083e7c5a548909">DTN_PAYLOAD_FILE</a>) {
<a name="l00257"></a>00257             <span class="keywordtype">int</span> cmdlen = 5 + strlen(buffer) + strlen(filepath);
<a name="l00258"></a>00258             <span class="keywordtype">char</span> *cmd = malloc(cmdlen);
<a name="l00259"></a>00259 
<a name="l00260"></a>00260             <span class="keywordflow">if</span> (cmd) {
<a name="l00261"></a>00261                 snprintf(cmd, cmdlen, <span class="stringliteral">"mv %.*s %s"</span>, bufsize, buffer,
<a name="l00262"></a>00262                          filepath);
<a name="l00263"></a>00263                 system(cmd);
<a name="l00264"></a>00264                 printf(<span class="stringliteral">"Moving payload to final filename: '%s'\n"</span>, cmd);
<a name="l00265"></a>00265                 free(cmd);
<a name="l00266"></a>00266             } <span class="keywordflow">else</span> {
<a name="l00267"></a>00267                 printf(<span class="stringliteral">"Out of memory. Find file in %*s.\n"</span>, bufsize,
<a name="l00268"></a>00268                         buffer);
<a name="l00269"></a>00269             }
<a name="l00270"></a>00270         } <span class="keywordflow">else</span> {
<a name="l00271"></a>00271 
<a name="l00272"></a>00272             target = fopen(filepath, <span class="stringliteral">"w"</span>);
<a name="l00273"></a>00273 
<a name="l00274"></a>00274             <span class="keywordflow">if</span> (target == NULL)
<a name="l00275"></a>00275             {
<a name="l00276"></a>00276                 fprintf(stderr, <span class="stringliteral">"Error opening file for writing %s\n"</span>,
<a name="l00277"></a>00277                          filepath);
<a name="l00278"></a>00278                 <span class="keywordflow">continue</span>;
<a name="l00279"></a>00279             }
<a name="l00280"></a>00280             <span class="keywordflow">if</span> (debug) printf (<span class="stringliteral">"--------------------------------------\n"</span>);
<a name="l00281"></a>00281         
<a name="l00282"></a>00282             marker = 0;
<a name="l00283"></a>00283             <span class="keywordflow">while</span> (marker &lt; bufsize)
<a name="l00284"></a>00284             {
<a name="l00285"></a>00285                 <span class="comment">// write 256 bytes at a time</span>
<a name="l00286"></a>00286                 i=0;
<a name="l00287"></a>00287                 maxwrite = (marker + 256) &gt; bufsize? bufsize-marker : 256;
<a name="l00288"></a>00288                 <span class="keywordflow">while</span> (i &lt; maxwrite)
<a name="l00289"></a>00289                 {
<a name="l00290"></a>00290                     i += fwrite(buffer + marker + i, 1, maxwrite - i, target);
<a name="l00291"></a>00291                 }
<a name="l00292"></a>00292             
<a name="l00293"></a>00293                 <span class="keywordflow">if</span> (debug)
<a name="l00294"></a>00294                 {
<a name="l00295"></a>00295                     <span class="keywordflow">for</span> (i=0; i &lt; maxwrite; i++)
<a name="l00296"></a>00296                     {
<a name="l00297"></a>00297                         <span class="keywordflow">if</span> (buffer[marker] &gt;= <span class="charliteral">' '</span> &amp;&amp; buffer[marker] &lt;= <span class="charliteral">'~'</span>)
<a name="l00298"></a>00298                             s_buffer[marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>] = buffer[i];
<a name="l00299"></a>00299                         <span class="keywordflow">else</span>
<a name="l00300"></a>00300                             s_buffer[marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>] = <span class="charliteral">'.'</span>;
<a name="l00301"></a>00301                     
<a name="l00302"></a>00302                         <span class="keywordflow">if</span> (marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> == 0) <span class="comment">// new line every 16 bytes</span>
<a name="l00303"></a>00303                         {
<a name="l00304"></a>00304                             printf(<span class="stringliteral">"%07x "</span>, marker);
<a name="l00305"></a>00305                         }
<a name="l00306"></a>00306                         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (marker%2 == 0)
<a name="l00307"></a>00307                         {
<a name="l00308"></a>00308                             printf(<span class="stringliteral">" "</span>); <span class="comment">// space every 2 bytes</span>
<a name="l00309"></a>00309                         }
<a name="l00310"></a>00310                     
<a name="l00311"></a>00311                         printf(<span class="stringliteral">"%02x"</span>, buffer[i] &amp; 0xff);
<a name="l00312"></a>00312                     
<a name="l00313"></a>00313                         <span class="comment">// print character summary (a la emacs hexl-mode)</span>
<a name="l00314"></a>00314                         <span class="keywordflow">if</span> (marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> == <a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>-1)
<a name="l00315"></a>00315                         {
<a name="l00316"></a>00316                             printf(<span class="stringliteral">" |  %s\n"</span>, s_buffer);
<a name="l00317"></a>00317                         }
<a name="l00318"></a>00318                         marker ++;
<a name="l00319"></a>00319                     }
<a name="l00320"></a>00320                 }
<a name="l00321"></a>00321                 <span class="keywordflow">else</span>
<a name="l00322"></a>00322                 {
<a name="l00323"></a>00323                     marker += maxwrite;
<a name="l00324"></a>00324                 }
<a name="l00325"></a>00325             }
<a name="l00326"></a>00326     
<a name="l00327"></a>00327             fclose(target);
<a name="l00328"></a>00328     
<a name="l00329"></a>00329             <span class="comment">// round off last line</span>
<a name="l00330"></a>00330             <span class="keywordflow">if</span> (debug &amp;&amp; marker % <a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> != 0)
<a name="l00331"></a>00331             {
<a name="l00332"></a>00332                 <span class="keywordflow">while</span> (marker % <a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> !=0)
<a name="l00333"></a>00333                 {
<a name="l00334"></a>00334                     s_buffer[marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>] = <span class="charliteral">' '</span>;
<a name="l00335"></a>00335     
<a name="l00336"></a>00336                     <span class="keywordflow">if</span> (marker%2 == 0)
<a name="l00337"></a>00337                     {
<a name="l00338"></a>00338                         printf(<span class="stringliteral">" "</span>); <span class="comment">// space every 2 bytes</span>
<a name="l00339"></a>00339                     }
<a name="l00340"></a>00340                         
<a name="l00341"></a>00341                     printf(<span class="stringliteral">"  "</span>);
<a name="l00342"></a>00342                         
<a name="l00343"></a>00343                     <span class="comment">// print character summary (a la emacs hexl-mode)</span>
<a name="l00344"></a>00344                     <span class="keywordflow">if</span> (marker%<a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a> == <a class="code" href="dtncpd_8c.html#eca034f67218340ecb2261a22c2f3dcd">BUFSIZE</a>-1)
<a name="l00345"></a>00345                     {
<a name="l00346"></a>00346                         printf(<span class="stringliteral">" |  %s\n"</span>, s_buffer);
<a name="l00347"></a>00347                     }
<a name="l00348"></a>00348                     marker ++;
<a name="l00349"></a>00349                 }
<a name="l00350"></a>00350             }
<a name="l00351"></a>00351             printf (<span class="stringliteral">"   size   : %d bytes\n"</span>, bufsize);
<a name="l00352"></a>00352         }
<a name="l00353"></a>00353     
<a name="l00354"></a>00354         printf (<span class="stringliteral">"======================================\n"</span>);
<a name="l00355"></a>00355         <a class="code" href="dtn__api_8c.html#50a363bf1bf2803892afc1438ab62cff" title="Frees dynamic storage allocated by the xdr for a bundle payload in dtn_recv.">dtn_free_payload</a>(&amp;payload);
<a name="l00356"></a>00356     }
<a name="l00357"></a>00357 
<a name="l00358"></a>00358     <a class="code" href="dtn__api_8c.html#887c16260463d6057bf5b03a1ff0e224" title="Close an open dtn handle.">dtn_close</a>(handle);
<a name="l00359"></a>00359     
<a name="l00360"></a>00360     <span class="keywordflow">return</span> 0;
<a name="l00361"></a>00361 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Generated on Fri Jan 30 09:26:55 2009 for DTN Reference Implementation by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address>
</body>
</html>