Created
September 12, 2008 13:13
-
-
Save aurelian/10435 to your computer and use it in GitHub Desktop.
2 functions to extend gawk with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* rextend.c - 2 functions to extend gawk | |
* | |
* Aurelian Oancea, aurelian at locknet [dot] ro | |
*/ | |
/* | |
* Copyright (C) 2008 Aurelian Oancea | |
* | |
* 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 "awk.h" | |
#include <openssl/md5.h> | |
#include <GeoIP.h> | |
/* compile with: | |
* | |
* gcc -fPIC -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. -I/S/include rextend.c | |
* ld -lssl -lGeoIP -L/S/lib -o rextend.so -shared rextend.o | |
* | |
* /S non standard path to GeoIP installation | |
*/ | |
/* do_geoip --- given an IP, it finds the country code using GeoIP library[GPL] */ | |
static NODE * | |
do_geoip(tree) | |
NODE *tree; | |
{ | |
NODE *str; | |
static GeoIP * gi = NULL; | |
const char * ret; | |
if (do_lint && get_curfunc_arg_count() != 1) | |
lintwarn("geoip: called with incorrect number of arguments"); | |
str = get_argument(tree, 0); | |
if(str!=NULL) { | |
(void)force_string(str); | |
if(gi==NULL) | |
gi= GeoIP_new(GEOIP_STANDARD); | |
#if DEBUG | |
printf("-> geoip(%s)\n", str->stptr); | |
#endif | |
ret= GeoIP_country_code_by_name(gi, str->stptr); | |
if(ret == NULL) { | |
#if DEBUG | |
printf("ip: ``%s\" not found!\n", str->stptr); | |
#endif | |
set_value(tmp_string("N/A", strlen("N/A"))); | |
} else { | |
set_value(tmp_string((char *)ret, strlen(ret))); | |
} | |
free_temp(str); | |
} else if(do_lint) { | |
lintwarn("geoip: called with no arguments"); | |
} | |
return tmp_number((AWKNUM) 0); | |
} | |
/* do_md5 --- calculates the md5 digest from the given string using the openssl library */ | |
static NODE * | |
do_md5(tree) | |
NODE *tree; | |
{ | |
unsigned char digest[16]; | |
char ret[32]; | |
int i; | |
NODE *str; | |
MD5_CTX ctx; | |
if (do_lint && get_curfunc_arg_count() != 1) | |
lintwarn("md5: called with incorrect number of arguments"); | |
str = get_argument(tree, 0); | |
if(str!=NULL) { | |
MD5_Init( &ctx ); | |
(void)force_string(str); | |
MD5_Update( &ctx, str->stptr, strlen(str->stptr) ); | |
free_temp(str); | |
MD5_Final( digest, &ctx ); | |
for( i=0; i<16; i++ ) | |
sprintf(ret+2*i, "%02x\n", digest[i]); | |
#if DEBUG | |
printf("->%s\n", ret); | |
#endif | |
set_value(tmp_string(ret, sizeof(ret))); | |
} | |
else if(do_lint) { | |
lintwarn("md5: called with no arguments"); | |
} | |
return tmp_number((AWKNUM) 0); | |
} | |
/* dlload --- load new builtins in this library */ | |
NODE * | |
dlload(tree, dl) | |
NODE *tree; | |
void *dl; | |
{ | |
make_builtin("md5", do_md5, 1); | |
make_builtin("geoip", do_geoip, 1); | |
return tmp_number((AWKNUM) 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment