database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Devel 21125 게시물 읽기
No. 21125
MySQL UDF -- ip address manipulation
작성자
정재익(advance)
작성일
2004-02-24 12:20
조회수
8,972

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 */

[Top]
No.
제목
작성자
작성일
조회
21132MySQL UDF -- soundex
정재익
2004-02-24
10521
21129MySQL UDF -- ip lookup
정재익
2004-02-24
10837
21128MySQL UDF -- unprefix
정재익
2004-02-24
10215
21125MySQL UDF -- ip address manipulation
정재익
2004-02-24
8972
21119MySQL UDF -- corba_string
정재익
2004-02-24
8415
21117MySQL UDF -- valid_date
정재익
2004-02-24
8831
21114MySQL UDF - il2_to_cp1250
정재익
2004-02-24
6731
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.042초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다