logo

Navigation:First_Community->Software->Linux development Goto:New TopicSettingSearch
Linux程式设计- 5.inetd
Posted by: imac Date: March 29, 2008 02:01AM
inetd提供被动式的伺服器服务,也就是伺服器是被使用端所启动,平时则无须存在。例如,ftp, telnetd, pop3,imap, auth等等,这些服务没有人使用时,无须启动。此外,inetd将socket转换成stdin/stdout,因而使得网路服务程式设计大大简化,您可以只用printf及fgets便可完成处理很复杂的网路协定。 

CopyRight owned by the original author.--(www.MegaEntry.com)

-------------------------------------------------------------------------------- inetd programming 利用inetd来做网路程式设计是个既简单又稳定的设计方法,您不需要考虑到复杂的socket programming。您的设计工作几乎在设计好通讯协定後就完成了,所需要的技巧,仅为简单的文字分析技巧。  goodie inet service

MegaEntry - Social networking and discussion site!

首先,我们先来撰写一个称为goodie的服务程式。  goodie.c  #include   #include   #include  

CopyRight owned by the original author.--(www.MegaEntry.com)

void main(void)  {    printf( "Welcome to goodie service! ");  }  这个程式很简单,不是吗? 

CopyRight owned by the original author.--(www.MegaEntry.com)

编译 gcc -o goodie goodie.c  设定/etc/services及/etc/inetd.conf 在/etc/services中加入以下这一行  goodie          20001/tcp 

MegaEntry - Social networking and discussion site!

其意义为goodie这项服务是在port 20001、TCP协定。  接下来在/etc/inetd.conf中加入以下这一行  goodie stream tcp nowait root /full_goodie_path_name/goodie 

MegaEntry - Social networking and discussion site!

各项参数的意义为          service_name需要为在services中存在的名称。  sock_type有很多种,大多用的是stream/dgram。 

MegaEntry - Social networking and discussion site!

proto一般用tcp/udp。  flags有wait/nowait。  user是您指定该程式要以那一个使用者来启动,这个例子中用的是root,如果有安全性的考量,应该要改用nobody。一般来说,建议您用低权限的使用者,除非必要,不开放root使用权。  server_path及args,这是您的服务程式的位置及您所想加入的参数。  接下来重新启动inetd 

CopyRight owned by the original author.--(www.MegaEntry.com)

killall inetd  inetd  这样我们便建立起一个port 20001的goodie service。  现在我们来检验一下goodie是否可以执行: 

MegaEntry - Social networking and discussion site!

telnet localhost 20001  或  telnet your_host_name 20001  执行结果

CopyRight owned by the original author.--(www.MegaEntry.com)

Trying 127.0.0.1...  Connected to localhost.  Escape character is  '^] '.  Welcome to goodie service!  Connection closed by foreign host.  很简单不是吗? 信不信由您,telnet/pop3/imap/ftp都是靠这种方式建立起来的服务。 

CopyRight owned by the original author.--(www.MegaEntry.com)

我们现在来建立一点小小的 "网路协定 ",这个协定使我们可以输入 "exit "时,离开程式,而其他的指令都是输出与输入相同的字串。  #include   #include   #include  

CopyRight owned by the original author.--(www.MegaEntry.com)

void main(void)  {    char buf[1024];    int ok; 

MegaEntry - Social networking and discussion site!

  printf( "Welcome to goodie service! ");    fflush(stdout);    ok=0;    do {      while (fgets(buf,1023,stdin)==NULL); 

MegaEntry - Social networking and discussion site!

    if (strncasecmp(buf, "exit ",4)==0) ok=1;      printf(buf);      fflush(stdout);    } while (!ok);  } 

MegaEntry - Social networking and discussion site!

执行结果 telnet localhost 20001  或  telnet your_host_name 20001     Trying 127.0.0.1... 

MegaEntry - Social networking and discussion site!

Connected to localhost.  Escape character is  '^] '.  Welcome to goodie service!  输入 "help " 

MegaEntry - Social networking and discussion site!

help  help  输入 "exit "  exit 

CopyRight owned by the original author.--(www.MegaEntry.com)

exit  Connection closed by foreign host.  接下来,我们将设计一个稍微复杂一点点的通讯协定,比较通用於一般用途。  #include   #include  

CopyRight owned by the original author.--(www.MegaEntry.com)

#include   char *cmds[]={     "help ",     "say ",     "hello ", 

CopyRight owned by the original author.--(www.MegaEntry.com)

   "bye ",     "exit ",    NULL  };  int getcmd(char *cmd) 

MegaEntry - Social networking and discussion site!

{    int n=0;    while (cmds[n]!=NULL) {      if (strncasecmp(cmd,cmds[n],strlen(cmds[n]))==0) return n;      n++;    } 

MegaEntry - Social networking and discussion site!

  return -1;  }  void main(void)  {    char buf[1024]; 

CopyRight owned by the original author.--(www.MegaEntry.com)

  int ok;    printf( "Welcome to goodie service! ");    fflush(stdout);    ok=0; 

MegaEntry - Social networking and discussion site!

  do {      while (fgets(buf,1023,stdin)==NULL);      switch (getcmd(buf)) {        case -1: printf( "Unknown command! "); break;        case  0: printf( "How may I help you, sir? "); break;        case  1: printf( "I will say %s ",&buf[3]); break; 

MegaEntry - Social networking and discussion site!

      case  2: printf( "How 're you doing today? "); break;        case  3: printf( "Si ya, mate! ");  ok=1; break;        case  4: printf( "Go ahead! "); ok=1; break;      }      fflush(stdout);    } while (!ok); 

MegaEntry - Social networking and discussion site!

}  telnet localhost 20001  或  telnet your_host_name 20001 

CopyRight owned by the original author.--(www.MegaEntry.com)

试试看输入 "help "、 "say "、 "hello "、 "bye "、 "exit "等等指令,及其它一些不在命令列中的指令。  在设计inetd服务程式时,要特别注意buffer overflow的问题,也就是以下这种状况:  char buffer_overflow[64]; 

MegaEntry - Social networking and discussion site!

fscanf(stdin, "%s ",buffer_overflow);  历来几乎所有的安全漏洞都是由此而来的。  你一定不可这样用,不论任何理由,类同的用法也不可以。Cracker可以透过将您的buffer塞爆,然後塞进他自己的程式进来执行。 

CopyRight owned by the original author.--(www.MegaEntry.com)



Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect