Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107474
  • 博文数量: 38
  • 博客积分: 1520
  • 博客等级: 上尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-20 11:12
文章分类

全部博文(38)

文章存档

2012年(4)

2011年(1)

2010年(7)

2009年(21)

2008年(1)

2007年(4)

我的朋友

分类:

2007-09-19 10:21:05

 

四.程序设计与实现(以下是以c来实现的)

1.  需求:

据以上来看,由于数据库只有API,不支持sql,也没server概念,数据库的维护管理全由程序来完成,其基本模块为:

A:添加

B:查找

C:删除

D:同步                                                    

2.  细节(子模块)

 从上面我们知道要实现上面的功能,其子模块有:

A:复制ID的获得

   通过取得dns_xfr中的zone+a space+host遍历dns_data的值,复制ID初值为1(SOA),其后相同的zone,复制ID++;

Breverse zone

  也就是把zone反向存储.

C:分离字符串判断是否为SOA记录,如果是则repid=1,否则repid++;

D:把整数转化为字符串函数

E:字符串分离与连接函数

F:命令行getop获取参数函数

Hbtreehash表要实现一Key多值,必须支持key值的复制,而且要提高查找的速度的话,还要支持数据的排序,默认为字典顺序,实现更好的算法则以自定义的callback函数来实现。

3.  程序实现

#include

#include

#define dlz_data "dns_data"

#define dlz_zone "dns_zone"

#define dlz_xfr "dns_xfr"

#define dlz_client "dns_client"

……………………………………………….

void Usage(void)

char *soa(char *str);

int bdbhpt_open(DBTYPE db_type, DB **db_out, const char *db_name, int flags);

int bdbhpt_create(void);

void put_data(char *db_name, char *input_key, char *input_data);

void bdbhpt_close(void);

void bdbhpt_find(void );

void bdbhpt_dele();

void bdbhpt_add(void);

static char * bdbhpt_strrev (char *str);

int get_last_repid(void);

int IntToStr(int num, char *buffer);

…………………………………………………………

int main(int argc, char *argv[])

{

    char ch;

    int ret; 

    opterr = 0;

    while ((ch = getopt(argc, argv, "c:f:h:i:j:m:z:adsn")) != -1)

    {

        switch (ch)

        {

                case 'a':             

                              operation = add;

                              break;

                case 'd':

           operation = dele;    

            break;

       case 's':

                              operation = list;

                              break;

                case 'n':

                              key_val =1;

                              operation = add;

                              break;

       case 'z':

    zone = optarg;

           break;

       case 'h':

           host = optarg;

           break;

       case 'c':

           c_zone = optarg;

           break;

       case 'i':

           c_ip = optarg;

           break;

       case 'j':

           a_data = optarg;

           break;

       case 'm':

           db_envdir = optarg;

           break;

       case 'f':

           db_file = optarg;

           break;   

        case '?':

        default:

            printf("please use -H\n");

            Usage();

        }

      }

  if (argc < 2)

       {

        fprintf(stderr, "Both a Berkeley DB environment and file must be specified\n");          

  }

 

switch(operation)

{

        case list:

          bdbhpt_find();

               break;

        case dele:

               bdbhpt_dele();

               break;

        case add:

               bdbhpt_add();

               break;

        default:

               fprintf(stderr, "\nNo operation was selected. "\

                      "Select an operation (s d a f)\n");

               break;

}

}

阅读(996) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~