UDPTunnel.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2006 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <oasys/io/NetUtils.h>
00040 
00041 #include "DTNTunnel.h"
00042 #include "UDPTunnel.h"
00043 
00044 namespace dtntunnel {
00045 
00046 //----------------------------------------------------------------------
00047 UDPTunnel::UDPTunnel()
00048     : IPTunnel("UDPTunnel", "/dtntunnel/udp"),
00049       sock_("/dtntunnel/udp/sock")
00050 {
00051     sock_.init_socket();
00052 }
00053 
00054 //----------------------------------------------------------------------
00055 void
00056 UDPTunnel::add_listener(in_addr_t listen_addr, u_int16_t listen_port,
00057                         in_addr_t remote_addr, u_int16_t remote_port)
00058 {
00059     // the object will delete itself when the thread exits
00060     new Listener(listen_addr, listen_port, remote_addr, remote_port);
00061 }
00062     
00063 //----------------------------------------------------------------------
00064 void
00065 UDPTunnel::handle_bundle(dtn::APIBundle* bundle)
00066 {
00067     DTNTunnel::BundleHeader hdr;
00068     memcpy(&hdr, bundle->payload_.buf(), sizeof(hdr));
00069     hdr.remote_port_ = htons(hdr.remote_port_);
00070 
00071     char* bp = bundle->payload_.buf() + sizeof(hdr);
00072     int  len = bundle->payload_.len() - sizeof(hdr);
00073     
00074     int cc = sock_.sendto(bp, len,
00075                            0, hdr.remote_addr_, hdr.remote_port_);
00076     if (cc != len) {
00077         log_err("error sending packet to %s:%d: %s",
00078                 intoa(hdr.remote_addr_), hdr.remote_port_, strerror(errno));
00079     } else {
00080         log_info("sent %zu byte packet to %s:%d",
00081                  bundle->payload_.len() - sizeof(hdr),
00082                  intoa(hdr.remote_addr_), hdr.remote_port_);
00083     }
00084 
00085     delete bundle;
00086 }
00087 
00088 //----------------------------------------------------------------------
00089 UDPTunnel::Listener::Listener(in_addr_t listen_addr, u_int16_t listen_port,
00090                               in_addr_t remote_addr, u_int16_t remote_port)
00091     : Thread("UDPTunnel::Listener", DELETE_ON_EXIT),
00092       Logger("UDPTunnel::Listener", "/dtntunnel/udp/listener"), 
00093       sock_("/dtntunnel/udp/listener/sock"),
00094       listen_addr_(listen_addr),
00095       listen_port_(listen_port),
00096       remote_addr_(remote_addr),
00097       remote_port_(remote_port)
00098 {
00099     start();
00100 }
00101 
00102 //----------------------------------------------------------------------
00103 void
00104 UDPTunnel::Listener::run()
00105 {
00106     DTNTunnel* tunnel = DTNTunnel::instance();
00107     int ret = sock_.bind(listen_addr_, listen_port_);
00108     if (ret != 0) {
00109         log_err("can't bind to %s:%u", intoa(listen_addr_), listen_port_);
00110         return; // die
00111     }
00112 
00113     DTNTunnel::BundleHeader hdr;
00114     hdr.protocol_    = IPPROTO_UDP;
00115     hdr.seqno_       = 0;
00116     hdr.client_addr_ = listen_addr_;
00117     hdr.client_port_ = htons(listen_port_);
00118     hdr.remote_addr_ = remote_addr_;
00119     hdr.remote_port_ = htons(remote_port_);
00120     
00121     while (1) {
00122         int len = sock_.recv(recv_buf_, sizeof(recv_buf_), 0);
00123         if (len <= 0) {
00124             log_err("error reading from socket: %s", strerror(errno));
00125             return; // die
00126         }
00127         
00128         log_info("got %d byte packet", len);
00129 
00130         dtn::APIBundle* b = new dtn::APIBundle();
00131         char* bp = b->payload_.buf(sizeof(hdr) + len);
00132         memcpy(bp, &hdr, sizeof(hdr));
00133         memcpy(bp + sizeof(hdr), recv_buf_, len);
00134         b->payload_.set_len(sizeof(hdr) + len);
00135         
00136         if (tunnel->send_bundle(b, tunnel->dest_eid()) != DTN_SUCCESS)
00137             exit(1);
00138     }
00139 }
00140 
00141 } // namespace dtntunnel

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1