telnetd源码分析

telnetd是用于提供telnet服务的程序,本文详细分析了其源码流程。首先,通过BusyBox获取并编译telnetd,源码位于networking/telnetd.c。程序核心在于两个缓冲区buf1和buf2,分别处理socket与pty的读写。当socket接收到数据,写入buf1,pty可写时,数据从buf1流向pty;pty可读时,数据写入buf2,准备发送到远端。关键函数make_new_session使用xgetpty打开pty,vfork创建子进程,子进程执行登录和shell程序。父进程通过pty与子进程的shell进行交互,实现远程终端连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  telnetd是一个telnet服务端程序

 

下载地址:http://www.busybox.net/

解压缩后进入busybox目录

make defconfig

make

make install

然后会生成 _install 目录,里面就是编译好的可执行文件

源码位于 ./networking/telnetd.c

 

程序流程图:

 

程序中非常重要的就是2个buf,位于struct tsession结构体之后

 

/*
   This is how the buffers are used. The arrows indicate data flow.

   +-------+     wridx1++     +------+     rdidx1++     +----------+
   |       | <--------------  | buf1 | <--------------  |          |
   |       |     size1--      +------+     size1++      |          |
   |  pty  |                                            |  socket  |
   |       |     rdidx2++     +------+     wridx2++     |          |
   |       |  --------------> | buf2 |  --------------> |          |
   +-------+     size2++      +------+     size2--      +----------+

   size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
   size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"

   Each session has got two buffers. Buffers are circular. If sizeN == 0,
   buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
   rdidxN == wridxN.
*/


 socket接收到远端的数据,写入count个字节到buf1中从rdidx1开始的空闲区域,然后size1 += count;rdidx1 += count;
pty可以写,从buf1中读取count个字节写入pty,然后size1 -= count;wridx1 += count;
pty可以读,写入count个字节到buf2中从rdidx2开始的空闲区域,然后size2 += count;rdidx2 += count;
socket可以发送数据到远端,从buf2中wridx2开始的位置读取count个字节,通过socket发送出去,然后size2

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值