s3:lib/addrchange: set ctx->sock to -1 after close
[obnox/samba-ctdb.git] / source3 / lib / addrchange.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Copyright (C) Volker Lendecke 2011
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "includes.h"
20 #include "lib/addrchange.h"
21
22 #if HAVE_LINUX_RTNETLINK_H
23
24 #include "linux/netlink.h"
25 #include "linux/rtnetlink.h"
26 #include "lib/async_req/async_sock.h"
27
28 struct addrchange_context {
29         int sock;
30 };
31
32 static int addrchange_context_destructor(struct addrchange_context *c);
33
34 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
35                                    struct addrchange_context **pctx)
36 {
37         struct addrchange_context *ctx;
38         struct sockaddr_nl addr;
39         NTSTATUS status;
40         int res;
41
42         ctx = talloc(mem_ctx, struct addrchange_context);
43         if (ctx == NULL) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46
47         ctx->sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
48         if (ctx->sock == -1) {
49                 status = map_nt_error_from_unix(errno);
50                 goto fail;
51         }
52         talloc_set_destructor(ctx, addrchange_context_destructor);
53
54         /*
55          * We're interested in address changes
56          */
57         ZERO_STRUCT(addr);
58         addr.nl_family = AF_NETLINK;
59         addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
60
61         res = bind(ctx->sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
62         if (res == -1) {
63                 status = map_nt_error_from_unix(errno);
64                 goto fail;
65         }
66
67         *pctx = ctx;
68         return NT_STATUS_OK;
69 fail:
70         TALLOC_FREE(ctx);
71         return status;
72 }
73
74 static int addrchange_context_destructor(struct addrchange_context *c)
75 {
76         if (c->sock != -1) {
77                 close(c->sock);
78                 c->sock = -1;
79         }
80         return 0;
81 }
82
83 struct addrchange_state {
84         struct tevent_context *ev;
85         struct addrchange_context *ctx;
86         uint8_t buf[8192];
87         struct sockaddr_storage fromaddr;
88         socklen_t fromaddr_len;
89
90         enum addrchange_type type;
91         struct sockaddr_storage addr;
92 };
93
94 static void addrchange_done(struct tevent_req *subreq);
95
96 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
97                                    struct tevent_context *ev,
98                                    struct addrchange_context *ctx)
99 {
100         struct tevent_req *req, *subreq;
101         struct addrchange_state *state;
102
103         req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
104         if (req == NULL) {
105                 return NULL;
106         }
107         state->ev = ev;
108         state->ctx = ctx;
109
110         state->fromaddr_len = sizeof(state->fromaddr);
111         subreq = recvfrom_send(state, state->ev, state->ctx->sock,
112                                state->buf, sizeof(state->buf), 0,
113                                &state->fromaddr, &state->fromaddr_len);
114         if (tevent_req_nomem(subreq, req)) {
115                 return tevent_req_post(req, state->ev);
116         }
117         tevent_req_set_callback(subreq, addrchange_done, req);
118         return req;
119 }
120
121 static void addrchange_done(struct tevent_req *subreq)
122 {
123         struct tevent_req *req = tevent_req_callback_data(
124                 subreq, struct tevent_req);
125         struct addrchange_state *state = tevent_req_data(
126                 req, struct addrchange_state);
127         struct nlmsghdr *h;
128         struct ifaddrmsg *ifa;
129         struct rtattr *rta;
130         ssize_t received;
131         int len;
132         int err;
133         bool found;
134
135         received = recvfrom_recv(subreq, &err);
136         TALLOC_FREE(subreq);
137         if (received == -1) {
138                 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
139                 tevent_req_nterror(req, map_nt_error_from_unix(err));
140                 return;
141         }
142         if (received < sizeof(struct nlmsghdr)) {
143                 DEBUG(10, ("received %d, expected at least %d\n",
144                            (int)received, (int)sizeof(struct nlmsghdr)));
145                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
146                 return;
147         }
148
149         h = (struct nlmsghdr *)state->buf;
150         if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
151                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
152                            (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
153                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
154                 return;
155         }
156         if (h->nlmsg_len > received) {
157                 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
158                            (int)h->nlmsg_len, (int)received));
159                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
160                 return;
161         }
162         switch (h->nlmsg_type) {
163         case RTM_NEWADDR:
164                 state->type = ADDRCHANGE_ADD;
165                 break;
166         case RTM_DELADDR:
167                 state->type = ADDRCHANGE_DEL;
168                 break;
169         default:
170                 DEBUG(10, ("Got unexpected type %d - ignoring\n", h->nlmsg_type));
171
172                 state->fromaddr_len = sizeof(state->fromaddr);
173                 subreq = recvfrom_send(state, state->ev, state->ctx->sock,
174                                state->buf, sizeof(state->buf), 0,
175                                &state->fromaddr, &state->fromaddr_len);
176                 if (tevent_req_nomem(subreq, req)) {
177                         return;
178                 }
179                 tevent_req_set_callback(subreq, addrchange_done, req);
180                 return;
181         }
182
183         if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
184                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
185                            (int)h->nlmsg_len,
186                            (int)(sizeof(struct nlmsghdr)
187                                  +sizeof(struct ifaddrmsg))));
188                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
189                 return;
190         }
191
192         ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
193
194         state->addr.ss_family = ifa->ifa_family;
195
196         rta = IFA_RTA(ifa);
197         len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
198
199         found = false;
200
201         for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
202
203                 if ((rta->rta_type != IFA_LOCAL)
204                     && (rta->rta_type != IFA_ADDRESS)) {
205                         continue;
206                 }
207
208                 switch (ifa->ifa_family) {
209                 case AF_INET: {
210                         struct sockaddr_in *v4_addr;
211                         v4_addr = (struct sockaddr_in *)(void *)&state->addr;
212
213                         if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
214                                 continue;
215                         }
216                         v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
217                         found = true;
218                         break;
219                 }
220                 case AF_INET6: {
221                         struct sockaddr_in6 *v6_addr;
222                         v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
223
224                         if (RTA_PAYLOAD(rta) !=
225                             sizeof(v6_addr->sin6_addr.s6_addr)) {
226                                 continue;
227                         }
228                         memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
229                                sizeof(v6_addr->sin6_addr.s6_addr));
230                         found = true;
231                         break;
232                 }
233                 }
234         }
235
236         if (!found) {
237                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
238                 return;
239         }
240
241         tevent_req_done(req);
242 }
243
244 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
245                          struct sockaddr_storage *addr)
246 {
247         struct addrchange_state *state = tevent_req_data(
248                 req, struct addrchange_state);
249         NTSTATUS status;
250
251         if (tevent_req_is_nterror(req, &status)) {
252                 return status;
253         }
254
255         *type = state->type;
256         *addr = state->addr;
257         return NT_STATUS_OK;
258 }
259
260 #else
261
262 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
263                                    struct addrchange_context **pctx)
264 {
265         return NT_STATUS_NOT_SUPPORTED;
266 }
267
268 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
269                                    struct tevent_context *ev,
270                                    struct addrchange_context *ctx)
271 {
272         return NULL;
273 }
274
275 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
276                          struct sockaddr_storage *addr)
277 {
278         return NT_STATUS_NOT_IMPLEMENTED;
279 }
280
281 #endif