[Assorted-commits] SF.net SVN: assorted:[1516] cpp-commons/trunk/src/commons/sockets.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-11-23 07:59:36
|
Revision: 1516 http://assorted.svn.sourceforge.net/assorted/?rev=1516&view=rev Author: yangzhang Date: 2009-11-23 07:59:25 +0000 (Mon, 23 Nov 2009) Log Message: ----------- added ostream formatter for in_addrs; added nl_to_in_addr, hl_to_in_addr; const/doc tweaks Modified Paths: -------------- cpp-commons/trunk/src/commons/sockets.h Modified: cpp-commons/trunk/src/commons/sockets.h =================================================================== --- cpp-commons/trunk/src/commons/sockets.h 2009-11-23 07:58:04 UTC (rev 1515) +++ cpp-commons/trunk/src/commons/sockets.h 2009-11-23 07:59:25 UTC (rev 1516) @@ -19,6 +19,8 @@ #include <commons/closing.h> #include <commons/nullptr.h> +#include <Poco/Net/IPAddress.h> + namespace commons { @@ -121,14 +123,14 @@ * Initialize an inet address. */ inline void - sockaddr_init(sockaddr_in &a, in_addr host, uint16_t port) + sockaddr_init(sockaddr_in &a, const in_addr &host, uint16_t port) { sockaddr_init(a, port); a.sin_addr = host; } /** - * Initialize an inet addres. + * Initialize an inet address. host is in network byte order. */ inline void sockaddr_init(sockaddr_in &a, uint32_t host, uint16_t port) @@ -286,14 +288,45 @@ } #endif - /** Get the IPv4 address as an int in network byte order. */ + /** + * Convert an IPv4 address from an in_addr in network byte order to an int + * in network byte order. + */ inline uint32_t ipaddr2nl(const in_addr &a) { return IFWIN32(a.S_un.S_addr, a.s_addr); } - /** Get the IPv4 address as an int in host byte order. */ + /** + * Convert an IPv4 address from an in_addr in network byte order to an int + * in host byte order. + */ inline uint32_t ipaddr2hl(const in_addr &a) { return ntohl(ipaddr2nl(a)); } + /** + * Convert an IPv4 address from an int in network byte order to an in_addr + * in network byte order. + */ + inline in_addr nl_to_in_addr(uint32_t x) + { + in_addr y; + IFWIN32(y.S_un.S_addr, y.s_addr) = x; + return y; + } + + /** + * Convert an IPv4 address from an int in host byte order to an in_addr + * in network byte order. + */ + inline in_addr hl_to_in_addr(uint32_t x) + { + in_addr y = nl_to_in_addr(x); + htonl(IFWIN32(y.S_un.S_addr, y.s_addr)); + return y; + } + + inline ostream& operator <<(ostream &o, const in_addr &ip) + { return o << Poco::Net::IPAddress(&ip, sizeof ip).toString(); } + } #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |