00001 // 00002 // The UDSSocketAddress class - holds the address of a UNIX Domain Socket 00003 // 00004 // ***** BEGIN LICENSE BLOCK ***** 00005 // Version: MPL 1.1/GPL 2.0/LGPL 2.1 00006 // 00007 // The contents of this file are subject to the Mozilla Public License 00008 // Version 1.1 (the "License"); you may not use this file except in 00009 // compliance with the License. You may obtain a copy of the License at 00010 // http://www.mozilla.org/MPL/ 00011 // 00012 // Software distributed under the License is distributed on an "AS IS" 00013 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00014 // the License for the specific language governing rights and limitations 00015 // under the License. 00016 // 00017 // The Original Code is the consmgr network/serial-line monitoring package. 00018 // 00019 // The Initial Developer of the Original Code is Chris P. Ross. 00020 // Portions created by the Initial Developer are Copyright (C) 2000-2008 00021 // the Initial Developer. All Rights Reserved. 00022 // 00023 // Contributor(s): 00024 // Geoff Adams 00025 // 00026 // Alternatively, the contents of this file may be used under the terms of 00027 // either the GNU General Public License Version 2 or later (the "GPL"), or 00028 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00029 // in which case the provisions of the GPL or the LGPL are applicable instead 00030 // of those above. If you wish to allow use of your version of this file only 00031 // under the terms of either the GPL or the LGPL, and not to allow others to 00032 // use your version of this file under the terms of the MPL, indicate your 00033 // decision by deleting the provisions above and replace them with the notice 00034 // and other provisions required by the GPL or the LGPL. If you do not delete 00035 // the provisions above, a recipient may use your version of this file under 00036 // the terms of any one of the MPL, the GPL or the LGPL. 00037 // 00038 // ***** END LICENSE BLOCK ***** 00039 00040 extern "C" { 00041 #include <unistd.h> 00042 #include <sys/stat.h> 00043 #include <sys/socket.h> 00044 #include <sys/un.h> 00045 } 00046 00047 #include <sstream> 00048 00049 #include "udssockaddr.h" 00050 #include "exceptions.h" 00051 00052 UDSSocketAddress::UDSSocketAddress(const std::string &path) 00053 : SocketAddress() 00054 { 00055 // addrlen is always just the size of the structure 00056 sock_addrlen = sizeof(struct sockaddr_un); 00057 00058 // Build a sockaddr and pass it to SocketAddress::set_addr() 00059 set_addr(path); 00060 } 00061 00062 UDSSocketAddress::~UDSSocketAddress() 00063 { 00064 // The destructor for SocketConnector will do all we need... 00065 } 00066 00067 00068 // 00069 // Set/initialize functions... 00070 // 00071 00074 void 00075 UDSSocketAddress::set_addr(const std::string &path) 00076 { 00077 size_t len = path.length() + 1; 00078 struct sockaddr_un s; 00079 00080 if (len > sizeof(s.sun_path)) 00081 throw(fireball(27, "Ack. set_addr called with too long of a path!")); 00082 00083 s.sun_family = AF_LOCAL; 00084 #ifndef NO_SA_LEN 00085 s.sun_len = (sizeof(s) - sizeof(s.sun_path)) + path.length(); 00086 #endif 00087 memmove(s.sun_path, path.c_str(), len); 00088 00090 SocketAddress::set_addr(reinterpret_cast<sockaddr*>(&s)); 00091 } 00092 00093 void 00094 UDSSocketAddress::unlink() const 00095 { 00096 ::unlink(reinterpret_cast<const struct sockaddr_un*>(get_addr())->sun_path); 00097 } 00098 00099 // 00100 // Generate a string to tell a human who we are... 00101 // 00102 00103 std::string 00104 UDSSocketAddress::get_id() const 00105 { 00106 std::ostringstream id_str; 00107 00108 #if 1 00109 sockaddr_un *addr_un = (sockaddr_un *)get_addr(); 00110 00111 id_str << "UDSSocketAddress[" << addr_un->sun_path << "]"; 00112 #else 00113 id_str << "UDSSocketAddress"; 00114 #endif 00115 00116 return(id_str.str()); 00117 }