当前位置--> 首 页 --> 文 章 -->Linux Develop
|
|
※阅读文章※ |
Linux程式设计- 5.inetd作者:不祥 [文章出自: www.fanqiang.com] inetd提供被动式的伺服器服务,也就是伺服器是被使用端所启动,平时则无须存在。例如,ftp, telnetd, pop3,imap, auth等等,这些服务没有人使用时,无须启动。此外,inetd将socket转换成stdin/stdout,因而使得网路服务程式设计大大简化,您可以只用printf及fgets便可完成处理很复杂的网路协定。 -------------------------------------------------------------------------------- inetd programming 利用inetd来做网路程式设计是个既简单又稳定的设计方法,您不需要考虑到复杂的socket programming。您的设计工作几乎在设计好通讯协定後就完成了,所需要的技巧,仅为简单的文字分析技巧。 goodie inet service 首先,我们先来撰写一个称为goodie的服务程式。 goodie.c #include #include #include void main(void) { printf("Welcome to goodie service!\n"); } 这个程式很简单,不是吗? 编译 gcc -o goodie goodie.c 设定/etc/services及/etc/inetd.conf 在/etc/services中加入以下这一行 goodie 20001/tcp 其意义为goodie这项服务是在port 20001、TCP协定。 接下来在/etc/inetd.conf中加入以下这一行 goodie stream tcp nowait root /full_goodie_path_name/goodie 各项参数的意义为 service_name需要为在services中存在的名称。 sock_type有很多种,大多用的是stream/dgram。 proto一般用tcp/udp。 flags有wait/nowait。 user是您指定该程式要以那一个使用者来启动,这个例子中用的是root,如果有安全性的考量,应该要改用nobody。一般来说,建议您用低权限的使用者,除非必要,不开放root使用权。 server_path及args,这是您的服务程式的位置及您所想加入的参数。 接下来重新启动inetd killall inetd inetd 这样我们便建立起一个port 20001的goodie service。 现在我们来检验一下goodie是否可以执行: telnet localhost 20001 或 telnet your_host_name 20001 执行结果 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Welcome to goodie service! Connection closed by foreign host. 很简单不是吗? 信不信由您,telnet/pop3/imap/ftp都是靠这种方式建立起来的服务。 我们现在来建立一点小小的"网路协定",这个协定使我们可以输入"exit"时,离开程式,而其他的指令都是输出与输入相同的字串。 #include #include #include void main(void) { char buf[1024]; int ok; printf("Welcome to goodie service!\n"); fflush(stdout); ok=0; do { while (fgets(buf,1023,stdin)==NULL); if (strncasecmp(buf,"exit",4)==0) ok=1; printf(buf); fflush(stdout); } while (!ok); } 执行结果 telnet localhost 20001 或 telnet your_host_name 20001 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Welcome to goodie service! 输入"help" help help 输入"exit" exit exit Connection closed by foreign host. 接下来,我们将设计一个稍微复杂一点点的通讯协定,比较通用於一般用途。 #include #include #include char *cmds[]={ "help", "say", "hello", "bye", "exit", NULL }; int getcmd(char *cmd) { int n=0; while (cmds[n]!=NULL) { if (strncasecmp(cmd,cmds[n],strlen(cmds[n]))==0) return n; n++; } return -1; } void main(void) { char buf[1024]; int ok; printf("Welcome to goodie service!\n"); fflush(stdout); ok=0; do { while (fgets(buf,1023,stdin)==NULL); switch (getcmd(buf)) { case -1: printf("Unknown command!\n"); break; case 0: printf("How may I help you, sir?\n"); break; case 1: printf("I will say %s",&buf[3]); break; case 2: printf("How're you doing today?\n"); break; case 3: printf("Si ya, mate!\n"); ok=1; break; case 4: printf("Go ahead!\n"); ok=1; break; } fflush(stdout); } while (!ok); } telnet localhost 20001 或 telnet your_host_name 20001 试试看输入"help"、"say"、"hello"、"bye"、"exit"等等指令,及其它一些不在命令列中的指令。 在设计inetd服务程式时,要特别注意buffer overflow的问题,也就是以下这种状况: char buffer_overflow[64]; fscanf(stdin,"%s",buffer_overflow); 历来几乎所有的安全漏洞都是由此而来的。 你一定不可这样用,不论任何理由,类同的用法也不可以。Cracker可以透过将您的buffer塞爆,然後塞进他自己的程式进来执行。 文章加入时间: 2004-11-17 14:56:32 责任编辑: w9 (3071 人次查阅) |