chiark / gitweb /
systemctl: don't hit an assert when we are run from a non-systemd boot
[elogind.git] / src / socket-util.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foosocketutilhfoo
4 #define foosocketutilhfoo
5
6 /***
7   This file is part of systemd.
8
9   Copyright 2010 Lennart Poettering
10
11   systemd is free software; you can redistribute it and/or modify it
12   under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15
16   systemd is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <sys/un.h>
28 #include <net/if.h>
29 #include <selinux/selinux.h>
30
31 #include "macro.h"
32 #include "util.h"
33
34 typedef struct SocketAddress {
35         union {
36                 struct sockaddr sa;
37                 struct sockaddr_in in4;
38                 struct sockaddr_in6 in6;
39                 struct sockaddr_un un;
40                 struct sockaddr_storage storage;
41         } sockaddr;
42
43         /* We store the size here explicitly due to the weird
44          * sockaddr_un semantics for abstract sockets */
45         socklen_t size;
46
47         /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
48         int type;
49 } SocketAddress;
50
51 typedef enum SocketAddressBindIPv6Only {
52         SOCKET_ADDRESS_DEFAULT,
53         SOCKET_ADDRESS_BOTH,
54         SOCKET_ADDRESS_IPV6_ONLY,
55         _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
56         _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -1
57 } SocketAddressBindIPv6Only;
58
59 #define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
60
61 int socket_address_parse(SocketAddress *a, const char *s);
62 int socket_address_print(const SocketAddress *a, char **p);
63 int socket_address_verify(const SocketAddress *a);
64
65 bool socket_address_can_accept(const SocketAddress *a);
66
67 int socket_address_listen(
68                 const SocketAddress *a,
69                 int backlog,
70                 SocketAddressBindIPv6Only only,
71                 const char *bind_to_device,
72                 bool free_bind,
73                 mode_t directory_mode,
74                 mode_t socket_mode,
75                 security_context_t scon,
76                 int *ret);
77
78 bool socket_address_is(const SocketAddress *a, const char *s, int type);
79
80 bool socket_address_equal(const SocketAddress *a, const SocketAddress *b);
81
82 bool socket_address_needs_mount(const SocketAddress *a, const char *prefix);
83
84 const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b);
85 SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s);
86
87 #endif