<!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: dtn_ipc.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.6 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main 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> <h1>dtn_ipc.c</h1><a href="dtn__ipc_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 <<a class="code" href="dtn-config_8h.html">dtn-config.h</a>></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 <stdio.h></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <stdlib.h></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <errno.h></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <string.h></span> <a name="l00025"></a>00025 <span class="preprocessor">#include <unistd.h></span> <a name="l00026"></a>00026 <span class="preprocessor">#include <sys/types.h></span> <a name="l00027"></a>00027 <span class="preprocessor">#include <sys/socket.h></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <netinet/in.h></span> <a name="l00029"></a>00029 <span class="preprocessor">#include <arpa/inet.h></span> <a name="l00030"></a>00030 <span class="preprocessor">#include <oasys/compat/inet_aton.h></span> <a name="l00031"></a>00031 <span class="preprocessor">#include <oasys/compat/inttypes.h></span> <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="preprocessor">#include "<a class="code" href="dtn__ipc_8h.html">dtn_ipc.h</a>"</span> <a name="l00034"></a>00034 <span class="preprocessor">#include "<a class="code" href="dtn__errno_8h.html">dtn_errno.h</a>"</span> <a name="l00035"></a>00035 <span class="preprocessor">#include "<a class="code" href="dtn__types_8h.html">dtn_types.h</a>"</span> <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="comment">/* exposed globally for testing purposes only */</span> <a name="l00038"></a><a class="code" href="dtntest_8cc.html#94b0ae7635a33f63da245751f707a69b">00038</a> <span class="keywordtype">int</span> <a class="code" href="dtn__ipc_8c.html#94b0ae7635a33f63da245751f707a69b">dtnipc_version</a> = <a class="code" href="dtn__ipc_8h.html#2afeec365b4613086d19c23e8551d838" title="DTN IPC version.">DTN_IPC_VERSION</a>; <a name="l00039"></a>00039 <a name="l00040"></a>00040 <span class="keyword">const</span> <span class="keywordtype">char</span>* <a name="l00041"></a><a class="code" href="dtn__ipc_8h.html#c5707d2923063f6fe2bde512f525f72b">00041</a> <a class="code" href="dtn__ipc_8c.html#c5707d2923063f6fe2bde512f525f72b" title="Type code to string conversion routine.">dtnipc_msgtoa</a>(u_int8_t type) <a name="l00042"></a>00042 { <a name="l00043"></a>00043 <span class="preprocessor">#define CASE(_type) case _type : return #_type; break;</span> <a name="l00044"></a>00044 <span class="preprocessor"></span> <a name="l00045"></a>00045 <span class="keywordflow">switch</span>(type) { <a name="l00046"></a>00046 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d4e9e92913a117e7b6c92b2dee3eab867">DTN_OPEN</a>); <a name="l00047"></a>00047 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d88988342732cf7736a9a17338475582e">DTN_CLOSE</a>); <a name="l00048"></a>00048 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d92dab95353850a6e914e4008347f07c7">DTN_LOCAL_EID</a>); <a name="l00049"></a>00049 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5de7933a4eff1b95879d4a353df40b195f">DTN_REGISTER</a>); <a name="l00050"></a>00050 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d1e3888bf2ac2f73a90c6c142080279f5">DTN_UNREGISTER</a>); <a name="l00051"></a>00051 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5df41ecaa2e442e281718ce6c7cd88fe30">DTN_FIND_REGISTRATION</a>); <a name="l00052"></a>00052 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5debfbbd5cdea8d1c82c92fe89b50d1b0f">DTN_CHANGE_REGISTRATION</a>); <a name="l00053"></a>00053 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d59676a9e0dd2cf2a2ebb493721f50198">DTN_BIND</a>); <a name="l00054"></a>00054 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d00609e70cdd50ac4a6d989603c2976bf">DTN_SEND</a>); <a name="l00055"></a>00055 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5da5a90d9130e7ad725dede5a0c4110495">DTN_RECV</a>); <a name="l00056"></a>00056 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d5f4461d4add4e2832436f9c8ae25e61e">DTN_BEGIN_POLL</a>); <a name="l00057"></a>00057 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5df37b9c7e51b4309e5f3ca74a8b743c3b">DTN_CANCEL_POLL</a>); <a name="l00058"></a>00058 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d8ac916eaefdf27a024e85c5a71ef7caf">DTN_CANCEL</a>); <a name="l00059"></a>00059 <a class="code" href="dtn__ipc_8c.html#6d24efd83036c2dde8d9100adb9abffd">CASE</a>(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d7f9ec4960a1d48ce448a1f7c2dfc0d5a">DTN_SESSION_UPDATE</a>); <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="keywordflow">default</span>: <a name="l00062"></a>00062 <span class="keywordflow">return</span> <span class="stringliteral">"(unknown type)"</span>; <a name="l00063"></a>00063 } <a name="l00064"></a>00064 <a name="l00065"></a>00065 <span class="preprocessor">#undef CASE</span> <a name="l00066"></a>00066 <span class="preprocessor"></span>} <a name="l00067"></a>00067 <a name="l00068"></a>00068 <span class="comment">/*</span> <a name="l00069"></a>00069 <span class="comment"> * Initialize the handle structure.</span> <a name="l00070"></a>00070 <span class="comment"> */</span> <a name="l00071"></a>00071 <span class="keywordtype">int</span> <a name="l00072"></a><a class="code" href="dtn__ipc_8h.html#0a70871adbaca23a535c898f854be3a8">00072</a> <a class="code" href="dtn__ipc_8c.html#0a70871adbaca23a535c898f854be3a8">dtnipc_open</a>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>* <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>) <a name="l00073"></a>00073 { <a name="l00074"></a>00074 <span class="keywordtype">int</span> remote_version, ret; <a name="l00075"></a>00075 <span class="keywordtype">char</span> *env, *<a class="code" href="dtncat_8c.html#7ab59805f7a1819164bc301293db600d">end</a>; <a name="l00076"></a>00076 <span class="keyword">struct </span>sockaddr_in sa; <a name="l00077"></a>00077 in_addr_t ipc_addr; <a name="l00078"></a>00078 u_int16_t ipc_port; <a name="l00079"></a>00079 u_int32_t handshake; <a name="l00080"></a>00080 u_int port; <a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="comment">// zero out the handle</span> <a name="l00083"></a>00083 memset(handle, 0, <span class="keyword">sizeof</span>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>)); <a name="l00084"></a>00084 <a name="l00085"></a>00085 <span class="comment">// check for debugging</span> <a name="l00086"></a>00086 <span class="keywordflow">if</span> (getenv(<span class="stringliteral">"DTNAPI_DEBUG"</span>) != 0) { <a name="l00087"></a>00087 handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a> = 1; <a name="l00088"></a>00088 } <a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="comment">// note that we leave eight bytes free to be used for the framing</span> <a name="l00091"></a>00091 <span class="comment">// -- the type code and length for send (which is only five</span> <a name="l00092"></a>00092 <span class="comment">// bytes), and the return code and length for recv (which is</span> <a name="l00093"></a>00093 <span class="comment">// actually eight bytes)</span> <a name="l00094"></a>00094 xdrmem_create(&handle-><a class="code" href="structdtnipc__handle.html#ded5c2335049e780a4ee8ab2b2356f95" title="XDR encoder.">xdr_encode</a>, handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a> + 8, <a name="l00095"></a>00095 <a class="code" href="dtn__ipc_8h.html#51d06f90137293f017ba7e9019d6602f" title="The maximum IPC message size (in bytes).">DTN_MAX_API_MSG</a> - 8, XDR_ENCODE); <a name="l00096"></a>00096 <a name="l00097"></a>00097 xdrmem_create(&handle-><a class="code" href="structdtnipc__handle.html#c09f26354224257a07c51d45f92725fd" title="XDR decoder.">xdr_decode</a>, handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a> + 8, <a name="l00098"></a>00098 <a class="code" href="dtn__ipc_8h.html#51d06f90137293f017ba7e9019d6602f" title="The maximum IPC message size (in bytes).">DTN_MAX_API_MSG</a> - 8, XDR_DECODE); <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="comment">// open the socket</span> <a name="l00101"></a>00101 handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a> = socket(PF_INET, SOCK_STREAM, 0); <a name="l00102"></a>00102 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a> < 0) <a name="l00103"></a>00103 { <a name="l00104"></a>00104 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00105"></a>00105 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00106"></a>00106 <span class="keywordflow">return</span> -1; <a name="l00107"></a>00107 } <a name="l00108"></a>00108 <a name="l00109"></a>00109 <span class="comment">// check for DTNAPI environment variables overriding the address /</span> <a name="l00110"></a>00110 <span class="comment">// port defaults</span> <a name="l00111"></a>00111 ipc_addr = htonl(INADDR_LOOPBACK); <a name="l00112"></a>00112 ipc_port = <a class="code" href="dtn__ipc_8h.html#7f9f6b66a37fb74f861dec857888c0ba" title="Default api ports.">DTN_IPC_PORT</a>; <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="keywordflow">if</span> ((env = getenv(<span class="stringliteral">"DTNAPI_ADDR"</span>)) != NULL) { <a name="l00115"></a>00115 <span class="keywordflow">if</span> (inet_aton(env, (<span class="keyword">struct</span> in_addr*)&ipc_addr) == 0) <a name="l00116"></a>00116 { <a name="l00117"></a>00117 fprintf(stderr, <span class="stringliteral">"DTNAPI_ADDR environment variable (%s) "</span> <a name="l00118"></a>00118 <span class="stringliteral">"not a valid ip address\n"</span>, env); <a name="l00119"></a>00119 exit(1); <a name="l00120"></a>00120 } <a name="l00121"></a>00121 } <a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="keywordflow">if</span> ((env = getenv(<span class="stringliteral">"DTNAPI_PORT"</span>)) != NULL) { <a name="l00124"></a>00124 port = strtoul(env, &end, 10); <a name="l00125"></a>00125 <span class="keywordflow">if</span> (*end != <span class="charliteral">'\0'</span> || port > 0xffff) <a name="l00126"></a>00126 { <a name="l00127"></a>00127 fprintf(stderr, <span class="stringliteral">"DTNAPI_PORT environment variable (%s) "</span> <a name="l00128"></a>00128 <span class="stringliteral">"not a valid ip port\n"</span>, env); <a name="l00129"></a>00129 exit(1); <a name="l00130"></a>00130 } <a name="l00131"></a>00131 ipc_port = (u_int16_t)port; <a name="l00132"></a>00132 } <a name="l00133"></a>00133 <a name="l00134"></a>00134 <span class="comment">// connect to the server</span> <a name="l00135"></a>00135 memset(&sa, 0, <span class="keyword">sizeof</span>(sa)); <a name="l00136"></a>00136 sa.sin_family = AF_INET; <a name="l00137"></a>00137 sa.sin_addr.s_addr = ipc_addr; <a name="l00138"></a>00138 sa.sin_port = htons(ipc_port); <a name="l00139"></a>00139 <a name="l00140"></a>00140 ret = connect(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, (<span class="keyword">const</span> <span class="keyword">struct</span> sockaddr*)&sa, <span class="keyword">sizeof</span>(sa)); <a name="l00141"></a>00141 <span class="keywordflow">if</span> (ret != 0) { <a name="l00142"></a>00142 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00143"></a>00143 fprintf(stderr, <span class="stringliteral">"dtn_ipc: error connecting to server: %s\n"</span>, <a name="l00144"></a>00144 strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>)); <a name="l00145"></a>00145 } <a name="l00146"></a>00146 <a name="l00147"></a>00147 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00148"></a>00148 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00149"></a>00149 <span class="keywordflow">return</span> -1; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a name="l00152"></a>00152 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00153"></a>00153 fprintf(stderr, <span class="stringliteral">"dtn_ipc: connected to server: fd %d\n"</span>, handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>); <a name="l00154"></a>00154 } <a name="l00155"></a>00155 <a name="l00156"></a>00156 <span class="comment">// send the session initiation to the server on the handshake</span> <a name="l00157"></a>00157 <span class="comment">// port. it consists of DTN_OPEN in the high 16 bits and IPC</span> <a name="l00158"></a>00158 <span class="comment">// version in the low 16 bits</span> <a name="l00159"></a>00159 handshake = htonl(<a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d4e9e92913a117e7b6c92b2dee3eab867">DTN_OPEN</a> << 16 | <a class="code" href="dtn__ipc_8c.html#94b0ae7635a33f63da245751f707a69b">dtnipc_version</a>); <a name="l00160"></a>00160 ret = write(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, &handshake, <span class="keyword">sizeof</span>(handshake)); <a name="l00161"></a>00161 <span class="keywordflow">if</span> (ret != <span class="keyword">sizeof</span>(handshake)) { <a name="l00162"></a>00162 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00163"></a>00163 fprintf(stderr, <span class="stringliteral">"dtn_ipc: handshake error\n"</span>); <a name="l00164"></a>00164 } <a name="l00165"></a>00165 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00166"></a>00166 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00167"></a>00167 <span class="keywordflow">return</span> -1; <a name="l00168"></a>00168 } <a name="l00169"></a>00169 handle-><a class="code" href="structdtnipc__handle.html#48ca99de45786eacf7d90206e66f1066" title="Counter for debugging.">total_sent</a> += ret; <a name="l00170"></a>00170 <a name="l00171"></a>00171 <span class="comment">// wait for the handshake response</span> <a name="l00172"></a>00172 handshake = 0; <a name="l00173"></a>00173 ret = read(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, &handshake, <span class="keyword">sizeof</span>(handshake)); <a name="l00174"></a>00174 <span class="keywordflow">if</span> (ret != <span class="keyword">sizeof</span>(handshake) || (ntohl(handshake) >> 16) != <a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d4e9e92913a117e7b6c92b2dee3eab867">DTN_OPEN</a>) { <a name="l00175"></a>00175 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00176"></a>00176 fprintf(stderr, <span class="stringliteral">"dtn_ipc: handshake error\n"</span>); <a name="l00177"></a>00177 } <a name="l00178"></a>00178 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00179"></a>00179 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00180"></a>00180 <span class="keywordflow">return</span> -1; <a name="l00181"></a>00181 } <a name="l00182"></a>00182 <a name="l00183"></a>00183 handle-><a class="code" href="structdtnipc__handle.html#d55013a175ceb1570d5fe30062098bb7" title="Counter for debugging.">total_rcvd</a> += ret; <a name="l00184"></a>00184 <a name="l00185"></a>00185 remote_version = (ntohl(handshake) & 0x0ffff); <a name="l00186"></a>00186 <span class="keywordflow">if</span> (remote_version != <a class="code" href="dtn__ipc_8c.html#94b0ae7635a33f63da245751f707a69b">dtnipc_version</a>) { <a name="l00187"></a>00187 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00188"></a>00188 fprintf(stderr, <span class="stringliteral">"dtn_ipc: version mismatch\n"</span>); <a name="l00189"></a>00189 } <a name="l00190"></a>00190 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00191"></a>00191 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#6841c2297bba5c4fd8b69ac2e4b1975d">DTN_EVERSION</a>; <a name="l00192"></a>00192 <span class="keywordflow">return</span> -1; <a name="l00193"></a>00193 } <a name="l00194"></a>00194 <a name="l00195"></a>00195 <span class="keywordflow">return</span> 0; <a name="l00196"></a>00196 } <a name="l00197"></a>00197 <a name="l00198"></a>00198 <span class="comment">/*</span> <a name="l00199"></a>00199 <span class="comment"> * Clean up the handle. dtnipc_open must have already been called on</span> <a name="l00200"></a>00200 <span class="comment"> * the handle.</span> <a name="l00201"></a>00201 <span class="comment"> */</span> <a name="l00202"></a>00202 <span class="keywordtype">int</span> <a name="l00203"></a><a class="code" href="dtn__ipc_8h.html#ca3de731f1395d03269a608a915d7539">00203</a> <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>* <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>) <a name="l00204"></a>00204 { <a name="l00205"></a>00205 <span class="keywordtype">int</span> ret; <a name="l00206"></a>00206 <a name="l00207"></a>00207 <span class="comment">// first send a close over RPC</span> <a name="l00208"></a>00208 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> != <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>) { <a name="l00209"></a>00209 ret = <a class="code" href="dtn__ipc_8c.html#2e471ca046cc85566e16b9f60ef9937e" title="Send a message and wait for a response over the dtn ipc protocol.">dtnipc_send_recv</a>(handle, <a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d88988342732cf7736a9a17338475582e">DTN_CLOSE</a>); <a name="l00210"></a>00210 } <span class="keywordflow">else</span> { <a name="l00211"></a>00211 ret = -1; <a name="l00212"></a>00212 } <a name="l00213"></a>00213 <a name="l00214"></a>00214 xdr_destroy(&handle-><a class="code" href="structdtnipc__handle.html#ded5c2335049e780a4ee8ab2b2356f95" title="XDR encoder.">xdr_encode</a>); <a name="l00215"></a>00215 xdr_destroy(&handle-><a class="code" href="structdtnipc__handle.html#c09f26354224257a07c51d45f92725fd" title="XDR decoder.">xdr_decode</a>); <a name="l00216"></a>00216 <a name="l00217"></a>00217 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a> > 0) { <a name="l00218"></a>00218 close(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>); <a name="l00219"></a>00219 } <a name="l00220"></a>00220 <a name="l00221"></a>00221 handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a> = 0; <a name="l00222"></a>00222 <a name="l00223"></a>00223 <span class="keywordflow">return</span> ret; <a name="l00224"></a>00224 } <a name="l00225"></a>00225 <a name="l00226"></a>00226 <a name="l00227"></a>00227 <span class="comment">/*</span> <a name="l00228"></a>00228 <span class="comment"> * Send a message over the dtn ipc protocol.</span> <a name="l00229"></a>00229 <span class="comment"> *</span> <a name="l00230"></a>00230 <span class="comment"> * Returns 0 on success, -1 on error.</span> <a name="l00231"></a>00231 <span class="comment"> */</span> <a name="l00232"></a>00232 <span class="keywordtype">int</span> <a name="l00233"></a><a class="code" href="dtn__ipc_8h.html#0c513c4c8a2438b359d7b53cec93e491">00233</a> <a class="code" href="dtn__ipc_8c.html#0c513c4c8a2438b359d7b53cec93e491">dtnipc_send</a>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>* <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>, <a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d" title="Type codes for api messages.">dtnapi_message_type_t</a> type) <a name="l00234"></a>00234 { <a name="l00235"></a>00235 <span class="keywordtype">int</span> ret; <a name="l00236"></a>00236 u_int32_t <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>, msglen, origlen; <a name="l00237"></a>00237 <a name="l00238"></a>00238 <span class="comment">// pack the message code in the fourth byte of the buffer and the</span> <a name="l00239"></a>00239 <span class="comment">// message length into the next four. we don't use xdr routines</span> <a name="l00240"></a>00240 <span class="comment">// for these since we need to be able to decode the length on the</span> <a name="l00241"></a>00241 <span class="comment">// other side to make sure we've read the whole message, and we</span> <a name="l00242"></a>00242 <span class="comment">// need the type to know which xdr decoder to call</span> <a name="l00243"></a>00243 handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>[3] = type; <a name="l00244"></a>00244 <a name="l00245"></a>00245 len = xdr_getpos(&handle-><a class="code" href="structdtnipc__handle.html#ded5c2335049e780a4ee8ab2b2356f95" title="XDR encoder.">xdr_encode</a>); <a name="l00246"></a>00246 msglen = len + 5; <a name="l00247"></a>00247 len = htonl(len); <a name="l00248"></a>00248 memcpy(&handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>[4], &len, <span class="keyword">sizeof</span>(len)); <a name="l00249"></a>00249 <a name="l00250"></a>00250 <span class="comment">// reset the xdr encoder</span> <a name="l00251"></a>00251 xdr_setpos(&handle-><a class="code" href="structdtnipc__handle.html#ded5c2335049e780a4ee8ab2b2356f95" title="XDR encoder.">xdr_encode</a>, 0); <a name="l00252"></a>00252 <a name="l00253"></a>00253 <span class="comment">// send the message, looping until it's all sent</span> <a name="l00254"></a>00254 origlen = msglen; <a name="l00255"></a>00255 <span class="keywordtype">char</span>* bp = &handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>[3]; <a name="l00256"></a>00256 <span class="keywordflow">do</span> { <a name="l00257"></a>00257 ret = write(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, bp, msglen); <a name="l00258"></a>00258 handle-><a class="code" href="structdtnipc__handle.html#48ca99de45786eacf7d90206e66f1066" title="Counter for debugging.">total_sent</a> += ret; <a name="l00259"></a>00259 <a name="l00260"></a>00260 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00261"></a>00261 fprintf(stderr, <span class="stringliteral">"dtn_ipc: send(%s) wrote %d/%d bytes (%s) "</span> <a name="l00262"></a>00262 <span class="stringliteral">"(total sent/rcvd %u/%u)\n"</span>, <a name="l00263"></a>00263 <a class="code" href="dtn__ipc_8c.html#c5707d2923063f6fe2bde512f525f72b" title="Type code to string conversion routine.">dtnipc_msgtoa</a>(type), ret, origlen, <a name="l00264"></a>00264 ret == -1 ? strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>) : <span class="stringliteral">"success"</span>, <a name="l00265"></a>00265 handle-><a class="code" href="structdtnipc__handle.html#48ca99de45786eacf7d90206e66f1066" title="Counter for debugging.">total_sent</a>, handle-><a class="code" href="structdtnipc__handle.html#d55013a175ceb1570d5fe30062098bb7" title="Counter for debugging.">total_rcvd</a>); <a name="l00266"></a>00266 } <a name="l00267"></a>00267 <a name="l00268"></a>00268 <span class="keywordflow">if</span> (ret <= 0) { <a name="l00269"></a>00269 <span class="keywordflow">if</span> (<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a> == EINTR) <a name="l00270"></a>00270 <span class="keywordflow">continue</span>; <a name="l00271"></a>00271 <a name="l00272"></a>00272 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00273"></a>00273 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00274"></a>00274 <span class="keywordflow">return</span> -1; <a name="l00275"></a>00275 } <a name="l00276"></a>00276 <a name="l00277"></a>00277 bp += ret; <a name="l00278"></a>00278 msglen -= ret; <a name="l00279"></a>00279 <a name="l00280"></a>00280 } <span class="keywordflow">while</span> (msglen > 0); <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="keywordflow">return</span> 0; <a name="l00283"></a>00283 } <a name="l00284"></a>00284 <a name="l00285"></a>00285 <span class="comment">/*</span> <a name="l00286"></a>00286 <span class="comment"> * Receive a message on the ipc channel. May block if there is no</span> <a name="l00287"></a>00287 <span class="comment"> * pending message.</span> <a name="l00288"></a>00288 <span class="comment"> *</span> <a name="l00289"></a>00289 <span class="comment"> * Sets status to the server-returned status code and returns the</span> <a name="l00290"></a>00290 <span class="comment"> * length of any reply message on success, returns -1 on internal</span> <a name="l00291"></a>00291 <span class="comment"> * error.</span> <a name="l00292"></a>00292 <span class="comment"> */</span> <a name="l00293"></a>00293 <span class="keywordtype">int</span> <a name="l00294"></a><a class="code" href="dtn__ipc_8h.html#92414569c9a91e622d91bfcb8d711045">00294</a> <a class="code" href="dtn__ipc_8c.html#92414569c9a91e622d91bfcb8d711045">dtnipc_recv</a>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>* <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>, <span class="keywordtype">int</span>* status) <a name="l00295"></a>00295 { <a name="l00296"></a>00296 <span class="keywordtype">int</span> ret; <a name="l00297"></a>00297 u_int32_t <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>, nread; <a name="l00298"></a>00298 u_int32_t statuscode; <a name="l00299"></a>00299 <a name="l00300"></a>00300 <span class="comment">// reset the xdr decoder before reading in any data</span> <a name="l00301"></a>00301 xdr_setpos(&handle-><a class="code" href="structdtnipc__handle.html#c09f26354224257a07c51d45f92725fd" title="XDR decoder.">xdr_decode</a>, 0); <a name="l00302"></a>00302 <a name="l00303"></a>00303 <span class="comment">// read the status code and length</span> <a name="l00304"></a>00304 ret = read(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>, 8); <a name="l00305"></a>00305 handle-><a class="code" href="structdtnipc__handle.html#d55013a175ceb1570d5fe30062098bb7" title="Counter for debugging.">total_rcvd</a> += ret; <a name="l00306"></a>00306 <a name="l00307"></a>00307 <span class="comment">// make sure we got it all</span> <a name="l00308"></a>00308 <span class="keywordflow">if</span> (ret != 8) { <a name="l00309"></a>00309 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00310"></a>00310 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00311"></a>00311 <span class="keywordflow">return</span> -1; <a name="l00312"></a>00312 } <a name="l00313"></a>00313 <a name="l00314"></a>00314 memcpy(&statuscode, handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>, <span class="keyword">sizeof</span>(statuscode)); <a name="l00315"></a>00315 statuscode = ntohl(statuscode); <a name="l00316"></a>00316 *status = statuscode; <a name="l00317"></a>00317 <a name="l00318"></a>00318 memcpy(&len, &handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>[4], <span class="keyword">sizeof</span>(len)); <a name="l00319"></a>00319 len = ntohl(len); <a name="l00320"></a>00320 <a name="l00321"></a>00321 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00322"></a>00322 fprintf(stderr, <span class="stringliteral">"dtn_ipc: recv() read %d/8 bytes for status (%s): "</span> <a name="l00323"></a>00323 <span class="stringliteral">"status %d len %d (total sent/rcvd %u/%u)\n"</span>, <a name="l00324"></a>00324 ret, ret == -1 ? strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>) : <span class="stringliteral">"success"</span>, <a name="l00325"></a>00325 *status, len, handle-><a class="code" href="structdtnipc__handle.html#48ca99de45786eacf7d90206e66f1066" title="Counter for debugging.">total_sent</a>, handle-><a class="code" href="structdtnipc__handle.html#d55013a175ceb1570d5fe30062098bb7" title="Counter for debugging.">total_rcvd</a>); <a name="l00326"></a>00326 } <a name="l00327"></a>00327 <a name="l00328"></a>00328 <span class="comment">// read the rest of the message </span> <a name="l00329"></a>00329 nread = 8; <a name="l00330"></a>00330 <span class="keywordflow">while</span> (nread < len + 8) { <a name="l00331"></a>00331 ret = read(handle-><a class="code" href="structdtnipc__handle.html#d7e67a3808764e8217255940427204bc" title="Socket file descriptor.">sock</a>, <a name="l00332"></a>00332 &handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>[nread], <span class="keyword">sizeof</span>(handle-><a class="code" href="structdtnipc__handle.html#21348da7414b669f8d78863e8a8e684e" title="send/recv buffer">buf</a>) - nread); <a name="l00333"></a>00333 handle-><a class="code" href="structdtnipc__handle.html#d55013a175ceb1570d5fe30062098bb7" title="Counter for debugging.">total_rcvd</a> += ret; <a name="l00334"></a>00334 <a name="l00335"></a>00335 <span class="keywordflow">if</span> (handle-><a class="code" href="structdtnipc__handle.html#810e16c65902b0a074c2790f0a20c8f4" title="Debug flag for channel.">debug</a>) { <a name="l00336"></a>00336 fprintf(stderr, <span class="stringliteral">"dtn_ipc: recv() read %d/%d bytes (%s)\n"</span>, <a name="l00337"></a>00337 ret, len, ret == -1 ? strerror(<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a>) : <span class="stringliteral">"success"</span>); <a name="l00338"></a>00338 } <a name="l00339"></a>00339 <a name="l00340"></a>00340 <span class="keywordflow">if</span> (ret <= 0) { <a name="l00341"></a>00341 <span class="keywordflow">if</span> (<a class="code" href="IPDiscovery_8cc.html#d65a8842cc674e3ddf69355898c0ecbf">errno</a> == EINTR) <a name="l00342"></a>00342 <span class="keywordflow">continue</span>; <a name="l00343"></a>00343 <a name="l00344"></a>00344 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = <a class="code" href="dtn__errno_8h.html#b4ae5bb2a6256b2dac33962e688584de">DTN_ECOMM</a>; <a name="l00345"></a>00345 <a class="code" href="dtn__ipc_8c.html#ca3de731f1395d03269a608a915d7539">dtnipc_close</a>(handle); <a name="l00346"></a>00346 <span class="keywordflow">return</span> -1; <a name="l00347"></a>00347 } <a name="l00348"></a>00348 <a name="l00349"></a>00349 nread += ret; <a name="l00350"></a>00350 } <a name="l00351"></a>00351 <a name="l00352"></a>00352 <span class="keywordflow">return</span> len; <a name="l00353"></a>00353 } <a name="l00354"></a>00354 <a name="l00355"></a>00355 <a name="l00361"></a><a class="code" href="dtn__ipc_8h.html#2e471ca046cc85566e16b9f60ef9937e">00361</a> <span class="keywordtype">int</span> <a class="code" href="dtn__ipc_8c.html#2e471ca046cc85566e16b9f60ef9937e" title="Send a message and wait for a response over the dtn ipc protocol.">dtnipc_send_recv</a>(<a class="code" href="structdtnipc__handle.html" title="State of a DTN IPC channel.">dtnipc_handle_t</a>* <a class="code" href="dtncat_8c.html#01daea96255bb21a8979b3b45f5bbb70">handle</a>, <a class="code" href="dtn__ipc_8h.html#c05301bf9560764f5e97fba507ef9e5d" title="Type codes for api messages.">dtnapi_message_type_t</a> type) <a name="l00362"></a>00362 { <a name="l00363"></a>00363 <span class="keywordtype">int</span> status; <a name="l00364"></a>00364 <a name="l00365"></a>00365 <span class="comment">// send the message</span> <a name="l00366"></a>00366 <span class="keywordflow">if</span> (<a class="code" href="dtn__ipc_8c.html#0c513c4c8a2438b359d7b53cec93e491">dtnipc_send</a>(handle, type) < 0) { <a name="l00367"></a>00367 <span class="keywordflow">return</span> -1; <a name="l00368"></a>00368 } <a name="l00369"></a>00369 <a name="l00370"></a>00370 <span class="comment">// wait for a response</span> <a name="l00371"></a>00371 <span class="keywordflow">if</span> (<a class="code" href="dtn__ipc_8c.html#92414569c9a91e622d91bfcb8d711045">dtnipc_recv</a>(handle, &status) < 0) { <a name="l00372"></a>00372 <span class="keywordflow">return</span> -1; <a name="l00373"></a>00373 } <a name="l00374"></a>00374 <a name="l00375"></a>00375 <span class="comment">// handle server-side errors</span> <a name="l00376"></a>00376 <span class="keywordflow">if</span> (status != <a class="code" href="dtn__errno_8h.html#3723251cb5d056b6ace3e22561da1e11" title="DTN API error codes.">DTN_SUCCESS</a>) { <a name="l00377"></a>00377 handle-><a class="code" href="structdtnipc__handle.html#b76f241bd49850e48c051fde495d0a78" title="Error code.">err</a> = status; <a name="l00378"></a>00378 <span class="keywordflow">return</span> -1; <a name="l00379"></a>00379 } <a name="l00380"></a>00380 <a name="l00381"></a>00381 <span class="keywordflow">return</span> 0; <a name="l00382"></a>00382 } </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Mon Jul 21 14:09:45 2008 for DTN Reference Implementation by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> </body> </html>