BSDSocketProtocol.cc

Go to the documentation of this file.
00001 // $Id: BSDSocketProtocol.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "BSDSocketProtocol.hh"
00027 //#include "BSDSocketProtocol.ih"
00028 
00029 // Custom includes
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <sys/ioctl.h>
00033 
00034 //#include "BSDSocketProtocol.mpp"
00035 #define prefix_
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 
00038 prefix_ std::pair<bool,unsigned> senf::BSDSocketProtocol::linger()
00039     const
00040 {
00041     struct linger ling;
00042     socklen_t len = sizeof(ling);
00043     ::memset(&ling, 0, sizeof(ling));
00044     if (::getsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
00045         SENF_THROW_SYSTEM_EXCEPTION("");
00046     return std::make_pair(ling.l_onoff, ling.l_linger);
00047 }
00048 
00049 prefix_ void senf::BSDSocketProtocol::linger(bool enable, unsigned timeout)
00050     const
00051 {
00052     struct linger ling;
00053     ling.l_onoff = enable;
00054     ling.l_linger = timeout;
00055     if (::setsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
00056         SENF_THROW_SYSTEM_EXCEPTION("");
00057 }
00058 
00059 prefix_ boost::uint8_t senf::BSDSocketProtocol::priority()
00060     const
00061 {
00062     int value;
00063     socklen_t len (sizeof(value));
00064     if (::getsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&value,&len) < 0)
00065         SENF_THROW_SYSTEM_EXCEPTION("");
00066     return value;
00067 }
00068 
00069 prefix_ void senf::BSDSocketProtocol::priority(boost::uint8_t value)
00070     const
00071 {
00072     int ivalue (value);
00073     if (::setsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&ivalue,sizeof(ivalue)) < 0)
00074         SENF_THROW_SYSTEM_EXCEPTION("");
00075 }
00076 
00077 prefix_ int senf::BSDSocketProtocol::error()
00078     const
00079 {
00080     int err;
00081     socklen_t len (sizeof(err));
00082     if (::getsockopt(fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
00083         SENF_THROW_SYSTEM_EXCEPTION("");
00084     return err;
00085 }
00086 
00087 prefix_ unsigned senf::BSDSocketProtocol::rcvbuf()
00088     const
00089 {
00090     unsigned size;
00091     socklen_t len (sizeof(size));
00092     if (::getsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,&len) < 0)
00093         SENF_THROW_SYSTEM_EXCEPTION("");
00094     // Linux doubles the bufer size on setting the RCVBUF to cater for internal
00095     // headers. We fix this up here .. (see lkml FAQ)
00096     return size/2;
00097 }
00098 
00099 prefix_ void senf::BSDSocketProtocol::rcvbuf(unsigned size)
00100     const
00101 {
00102     if (::setsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) < 0)
00103         SENF_THROW_SYSTEM_EXCEPTION("");
00104 }
00105 
00106 prefix_ unsigned senf::BSDSocketProtocol::sndbuf()
00107     const
00108 {
00109     unsigned size;
00110     socklen_t len (sizeof(size));
00111     if (::getsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,&len) < 0)
00112         SENF_THROW_SYSTEM_EXCEPTION("");
00113     // Linux doubles the bufer size on setting the SNDBUF to cater for internal
00114     // headers. We fix this up here .. (see lkml FAQ)
00115     return size/2;
00116 }
00117 
00118 prefix_ void senf::BSDSocketProtocol::sndbuf(unsigned size)
00119     const
00120 {
00121     if (::setsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,sizeof(size)) < 0)
00122         SENF_THROW_SYSTEM_EXCEPTION("");
00123 }
00124 
00125 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00126 
00127 prefix_ bool senf::AddressableBSDSocketProtocol::reuseaddr()
00128     const
00129 {
00130     int value;
00131     socklen_t len (sizeof(value));
00132     if (::getsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
00133         SENF_THROW_SYSTEM_EXCEPTION("");
00134     return value;
00135 }
00136 
00137 prefix_ void senf::AddressableBSDSocketProtocol::reuseaddr(bool value)
00138     const
00139 {
00140     int ivalue (value);
00141     if (::setsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
00142         SENF_THROW_SYSTEM_EXCEPTION("");
00143 }
00144 
00145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00146 #undef prefix_
00147 //#include "BSDSocketProtocol.mpp"
00148 
00149 
00150 // Local Variables:
00151 // mode: c++
00152 // fill-column: 100
00153 // c-file-style: "senf"
00154 // indent-tabs-mode: nil
00155 // ispell-local-dictionary: "american"
00156 // compile-command: "scons -u test"
00157 // comment-column: 40
00158 // End: