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.
 

Data Structures

struct  pseudohdr
 IPv4 header structur to cast a packet too. More...
 

Functions

int score_ipv4 (struct sockaddr_in *sa4, char *ipaddr, int iplen)
 Return a score for a IPv4 addrress. More...
 
void ipv4tcpchecksum (uint8_t *pkt)
 Update the TCP checksum of a IPv4 packet. More...
 
void ipv4udpchecksum (uint8_t *pkt)
 Update the UDP checksum of a IPv4 packet. More...
 
void ipv4icmpchecksum (uint8_t *pkt)
 Set the checksup of a IPv4 ICMP packet. More...
 
void ipv4checksum (uint8_t *pkt)
 Set the checksup of a IPv4 Packet. More...
 
int packetchecksumv4 (uint8_t *pkt)
 Update the checksum of a IPv4 packet. More...
 
const char * cidrtosn (int bitlen, char *buf, int size)
 Return the dotted quad notation subnet mask from a CIDR. More...
 
const char * getnetaddr (const char *ipaddr, int cidr, char *buf, int size)
 Return the network address. More...
 
const char * getfirstaddr (const char *ipaddr, int cidr, char *buf, int size)
 Get the first usable address. More...
 
const char * getbcaddr (const char *ipaddr, int cidr, char *buf, int size)
 Return broadcast address. More...
 
const char * getlastaddr (const char *ipaddr, int cidr, char *buf, int size)
 Get the last usable address. More...
 
uint32_t cidrcnt (int bitlen)
 Return the number of IP addresses in a given bitmask. More...
 
int reservedip (const char *ipaddr)
 Check IP against list of reserved IP's. More...
 
int check_ipv4 (const char *ip, int cidr, const char *test)
 Check if a IP address is in a network. More...
 

Detailed Description

Helper functions for various calculations.

Function Documentation

int check_ipv4 ( const char *  ip,
int  cidr,
const char *  test 
)

Check if a IP address is in a network.

Note
ipaddr will be truncated to network address based on cidr.
Parameters
ipNetwork address to check against.
cidrNumber of bits in the subnet.
testIP address to check
Returns
0 if test is not in the network ip/cidr.

Definition at line 456 of file iputil.c.

456  {
457  uint32_t ip1, ip2;
458 
459 #ifndef __WIN32
460  inet_pton(AF_INET, ip, &ip1);
461  inet_pton(AF_INET, test, &ip2);
462 #else
463  ip1 = inet_addr(ip);
464  ip2 = inet_addr(test);
465 #endif
466 
467  ip1 = ntohl(ip1) >> (32-cidr);
468  ip2 = ntohl(ip2) >> (32-cidr);
469 
470  if (!(ip1 ^ ip2)) {
471  return 1;
472  } else {
473  return 0;
474  }
475 }
uint32_t cidrcnt ( int  bitlen)

Return the number of IP addresses in a given bitmask.

Parameters
bitlenSubnet bits (CIDR).
Returns
Number of IP addreses including network and broadcast address.

Definition at line 372 of file iputil.c.

372  {
373  if (bitlen) {
374  return pow(2, (32-bitlen));
375  } else {
376  return 0xFFFFFFFF;
377  }
378 }
const char* cidrtosn ( int  bitlen,
char *  buf,
int  size 
)

Return the dotted quad notation subnet mask from a CIDR.

Parameters
bitlenSubnet length bits.
bufBuffer to copy the subnet address too.
sizeSize of buffer.
Returns
pointer to buffer on success or NULL.

Definition at line 228 of file iputil.c.

228  {
229  uint32_t nm;
230  uint8_t *nmb = (uint8_t*)&nm;
231 
232  if (!buf) {
233  return NULL;
234  }
235 
236  if (bitlen) {
237  nm = ~((1 << (32-bitlen))-1);
238  } else {
239  nm = 0;
240  }
241 
242  snprintf(buf, size, "%i.%i.%i.%i", nmb[3], nmb[2], nmb[1], nmb[0]);
243  return buf;
244 }
const char* getbcaddr ( const char *  ipaddr,
int  cidr,
char *  buf,
int  size 
)

