ipa_htoa,ipa_atoh - dotted-decimal <-> int4 conversions. ipa_network - returns network part of IP address. ipa_netblock - checks address to be in subnet specified.
========================================
#ifdef STANDARD #include <stdio.h> #include <string.h> #else #include <global.h> #include <my_sys.h> #endif #include <mysql.h> #include <m_ctype.h> #include <m_string.h> // To get strmov()
#ifdef HAVE_DLOPEN
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <strings.h>
/* These must be right or mysqld will not find the symbol! */
/***********************************************************/ /* */ /* ipa_htoa() */ /* */ /***********************************************************/
extern "C" { my_bool ipa_htoa_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void ipa_htoa_deinit(UDF_INIT *initid); char *ipa_htoa( UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error); }
#define MAX_IPADDR 20
my_bool ipa_htoa_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 1 || args->arg_type[0] != INT_RESULT) { strcpy(message,"Wrong arguments to ipa_htoa_init"); return 1; } initid->max_length=MAX_IPADDR; return 0; }
void ipa_htoa_deinit(UDF_INIT *initid) { }
char *ipa_htoa( UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { unsigned long int host_addr = *( (unsigned long int *) args->args[0] );
struct in_addr in; char *addr;
in.s_addr = (unsigned long int) htonl(host_addr); addr = inet_ntoa(in);
*length = strlen(addr); memcpy(result, addr, *length);
return result; }
/***********************************************************/ /* */ /* ipa_atoh() */ /* */ /***********************************************************/
extern "C" { my_bool ipa_atoh_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void ipa_atoh_deinit(UDF_INIT *initid); longlong ipa_atoh(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); }
my_bool ipa_atoh_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT) { strcpy(message,"Wrong arguments to ipa_atoh_init"); return 1; } return 0; }
void ipa_atoh_deinit(UDF_INIT *initid) { }
long long ipa_atoh(UDF_INIT *initid, UDF_ARGS *args, char *is_null,char *error) { unsigned long int net_addr;
net_addr = inet_addr(args->args[0]); return (unsigned long int) ntohl(net_addr); }
/***********************************************************/ /* */ /* ipa_netblock() */ /* */ /***********************************************************/
extern "C" { my_bool ipa_netblock_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void ipa_netblock_deinit(UDF_INIT *initid); longlong ipa_netblock(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); }
my_bool ipa_netblock_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 3 || args->arg_type[0] != INT_RESULT || args->arg_type[1] != INT_RESULT || args->arg_type[2] != INT_RESULT) { strcpy(message,"Wrong arguments to ipa_netblock_init"); return 1; } return 0; }
void ipa_netblock_deinit(UDF_INIT *initid) { }
long long ipa_netblock(UDF_INIT *initid, UDF_ARGS *args, char *is_null,char *error) { unsigned long int addr = *( (unsigned long int *) args->args[0] ); unsigned long int network = *( (unsigned long int *) args->args[1] ); unsigned long int netmask = *( (unsigned long int *) args->args[2] );
return (network == (addr & netmask)); }
/***********************************************************/ /* */ /* ipa_network() */ /* */ /***********************************************************/
extern "C" { my_bool ipa_network_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void ipa_network_deinit(UDF_INIT *initid); longlong ipa_network(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); }
my_bool ipa_network_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 2 || args->arg_type[0] != INT_RESULT || args->arg_type[1] != INT_RESULT ) { strcpy(message,"Wrong arguments to ipa_network_init"); return 1; } return 0; }
void ipa_network_deinit(UDF_INIT *initid) { }
long long ipa_network(UDF_INIT *initid, UDF_ARGS *args, char *is_null,char *error) { unsigned long int addr = *( (unsigned long int *) args->args[0] ); unsigned long int netmask = *( (unsigned long int *) args->args[1] );
return (addr & netmask); }
#endif /* HAVE_DLOPEN */
|