diff options
Diffstat (limited to 'src/ipv4.c')
-rw-r--r-- | src/ipv4.c | 115 |
1 files changed, 111 insertions, 4 deletions
@@ -1,13 +1,62 @@ +/** + * Copyright (C) 2015 Aaron Ball <nullspoon@iohq.net> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ #include "ipv4.h" +/** + * Ipv4 initializer + * + * @param ip Declared ipv4 struct to be (re-)initialized + * + * @return void + */ void ipv4_new(ipv4* ip) { ip->octets[0] = 0; ip->octets[1] = 0; ip->octets[2] = 0; ip->octets[3] = 0; + ip->mask = 32; // assume a /32 } +/** + * Ipv4 initializer + * + * @param ip Declared ipv4 struct to be (re-)initialized + * + * @return void + */ +void ipv4_cpy(ipv4* orig, ipv4* new) { + new->octets[0] = orig->octets[0]; + new->octets[1] = orig->octets[1]; + new->octets[2] = orig->octets[2]; + new->octets[3] = orig->octets[3]; + new->mask = orig->mask; +} + + +/** + * Converts the given ipv4 struct to its binary equivelant. + * Note that despite the specified netmask, this converts the ipv4 struct as if + * it were a /32 (a single ip address). + * + * @param addr Initialized ipv4 struct + * + * @return unsigned int Integer representation of the specified ipv4 address + */ unsigned int ipv4_to_int(ipv4* addr) { unsigned int out; out = addr->octets[3] * 16777216; // 2^24 or 256^3 @@ -19,6 +68,13 @@ unsigned int ipv4_to_int(ipv4* addr) { } +/** + * Converts a string to an ipv4 struct + * + * @param ip String representation of an ipv4 address + * + * @return ipv4 Initialized ipv4 struct + */ ipv4 str_to_ipv4(char* ip) { int octet = 3; int cursor = 0; @@ -29,7 +85,7 @@ ipv4 str_to_ipv4(char* ip) { // Initialize ipv4_new(&out); - while(ip[i] != '\0') { + while(ip[i] != '\0' && ip[i] != '/') { if(ip[i] == '.') { // Copy the string contents strncpy(buf, &ip[cursor], i-cursor); @@ -43,35 +99,86 @@ ipv4 str_to_ipv4(char* ip) { } i++; } + // For the last octet, one more do-over strncpy(buf, &ip[cursor], i-cursor); buf[i-cursor] = '\0'; out.octets[octet] = atoi(buf); + // Set the mask + // Default the mask to /32. It'll be overwritten if it was specified. + char cmask[4] = "32"; + if(ip[i] == '/') { + strcpy(&cmask[0], &ip[i+1]); + } + out.mask = atoi(cmask); + return out; } +/** + * Converts an ipv4 struct to a string, using slash notation to indicate + * netmask. + * + * @param ip Initialized ipv4 struct to be converted to string + * @param out Output char array where the ipv4 string will be copied + * + * @return void + */ void ipv4_to_str(ipv4* ip, char* out) { int octet = 3; int cursor = 0; - + out[0] = '\0'; while(octet >= 0) { int len = itoa(ip->octets[octet], &out[cursor]); cursor += len; out[cursor] = '.'; cursor++; - printf("%d\n", octet); + octet--; } + // Erase the trailing period + out[cursor - 1] = '\0'; } +/** + * Increments an ipv4 address + * Handles octet resets and carrying the 1. + * + * @param ip Initialized ipv4 struct + * + * @return void + */ void ipv4_inc(ipv4* ip) { int octet = 0; - if(ip->octets[octet] == 255) { + while(ip->octets[octet] == 255) { ip->octets[octet] = 0; octet++; } ip->octets[octet]++; } + + +/** + * Prints all ip addresses for the given ipv4 network (uses the mask value from + * the ipv4 struct) + * + * @param ip Initialized ipv4 struct pointer containing all the information to + * calculate every ip in the specified ipv4 network. + * + * @return void + */ +void ipv4_ips(ipv4* ip) { + unsigned long count = pow(2, 32 - ip->mask); + char buf[20]; + + while(count > 0) { + ipv4_to_str(ip, buf); + // TODO: Move this into a return array + printf("%s\n", buf); + ipv4_inc(ip); + count--; + } +} |