ee628a5715994a0b5812aa890f4e12e7aca4f416
[project/odhcpd.git] / src / odhcpd.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15 #include <time.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <resolv.h>
20 #include <getopt.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <syslog.h>
28 #include <alloca.h>
29
30 #include <arpa/inet.h>
31 #include <net/if.h>
32 #include <netinet/ip6.h>
33 #include <netpacket/packet.h>
34 #include <linux/netlink.h>
35 #include <linux/if_addr.h>
36 #include <linux/neighbour.h>
37 #include <linux/rtnetlink.h>
38
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/epoll.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <sys/syscall.h>
45
46 #include <netlink/msg.h>
47 #include <netlink/socket.h>
48 #include <netlink/attr.h>
49 #include <libubox/uloop.h>
50 #include "odhcpd.h"
51
52
53
54 static int ioctl_sock;
55 static struct nl_sock *rtnl_socket = NULL;
56 static int urandom_fd = -1;
57
58 static void sighandler(_unused int signal)
59 {
60 uloop_end();
61 }
62
63 static void print_usage(const char *app)
64 {
65 printf(
66 "== %s Usage ==\n\n"
67 " -h, --help Print this help\n"
68 " -l level Specify log level 0..7 (default %d)\n",
69 app, config.log_level
70 );
71 }
72
73 int main(int argc, char **argv)
74 {
75 openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
76 int opt;
77
78 while ((opt = getopt(argc, argv, "hl:")) != -1) {
79 switch (opt) {
80 case 'h':
81 print_usage(argv[0]);
82 return 0;
83 case 'l':
84 config.log_level = (atoi(optarg) & LOG_PRIMASK);
85 fprintf(stderr, "Log level set to %d\n", config.log_level);
86 break;
87 }
88 }
89 setlogmask(LOG_UPTO(config.log_level));
90 uloop_init();
91
92 if (getuid() != 0) {
93 syslog(LOG_ERR, "Must be run as root!");
94 return 2;
95 }
96
97 ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
98
99 if (!(rtnl_socket = odhcpd_create_nl_socket(NETLINK_ROUTE))) {
100 syslog(LOG_ERR, "Unable to open nl socket: %s", strerror(errno));
101 return 2;
102 }
103
104 if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
105 return 4;
106
107 signal(SIGUSR1, SIG_IGN);
108 signal(SIGINT, sighandler);
109 signal(SIGTERM, sighandler);
110
111 if (init_router())
112 return 4;
113
114 if (init_dhcpv6())
115 return 4;
116
117 if (init_ndp())
118 return 4;
119
120 if (init_dhcpv4())
121 return 4;
122
123 odhcpd_run();
124 return 0;
125 }
126
127 struct nl_sock *odhcpd_create_nl_socket(int protocol)
128 {
129 struct nl_sock *nl_sock;
130
131 nl_sock = nl_socket_alloc();
132 if (!nl_sock)
133 goto err;
134
135 if (nl_connect(nl_sock, protocol) < 0)
136 goto err;
137
138 return nl_sock;
139
140 err:
141 if (nl_sock)
142 nl_socket_free(nl_sock);
143
144 return NULL;
145 }
146
147
148 // Read IPv6 MTU for interface
149 int odhcpd_get_interface_config(const char *ifname, const char *what)
150 {
151 char buf[64];
152 const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
153 snprintf(buf, sizeof(buf), sysctl_pattern, ifname, what);
154
155 int fd = open(buf, O_RDONLY);
156 ssize_t len = read(fd, buf, sizeof(buf) - 1);
157 close(fd);
158
159 if (len < 0)
160 return -1;
161
162 buf[len] = 0;
163 return atoi(buf);
164 }
165
166
167 // Read IPv6 MAC for interface
168 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
169 {
170 struct ifreq ifr;
171 memset(&ifr, 0, sizeof(ifr));
172 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
173 if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
174 return -1;
175 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
176 return 0;
177 }
178
179
180 // Forwards a packet on a specific interface
181 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
182 struct iovec *iov, size_t iov_len,
183 const struct interface *iface)
184 {
185 // Construct headers
186 uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
187 struct msghdr msg = {
188 .msg_name = (void *) dest,
189 .msg_namelen = sizeof(*dest),
190 .msg_iov = iov,
191 .msg_iovlen = iov_len,
192 .msg_control = cmsg_buf,
193 .msg_controllen = sizeof(cmsg_buf),
194 .msg_flags = 0
195 };
196
197 // Set control data (define destination interface)
198 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
199 chdr->cmsg_level = IPPROTO_IPV6;
200 chdr->cmsg_type = IPV6_PKTINFO;
201 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
202 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
203 pktinfo->ipi6_ifindex = iface->ifindex;
204
205 // Also set scope ID if link-local
206 if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
207 || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
208 dest->sin6_scope_id = iface->ifindex;
209
210 char ipbuf[INET6_ADDRSTRLEN];
211 inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
212
213 ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
214 if (sent < 0)
215 syslog(LOG_NOTICE, "Failed to send to %s%%%s (%s)",
216 ipbuf, iface->ifname, strerror(errno));
217 else
218 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
219 (long)sent, ipbuf, iface->ifname);
220 return sent;
221 }
222
223 struct addr_info {
224 int ifindex;
225 struct odhcpd_ipaddr **addrs;
226 int pending;
227 ssize_t ret;
228 };
229
230 static int cb_valid_handler(struct nl_msg *msg, void *arg)
231 {
232 struct addr_info *ctxt = (struct addr_info *)arg;
233 struct odhcpd_ipaddr *addrs = *(ctxt->addrs);
234 struct nlmsghdr *hdr = nlmsg_hdr(msg);
235 struct ifaddrmsg *ifa;
236 struct nlattr *nla[__IFA_MAX];
237
238 if (hdr->nlmsg_type != RTM_NEWADDR)
239 return NL_SKIP;
240
241 ifa = NLMSG_DATA(hdr);
242 if (ifa->ifa_scope != RT_SCOPE_UNIVERSE ||
243 (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex))
244 return NL_SKIP;
245
246 nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
247 if (!nla[IFA_ADDRESS])
248 return NL_SKIP;
249
250 addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
251 if (!addrs)
252 return NL_SKIP;
253
254 memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
255 addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
256
257 nla_memcpy(&addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
258 sizeof(addrs[ctxt->ret].addr));
259
260 if (nla[IFA_CACHEINFO]) {
261 struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
262
263 addrs[ctxt->ret].preferred = ifc->ifa_prefered;
264 addrs[ctxt->ret].valid = ifc->ifa_valid;
265 }
266
267 if (ifa->ifa_flags & IFA_F_DEPRECATED)
268 addrs[ctxt->ret].preferred = 0;
269
270 ctxt->ret++;
271 *(ctxt->addrs) = addrs;
272
273 return NL_OK;
274 }
275
276 static int cb_finish_handler(_unused struct nl_msg *msg, void *arg)
277 {
278 struct addr_info *ctxt = (struct addr_info *)arg;
279
280 ctxt->pending = 0;
281
282 return NL_STOP;
283 }
284
285 static int cb_error_handler(_unused struct sockaddr_nl *nla, struct nlmsgerr *err,
286 void *arg)
287 {
288 struct addr_info *ctxt = (struct addr_info *)arg;
289
290 ctxt->pending = 0;
291 ctxt->ret = err->error;
292
293 return NL_STOP;
294 }
295
296 // Detect an IPV6-address currently assigned to the given interface
297 ssize_t odhcpd_get_interface_addresses(int ifindex, struct odhcpd_ipaddr **addrs)
298 {
299 struct nl_msg *msg;
300 struct ifaddrmsg ifa = {
301 .ifa_family = AF_INET6,
302 .ifa_prefixlen = 0,
303 .ifa_flags = 0,
304 .ifa_scope = 0,
305 .ifa_index = ifindex, };
306 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
307 struct addr_info ctxt = {
308 .ifindex = ifindex,
309 .addrs = addrs,
310 .ret = 0,
311 .pending = 1,
312 };
313
314 if (!cb) {
315 ctxt.ret = -1;
316 goto out;
317 }
318
319 msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
320
321 if (!msg) {
322 ctxt.ret = - 1;
323 goto out;
324 }
325
326 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
327
328 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_valid_handler, &ctxt);
329 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_handler, &ctxt);
330 nl_cb_err(cb, NL_CB_CUSTOM, cb_error_handler, &ctxt);
331
332 nl_send_auto_complete(rtnl_socket, msg);
333 while (ctxt.pending > 0)
334 nl_recvmsgs(rtnl_socket, cb);
335
336 nlmsg_free(msg);
337 out:
338 nl_cb_put(cb);
339
340 return ctxt.ret;
341 }
342
343 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
344 {
345 int status = -1;
346 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
347 socklen_t alen = sizeof(addr);
348 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
349
350 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
351 !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
352 *lladdr = addr.sin6_addr;
353 status = 0;
354 }
355
356 close(sock);
357
358 return status;
359 }
360
361 /*
362 * DNS address selection criteria order :
363 * - use IPv6 address with valid lifetime if none is yet selected
364 * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated
365 * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address
366 * - use the IPv6 address with the longest preferred lifetime
367 */
368 int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr)
369 {
370 time_t now = odhcpd_time();
371 ssize_t m = -1;
372
373 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
374 if (iface->ia_addr[i].valid <= (uint32_t)now)
375 continue;
376
377 if (m < 0) {
378 m = i;
379 continue;
380 }
381
382 if (iface->ia_addr[m].preferred >= (uint32_t)now &&
383 iface->ia_addr[i].preferred < (uint32_t)now)
384 continue;
385
386 if (IN6_IS_ADDR_ULA(&iface->ia_addr[i].addr)) {
387 if (!IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr)) {
388 m = i;
389 continue;
390 }
391 } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr))
392 continue;
393
394 if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred)
395 m = i;
396 }
397
398 if (m >= 0) {
399 *addr = iface->ia_addr[m].addr;
400 return 0;
401 }
402
403 return odhcpd_get_linklocal_interface_address(iface->ifindex, addr);
404 }
405
406 int odhcpd_setup_route(const struct in6_addr *addr, const int prefixlen,
407 const struct interface *iface, const struct in6_addr *gw,
408 const uint32_t metric, const bool add)
409 {
410 struct nl_msg *msg;
411 struct rtmsg rtm = {
412 .rtm_family = AF_INET6,
413 .rtm_dst_len = prefixlen,
414 .rtm_src_len = 0,
415 .rtm_table = RT_TABLE_MAIN,
416 .rtm_protocol = (add ? RTPROT_STATIC : RTPROT_UNSPEC),
417 .rtm_scope = (add ? (gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK) : RT_SCOPE_NOWHERE),
418 .rtm_type = (add ? RTN_UNICAST : RTN_UNSPEC),
419 };
420 int ret = 0;
421
422 msg = nlmsg_alloc_simple(add ? RTM_NEWROUTE : RTM_DELROUTE,
423 add ? NLM_F_CREATE | NLM_F_REPLACE : 0);
424 if (!msg)
425 return -1;
426
427 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
428
429 nla_put(msg, RTA_DST, sizeof(*addr), addr);
430 nla_put_u32(msg, RTA_OIF, iface->ifindex);
431 nla_put_u32(msg, RTA_PRIORITY, metric);
432
433 if (gw)
434 nla_put(msg, RTA_GATEWAY, sizeof(*gw), gw);
435
436 ret = nl_send_auto_complete(rtnl_socket, msg);
437 nlmsg_free(msg);
438
439 if (ret < 0)
440 return ret;
441
442 return nl_wait_for_ack(rtnl_socket);
443 }
444
445 int odhcpd_setup_proxy_neigh(const struct in6_addr *addr,
446 const struct interface *iface, const bool add)
447 {
448 struct nl_msg *msg;
449 struct ndmsg ndm = {
450 .ndm_family = AF_INET6,
451 .ndm_flags = NTF_PROXY,
452 .ndm_ifindex = iface->ifindex,
453 };
454 int ret = 0, flags = NLM_F_REQUEST;
455
456 if (add)
457 flags |= NLM_F_REPLACE | NLM_F_CREATE;
458
459 msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags);
460 if (!msg)
461 return -1;
462
463 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
464
465 nla_put(msg, NDA_DST, sizeof(*addr), addr);
466
467 ret = nl_send_auto_complete(rtnl_socket, msg);
468 nlmsg_free(msg);
469
470 if (ret < 0)
471 return ret;
472
473 return nl_wait_for_ack(rtnl_socket);
474 }
475
476 struct interface* odhcpd_get_interface_by_index(int ifindex)
477 {
478 struct interface *iface;
479 list_for_each_entry(iface, &interfaces, head)
480 if (iface->ifindex == ifindex)
481 return iface;
482
483 return NULL;
484 }
485
486
487 struct interface* odhcpd_get_interface_by_name(const char *name)
488 {
489 struct interface *iface;
490 list_for_each_entry(iface, &interfaces, head)
491 if (!strcmp(iface->ifname, name))
492 return iface;
493
494 return NULL;
495 }
496
497
498 struct interface* odhcpd_get_master_interface(void)
499 {
500 struct interface *iface;
501 list_for_each_entry(iface, &interfaces, head)
502 if (iface->master)
503 return iface;
504
505 return NULL;
506 }
507
508
509 // Convenience function to receive and do basic validation of packets
510 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
511 {
512 struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
513
514 uint8_t data_buf[RELAYD_BUFFER_SIZE], cmsg_buf[128];
515 union {
516 struct sockaddr_in6 in6;
517 struct sockaddr_in in;
518 struct sockaddr_ll ll;
519 struct sockaddr_nl nl;
520 } addr;
521
522 if (u->error) {
523 int ret = -1;
524 socklen_t ret_len = sizeof(ret);
525 getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len);
526 u->error = false;
527 if (e->handle_error)
528 e->handle_error(e, ret);
529 }
530
531 if (e->recv_msgs) {
532 e->recv_msgs(e);
533 return;
534 }
535
536 while (true) {
537 struct iovec iov = {data_buf, sizeof(data_buf)};
538 struct msghdr msg = {
539 .msg_name = (void *) &addr,
540 .msg_namelen = sizeof(addr),
541 .msg_iov = &iov,
542 .msg_iovlen = 1,
543 .msg_control = cmsg_buf,
544 .msg_controllen = sizeof(cmsg_buf),
545 .msg_flags = 0
546 };
547
548 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
549 if (len < 0) {
550 if (errno == EAGAIN)
551 break;
552 else
553 continue;
554 }
555
556
557 // Extract destination interface
558 int destiface = 0;
559 int *hlim = NULL;
560 void *dest = NULL;
561 struct in6_pktinfo *pktinfo;
562 struct in_pktinfo *pkt4info;
563 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
564 if (ch->cmsg_level == IPPROTO_IPV6 &&
565 ch->cmsg_type == IPV6_PKTINFO) {
566 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
567 destiface = pktinfo->ipi6_ifindex;
568 dest = &pktinfo->ipi6_addr;
569 } else if (ch->cmsg_level == IPPROTO_IP &&
570 ch->cmsg_type == IP_PKTINFO) {
571 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
572 destiface = pkt4info->ipi_ifindex;
573 dest = &pkt4info->ipi_addr;
574 } else if (ch->cmsg_level == IPPROTO_IPV6 &&
575 ch->cmsg_type == IPV6_HOPLIMIT) {
576 hlim = (int*)CMSG_DATA(ch);
577 }
578 }
579
580 // Check hoplimit if received
581 if (hlim && *hlim != 255)
582 continue;
583
584 // Detect interface for packet sockets
585 if (addr.ll.sll_family == AF_PACKET)
586 destiface = addr.ll.sll_ifindex;
587
588 struct interface *iface =
589 odhcpd_get_interface_by_index(destiface);
590
591 if (!iface && addr.nl.nl_family != AF_NETLINK)
592 continue;
593
594 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
595 if (addr.ll.sll_family == AF_PACKET &&
596 len >= (ssize_t)sizeof(struct ip6_hdr))
597 inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
598 else if (addr.in6.sin6_family == AF_INET6)
599 inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
600 else if (addr.in.sin_family == AF_INET)
601 inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
602
603 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
604 ipbuf, (iface) ? iface->ifname : "netlink");
605
606 e->handle_dgram(&addr, data_buf, len, iface, dest);
607 }
608 }
609
610 // Register events for the multiplexer
611 int odhcpd_register(struct odhcpd_event *event)
612 {
613 event->uloop.cb = odhcpd_receive_packets;
614 return uloop_fd_add(&event->uloop, ULOOP_READ |
615 ((event->handle_error) ? ULOOP_ERROR_CB : 0));
616 }
617
618 int odhcpd_deregister(struct odhcpd_event *event)
619 {
620 event->uloop.cb = NULL;
621 return uloop_fd_delete(&event->uloop);
622 }
623
624 void odhcpd_process(struct odhcpd_event *event)
625 {
626 odhcpd_receive_packets(&event->uloop, 0);
627 }
628
629 int odhcpd_urandom(void *data, size_t len)
630 {
631 return read(urandom_fd, data, len);
632 }
633
634
635 time_t odhcpd_time(void)
636 {
637 struct timespec ts;
638 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
639 return ts.tv_sec;
640 }
641
642
643 static const char hexdigits[] = "0123456789abcdef";
644 static const int8_t hexvals[] = {
645 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
646 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
647 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
648 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
649 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
650 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
651 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
652 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
653 };
654
655 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
656 {
657 size_t c;
658 for (c = 0; c < len && src[0] && src[1]; ++c) {
659 int8_t x = (int8_t)*src++;
660 int8_t y = (int8_t)*src++;
661 if (x < 0 || (x = hexvals[x]) < 0
662 || y < 0 || (y = hexvals[y]) < 0)
663 return -1;
664 dst[c] = x << 4 | y;
665 while (((int8_t)*src) < 0 ||
666 (*src && hexvals[(uint8_t)*src] < 0))
667 src++;
668 }
669
670 return c;
671 }
672
673
674 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
675 {
676 for (size_t i = 0; i < len; ++i) {
677 *dst++ = hexdigits[src[i] >> 4];
678 *dst++ = hexdigits[src[i] & 0x0f];
679 }
680 *dst = 0;
681 }
682
683
684 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
685 {
686 const uint8_t *a = av, *b = bv;
687 size_t bytes = bits / 8;
688 bits %= 8;
689
690 int res = memcmp(a, b, bytes);
691 if (res == 0 && bits > 0)
692 res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
693
694 return res;
695 }
696
697
698 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
699 {
700 uint8_t *a = av;
701 const uint8_t *b = bv;
702
703 size_t bytes = bits / 8;
704 bits %= 8;
705 memcpy(a, b, bytes);
706
707 if (bits > 0) {
708 uint8_t mask = (1 << (8 - bits)) - 1;
709 a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);
710 }
711 }