00001 // 00002 // The SocketAddress class - Holds a socket (unix domain or network) address 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 <string.h> 00043 #include <sys/types.h> 00044 #include <sys/socket.h> 00045 #include <sys/un.h> 00046 #include <netinet/in.h> 00047 } 00048 00049 #include <iostream> 00050 00051 #include "consmgr.h" 00052 #include "sockaddr.h" 00053 #include "guard.h" 00054 00055 SocketAddress::SocketAddress() 00056 { 00057 memset(&addr, 0, sizeof(addr)); 00058 } 00059 00060 SocketAddress::SocketAddress(const struct sockaddr *initaddr) 00061 { 00062 memset(&addr, 0, sizeof(addr)); 00063 set_addr(initaddr); 00064 } 00065 00066 SocketAddress::~SocketAddress() 00067 { 00068 } 00069 00070 // 00071 // Set/initialize functions... 00072 // 00073 00074 void 00075 SocketAddress::set_addr(const struct sockaddr *val) 00076 { 00077 // Parameter checking 00078 if (val == NULL) { 00079 // Throw an actual InvalidParameterError, or some such exception. 00080 throw "Parameter error: Someone called set_addr(NULL)!"; 00081 } 00082 00083 #ifdef NO_SA_LEN 00084 sock_addrlen = build_addrlen(val); 00085 #else 00086 sock_addrlen = val->sa_len; 00087 #endif /* NO_SA_LEN */ 00088 00089 memcpy(&addr, val, sock_addrlen); 00090 } 00091 00092 // 00093 // Inquiry functions 00094 // 00095 00096 // Return a pointer to the sockaddr we're using... 00097 const sockaddr * 00098 SocketAddress::get_addr() const 00099 { 00100 return (const sockaddr*)&addr; 00101 } 00102 00103 // How big is this particular sockaddr? 00104 socklen_t 00105 SocketAddress::length() const 00106 { 00107 return sock_addrlen; 00108 } 00109 00110 // What socket family is this sockaddr? 00111 int 00112 SocketAddress::family() const 00113 { 00114 return addr.ss_family; 00115 } 00116