Merge pull request #17855 from poettering/more-socktops
socket-util: some helpers for various sockopts
This commit is contained in:
commit
77613416e0
|
@ -67,6 +67,14 @@ struct sockaddr_vm {
|
||||||
#define IPV6_FREEBIND 78
|
#define IPV6_FREEBIND 78
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef IP_RECVFRAGSIZE
|
||||||
|
#define IP_RECVFRAGSIZE 25
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IPV6_RECVFRAGSIZE
|
||||||
|
#define IPV6_RECVFRAGSIZE 77
|
||||||
|
#endif
|
||||||
|
|
||||||
/* linux/sockios.h */
|
/* linux/sockios.h */
|
||||||
#ifndef SIOCGSKNS
|
#ifndef SIOCGSKNS
|
||||||
#define SIOCGSKNS 0x894C
|
#define SIOCGSKNS 0x894C
|
||||||
|
|
|
@ -1314,3 +1314,35 @@ int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
|
||||||
return -EAFNOSUPPORT;
|
return -EAFNOSUPPORT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int socket_get_mtu(int fd, int af, size_t *ret) {
|
||||||
|
int mtu, r;
|
||||||
|
|
||||||
|
if (af == AF_UNSPEC) {
|
||||||
|
r = socket_get_family(fd, &af);
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (af) {
|
||||||
|
|
||||||
|
case AF_INET:
|
||||||
|
r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AF_INET6:
|
||||||
|
r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return -EAFNOSUPPORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
if (mtu <= 0)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
*ret = (size_t) mtu;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -258,6 +258,19 @@ static inline int setsockopt_int(int fd, int level, int optname, int value) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int getsockopt_int(int fd, int level, int optname, int *ret) {
|
||||||
|
int v;
|
||||||
|
socklen_t sl = sizeof(v);
|
||||||
|
|
||||||
|
if (getsockopt(fd, level, optname, &v, &sl) < 0)
|
||||||
|
return -errno;
|
||||||
|
if (sl != sizeof(v))
|
||||||
|
return -EIO;
|
||||||
|
|
||||||
|
*ret = v;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int socket_bind_to_ifname(int fd, const char *ifname);
|
int socket_bind_to_ifname(int fd, const char *ifname);
|
||||||
int socket_bind_to_ifindex(int fd, int ifindex);
|
int socket_bind_to_ifindex(int fd, int ifindex);
|
||||||
|
|
||||||
|
@ -266,6 +279,7 @@ ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags);
|
||||||
int socket_get_family(int fd, int *ret);
|
int socket_get_family(int fd, int *ret);
|
||||||
int socket_set_recvpktinfo(int fd, int af, bool b);
|
int socket_set_recvpktinfo(int fd, int af, bool b);
|
||||||
int socket_set_unicast_if(int fd, int af, int ifi);
|
int socket_set_unicast_if(int fd, int af, int ifi);
|
||||||
|
|
||||||
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val);
|
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val);
|
||||||
static inline int socket_set_recverr(int fd, int af, bool b) {
|
static inline int socket_set_recverr(int fd, int af, bool b) {
|
||||||
return socket_set_option(fd, af, IP_RECVERR, IPV6_RECVERR, b);
|
return socket_set_option(fd, af, IP_RECVERR, IPV6_RECVERR, b);
|
||||||
|
@ -282,3 +296,8 @@ static inline int socket_set_freebind(int fd, int af, bool b) {
|
||||||
static inline int socket_set_transparent(int fd, int af, bool b) {
|
static inline int socket_set_transparent(int fd, int af, bool b) {
|
||||||
return socket_set_option(fd, af, IP_TRANSPARENT, IPV6_TRANSPARENT, b);
|
return socket_set_option(fd, af, IP_TRANSPARENT, IPV6_TRANSPARENT, b);
|
||||||
}
|
}
|
||||||
|
static inline int socket_set_recvfragsize(int fd, int af, bool b) {
|
||||||
|
return socket_set_option(fd, af, IP_RECVFRAGSIZE, IPV6_RECVFRAGSIZE, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int socket_get_mtu(int fd, int af, size_t *ret);
|
||||||
|
|
Loading…
Reference in New Issue