#include <InetAddrBSD.h>
Inheritance diagram for vpr::InetAddrBSD:


Public Types | |
| typedef vpr::InetAddrHash | hash |
Public Methods | |
| InetAddrBSD () | |
| Default constructor. More... | |
| InetAddrBSD (const InetAddrBSD &addr) | |
| Copy constructor. More... | |
| vpr::ReturnStatus | setAddress (const std::string &address) |
| Constructs an address object using the given address. More... | |
| vpr::ReturnStatus | setAddress (const std::string &address, const Uint16 port) |
| Sets the address for this object using the given address and port number. More... | |
| vpr::ReturnStatus | setAddress (const vpr::Uint32 address, const vpr::Uint16 port) |
| Sets the address for this object using the given address and port number. More... | |
| unsigned char | getLength () const |
| Gets the length of the address structure (if supported by the host OS). More... | |
| void | setLength (const unsigned char length) |
| Sets the length of the address structure (if the host OS allows such an operation). More... | |
| vpr::SocketTypes::Domain | getFamily () const |
| Gets the protocol family of this address structure. More... | |
| void | setFamily (const vpr::SocketTypes::Domain family) |
| Sets the protocol family of this address structure. More... | |
| vpr::Uint16 | getPort () const |
| Gets this address' port in host byte order. More... | |
| void | setPort (const vpr::Uint16 port) |
| Sets this address' port. More... | |
| vpr::Uint32 | getAddressValue () const |
| Gets this address's Internet address in host byte order. More... | |
| std::string | getAddressString () const |
| Get the IP address associated with this structure as a human-readable string. More... | |
| std::string | getHostname () const |
| Returns the fully qualified hostname for this address. More... | |
| std::vector< std::string > | getHostnames () const |
| Returns the fully qualified primary hostname for this address and all known aliases. More... | |
| InetAddrBSD & | operator= (const InetAddrBSD &addr) |
| Overloaded assignment operator to ensure that assignments work correctly. More... | |
| bool | operator== (const InetAddrBSD &addr) const |
| Overloaded equality operator. More... | |
| bool | operator!= (const InetAddrBSD &addr) const |
| Overloaded inequality operator. More... | |
Static Public Methods | |
| vpr::ReturnStatus | getLocalHost (vpr::InetAddrBSD &host_addr) |
| Returns the local host's address via the given object reference. More... | |
Static Public Attributes | |
| const InetAddrBSD | AnyAddr |
Protected Methods | |
| InetAddrBSD (const struct sockaddr *addr) | |
| Constructs a vpr::InetAddrBSD object from a pointer to a sockaddr struct. More... | |
| void | copyAddressValue (const char *addr_value) |
| Copies the given array of bytes (an A record) into this structure's IP address value. More... | |
| void | setAddressValue (const vpr::Uint32 addr_value) |
| Sets this objects's IP address. More... | |
| size_t | size () const |
| Gets the size of this object's encapsulated address structure. More... | |
| size_t | addressSize () const |
| Gets the size of this object's IP address value. More... | |
| void | setSockaddr (const struct sockaddr *addr) |
| Initializes the internal socket address structure using the given sockaddr* object. More... | |
| sockaddr_in | toSockaddrInet () |
| Converts this object to a sockaddr_in struct. More... | |
| void | copy (const InetAddrBSD &addr) |
| Makes a copy of the given vpr::InetAddrBSD object in this object. More... | |
| vpr::ReturnStatus | lookupAddress (const std::string &addr) |
| Look up the given address and store the address in mAddr. More... | |
Protected Attributes | |
| sockaddr_in | mAddr |
| The Ineternet address structure. More... | |
Friends | |
| class | vpr::SocketImplBSD |
| class | vpr::SocketDatagramImplBSD |
| class | vpr::SocketStreamImplBSD |
Definition at line 68 of file InetAddrBSD.h.
|
|
Definition at line 73 of file InetAddrBSD.h. |
|
|
Default constructor. This initializes the memory for the encapsulated address structure.
Definition at line 67 of file InetAddrBSD.cpp. References mAddr, setAddressValue, setFamily, and setPort. Referenced by InetAddrBSD, operator!=, and operator=.
00068 {
00069 memset(&mAddr, 0, sizeof(mAddr));
00070 setAddressValue(INADDR_ANY);
00071 setPort(0);
00072 setFamily(SocketTypes::INET);
00073 }
|
|
|
Copy constructor.
Definition at line 94 of file InetAddrBSD.h. References copy, and InetAddrBSD.
00095 {
00096 copy(addr);
00097 }
|
|
|
Constructs a vpr::InetAddrBSD object from a pointer to a sockaddr struct. This is not typically useful to users but may be very important internally.
Definition at line 296 of file InetAddrBSD.h. References InetAddrBSD, and setSockaddr.
00297 {
00298 setSockaddr(addr);
00299 }
|
|
|
Returns the local host's address via the given object reference.
Definition at line 75 of file InetAddrBSD.cpp. References vpr::ReturnStatus::Fail, setAddress, and vpr::ReturnStatus::setCode.
00076 {
00077 char local_host_name[MAXHOSTNAMELEN + 1];
00078 vpr::ReturnStatus status;
00079
00080 if ( gethostname(local_host_name, MAXHOSTNAMELEN) == 0 )
00081 {
00082 host_addr.setAddress(std::string(local_host_name), 0);
00083 }
00084 else
00085 {
00086 status.setCode(vpr::ReturnStatus::Fail);
00087 }
00088
00089 return status;
00090 }
|
|
|
Constructs an address object using the given address. It must be of the form <address>:<port> where <address> can be a hostname or a dotted-decimal IP address.
Reimplemented from vpr::InetAddrBase. Definition at line 97 of file InetAddrBSD.cpp. References lookupAddress, setFamily, and setPort. Referenced by getLocalHost.
00098 {
00099 std::string::size_type pos;
00100 std::string host_addr, host_port;
00101 vpr::Uint16 port;
00102 vpr::ReturnStatus retval;
00103
00104 // Extract the address and the port number from the given string.
00105 pos = address.find(":");
00106 host_addr = address.substr(0, pos);
00107 host_port = address.substr(pos + 1);
00108 port = (Uint16) atoi(host_port.c_str());
00109
00110 retval = lookupAddress(host_addr);
00111 setPort(port);
00112 setFamily(vpr::SocketTypes::INET);
00113
00114 return retval;
00115 }
|
|
||||||||||||
|
Sets the address for this object using the given address and port number. The address string can be a hostname or a dotted-decimal IP address.
Reimplemented from vpr::InetAddrBase. Definition at line 117 of file InetAddrBSD.cpp. References lookupAddress, setFamily, setPort, and vpr::Uint16.
00119 {
00120 vpr::ReturnStatus retval;
00121
00122 retval = lookupAddress(address);
00123 setPort(port);
00124 setFamily(SocketTypes::INET);
00125
00126 return retval;
00127 }
|
|
||||||||||||
|
Sets the address for this object using the given address and port number. The address must be the actual 32-bit integer value.
Reimplemented from vpr::InetAddrBase. Definition at line 129 of file InetAddrBSD.cpp. References setAddressValue, setFamily, and setPort.
00131 {
00132 setAddressValue(address);
00133 setPort(port);
00134 setFamily(SocketTypes::INET);
00135 return ReturnStatus();
00136 }
|
|
|
Gets the length of the address structure (if supported by the host OS).
Definition at line 138 of file InetAddrBSD.cpp. References mAddr.
00139 {
00140 #ifdef _HAVE_SIN_LEN
00141 return mAddr.sin_len;
00142 #else
00143 return 0;
00144 #endif
00145 }
|
|
|
Sets the length of the address structure (if the host OS allows such an operation).
Definition at line 147 of file InetAddrBSD.cpp. References mAddr.
00148 {
00149 #ifdef _HAVE_SIN_LEN
00150 mAddr.sin_len = length;
00151 #else
00152 boost::ignore_unused_variable_warning(length);
00153 #endif
00154 }
|
|
|
Gets the protocol family of this address structure.
Reimplemented from vpr::InetAddrBase. Definition at line 159 of file InetAddrBSD.cpp. References mAddr. Referenced by vpr::SocketImplNSPR::open, and vpr::SocketImplBSD::open.
00160 {
00161 vpr::SocketTypes::Domain family;
00162
00163 switch ( mAddr.sin_family )
00164 {
00165 #ifdef PF_LOCAL
00166 case PF_LOCAL:
00167 #else
00168 case PF_UNIX:
00169 #endif
00170 family = SocketTypes::LOCAL;
00171 break;
00172 case PF_INET:
00173 family = SocketTypes::INET;
00174 break;
00175 #ifdef PF_INET6
00176 case PF_INET6:
00177 family = SocketTypes::INET6;
00178 break;
00179 #endif
00180 #if defined(PF_LINK) || defined(PF_RAW)
00181 #ifdef PF_LINK
00182 case PF_LINK:
00183 #else
00184 case PF_RAW:
00185 #endif
00186 family = SocketTypes::LINK;
00187 break;
00188 #endif
00189 default:
00190 family = SocketTypes::INET;
00191 break;
00192 }
00193
00194 return family;
00195 }
|
|
|
Sets the protocol family of this address structure.
Reimplemented from vpr::InetAddrBase. Definition at line 200 of file InetAddrBSD.cpp. References mAddr. Referenced by InetAddrBSD, and setAddress.
00201 {
00202 switch ( family )
00203 {
00204 case SocketTypes::LOCAL:
00205 #ifdef PF_LOCAL
00206 mAddr.sin_family = PF_LOCAL;
00207 #else
00208 mAddr.sin_family = PF_UNIX;
00209 #endif
00210 break;
00211 case SocketTypes::INET:
00212 mAddr.sin_family = PF_INET;
00213 break;
00214 case SocketTypes::INET6:
00215 #ifdef PF_INET6
00216 mAddr.sin_family = PF_INET6;
00217 #else
00218 fprintf(stderr,
00219 "[vpr::InetAddrBSD] WARNING: IPv6 not supported on this host!\n");
00220 #endif
00221 break;
00222 #if defined(PF_LINK) || defined(PF_RAW)
00223 case SocketTypes::LINK:
00224 #ifdef PF_LINK
00225 mAddr.sin_family = PF_LINK;
00226 #else
00227 mAddr.sin_family = PF_RAW;
00228 #endif
00229 break;
00230 #endif
00231 default:
00232 fprintf(stderr,
00233 "[vpr::InetAddrBSD] ERROR: Unknown socket family value %d\n",
00234 family);
00235 break;
00236 }
00237 }
|
|
|
Gets this address' port in host byte order.
Reimplemented from vpr::InetAddrBase. Definition at line 239 of file InetAddrBSD.cpp. References mAddr. Referenced by vpr::sim::NetworkNode::addSocket, vpr::InetAddrHash::operator(), vpr::SocketDatagramImplBSD::recvfrom, vpr::sim::NetworkNode::removeSocket, and vpr::SocketDatagramImplBSD::sendto.
00240 {
00241 return ntohs(mAddr.sin_port);
00242 }
|
|
|
Sets this address' port. The given port must be in host byte order.
Reimplemented from vpr::InetAddrBase. Definition at line 244 of file InetAddrBSD.cpp. References mAddr. Referenced by vpr::SocketStreamImplSIM::accept, InetAddrBSD, and setAddress.
00245 {
00246 mAddr.sin_port = htons(port);
00247 }
|
|
|
Gets this address's Internet address in host byte order.
Reimplemented from vpr::InetAddrBase. Definition at line 249 of file InetAddrBSD.cpp. References mAddr. Referenced by vpr::sim::NetworkNode::addSocket, vpr::InetAddrHash::operator(), and vpr::sim::NetworkNode::removeSocket.
00250 {
00251 return ntohl(mAddr.sin_addr.s_addr);
00252 }
|
|
|
Get the IP address associated with this structure as a human-readable string.
Reimplemented from vpr::InetAddrBase. Definition at line 258 of file InetAddrBSD.cpp. References mAddr, and vpr::Uint8. Referenced by vpr::SocketImplBSD::connect, vpr::SocketImplBSD::open, vpr::SocketDatagramImplBSD::recvfrom, vpr::SocketDatagramImplBSD::sendto, vpr::SocketImplBSD::SocketImplBSD, and vpr::SocketImplNSPR::SocketImplNSPR.
00259 {
00260 char ip_addr[sizeof("255.255.255.255")];
00261 std::string ip_str;
00262 union
00263 {
00264 char c[sizeof(vpr::Uint32)];
00265 vpr::Uint32 value;
00266 } addr;
00267
00268 addr.value = mAddr.sin_addr.s_addr;
00269
00270 snprintf(ip_addr, sizeof(ip_addr), "%u.%u.%u.%u", (Uint8) addr.c[0],
00271 (Uint8) addr.c[1], (Uint8) addr.c[2], (Uint8) addr.c[3]);
00272 ip_str = ip_addr;
00273
00274 return ip_str;
00275 }
|
|
|
Returns the fully qualified hostname for this address.
Reimplemented from vpr::InetAddrBase. Definition at line 277 of file InetAddrBSD.cpp. References mAddr.
00278 {
00279 std::string hostname;
00280 struct hostent* entry;
00281
00282 entry = gethostbyaddr((const char*) &mAddr.sin_addr,
00283 sizeof(mAddr.sin_addr), mAddr.sin_family);
00284
00285 if ( NULL == entry )
00286 {
00287 hostname = std::string("<hostname lookup failed>");
00288 }
00289 else
00290 {
00291 hostname = entry->h_name;
00292 }
00293
00294 return hostname;
00295 }
|
|
|
Returns the fully qualified primary hostname for this address and all known aliases.
Reimplemented from vpr::InetAddrBase. Definition at line 297 of file InetAddrBSD.cpp. References mAddr.
00298 {
00299 std::vector<std::string> names;
00300 struct hostent* entry;
00301
00302 entry = gethostbyaddr((const char*) &mAddr.sin_addr,
00303 sizeof(mAddr.sin_addr), mAddr.sin_family);
00304
00305 if ( NULL != entry )
00306 {
00307 names.push_back(std::string(entry->h_name));
00308
00309 for ( char** ptr = entry->h_aliases; *ptr != NULL; ptr++ )
00310 {
00311 names.push_back(std::string(*ptr));
00312 }
00313 }
00314
00315 return names;
00316 }
|
|
|
Overloaded assignment operator to ensure that assignments work correctly.
Definition at line 262 of file InetAddrBSD.h. References copy, and InetAddrBSD.
00263 {
00264 copy(addr);
00265 return *this;
00266 }
|
|
|
Overloaded equality operator.
Definition at line 319 of file InetAddrBSD.cpp. References mAddr.
|
|
|
Overloaded inequality operator.
Definition at line 276 of file InetAddrBSD.h. References InetAddrBSD.
00277 {
00278 return ! (*this == addr);
00279 }
|
|
|
Copies the given array of bytes (an A record) into this structure's IP address value. The record must be in network byte order. This method is useful when working with host entries returned by gethostname(3).
Definition at line 326 of file InetAddrBSD.cpp. References mAddr, and vprASSERT. Referenced by lookupAddress.
|
|
|
Sets this objects's IP address. The given address must be in host byte order.
Definition at line 333 of file InetAddrBSD.cpp. References mAddr. Referenced by InetAddrBSD, lookupAddress, and setAddress.
00334 {
00335 mAddr.sin_addr.s_addr = htonl(addr_value);
00336 }
|
|
|
Gets the size of this object's encapsulated address structure.
Definition at line 338 of file InetAddrBSD.cpp. Referenced by vpr::SocketDatagramImplBSD::recvfrom.
00339 {
00340 return sizeof(mAddr);
00341 }
|
|
|
Gets the size of this object's IP address value.
Definition at line 343 of file InetAddrBSD.cpp. References mAddr.
00344 {
00345 return sizeof(mAddr.sin_addr.s_addr);
00346 }
|
|
|
Initializes the internal socket address structure using the given sockaddr* object. This will overwrite a previously set address.
Definition at line 348 of file InetAddrBSD.cpp. References mAddr. Referenced by vpr::SocketImplBSD::connect, and InetAddrBSD.
|
|
|
Converts this object to a sockaddr_in struct.
Definition at line 353 of file InetAddrBSD.cpp.
00354 {
00355 return mAddr;
00356 }
|
|
|
Makes a copy of the given vpr::InetAddrBSD object in this object.
Definition at line 358 of file InetAddrBSD.cpp. References mAddr. Referenced by InetAddrBSD, and operator=.
|
|
|
Look up the given address and store the address in mAddr.
Definition at line 366 of file InetAddrBSD.cpp. References copyAddressValue, errno, INADDR_NONE, setAddressValue, and vpr::ReturnStatus::setCode. Referenced by setAddress.
00367 {
00368 vpr::ReturnStatus retval;
00369 struct hostent* host_entry;
00370
00371 // First, try looking the host up by name.
00372 host_entry = gethostbyname(address.c_str());
00373
00374 // If that succeeded, put the result in mRemoteAddr.
00375 if ( host_entry != NULL )
00376 {
00377 copyAddressValue(host_entry->h_addr);
00378 }
00379 // If gethostbyname(3) failed, the address string may be an IP address.
00380 else
00381 {
00382 in_addr_t addr;
00383
00384 // Try looking it up with inet_addr(3).
00385 addr = inet_addr(address.c_str());
00386
00387 // If the address string could not be found using inet_addr(3), then
00388 // return error status.
00389 if ( addr == INADDR_NONE )
00390 {
00391 fprintf(stderr,
00392 "[vpr::InetAddrBSD] Could not find address for '%s': %s\n",
00393 address.c_str(), strerror(errno));
00394 retval.setCode(ReturnStatus::Fail);
00395 }
00396 // Otherwise, we found the integer address successfully.
00397 else
00398 {
00399 setAddressValue(addr);
00400 }
00401 }
00402
00403 return retval;
00404 }
|
|
|
Definition at line 282 of file InetAddrBSD.h. |
|
|
Definition at line 283 of file InetAddrBSD.h. |
|
|
Definition at line 284 of file InetAddrBSD.h. |
|
|
Definition at line 65 of file InetAddrBSD.cpp. |
|
|
The Ineternet address structure.
Definition at line 393 of file InetAddrBSD.h. Referenced by addressSize, copy, copyAddressValue, getAddressString, getAddressValue, getFamily, getHostname, getHostnames, getLength, getPort, InetAddrBSD, operator==, setAddressValue, setFamily, setLength, setPort, and setSockaddr. |
1.2.14 written by Dimitri van Heesch,
© 1997-2002