/*
 * Umrechnung von Longint <-> IP
 * Copyright by PoC@pocnet.net 12.2.2002 (mit Tips von Heiko)
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * or get it at http://www.gnu.org/licenses/gpl.html
 *
 * Kompilieren mit gcc -o iprechner iprechner.c
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/* --------------------------------------------------------------------------*/

void ip2num(char *buf) {
	unsigned char	byte[4], i, buflen, dotcount = 0;
	char			*_byte[4];
	unsigned long	ipnum;

	// Variablen vorbereiten
	buflen = strlen(buf);

	_byte[0] = malloc(buflen);
	_byte[1] = malloc(buflen);
	_byte[2] = malloc(buflen);
	_byte[3] = malloc(buflen);

	*_byte[0] = *_byte[1] = *_byte[2] = *_byte[3] = 0;

	// Buffer sequentiell durchgehen
	for (i = 0; i < buflen; i++) {
		if ( isdigit(buf[i]) ) {						// Isses 'ne Zahl?
			strncat(_byte[dotcount], &buf[i], 1);		// Hintendranhängen
		} else if ( buf[i] == '.' ) {					// Isses'n Punkt
			if ( dotcount < 3 ) {
				dotcount++;								// Nächstes Array füllen
			} else {
				fprintf(stderr, "Dotted IP adresses only have three dots in it.\n");
				exit(1);
			}
		} else {										// Alles andere darf net sein
			fprintf(stderr, "Illegal Character '%c' encountered.\n", buf[i]);
			exit(1);
		}
	}

	// Strings konvertieren
	byte[3] = atoi(_byte[0]);
	byte[2] = atoi(_byte[1]);
	byte[1] = atoi(_byte[2]);
	byte[0] = atoi(_byte[3]);

	// *byte oder byte[] wird gecastet auf unsigned long byte[].
	// Das wiederum wird gecastet auf unsigned long *byte[]
	// Danke an Heiko für den Gehirnknoten ;-)
	ipnum = *(unsigned long*)byte;
	printf("%lu\n", ipnum);
}

/* --------------------------------------------------------------------------*/

void num2ip(char *buf) {
	unsigned long	ipnum;
	unsigned char	*byte;

	if ( isdigit(buf[0]) ) {							// Isses erste 'ne Zahl?
		ipnum = strtoul(buf, (char **)NULL, 10);
		if ( ipnum > 4294967295 ) {
			fprintf(stderr, "Number represents more than 255.255.255.255 which makes no sense.\n");
			exit(1);
		}

		// Wie oben, bloß rückwärts
		byte = (unsigned char*)&ipnum;
		printf("%d.%d.%d.%d\n", byte[3], byte[2], byte[1], byte[0]);
	}
}

/* --------------------------------------------------------------------------*/

int main (int argc, char *argv[]) {

	if (argc != 3) {
		printf("Mit -i <IP> oder -n <Zahl> zum Umrechnen aufrufen.\n");
		exit(1);
	} else {
		if ( strncmp(argv[1], "-i", 2) == 0 ) {
			ip2num(argv[2]);
		} else if ( strncmp(argv[1], "-n", 2) == 0 ) {
			num2ip(argv[2]);
		} else {
			printf("Mit -i <IP> oder -n <Zahl> zum Umrechnen aufrufen.\n");
			exit(1);
		}
	}
	return(0);
}

/* --------------------------------------------------------------------------*/
