Added methods used to fill a binary IP address buffer from stringified address.

git-svn-id: http://yate.null.ro/svn/yate/trunk@5657 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2013-10-01 13:15:30 +00:00
parent e27a590ebb
commit ac29008a7b
2 changed files with 41 additions and 0 deletions

View File

@ -477,6 +477,24 @@ bool SocketAddr::stringify(String& s, struct sockaddr* addr)
return false;
}
// Copy a host address to a buffer
int SocketAddr::copyAddr(uint8_t* buf, struct sockaddr* addr)
{
if (!(buf && addr))
return Unknown;
switch (addr->sa_family) {
case AF_INET:
::memcpy(buf,&((struct sockaddr_in*)addr)->sin_addr,4);
return IPv4;
#ifdef AF_INET6
case AF_INET6:
::memcpy(buf,&((struct sockaddr_in6*)addr)->sin6_addr,16);
return IPv6;
#endif
}
return Unknown;
}
// Append an address to a buffer
String& SocketAddr::appendAddr(String& buf, const String& addr, int family)
{

View File

@ -5677,6 +5677,29 @@ public:
*/
static bool stringify(String& buf, struct sockaddr* addr);
/**
* Put a host address to a buffer
* @param buf Destination buffer. It must be large enough to keep the address
* (4 bytes for IPv4, 16 bytes for IPv6)
* @param host The host address
* @param family Address family, set it to Unknown to detect
* @return Address family, Unknown on failure
*/
static inline int unStringify(uint8_t* buf, const String& host,
int family = Unknown) {
SocketAddr sa(family);
return sa.host(host) ? copyAddr(buf,sa.address()) : Unknown;
}
/**
* Copy a host address to a buffer
* @param buf Destination buffer. It must be large enough to keep the address
* (4 bytes for IPv4, 16 bytes for IPv6)
* @param addr The host address
* @return Address family, Unknown on failure
*/
static int copyAddr(uint8_t* buf, struct sockaddr* addr);
/**
* Retrieve the scope id value of an IPv6 address
* @param addr The address