This program, "ip4dec", converts lists of IPv4 addresses to decimal. Wrote this while experimenting with storing domain->ip mappings in a trie, such as
https://github.com/tlwg/libdatrieName borrowed from https://github.com/ian-hamlin/ipdec
Note the trietool "list" command prints data as %d not %u. To fix, edit list_enum_func() in trietool.c
sed -n 's/ //;wip4dec.l' << eof
/*
not a domain name or ip address validator
input file format:
(left-justified, no leading spaces)
example.com 93.184.216.34
example.net 93.184.216.34 comment
*/
int fileno(FILE *);
int setenv(const char*,const char*,int);
int unsetenv(const char*);
#define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0)
#define jmp (yy_start) = 1 + 2 *
int x=0,y=0,o=0;
xa [0-9]{1,3}\x2e
xb [0-9]{1,3}
xc [0-9]{4,5}
xd ^[A-Za-z0-9\.-]*
xe ^[^A-Za-z0-9]
%s xa
%option noyywrap nounput noinput
%%
{xd} if(yytext[0]=='-'||yytext[0]=='.')jmp 0;else{o=0;y=0;x=0;setenv("x",yytext,1);jmp xa;}
{xe} jmp 0;
<xa>{xc} jmp 0;
<xa>{xa}|{xb} {
switch(o){
case 0: y=atoi(yytext);if(y<1)break;x=y*16777216;y=0;o++;break;
case 1: y=atoi(yytext);if(y>255)break;x=x+y*65536;y=0;o++;break;
case 2: y=atoi(yytext);if(y>255)break;x=x+y*256;y=0;o++;break;
case 3: y=atoi(yytext);if(y>255)break;x=x+y;printf("%s\t%u\n",getenv("x"),x);unsetenv("x");break;
default: break;
}
}
.|\n
%%
int main(){ yylex();exit(0) ;}
eof
flex -8iCrf ip4dec.l
cc -std=c89 -Wall -pedantic -I. -pipe lex.yy.c -static -o ip4dec
usage: ip4dec < input-file
example:
echo example.com 93.184.216.34 icann|ip4dec
output:
example.com 1572395042