Return broadcast address.

Note
ipaddr will be truncated to network address based on cidr.
Parameters
ipaddrNetwork address.
cidrCIDR subnet bit length.
bufBuffer to copy address too.
sizeLength of buffer.
Returns
Pointer to buffer or NULL on error.

Definition at line 319 of file iputil.c.

319  {
320  uint32_t ip, mask;
321  uint8_t *ipb = (uint8_t*)&ip;
322 
323 #ifndef __WIN32
324  inet_pton(AF_INET, ipaddr, &ip);
325 #else
326  ip = inet_addr(ipaddr);
327 #endif
328  if (cidr) {
329  mask = (1 << (32-cidr))-1;
330  ip = ntohl(ip);
331  ip = (ip & ~mask) | mask;
332  } else {
333  ip = 0;
334  }
335  snprintf(buf, size, "%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
336  return buf;
337 }
const char* getfirstaddr ( const char *  ipaddr,
int  cidr,
char *  buf,
int  size 
)

Get the first usable address.

Note
ipaddr will be truncated to network address based on cidr.
Parameters
ipaddrNetwork address.
cidrBits in the subnet mask.
bufBuffer that the result is placed in.
sizeLength of buffer.
Returns
Pointer to buf with the result copied to buf.

Definition at line 286 of file iputil.c.

286  {
287  uint32_t ip;
288  uint8_t *ipb = (uint8_t*)&ip;
289 
290  if (!buf) {
291  return NULL;
292  }
293 
294 #ifndef __WIN32
295  inet_pton(AF_INET, ipaddr, &ip);
296 #else
297  ip = inet_addr(ipaddr);
298 #endif
299  if (cidr) {
300  ip = ntohl(ip);
301  ip = ip & ~((1 << (32-cidr))-1);
302  ip++;
303  } else {
304  ip = 1;
305  }
306 
307  snprintf(buf, size, "%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
308  return buf;
309 }
const char* getlastaddr ( const char *  ipaddr,
int  cidr,
char *  buf,
int  size 
)

Get the last usable address.

Note
ipaddr will be truncated to network address based on cidr.
Parameters
ipaddrNetwork address.
cidrBits in the subnet mask.
bufBuffer that the result is placed in.
sizeLength of buffer.
Returns
Pointer to buf with the result copied to buf.

Definition at line 347 of file iputil.c.

347  {
348  uint32_t ip, mask;
349  uint8_t *ipb = (uint8_t*)&ip;
350 
351 #ifndef __WIN32
352  inet_pton(AF_INET, ipaddr, &ip);
353 #else
354  ip = inet_addr(ipaddr);
355 #endif
356  if (cidr) {
357  mask = (1 << (32-cidr))-1;
358  ip = ntohl(ip);
359  ip = (ip & ~mask) | mask;
360  ip--;
361  } else {
362  ip = 0;
363  }
364  snprintf(buf, size, "%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
365  return buf;
366 }
const char* getnetaddr ( const char *  ipaddr,
int  cidr,
char *  buf,
int  size 
)

Return the network address.

Note
ipaddr will be truncated to network address based on cidr.
Parameters
ipaddrIpaddr to calculate for
cidrLength of the subnet bitmask.
bufBuffer that the result is placed in.
sizeLength of buffer.
Returns
Pointer to buf with the result copied to buf.

Definition at line 254 of file iputil.c.

254  {
255  uint32_t ip;
256  uint8_t *ipb = (uint8_t*)&ip;
257 
258  if (!buf) {
259  return NULL;
260  }
261 
262 #ifndef __WIN32
263  inet_pton(AF_INET, ipaddr, &ip);
264 #else
265  ip = inet_addr(ipaddr);
266 #endif
267  if (cidr) {
268  ip = ntohl(ip);
269  ip = ip & ~((1 << (32-cidr))-1);
270  } else {
271  ip = 0;
272  }
273 
274  snprintf(buf, size, "%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
275  return buf;
276 }
void ipv4checksum ( uint8_t *  pkt)

Set the checksup of a IPv4 Packet.

Parameters
pktPacket to update.

Definition at line 154 of file iputil.c.

References checksum().

Referenced by packetchecksumv4().

154  {
155  struct iphdr *ip = (struct iphdr *)pkt;
156 
157  ip->check = 0;
158  ip->check = checksum(ip, (4 * ip->ihl));
159 }
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
Definition: util.c:452
void ipv4icmpchecksum ( uint8_t *  pkt)

Set the checksup of a IPv4 ICMP packet.

Parameters
pktICMP Packet to update.

Definition at line 143 of file iputil.c.

References checksum().

Referenced by packetchecksumv4().

143  {
144  struct iphdr *ip = (struct iphdr *)pkt;
145  struct icmphdr *icmp = (struct icmphdr *)(pkt + (4 * ip->ihl));
146 
147  icmp->checksum = 0;
148  icmp->checksum = checksum(icmp, ntohs(ip->tot_len) - (ip->ihl *4));
149 }
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
Definition: util.c:452
void ipv4tcpchecksum ( uint8_t *  pkt)

Update the TCP checksum of a IPv4 packet.

Parameters
pktPacket to update TCP checksum.

Definition at line 101 of file iputil.c.

References checksum(), checksum_add(), pseudohdr::daddr, pseudohdr::len, pseudohdr::proto, pseudohdr::saddr, and pseudohdr::zero.

Referenced by packetchecksumv4().

101  {
102  struct iphdr *ip = (struct iphdr *)pkt;
103  struct tcphdr *tcp = (struct tcphdr *)(pkt + (4 * ip->ihl));
104  uint16_t plen, csum;
105  struct pseudohdr phdr;
106 
107  /* get tcp packet len*/
108  plen = ntohs(ip->tot_len) - (4 * ip->ihl);
109  tcp->check = 0;
110  phdr.saddr = ip->saddr;
111  phdr.daddr = ip->daddr;
112  phdr.zero = 0;
113  phdr.proto = ip->protocol;
114  phdr.len = htons(plen);
115  csum = checksum(&phdr, sizeof(phdr));
116  tcp->check = checksum_add(csum, tcp, plen);
117 }
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
Definition: util.c:452
uint16_t checksum_add(const uint16_t checksum, const void *data, int len)
Obtain the checksum for a buffer adding a checksum.
Definition: util.c:463
uint32_t saddr
Source address.
Definition: iputil.c:86
IPv4 header structur to cast a packet too.
Definition: iputil.c:84
void ipv4udpchecksum ( uint8_t *  pkt)

Update the UDP checksum of a IPv4 packet.

Parameters
pktPacket to update UDP checksum.

Definition at line 122 of file iputil.c.

References checksum(), checksum_add(), pseudohdr::daddr, pseudohdr::len, pseudohdr::proto, pseudohdr::saddr, and pseudohdr::zero.

Referenced by packetchecksumv4().

122  {
123  struct iphdr *ip = (struct iphdr *)pkt;
124  struct udphdr *udp = (struct udphdr *)(pkt + (4 * ip->ihl));
125  uint16_t csum, plen;
126  struct pseudohdr phdr;
127 
128  /* get tcp packet len*/
129  plen = ntohs(ip->tot_len) - (4 * ip->ihl);
130  udp->check = 0;
131  phdr.saddr = ip->saddr;
132  phdr.daddr = ip->daddr;
133  phdr.zero = 0;
134  phdr.proto = ip->protocol;
135  phdr.len = htons(plen);
136  csum = checksum(&phdr, sizeof(phdr));
137  udp->check = checksum_add(csum, udp, plen);
138 }
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
Definition: util.c:452
uint16_t checksum_add(const uint16_t checksum, const void *data, int len)
Obtain the checksum for a buffer adding a checksum.
Definition: util.c:463
uint32_t saddr
Source address.
Definition: iputil.c:86
IPv4 header structur to cast a packet too.
Definition: iputil.c:84
int packetchecksumv4 ( uint8_t *  pkt)

Update the checksum of a IPv4 packet.

Parameters
pktPacket buffer to update check.
Returns
0 on success.

Definition at line 165 of file iputil.c.

References ipv4checksum(), ipv4icmpchecksum(), ipv4tcpchecksum(), and ipv4udpchecksum().

Referenced by packetchecksum().

165  {
166  struct iphdr *ip = (struct iphdr *)pkt;
167 
168  ipv4checksum(pkt);
169 
170  switch(ip->protocol) {
171  case IPPROTO_ICMP:
172  ipv4icmpchecksum(pkt);
173  break;
174  case IPPROTO_TCP:
175  ipv4tcpchecksum(pkt);
176  break;
177  case IPPROTO_UDP:
178  ipv4udpchecksum(pkt);
179  break;
180  default:
181  return (-1);
182  }
183  return (0);
184 }
void ipv4tcpchecksum(uint8_t *pkt)
Update the TCP checksum of a IPv4 packet.
Definition: iputil.c:101
void ipv4checksum(uint8_t *pkt)
Set the checksup of a IPv4 Packet.
Definition: iputil.c:154
void ipv4icmpchecksum(uint8_t *pkt)
Set the checksup of a IPv4 ICMP packet.
Definition: iputil.c:143
void ipv4udpchecksum(uint8_t *pkt)
Update the UDP checksum of a IPv4 packet.
Definition: iputil.c:122
int reservedip ( const char *  ipaddr)

Check IP against list of reserved IP's.

Parameters
ipaddrIP addr to check.
Returns
1 if its a private/resrved/not routed IP

Definition at line 384 of file iputil.c.

Referenced by score_ipv4().

384  {
385  uint32_t ip;
386 
387 #ifndef __WIN32
388  inet_pton(PF_INET, ipaddr, &ip);
389 #else
390  ip = inet_addr(ipaddr);
391 #endif
392 
393  ip = ntohl(ip);
394 
395  if (!((0xe0000000 ^ ip) >> 28)) { /* 224/4*/
396  return 1;
397  } else if (!((0x00000000 ^ ip) >> 24)) { /* 0/8 */
398  return 1;
399  } else if (!((0x0a000000 ^ ip) >> 24)) { /* 10/8 */
400  return 1;
401  } else if (!((0x7f000000 ^ ip) >> 24)) { /* 127/8 */
402  return 1;
403  } else if (!((0x64400000 ^ ip) >> 22)) { /* 100.64/10 */
404  return 1;
405  } else if (!((0xac100000 ^ ip) >> 20)) { /* 172.16/12 */
406  return 1;
407  } else if (!((0xc6120000 ^ ip) >> 17)) { /* 198.18/15 */
408  return 1;
409  } else if (!((0xc0a80000 ^ ip) >> 16)) { /* 192.168/16 */
410  return 1;
411  } else if (!((0xa9fe0000 ^ ip) >> 16)) { /* 169.254/16 */
412  return 1;
413  } else if (!((0xc0000200 ^ ip) >> 8)) { /* 192.0.2/24 */
414  return 1;
415  } else if (!((0xc6336400 ^ ip) >> 8)) { /* 198.51.100/24 */
416  return 1;
417  } else if (!((0xcb007100 ^ ip) >> 8)) { /* 203.0.113/24 */
418  return 1;
419  }
420  return 0;
421 }
int score_ipv4 ( struct sockaddr_in *  sa4,
char *  ipaddr,
int  iplen 
)

Return a score for a IPv4 addrress.

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

Definition at line 718 of file interface.c.

References inet_ntop(), IPV4_SCORE_RESERVED, IPV4_SCORE_ROUTABLE, IPV4_SCORE_ZEROCONF, and reservedip().

Referenced by get_ifinfo(), and get_ifipaddr().

718  {
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 }
Reseverd &quot;private&quot; ip addresses.
Definition: interface.c:67
Routable IP&#39;s.
Definition: interface.c:69
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
const char * inet_ntop(int af, const void *src, char *dest, socklen_t size)
Win32 implementation of inet_ntop.
Definition: winiface.cpp:43