chiark / gitweb /
util: unify getenv() logic for other PID
[elogind.git] / src / shared / socket-util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
13   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser 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 <asm/types.h>
30 #include <linux/netlink.h>
31
32 #include "macro.h"
33 #include "util.h"
34
35 union sockaddr_union {
36         struct sockaddr sa;
37         struct sockaddr_in in4;
38         struct sockaddr_in6 in6;
39         struct sockaddr_un un;
40         struct sockaddr_nl nl;
41         struct sockaddr_storage storage;
42 };
43
44 typedef struct SocketAddress {
45         union sockaddr_union sockaddr;
46
47         /* We store the size here explicitly due to the weird
48          * sockaddr_un semantics for abstract sockets */
49         socklen_t size;
50
51         /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
52         int type;
53
54         /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
55         int protocol;
56 } SocketAddress;
57
58 typedef enum SocketAddressBindIPv6Only {
59         SOCKET_ADDRESS_DEFAULT,
60         SOCKET_ADDRESS_BOTH,
61         SOCKET_ADDRESS_IPV6_ONLY,
62         _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
63         _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -1
64 } SocketAddressBindIPv6Only;
65
66 #define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
67
68 int socket_address_parse(SocketAddress *a, const char *s);
69 int socket_address_parse_netlink(SocketAddress *a, const char *s);
70 int socket_address_print(const SocketAddress *a, char **p);
71 int socket_address_verify(const SocketAddress *a);
72
73 bool socket_address_can_accept(const SocketAddress *a);
74
75 int socket_address_listen(
76                 const SocketAddress *a,
77                 int backlog,
78                 SocketAddressBindIPv6Only only,
79                 const char *bind_to_device,
80                 bool free_bind,
81                 bool transparent,
82                 mode_t directory_mode,
83                 mode_t socket_mode,
84                 const char *label,
85                 int *ret);
86
87 bool socket_address_is(const SocketAddress *a, const char *s, int type);
88 bool socket_address_is_netlink(const SocketAddress *a, const char *s);
89
90 bool socket_address_equal(const SocketAddress *a, const SocketAddress *b);
91
92 bool socket_address_needs_mount(const SocketAddress *a, const char *prefix);
93
94 const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b);
95 SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s);
96
97 const char* netlink_family_to_string(int b);
98 int netlink_family_from_string(const char *s);
99
100 bool socket_ipv6_is_supported(void);
101
102 #endif