mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-21 13:51:26 +02:00
MINOR: tools: Implement ipaddrcpy().
Implement ipaddrcpy() new function to copy only the IP address from a sockaddr_storage struct object into a buffer.
This commit is contained in:
parent
a33407b499
commit
fb7a092203
@ -842,6 +842,11 @@ int ipcmp2net(const struct sockaddr_storage *addr, const struct net_addr *net);
|
||||
*/
|
||||
struct sockaddr_storage *ipcpy(const struct sockaddr_storage *source, struct sockaddr_storage *dest);
|
||||
|
||||
/* Copy only the IP address from <saddr> socket address data into <buf> buffer. *
|
||||
* Return the number of bytes copied.
|
||||
*/
|
||||
size_t ipaddrcpy(unsigned char *buf, const struct sockaddr_storage *saddr);
|
||||
|
||||
char *human_time(int t, short hz_div);
|
||||
|
||||
extern const char *monthname[];
|
||||
|
27
src/tools.c
27
src/tools.c
@ -3685,6 +3685,33 @@ struct sockaddr_storage *ipcpy(const struct sockaddr_storage *source, struct soc
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* Copy only the IP address from <saddr> socket address data into <buf> buffer.
|
||||
* This is the responsibility of the caller to check the <buf> buffer is big
|
||||
* enough to contain these socket address data.
|
||||
* Return the number of bytes copied.
|
||||
*/
|
||||
size_t ipaddrcpy(unsigned char *buf, const struct sockaddr_storage *saddr)
|
||||
{
|
||||
void *addr;
|
||||
unsigned char *p;
|
||||
size_t addr_len;
|
||||
|
||||
p = buf;
|
||||
if (saddr->ss_family == AF_INET6) {
|
||||
addr = &((struct sockaddr_in6 *)saddr)->sin6_addr;
|
||||
addr_len = sizeof ((struct sockaddr_in6 *)saddr)->sin6_addr;
|
||||
}
|
||||
else {
|
||||
addr = &((struct sockaddr_in *)saddr)->sin_addr;
|
||||
addr_len = sizeof ((struct sockaddr_in *)saddr)->sin_addr;
|
||||
}
|
||||
memcpy(p, addr, addr_len);
|
||||
p += addr_len;
|
||||
|
||||
return p - buf;
|
||||
}
|
||||
|
||||
|
||||
char *human_time(int t, short hz_div) {
|
||||
static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
|
||||
char *p = rv;
|
||||
|
Loading…
x
Reference in New Issue
Block a user