]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-internal.h
Merge pull request #530 from dvdhrm/resolve-host-dbus
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-internal.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2013 Tom Gundersen <teg@jklm.no>
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <linux/netlink.h>
25
26 #include "refcnt.h"
27 #include "prioq.h"
28 #include "list.h"
29
30 #include "sd-netlink.h"
31
32 #include "netlink-types.h"
33
34 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
35
36 #define RTNL_WQUEUE_MAX 1024
37 #define RTNL_RQUEUE_MAX 64*1024
38
39 #define RTNL_CONTAINER_DEPTH 32
40
41 struct reply_callback {
42 sd_netlink_message_handler_t callback;
43 void *userdata;
44 usec_t timeout;
45 uint64_t serial;
46 unsigned prioq_idx;
47 };
48
49 struct match_callback {
50 sd_netlink_message_handler_t callback;
51 uint16_t type;
52 void *userdata;
53
54 LIST_FIELDS(struct match_callback, match_callbacks);
55 };
56
57 struct sd_netlink {
58 RefCount n_ref;
59
60 int fd;
61
62 union {
63 struct sockaddr sa;
64 struct sockaddr_nl nl;
65 } sockaddr;
66
67 sd_netlink_message **rqueue;
68 unsigned rqueue_size;
69 size_t rqueue_allocated;
70
71 sd_netlink_message **rqueue_partial;
72 unsigned rqueue_partial_size;
73 size_t rqueue_partial_allocated;
74
75 struct nlmsghdr *rbuffer;
76 size_t rbuffer_allocated;
77
78 bool processing:1;
79
80 uint32_t serial;
81
82 struct Prioq *reply_callbacks_prioq;
83 Hashmap *reply_callbacks;
84
85 LIST_HEAD(struct match_callback, match_callbacks);
86
87 pid_t original_pid;
88
89 sd_event_source *io_event_source;
90 sd_event_source *time_event_source;
91 sd_event_source *exit_event_source;
92 sd_event *event;
93 };
94
95 struct netlink_attribute {
96 size_t offset; /* offset from hdr to attribute */
97 };
98
99 struct netlink_container {
100 const struct NLTypeSystem *type_system; /* the type system of the container */
101 size_t offset; /* offset from hdr to the start of the container */
102 struct netlink_attribute *attributes;
103 unsigned short n_attributes; /* number of attributes in container */
104 };
105
106 struct sd_netlink_message {
107 RefCount n_ref;
108
109 sd_netlink *rtnl;
110
111 struct nlmsghdr *hdr;
112 struct netlink_container containers[RTNL_CONTAINER_DEPTH];
113 unsigned n_containers; /* number of containers */
114 bool sealed:1;
115 bool broadcast:1;
116
117 sd_netlink_message *next; /* next in a chain of multi-part messages */
118 };
119
120 int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type);
121 int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret);
122
123 int socket_open(int family);
124 int socket_bind(sd_netlink *nl);
125 int socket_join_broadcast_group(sd_netlink *nl, unsigned group);
126 int socket_write_message(sd_netlink *nl, sd_netlink_message *m);
127 int socket_read_message(sd_netlink *nl);
128
129 int rtnl_rqueue_make_room(sd_netlink *rtnl);
130 int rtnl_rqueue_partial_make_room(sd_netlink *rtnl);
131
132 /* Make sure callbacks don't destroy the rtnl connection */
133 #define RTNL_DONT_DESTROY(rtnl) \
134 _cleanup_netlink_unref_ _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)