DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
interface.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2012 Gregory Nietsky <gregory@distrotetch.co.za>
3  http://www.distrotech.co.za
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 
26 #ifndef __WIN32
27 #include <netinet/in.h>
28 #include <linux/if_vlan.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_tun.h>
32 #include <linux/if_arp.h>
33 #include <linux/sockios.h>
34 #include <linux/if.h>
35 #include <ifaddrs.h>
36 #include <sys/ioctl.h>
37 #include <netdb.h>
38 #else
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #define ETH_ALEN 8
42 #endif
43 
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdint.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "include/dtsapp.h"
52 #ifndef __WIN32
53 #include "libnetlink/include/libnetlink.h"
54 #include "libnetlink/include/ll_map.h"
55 #include "libnetlink/include/utils.h"
56 
57 static struct rtnl_handle *nlh;
58 
59 #endif
60 
61 
63 enum ipv4_score {
70 };
71 
73 enum ipv6_score {
80 };
81 
82 #ifndef __WIN32
83 
85 struct iplink_req {
87  struct nlmsghdr n;
89  struct ifinfomsg i;
91  char buf[1024];
92 };
93 
95 struct ipaddr_req {
97  struct nlmsghdr n;
99  struct ifaddrmsg i;
101  char buf[1024];
102 };
103 
104 static void nlhandle_free(void *data) {
105  struct rtnl_handle *nlh = data;
106 
107  if (data) {
108  rtnl_close(nlh);
109  }
110 }
111 
112 static struct rtnl_handle *nlhandle(int subscriptions) {
113  struct rtnl_handle *nlh;
114 
115  if (!(nlh = objalloc(sizeof(*nlh), nlhandle_free)) || (rtnl_open(nlh, 0))) {
116  if (nlh) {
117  objunref(nlh);
118  }
119  return (NULL);
120  }
121 
122  /*initilise the map*/
123  ll_init_map(nlh, 0);
124  objref(nlh);
125 
126  return (nlh);
127 }
128 
130 extern void closenetlink() {
131  if (nlh) {
132  objunref(nlh);
133  }
134 }
135 
139 extern int get_iface_index(const char *ifname) {
140  int ifindex;
141 
142  if (!objref(nlh) && !(nlh = nlhandle(0))) {
143  return (0);
144  }
145 
146  objlock(nlh);
147  ll_init_map(nlh, 1);
148  objunlock(nlh);
149 
150  ifindex = ll_name_to_index(ifname);
151 
152  objunref(nlh);
153  return (ifindex);
154 }
155 
159 static int delete_interface(char *iface) {
160  struct iplink_req *req;
161  int ifindex, ret;
162 
163  /*check ifname grab a ref to nlh or open it*/
164  if (strlenzero(iface) || (strlen(iface) > IFNAMSIZ) ||
165  (!objref(nlh) && !(nlh = nlhandle(0)))) {
166  return (-1);
167  }
168 
169  /*set the index of base interface*/
170  if (!(ifindex = get_iface_index(iface))) {
171  objunref(nlh);
172  return (-1);
173  }
174 
175  if (!(req = objalloc(sizeof(*req), NULL))) {
176  objunref(nlh);
177  return (-1);
178  }
179 
180  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
181  req->n.nlmsg_type = RTM_DELLINK;
182  req->n.nlmsg_flags = NLM_F_REQUEST;
183 
184  /*config base/dev/mac*/
185  req->i.ifi_index = ifindex;
186 
187  objlock(nlh);
188  ret = rtnl_talk(nlh, &req->n, 0, 0, NULL);
189  objunlock(nlh);
190 
191  objunref(nlh);
192  objunref(req);
193 
194  return (ret);
195 }
196 
201 extern int delete_kernvlan(char *ifname, int vid) {
202  char iface[IFNAMSIZ+1];
203 
204  /*check ifname grab a ref to nlh or open it*/
205  snprintf(iface, IFNAMSIZ, "%s.%i", ifname, vid);
206  return (delete_interface(iface));
207 }
208 
209 
214 extern int create_kernvlan(char *ifname, unsigned short vid) {
215  struct iplink_req *req;
216  char iface[IFNAMSIZ+1];
217  struct rtattr *data, *linkinfo;
218  char *type = "vlan";
219  int ifindex, ret;
220 
221  if (strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
222  (!objref(nlh) && !(nlh = nlhandle(0)))) {
223  return (-1);
224  }
225 
226  /*set the index of base interface*/
227  if (!(ifindex = get_iface_index(ifname))) {
228  objunref(nlh);
229  return (-1);
230  }
231 
232  if (!(req = objalloc(sizeof(*req), NULL))) {
233  objunref(nlh);
234  return (-1);
235  }
236 
237  snprintf(iface, IFNAMSIZ, "%s.%i", ifname, vid);
238  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
239  req->n.nlmsg_type = RTM_NEWLINK;
240  req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL | NLM_F_REQUEST;
241 
242  /*config base/dev/mac*/
243  addattr_l(&req->n, sizeof(*req), IFLA_LINK, &ifindex, sizeof(ifindex));
244  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, iface, strlen(iface));
245 
246  /*type*/
247  linkinfo = NLMSG_TAIL(&req->n);
248  addattr_l(&req->n, sizeof(*req), IFLA_LINKINFO, NULL, 0);
249  addattr_l(&req->n, sizeof(*req), IFLA_INFO_KIND, type, strlen(type));
250 
251  /*vid*/
252  data = NLMSG_TAIL(&req->n);
253  addattr_l(&req->n, sizeof(*req), IFLA_INFO_DATA, NULL, 0);
254  addattr_l(&req->n, sizeof(*req), IFLA_VLAN_ID, &vid, sizeof(vid));
255 
256  data->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)data;
257  linkinfo->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)linkinfo;
258 
259  objlock(nlh);
260  ret = rtnl_talk(nlh, &req->n, 0, 0, NULL);
261  objunlock(nlh);
262 
263  objunref(nlh);
264  objunref(req);
265 
266  return (ret);
267 }
268 
272 extern int delete_kernmac(char *ifname) {
273 
274  return (delete_interface(ifname));
275 }
276 
282 extern int create_kernmac(char *ifname, char *macdev, unsigned char *mac) {
283  struct iplink_req *req;
284  struct rtattr *data, *linkinfo;
285  unsigned char lmac[ETH_ALEN];
286  char *type = "macvlan";
287  int ifindex, ret;
288 
289  if (strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
290  strlenzero(macdev) || (strlen(macdev) > IFNAMSIZ) ||
291  (!objref(nlh) && !(nlh = nlhandle(0)))) {
292  return (-1);
293  }
294 
295  /*set the index of base interface*/
296  if (!(ifindex = get_iface_index(ifname))) {
297  objunref(nlh);
298  return (-1);
299  }
300 
301  if (!mac) {
302  randhwaddr(lmac);
303  } else {
304  strncpy((char *)lmac, (char *)mac, ETH_ALEN);
305  }
306 
307  if (!(req = objalloc(sizeof(*req), NULL))) {
308  objunref(nlh);
309  return (-1);
310  }
311 
312  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
313  req->n.nlmsg_type = RTM_NEWLINK;
314  req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL | NLM_F_REQUEST;
315 
316  /*config base/dev/mac*/
317  addattr_l(&req->n, sizeof(*req), IFLA_LINK, &ifindex, 4);
318  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, macdev, strlen(macdev));
319  addattr_l(&req->n, sizeof(*req), IFLA_ADDRESS, lmac, ETH_ALEN);
320 
321  /*type*/
322  linkinfo = NLMSG_TAIL(&req->n);
323  addattr_l(&req->n, sizeof(*req), IFLA_LINKINFO, NULL, 0);
324  addattr_l(&req->n, sizeof(*req), IFLA_INFO_KIND, type, strlen(type));
325 
326  /*mode*/
327  data = NLMSG_TAIL(&req->n);
328  addattr_l(&req->n, sizeof(*req), IFLA_INFO_DATA, NULL, 0);
329  addattr32(&req->n, sizeof(*req), IFLA_MACVLAN_MODE, MACVLAN_MODE_PRIVATE);
330  data->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)data;
331  linkinfo->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)linkinfo;
332 
333  objlock(nlh);
334  ret = rtnl_talk(nlh, &req->n, 0, 0, NULL);
335  objunlock(nlh);
336 
337  objunref(nlh);
338  objunref(req);
339 
340  return (ret);
341 }
342 
348 extern int set_interface_flags(int ifindex, int set, int clear) {
349  struct iplink_req *req;
350  int flags;
351 
352  if (!objref(nlh) && !(nlh = nlhandle(0))) {
353  return (-1);
354  }
355 
356  flags = ll_index_to_flags(ifindex);
357 
358  flags |= set;
359  flags &= ~(clear);
360 
361  if (!(req = objalloc(sizeof(*req), NULL))) {
362  objunref(nlh);
363  return (-1);
364  }
365 
366  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
367  req->n.nlmsg_type = RTM_NEWLINK;
368  req->n.nlmsg_flags = NLM_F_REQUEST;
369 
370  /*config base/dev/mac*/
371  req->i.ifi_index = ifindex;
372  req->i.ifi_flags = flags;
373  req->i.ifi_change = set | clear;
374 
375  objlock(nlh);
376  rtnl_talk(nlh, &req->n, 0, 0, NULL);
377  objunlock(nlh);
378 
379  objunref(nlh);
380  objunref(req);
381  return (0);
382 }
383 
388 extern int set_interface_addr(int ifindex, const unsigned char *hwaddr) {
389  struct iplink_req *req;
390 
391  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
392  return (-1);
393  }
394 
395  if (!(req = objalloc(sizeof(*req), NULL))) {
396  objunref(nlh);
397  return (-1);
398  }
399 
400  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
401  req->n.nlmsg_type = RTM_NEWLINK;
402  req->n.nlmsg_flags = NLM_F_REQUEST;
403  req->i.ifi_index = ifindex;
404 
405  /*config base/dev/mac*/
406  addattr_l(&req->n, sizeof(*req), IFLA_ADDRESS, hwaddr, ETH_ALEN);
407 
408  objlock(nlh);
409  rtnl_talk(nlh, &req->n, 0, 0, NULL);
410  objunlock(nlh);
411 
412  objunref(nlh);
413  objunref(req);
414  return (0);
415 }
416 
421 extern int set_interface_name(int ifindex, const char *name) {
422  struct iplink_req *req;
423 
424  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
425  return (-1);
426  }
427 
428  if (!(req = objalloc(sizeof(*req), NULL))) {
429  objunref(nlh);
430  return (-1);
431  }
432 
433  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
434  req->n.nlmsg_type = RTM_NEWLINK;
435  req->n.nlmsg_flags = NLM_F_REQUEST;
436  req->i.ifi_index = ifindex;
437 
438  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, name, strlen((char *)name));
439 
440  objlock(nlh);
441  rtnl_talk(nlh, &req->n, 0, 0, NULL);
442  objunlock(nlh);
443 
444  objunref(nlh);
445  objunref(req);
446  return (0);
447 }
448 
453 extern int interface_bind(char *iface, int protocol) {
454  struct sockaddr_ll sll;
455  int proto = htons(protocol);
456  int fd, ifindex;
457 
458  /*set the network dev up*/
459  if (!(ifindex = get_iface_index(iface))) {
460  return (-1);
461  }
462  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING, 0);
463 
464  /* open network raw socket */
465  if ((fd = socket(PF_PACKET, SOCK_RAW, proto)) < 0) {
466  return (-1);
467  }
468 
469  /*bind to the interface*/
470  memset(&sll, 0, sizeof(sll));
471  sll.sll_family = PF_PACKET;
472  sll.sll_protocol = proto;
473  sll.sll_ifindex = ifindex;
474  if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
475  perror("bind failed");
476  close(fd);
477  return (-1);
478  }
479 
480  return (fd);
481 }
482 
485 extern void randhwaddr(unsigned char *addr) {
486  genrand(addr, ETH_ALEN);
487  addr [0] &= 0xfe; /* clear multicast bit */
488  addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
489 }
490 
496 extern int create_tun(const char *ifname, const unsigned char *hwaddr, int flags) {
497  struct ifreq ifr;
498  int fd, ifindex;
499  char *tundev = "/dev/net/tun";
500 
501  /* open the tun/tap clone dev*/
502  if ((fd = open(tundev, O_RDWR)) < 0) {
503  return (-1);
504  }
505 
506  /* configure the device*/
507  memset(&ifr, 0, sizeof(ifr));
508  ifr.ifr_flags = flags;
509  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
510  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0 ) {
511  perror("ioctl(TUNSETIFF) failed\n");
512  close(fd);
513  return (-1);
514  }
515 
516  if (!(ifindex = get_iface_index(ifname))) {
517  return (-1);
518  }
519 
520  /* set the MAC address*/
521  if (hwaddr) {
522  set_interface_addr(ifindex, hwaddr);
523  }
524 
525  /*set the network dev up*/
526  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING | IFF_MULTICAST | IFF_BROADCAST, 0);
527 
528  return (fd);
529 }
530 
535 extern int ifdown(const char *ifname, int flags) {
536  int ifindex;
537 
538  /*down the device*/
539  if (!(ifindex = get_iface_index(ifname))) {
540  return (-1);
541  }
542 
543  /*set the network dev up*/
544  set_interface_flags(ifindex, 0, IFF_UP | IFF_RUNNING | flags);
545 
546  return (0);
547 }
548 
553 extern int ifup(const char *ifname, int flags) {
554  int ifindex;
555 
556  /*down the device*/
557  if (!(ifindex = get_iface_index(ifname))) {
558  return (-1);
559  }
560 
561  /*set the network dev up*/
562  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING | flags, 0);
563 
564  return (0);
565 }
566 
571 extern int ifrename(const char *oldname, const char *newname) {
572  int ifindex;
573 
574  ifdown(oldname, 0);
575 
576  if (!(ifindex = get_iface_index(oldname))) {
577  return (-1);
578  }
579  set_interface_name(ifindex, newname);
580 
581  return (0);
582 }
583 
588 extern int ifhwaddr(const char *ifname, unsigned char *hwaddr) {
589  int ifindex;
590 
591  if (!hwaddr || strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
592  (!objref(nlh) && !(nlh = nlhandle(0)))) {
593  return (-1);
594  }
595 
596  /*set the index of base interface*/
597  if (!(ifindex = get_iface_index(ifname))) {
598  objunref(nlh);
599  return (-1);
600  }
601 
602  ll_index_to_addr(ifindex, hwaddr, ETH_ALEN);
603  objunref(nlh);
604  return (0);
605 }
606 
611 extern int set_interface_ipaddr(char *ifname, char *ipaddr) {
612  struct ipaddr_req *req;
613  inet_prefix lcl;
614  int ifindex, bcast;
615 
616  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
617  return (-1);
618  }
619 
620  if (!(req = objalloc(sizeof(*req), NULL))) {
621  objunref(nlh);
622  return (-1);
623  }
624 
625  /*set the index of base interface*/
626  if (!(ifindex = get_iface_index(ifname))) {
627  objunref(nlh);
628  return (-1);
629  }
630 
631  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
632  req->n.nlmsg_type = RTM_NEWADDR;
633  req->n.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE;
634 
635  req->i.ifa_scope = RT_SCOPE_HOST;
636  req->i.ifa_index = ifindex;
637 
638  get_prefix(&lcl, ipaddr, AF_UNSPEC);
639  req->i.ifa_family = lcl.family;
640  req->i.ifa_prefixlen = lcl.bitlen;
641 
642  addattr_l(&req->n, sizeof(*req), IFA_LOCAL, &lcl.data, lcl.bytelen);
643  addattr_l(&req->n, sizeof(*req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
644  if (lcl.family == AF_INET) {
645  bcast = htonl((1 << (32 - lcl.bitlen)) - 1);
646  addattr32(&req->n, sizeof(*req), IFA_BROADCAST, lcl.data[0] | bcast);
647  }
648 
649  objlock(nlh);
650  rtnl_talk(nlh, &req->n, 0, 0, NULL);
651  objunlock(nlh);
652 
653  objunref(nlh);
654  objunref(req);
655  return (0);
656 }
657 #endif
658 
668 extern void eui48to64(unsigned char *mac48, unsigned char *eui64) {
669  eui64[0] = (mac48[0] & 0xFE) ^ 0x02; /*clear multicast bit and flip local asignment*/
670  eui64[1] = mac48[1];
671  eui64[2] = mac48[2];
672  eui64[3] = 0xFF;
673  eui64[4] = 0xFE;
674  eui64[5] = mac48[3];
675  eui64[6] = mac48[4];
676  eui64[7] = mac48[5];
677 }
678 
686 #ifndef __WIN32
687 extern int get_ip6_addrprefix(const char *iface, unsigned char *prefix) {
688  uint64_t ntpts;
689  unsigned char eui64[8];
690  unsigned char sha1[20];
691  unsigned char mac48[ETH_ALEN];
692  struct timeval tv;
693 
694  if (ifhwaddr(iface, mac48)) {
695  return (-1);
696  }
697 
698  gettimeofday(&tv, NULL);
699  ntpts = tvtontp64(&tv);
700 
701  eui48to64(mac48, eui64);
702  sha1sum2(sha1, (void *)&ntpts, sizeof(ntpts), (void *)eui64, sizeof(eui64));
703 
704  prefix[0] = 0xFD; /*0xFC | 0x01 FC00/7 with local bit set [8th bit]*/
705  memcpy(prefix + 1, sha1+15, 5); /*LSD 40 bits of the SHA hash*/
706 
707  return (0);
708 }
709 #endif
710 
718 int score_ipv4(struct sockaddr_in *sa4, char *ipaddr, int iplen) {
719  uint32_t addr;
720  int nscore;
721 
722  addr = sa4->sin_addr.s_addr;
723 
724  /* Get ipaddr string*/
725  inet_ntop(AF_INET, &sa4->sin_addr, ipaddr, iplen);
726 
727  /* Score the IP*/
728  if (!((0xa9fe0000 ^ ntohl(addr)) >> 16)) {
729  nscore = IPV4_SCORE_ZEROCONF;
730  } else if (reservedip(ipaddr)) {
731  nscore = IPV4_SCORE_RESERVED;
732  } else {
733  nscore = IPV4_SCORE_ROUTABLE;
734  }
735 
736  return nscore;
737 }
738 
746 int score_ipv6(struct sockaddr_in6 *sa6, char *ipaddr, int iplen) {
747  uint32_t *ipptr, match;
748  int nscore;
749 
750 #ifndef __WIN32
751  ipptr = sa6->sin6_addr.s6_addr32;
752 #else
753  ipptr = (uint32_t*)sa6->sin6_addr.u.Word;
754 #endif
755  match = ntohl(ipptr[0]) >> 16;
756 
757  /* exclude link local multicast and special addresses */
758  if (!(0xFE80 ^ match) || !(0xFF ^ (match >> 8)) || !match) {
759  return 0;
760  }
761 
762  /*Score ip private/sixin4/routable*/
763  if (!(0xFC ^ (match >> 9))) {
764  nscore = IPV6_SCORE_RESERVED;
765  } else if (match == 2002) {
766  nscore = IPV6_SCORE_SIXIN4;
767  } else {
768  nscore = IPV6_SCORE_ROUTABLE;
769  }
770  inet_ntop(AF_INET6, ipptr, ipaddr, iplen);
771 
772  return nscore;
773 }
774 
775 
782 #ifndef __WIN32
783 const char *get_ifipaddr(const char *iface, int family) {
784  struct ifaddrs *ifaddr, *ifa;
785  struct sockaddr_in *ipv4addr;
786  int score = 0, nscore, iflen;
787  uint32_t subnet = 0, match;
788  char host[NI_MAXHOST] = "", tmp[NI_MAXHOST];
789 
790  if (!iface || getifaddrs(&ifaddr) == -1) {
791  return NULL;
792  }
793 
794  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
795  iflen = strlen(iface);
796  if ((ifa->ifa_addr == NULL) || strncmp(ifa->ifa_name, iface, iflen) || (ifa->ifa_addr->sa_family != family)) {
797  continue;
798  }
799 
800  /* Match aliases not vlans*/
801  if ((strlen(ifa->ifa_name) > iflen) && (ifa->ifa_name[iflen] != ':')) {
802  continue;
803  }
804 
805  switch (ifa->ifa_addr->sa_family) {
806  case AF_INET:
807  /* Find best ip address for a interface lowest priority is given to zeroconf then reserved ip's
808  * finally find hte ip with shortest subnet bits.*/
809  ipv4addr = (struct sockaddr_in*)ifa->ifa_netmask;
810  match = ntohl(~ipv4addr->sin_addr.s_addr);
811 
812  nscore = score_ipv4((struct sockaddr_in*)ifa->ifa_addr, tmp, NI_MAXHOST);
813 
814  /* match score and subnet*/
815  if ((nscore > score) || ((nscore == score) && (match > subnet))) {
816  score = nscore;
817  subnet = match;
818  strncpy(host, tmp, NI_MAXHOST);
819  }
820  break;
821  case AF_INET6:
822  nscore = score_ipv6((struct sockaddr_in6*)ifa->ifa_addr, tmp, NI_MAXHOST);
823 
824  if (nscore > score) {
825  score = nscore;
826  strncpy(host, tmp, NI_MAXHOST);
827  }
828  break;
829  }
830  }
831  freeifaddrs(ifaddr);
832  return (strlenzero(host)) ? NULL : strdup(host);
833 }
834 #endif
uint64_t tvtontp64(struct timeval *tv)
Convert a timeval struct to 64bit NTP time.
Definition: util.c:405
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int ifrename(const char *oldname, const char *newname)
Rename interface helper.
Definition: interface.c:571
int objref(void *data)
Reference a object.
Definition: refobj.c:153
ipv4_score
Order of precidence of ipv4.
Definition: interface.c:63
Reseverd &quot;private&quot; ip addresses.
Definition: interface.c:67
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int set_interface_addr(int ifindex, const unsigned char *hwaddr)
Set interface MAC addr.
Definition: interface.c:388
int interface_bind(char *iface, int protocol)
Bind to device fd may be a existing socket.
Definition: interface.c:453
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
void closenetlink(void)
Close netlink socket on application termination.
Definition: interface.c:130
int score_ipv6(struct sockaddr_in6 *sa6, char *ipaddr, int iplen)
Return a score for a IPv6 addrress.
Definition: interface.c:746
int create_kernmac(char *ifname, char *macdev, unsigned char *mac)
Create a kernal MAC VLAN.
Definition: interface.c:282
int ifhwaddr(const char *ifname, unsigned char *hwaddr)
Get MAC addr for interface.
Definition: interface.c:588
void randhwaddr(unsigned char *addr)
create random MAC address
Definition: interface.c:485
const char * get_ifipaddr(const char *iface, int family)
Find best IP adress for a interface.
Definition: interface.c:783
int set_interface_ipaddr(char *ifname, char *ipaddr)
Set IP addr on interface.
Definition: interface.c:611
int score_ipv4(struct sockaddr_in *sa4, char *ipaddr, int iplen)
Return a score for a IPv4 addrress.
Definition: interface.c:718
DTS Application library API Include file.
void sha1sum2(unsigned char *buff, const void *data, unsigned long len, const void *data2, unsigned long len2)
Calculate the SHA1 hash accross 2 data chunks.
Definition: util.c:156
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int create_tun(const char *ifname, const unsigned char *hwaddr, int flags)
Create a tunnel device.
Definition: interface.c:496
int set_interface_flags(int ifindex, int set, int clear)
Alter interface flags.
Definition: interface.c:348
Routable IP&#39;s.
Definition: interface.c:69
struct nlmsghdr n
Netlink message header.
Definition: interface.c:97
Zeroconf IP&#39;s 169.254/16.
Definition: interface.c:65
int reservedip(const char *ipaddr)
Check IP against list of reserved IP&#39;s.
Definition: iputil.c:384
void eui48to64(unsigned char *mac48, unsigned char *eui64)
Generate IPv6 address from mac address.
Definition: interface.c:668
int create_kernvlan(char *ifname, unsigned short vid)
Create a VLAN on a interface.
Definition: interface.c:214
int set_interface_name(int ifindex, const char *name)
Rename interface.
Definition: interface.c:421
int genrand(void *buf, int len)
Generate random sequence.
Definition: util.c:82
int get_ip6_addrprefix(const char *iface, unsigned char *prefix)
Generate Unique Local IPv6 Unicast Addresses RFC 4193.
Definition: interface.c:687
int delete_kernvlan(char *ifname, int vid)
Delete a VLAN.
Definition: interface.c:201
ipv6_score
Return best ipv6 address in order of FFC/7 2002/16 ...
Definition: interface.c:73
char buf[1024]
Request buffer.
Definition: interface.c:101
Adminstrivly allocated addresses (FC/7)
Definition: interface.c:75
6in4 address space
Definition: interface.c:77
IP Netlink IP addr request.
Definition: interface.c:95
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct ifaddrmsg i
Interface addr message.
Definition: interface.c:99
Other routable addresses.
Definition: interface.c:79
const char * inet_ntop(int af, const void *src, char *dest, socklen_t size)
Win32 implementation of inet_ntop.
Definition: winiface.cpp:43
int delete_kernmac(char *macdev)
Delete Kernel MAC VLAN.
Definition: interface.c:272
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ifup(const char *ifname, int flags)
Set interface up.
Definition: interface.c:553
int ifdown(const char *ifname, int flags)
Set interface down.
Definition: interface.c:535