30 #include <linux/icmp.h>
31 #include <linux/tcp.h>
32 #include <linux/udp.h>
47 extern int checkipv6mask(
const char *ipaddr,
const char *network, uint8_t bits) {
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;
54 if ((bitlen = bits % 32)) {
55 bytelen = (bits - bitlen) / 32;
62 for(cnt = 0; (!res && (cnt < (bytelen - 1))); cnt++) {
63 res += nw[cnt] ^ ip[cnt];
68 mask = (bitlen) ? htonl(~((1 << (32 - bitlen)) - 1)) : -1;
69 res += (nw[cnt] & mask) ^ (ip[cnt] & mask);
102 struct iphdr *ip = (
struct iphdr *)pkt;
103 struct tcphdr *tcp = (
struct tcphdr *)(pkt + (4 * ip->ihl));
108 plen = ntohs(ip->tot_len) - (4 * ip->ihl);
110 phdr.
saddr = ip->saddr;
111 phdr.
daddr = ip->daddr;
113 phdr.
proto = ip->protocol;
114 phdr.
len = htons(plen);
115 csum =
checksum(&phdr,
sizeof(phdr));
123 struct iphdr *ip = (
struct iphdr *)pkt;
124 struct udphdr *udp = (
struct udphdr *)(pkt + (4 * ip->ihl));
129 plen = ntohs(ip->tot_len) - (4 * ip->ihl);
131 phdr.
saddr = ip->saddr;
132 phdr.
daddr = ip->daddr;
134 phdr.
proto = ip->protocol;
135 phdr.
len = htons(plen);
136 csum =
checksum(&phdr,
sizeof(phdr));
144 struct iphdr *ip = (
struct iphdr *)pkt;
145 struct icmphdr *icmp = (
struct icmphdr *)(pkt + (4 * ip->ihl));
148 icmp->checksum =
checksum(icmp, ntohs(ip->tot_len) - (ip->ihl *4));
155 struct iphdr *ip = (
struct iphdr *)pkt;
158 ip->check =
checksum(ip, (4 * ip->ihl));
166 struct iphdr *ip = (
struct iphdr *)pkt;
170 switch(ip->protocol) {
190 struct iphdr *ip = (
struct iphdr *)pkt;
191 switch(ip->protocol) {
209 struct iphdr *ip = (
struct iphdr *)pkt;
211 switch(ip->version) {
228 extern const char *
cidrtosn(
int bitlen,
char *buf,
int size) {
230 uint8_t *nmb = (uint8_t*)&nm;
237 nm = ~((1 << (32-bitlen))-1);
242 snprintf(buf, size,
"%i.%i.%i.%i", nmb[3], nmb[2], nmb[1], nmb[0]);
254 extern const char *
getnetaddr(
const char *ipaddr,
int cidr,
char *buf,
int size) {
256 uint8_t *ipb = (uint8_t*)&ip;
263 inet_pton(AF_INET, ipaddr, &ip);
265 ip = inet_addr(ipaddr);
269 ip = ip & ~((1 << (32-cidr))-1);
274 snprintf(buf, size,
"%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
286 extern const char *
getfirstaddr(
const char *ipaddr,
int cidr,
char *buf,
int size) {
288 uint8_t *ipb = (uint8_t*)&ip;
295 inet_pton(AF_INET, ipaddr, &ip);
297 ip = inet_addr(ipaddr);
301 ip = ip & ~((1 << (32-cidr))-1);
307 snprintf(buf, size,
"%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
319 extern const char *
getbcaddr(
const char *ipaddr,
int cidr,
char *buf,
int size) {
321 uint8_t *ipb = (uint8_t*)&ip;
324 inet_pton(AF_INET, ipaddr, &ip);
326 ip = inet_addr(ipaddr);
329 mask = (1 << (32-cidr))-1;
331 ip = (ip & ~mask) | mask;
335 snprintf(buf, size,
"%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
347 extern const char *
getlastaddr(
const char *ipaddr,
int cidr,
char *buf,
int size) {
349 uint8_t *ipb = (uint8_t*)&ip;
352 inet_pton(AF_INET, ipaddr, &ip);
354 ip = inet_addr(ipaddr);
357 mask = (1 << (32-cidr))-1;
359 ip = (ip & ~mask) | mask;
364 snprintf(buf, size,
"%i.%i.%i.%i", ipb[3], ipb[2], ipb[1], ipb[0]);
374 return pow(2, (32-bitlen));
388 inet_pton(PF_INET, ipaddr, &ip);
390 ip = inet_addr(ipaddr);
395 if (!((0xe0000000 ^ ip) >> 28)) {
397 }
else if (!((0x00000000 ^ ip) >> 24)) {
399 }
else if (!((0x0a000000 ^ ip) >> 24)) {
401 }
else if (!((0x7f000000 ^ ip) >> 24)) {
403 }
else if (!((0x64400000 ^ ip) >> 22)) {
405 }
else if (!((0xac100000 ^ ip) >> 20)) {
407 }
else if (!((0xc6120000 ^ ip) >> 17)) {
409 }
else if (!((0xc0a80000 ^ ip) >> 16)) {
411 }
else if (!((0xa9fe0000 ^ ip) >> 16)) {
413 }
else if (!((0xc0000200 ^ ip) >> 8)) {
415 }
else if (!((0xc6336400 ^ ip) >> 8)) {
417 }
else if (!((0xcb007100 ^ ip) >> 8)) {
433 if (!inet_pton(AF_INET, ipaddr, &ip)) {
437 if (!(ip = inet_addr(ipaddr))) {
444 snprintf(pre6, 10,
"%02x%02x:%02x%02x", ipa[0], ipa[1], ipa[2], ipa[3]);
456 extern int check_ipv4(
const char* ip,
int cidr,
const char *test) {
460 inet_pton(AF_INET, ip, &ip1);
461 inet_pton(AF_INET, test, &ip2);
464 ip2 = inet_addr(test);
467 ip1 = ntohl(ip1) >> (32-cidr);
468 ip2 = ntohl(ip2) >> (32-cidr);
485 i = (uint32_t*)&addr->s6_addr32;
487 i = (uint32_t*)&addr->u.Word;
489 i[0] = htonl(0xFF350000);
498 i[3] = htonl(i[3] | mip);
510 }
while (!rand || !(mip >> 8));
513 addr->s_addr = htonl(mip);
523 int inet_lookup(
int family,
const char *host,
void *addr, socklen_t len) {
524 struct addrinfo hint, *result, *ainfo;
527 memset(&hint, 0,
sizeof(hint));
528 hint.ai_family = family;
530 if (getaddrinfo(host, NULL, &hint, &result) || !result) {
534 for(ainfo = result; ainfo; ainfo = ainfo->ai_next) {
535 switch(ainfo->ai_family) {
537 if (len >=
sizeof(
struct in_addr)) {
538 struct sockaddr_in *sa4 = (
struct sockaddr_in*)ainfo->ai_addr;
539 memcpy(addr, &sa4->sin_addr, len);
544 if (len >=
sizeof(
struct in6_addr)) {
545 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6*)ainfo->ai_addr;
546 memcpy(addr, &sa6->sin6_addr, len);
555 freeaddrinfo(result);
uint16_t len
Packet length.
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
void ipv4tcpchecksum(uint8_t *pkt)
Update the TCP checksum of a IPv4 packet.
void mcast6_ip(struct in6_addr *addr)
Randomally assign a SSM Multicast address.param addr Ip address structure to fill out...
int inet_lookup(int family, const char *host, void *addr, socklen_t len)
Perform DNS lookup on a host/ip retun the IP address.
const char * getbcaddr(const char *ipaddr, int cidr, char *buf, int size)
Return broadcast address.
const char * cidrtosn(int bitlen, char *buf, int size)
Return the dotted quad notation subnet mask from a CIDR.
int packetchecksum(uint8_t *pkt)
Generic IPv4 and IPv6 Checksum.
uint32_t daddr
Destination address.
ipversion
IP Protocol numbers.
uint16_t checksum_add(const uint16_t checksum, const void *data, int len)
Obtain the checksum for a buffer adding a checksum.
void ipv4checksum(uint8_t *pkt)
Set the checksup of a IPv4 Packet.
int checkipv6mask(const char *ipaddr, const char *network, uint8_t bits)
Check if ipaddr is in a network.
void ipv4icmpchecksum(uint8_t *pkt)
Set the checksup of a IPv4 ICMP packet.
DTS Application library API Include file.
int packetchecksumv6(uint8_t *pkt)
Prototype to check checksup on packet.
int reservedip(const char *ipaddr)
Check IP against list of reserved IP's.
int genrand(void *buf, int len)
Generate random sequence.
uint32_t saddr
Source address.
void ipv4udpchecksum(uint8_t *pkt)
Update the UDP checksum of a IPv4 packet.
const char * getnetaddr(const char *ipaddr, int cidr, char *buf, int size)
Return the network address.
uint32_t cidrcnt(int bitlen)
Return the number of IP addresses in a given bitmask.
void mcast4_ip(struct in_addr *addr)
Randomally assign a SSM Multicast address.
IPv4 header structur to cast a packet too.
int packetchecksumv4(uint8_t *pkt)
Update the checksum of a IPv4 packet.
int check_ipv4(const char *ip, int cidr, const char *test)
Check if a IP address is in a network.
char * ipv6to4prefix(const char *ipaddr)
Return IPv6 to IPv4 Prefix fot the address.
const char * getfirstaddr(const char *ipaddr, int cidr, char *buf, int size)
Get the first usable address.
const char * getlastaddr(const char *ipaddr, int cidr, char *buf, int size)
Get the last usable address.