socket-util.h revision 54255c64e6d223deb7d3863e426e78c443fda37c
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#pragma once
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering/***
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering This file is part of systemd.
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering Copyright 2010 Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering (at your option) any later version.
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering systemd is distributed in the hope that it will be useful, but
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering Lesser General Public License for more details.
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen***/
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <sys/socket.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <netinet/in.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <netinet/ether.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <sys/un.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <linux/netlink.h>
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen#include <linux/if_packet.h>
28cb17ef0281efc3a46e5d0e702b0b0ddeaafaa4Filipe Brandenburger
28cb17ef0281efc3a46e5d0e702b0b0ddeaafaa4Filipe Brandenburger#include "macro.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include "util.h"
11c3a36649e5e5e77db499c92f3cdcbd619efd3aThomas Hindoe Paaboel Andersen
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringunion sockaddr_union {
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr sa;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_in in;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_in6 in6;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_un un;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_nl nl;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_storage storage;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering struct sockaddr_ll ll;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering};
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringtypedef struct SocketAddress {
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering union sockaddr_union sockaddr;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering /* We store the size here explicitly due to the weird
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering * sockaddr_un semantics for abstract sockets */
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering socklen_t size;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering int type;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering int protocol;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering} SocketAddress;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringtypedef enum SocketAddressBindIPv6Only {
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering SOCKET_ADDRESS_DEFAULT,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering SOCKET_ADDRESS_BOTH,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering SOCKET_ADDRESS_IPV6_ONLY,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -1
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering} SocketAddressBindIPv6Only;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_parse(SocketAddress *a, const char *s);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_parse_and_warn(SocketAddress *a, const char *s);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_parse_netlink(SocketAddress *a, const char *s);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_print(const SocketAddress *a, char **p);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_verify(const SocketAddress *a) _pure_;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_unlink(SocketAddress *a);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringbool socket_address_can_accept(const SocketAddress *a) _pure_;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socket_address_listen(
2d49a208f8ad9d1c4e79fa4302451e35d06de707Lennart Poettering const SocketAddress *a,
2d49a208f8ad9d1c4e79fa4302451e35d06de707Lennart Poettering int flags,
2d49a208f8ad9d1c4e79fa4302451e35d06de707Lennart Poettering int backlog,
2d49a208f8ad9d1c4e79fa4302451e35d06de707Lennart Poettering SocketAddressBindIPv6Only only,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering const char *bind_to_device,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering bool reuse_port,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering bool free_bind,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering bool transparent,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering mode_t directory_mode,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering mode_t socket_mode,
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering const char *label);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint make_socket_fd(int log_level, const char* address, int flags);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringbool socket_address_is(const SocketAddress *a, const char *s, int type);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringbool socket_address_is_netlink(const SocketAddress *a, const char *s);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringbool socket_address_matches_fd(const SocketAddress *a, int fd);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringbool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_;
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringconst char* socket_address_get_path(const SocketAddress *a);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringbool socket_ipv6_is_supported(void);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringint sockaddr_port(const struct sockaddr *_sa) _pure_;
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringint sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringint getpeername_pretty(int fd, char **ret);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poetteringint getsockname_pretty(int fd, char **ret);
6ad623a3f77e087e308f334525fd4046811f2a9aLennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint getnameinfo_pretty(int fd, char **ret);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringconst char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b) _const_;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart PoetteringSocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s) _pure_;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint netlink_family_to_string_alloc(int b, char **s);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringint netlink_family_from_string(const char *s) _pure_;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringbool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#define ETHER_ADDR_TO_STRING_MAX (3*6)
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poetteringchar* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering