Commit ccf0ed90 authored by Marek Vavruša's avatar Marek Vavruša

sockaddr: raw address getter, updated api

parent 7df4cbec
......@@ -82,6 +82,21 @@ int sockaddr_set(struct sockaddr_storage *ss, int family, const char *straddr, i
return KNOT_EINVAL;
}
void *sockaddr_raw(struct sockaddr_storage *ss, size_t *addr_size)
{
if (ss->ss_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)ss;
*addr_size = sizeof(ipv4->sin_addr);
return &ipv4->sin_addr;
} else if (ss->ss_family == AF_INET6) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)ss;
*addr_size = sizeof(ipv6->sin6_addr);
return &ipv6->sin6_addr;
} else {
return NULL;
}
}
int sockaddr_set_raw(struct sockaddr_storage *ss, int family,
const uint8_t *raw_addr, size_t raw_addr_size)
{
......@@ -89,18 +104,10 @@ int sockaddr_set_raw(struct sockaddr_storage *ss, int family,
return KNOT_EINVAL;
}
void *sa_data = NULL;
size_t sa_size = 0;
ss->ss_family = family;
if (family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)ss;
sa_data = &ipv4->sin_addr;
sa_size = sizeof(ipv4->sin_addr);
} else if (family == AF_INET6) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)ss;
sa_data = &ipv6->sin6_addr;
sa_size = sizeof(ipv6->sin6_addr);
}
size_t sa_size = 0;
void *sa_data = sockaddr_raw(ss, &sa_size);
if (sa_data == NULL || sa_size != raw_addr_size) {
return KNOT_EINVAL;
......@@ -113,7 +120,7 @@ int sockaddr_set_raw(struct sockaddr_storage *ss, int family,
return KNOT_EOK;
}
int sockaddr_tostr(const struct sockaddr_storage *ss, char *buf, size_t maxlen)
int sockaddr_tostr(char *buf, size_t maxlen, const struct sockaddr_storage *ss)
{
if (ss == NULL || buf == NULL) {
return KNOT_EINVAL;
......@@ -142,17 +149,19 @@ int sockaddr_tostr(const struct sockaddr_storage *ss, char *buf, size_t maxlen)
}
/* Write separator and port. */
int written = strlen(buf);
int port = sockaddr_port(ss);
if (port > 0) {
size_t written = strlen(buf);
int ret = snprintf(&buf[written], maxlen - written, "@%d", port);
if (ret <= 0 || (size_t)ret >= maxlen - written) {
*buf = '\0';
return KNOT_ESPACE;
}
written += ret;
}
return KNOT_EOK;
return written;
}
int sockaddr_port(const struct sockaddr_storage *ss)
......
......@@ -75,6 +75,15 @@ int sockaddr_cmp(const struct sockaddr_storage *k1, const struct sockaddr_storag
*/
int sockaddr_set(struct sockaddr_storage *ss, int family, const char *straddr, int port);
/*!
* \brief Return raw network address in network byte order.
*
* \param ss Socket address storage.
* \param addr_size Length of the address will be stored in addr_size.
* \return pointer to raw address
*/
void *sockaddr_raw(struct sockaddr_storage *ss, size_t *addr_size);
/*!
* \brief Set raw address.
*
......@@ -99,7 +108,7 @@ int sockaddr_set_raw(struct sockaddr_storage *ss, int family,
*
* \return EOK on success, error code on failure
*/
int sockaddr_tostr(const struct sockaddr_storage *ss, char *buf, size_t maxlen);
int sockaddr_tostr(char *buf, size_t maxlen, const struct sockaddr_storage *ss);
/*!
* \brief Return port number from address.
......
......@@ -214,7 +214,7 @@ static int conf_process(conf_t *conf)
if (ctl_if->addr.ss_family == AF_UNIX) {
char *full_path = malloc(SOCKADDR_STRLEN);
memset(full_path, 0, SOCKADDR_STRLEN);
sockaddr_tostr(&ctl_if->addr, full_path, SOCKADDR_STRLEN);
sockaddr_tostr(full_path, SOCKADDR_STRLEN, &ctl_if->addr);
/* Convert to absolute path. */
full_path = conf_abs_path(conf->rundir, full_path);
......
......@@ -235,7 +235,7 @@ static int cmd_remote(const char *cmd, uint16_t rrt, int argc, char *argv[])
/* Connect to remote. */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&r->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &r->addr);
int s = net_connected_socket(SOCK_STREAM, &r->addr, &r->via, 0);
if (s < 0) {
......
......@@ -465,7 +465,7 @@ int remote_bind(conf_iface_t *desc)
}
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&desc->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &desc->addr);
log_info("binding remote control interface to '%s'", addr_str);
/* Create new socket. */
......@@ -496,7 +496,7 @@ int remote_unbind(conf_iface_t *desc, int sock)
/* Remove control socket file. */
if (desc->addr.ss_family == AF_UNIX) {
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&desc->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &desc->addr);
unlink(addr_str);
}
......@@ -831,7 +831,7 @@ int remote_process(server_t *s, conf_iface_t *ctl_if, int sock,
/* Check ACL list. */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &ss);
knot_tsig_key_t *tsig_key = NULL;
const knot_dname_t *tsig_name = NULL;
if (pkt->tsig_rr) {
......
......@@ -38,7 +38,7 @@ const knot_process_module_t *process_query_get_module(void);
/*! \brief Query processing logging common base. */
#define NS_PROC_LOG(severity, remote, zone_name, operation, msg, ...) do { \
char addr_str[SOCKADDR_STRLEN] = {0}; \
sockaddr_tostr(remote, addr_str, sizeof(addr_str)); \
sockaddr_tostr(addr_str, sizeof(addr_str), remote); \
log_msg_zone(severity, zone_name, operation ", %s: " msg, \
addr_str, ##__VA_ARGS__); \
} while (0)
......
......@@ -52,7 +52,7 @@ int net_unbound_socket(int type, const struct sockaddr_storage *ss)
/* Convert to string address format. */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
/* Create socket. */
int socket = socket_create(ss->ss_family, type, 0);
......@@ -75,7 +75,7 @@ int net_bound_socket(int type, const struct sockaddr_storage *ss)
/* Convert to string address format. */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
/* Reuse old address if taken. */
int flag = 1;
......
......@@ -279,7 +279,7 @@ static void rrl_log_state(const struct sockaddr_storage *ss, uint16_t flags, uin
{
#ifdef RRL_ENABLE_LOG
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
const char *what = "leaves";
if (flags & RRL_BF_ELIMIT) {
......
......@@ -115,7 +115,7 @@ static int server_init_iface(iface_t *new_if, conf_iface_t *cfg_if)
/* Convert to string address format. */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&cfg_if->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &cfg_if->addr);
/* Create bound UDP socket. */
int sock = net_bound_socket(SOCK_DGRAM, &cfg_if->addr);
......@@ -166,7 +166,7 @@ static void remove_ifacelist(struct ref_t *p)
char addr_str[SOCKADDR_STRLEN] = {0};
iface_t *n = NULL, *m = NULL;
WALK_LIST_DELSAFE(n, m, ifaces->u) {
sockaddr_tostr(&n->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &n->addr);
log_info("removing interface '%s'", addr_str);
server_remove_iface(n);
}
......@@ -223,7 +223,7 @@ static int reconfigure_sockets(const struct conf_t *conf, server_t *s)
if (found_match) {
rem_node((node_t *)m);
} else {
sockaddr_tostr(&cfg_if->addr, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &cfg_if->addr);
log_info("binding to interface '%s'", addr_str);
/* Create new interface. */
......
......@@ -81,7 +81,7 @@ static enum fdset_sweep_state tcp_sweep(fdset_t *set, int i, void *data)
/* Translate */
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &ss);
log_notice("connection terminated due to inactivity, address '%s'", addr_str);
close(fd);
......@@ -121,7 +121,7 @@ static int tcp_handle(tcp_context_t *tcp, int fd,
if (ret == KNOT_EAGAIN) {
rcu_read_lock();
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(&ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), &ss);
log_warning("connection timed out, address '%s', "
"timeout %d seconds",
addr_str, conf()->max_conn_idle);
......
......@@ -227,7 +227,7 @@ int zone_dump_text(zone_contents_t *zone, const struct sockaddr_storage *from, F
// If a master server is configured, dump info about it.
if (from) {
char addr_str[SOCKADDR_STRLEN] = {0};
sockaddr_tostr(from, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), from);
fprintf(file, ";; Transfered from %s\n", addr_str);
}
......
......@@ -142,7 +142,7 @@ void get_addr_str(const struct sockaddr_storage *ss,
char addr_str[SOCKADDR_STRLEN] = {0};
// Get network address string and port number.
sockaddr_tostr(ss, addr_str, sizeof(addr_str));
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
// Calculate needed buffer size
const char *sock_name = get_sockname(socktype);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment