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

Helper functions for various calculations. More...

Files

file  iputil.c
 IPv4 And IPv6 Utiliies.
 

Functions

void eui48to64 (unsigned char *mac48, unsigned char *eui64)
 Generate IPv6 address from mac address. More...
 
int get_ip6_addrprefix (const char *iface, unsigned char *prefix)
 Generate Unique Local IPv6 Unicast Addresses RFC 4193. More...
 
int score_ipv6 (struct sockaddr_in6 *sa6, char *ipaddr, int iplen)
 Return a score for a IPv6 addrress. More...
 
int checkipv6mask (const char *ipaddr, const char *network, uint8_t bits)
 Check if ipaddr is in a network. More...
 
int packetchecksumv6 (uint8_t *pkt)
 Prototype to check checksup on packet. More...
 
char * ipv6to4prefix (const char *ipaddr)
 Return IPv6 to IPv4 Prefix fot the address. More...
 

Detailed Description

Helper functions for various calculations.

Function Documentation

int checkipv6mask ( const char *  ipaddr,
const char *  network,
uint8_t  bits 
)

Check if ipaddr is in a network.

Parameters
ipaddrTo check.
networkNetwork to check against.
bitsNetwork length.
Returns
0 if the ipaddr is in the network.

Definition at line 47 of file iputil.c.

47  {
48  uint8_t cnt, bytelen, bitlen;
49  uint32_t mask, res = 0;
50  uint32_t *nw = (uint32_t *)network;
51  uint32_t *ip = (uint32_t *)ipaddr;
52 
53  /*calculate significant bytes and bits outside boundry*/
54  if ((bitlen = bits % 32)) {
55  bytelen = (bits - bitlen) / 32;
56  bytelen++;
57  } else {
58  bytelen = bits / 32;
59  }
60 
61  /*end loop on first mismatch do not check last block*/
62  for(cnt = 0; (!res && (cnt < (bytelen - 1))); cnt++) {
63  res += nw[cnt] ^ ip[cnt];
64  }
65 
66  /*process last block if no error sofar*/
67  if (!res) {
68  mask = (bitlen) ? htonl(~((1 << (32 - bitlen)) - 1)) : -1;
69  res += (nw[cnt] & mask) ^ (ip[cnt] & mask);
70  }
71 
72  return (res);
73 }
void eui48to64 ( unsigned char *  mac48,
unsigned char *  eui64 
)

Generate IPv6 address from mac address.

this method is sourced from the following IEEE publication Guidelines for 64-bit Global Identifier (EUI-64TM) Registration Authority mac48 is char[ETH_ALEN] eui64 is char[8]

Parameters
mac48Buffer containing MAC address 6 bytes.
eui64Buffer that will be written with address 8bytes.

Definition at line 668 of file interface.c.

Referenced by get_ip6_addrprefix().

668  {
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 }
int get_ip6_addrprefix ( const char *  iface,
unsigned char *  prefix 
)

Generate Unique Local IPv6 Unicast Addresses RFC 4193.

Todo:
WIN32 support
Parameters
ifaceExternal system interface name.
prefixA buffer char[6] that will contain the prefix.
Returns
-1 on error.

Definition at line 687 of file interface.c.

References eui48to64(), ifhwaddr(), sha1sum2(), and tvtontp64().

687  {
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 }
uint64_t tvtontp64(struct timeval *tv)
Convert a timeval struct to 64bit NTP time.
Definition: util.c:405
int ifhwaddr(const char *ifname, unsigned char *hwaddr)
Get MAC addr for interface.
Definition: interface.c:588
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
void eui48to64(unsigned char *mac48, unsigned char *eui64)
Generate IPv6 address from mac address.
Definition: interface.c:668
char* ipv6to4prefix ( const char *  ipaddr)

Return IPv6 to IPv4 Prefix fot the address.

Parameters
ipaddrIPv4 Address to obtain mapping for
Returns
6to4 Address prefix.

Definition at line 427 of file iputil.c.

427  {
428  uint32_t ip;
429  uint8_t *ipa;
430  char *pre6;
431 
432 #ifndef __WIN32
433  if (!inet_pton(AF_INET, ipaddr, &ip)) {
434  return NULL;
435  }
436 #else
437  if (!(ip = inet_addr(ipaddr))) {
438  return NULL;
439  }
440 #endif
441 
442  pre6 = malloc(10);
443  ipa=(uint8_t*)&ip;
444  snprintf(pre6, 10, "%02x%02x:%02x%02x", ipa[0], ipa[1], ipa[2], ipa[3]);
445  return pre6;
446 }
int packetchecksumv6 ( uint8_t *  pkt)

Prototype to check checksup on packet.

Parameters
pktPacket buffer to check.

Definition at line 189 of file iputil.c.

189  {
190  struct iphdr *ip = (struct iphdr *)pkt;
191  switch(ip->protocol) {
192  case IPPROTO_ICMP:
193  break;
194  case IPPROTO_TCP:
195  break;
196  case IPPROTO_UDP:
197  break;
198  default:
199  return (-1);
200  }
201  return (0);
202 }
int score_ipv6 ( struct sockaddr_in6 *  sa6,
char *  ipaddr,
int  iplen 
)

Return a score for a IPv6 addrress.

Note
This does not follow the RFC as gettaddrinfo would.
Parameters
sa6Socket addr to check.
ipaddrBuffer to place IP address.
iplenLength of IP buffer.
Returns
Score based on the IP address Highest is "routable" lowest is Internal allocation.

Definition at line 746 of file interface.c.

References inet_ntop(), IPV6_SCORE_RESERVED, IPV6_SCORE_ROUTABLE, and IPV6_SCORE_SIXIN4.

Referenced by get_ifinfo(), and get_ifipaddr().

746  {
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 }
Adminstrivly allocated addresses (FC/7)
Definition: interface.c:75
6in4 address space
Definition: interface.c:77
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