|
连上去以后出以下提示,好象是数据处理得不对
: command not found
ls
: command not found
exit
: command not found
程序收到连接后fork一个bash,重定向它的输入输出,然后启动两个线程接收和发送数据
/* A telnet daemon
gcc -pedantic -Wall -o telnetd telnetd.c -lpthread
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 10000
#define BUF_SIZE 2048
int sock_client, quit_thread;
void create_shell();
void *recv_data(int file_pipe);
void *send_data(int file_pipe);
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in sa;
/* Create an unnamed socket */
sock=socket(AF_INET, SOCK_STREAM, 0);
if (sock==-1)
{
perror("socket error");
exit(EXIT_FAILURE);
}
while (sock!=-1)
{
/* Name the socket */
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=htonl(INADDR_ANY);
sa.sin_port=htons(PORT);
if (bind(sock, (struct sockaddr *)&sa, sizeof(sa))==-1)
{
perror("bind error");
break;
}
/* Wait for clients */
if (listen(sock, 5)==-1)
{
perror("listen error");
break;
}
/* Accept a connection */
sock_client=accept(sock, NULL, NULL);
if (sock_client==-1)
{
perror("accept error");
break;
}
else
{
close(sock);
create_shell();
close(sock_client);
sock=socket(AF_INET, SOCK_STREAM, 0);
usleep(1000);
}
}
close(sock);
exit(EXIT_SUCCESS);
}
void create_shell()
{
int read_pipes[2], write_pipes[2];
pid_t fork_pid;
if (pipe(read_pipes)==-1 || pipe(write_pipes)==-1)
{
perror("pipe error");
return;
}
fork_pid=fork();
if (fork_pid==-1)
{
perror("fork error");
return;
}
if (fork_pid==0)
{
/* redirect stdin */
close(0);
dup(write_pipes[0]);
close(write_pipes[0]);
close(write_pipes[1]);
/* redirect stdout */
close(1);
dup(read_pipes[1]);
/* redirect stderr */
close(2);
dup(read_pipes[1]);
close(read_pipes[1]);
close(read_pipes[0]);
execlp("bash", "bash", NULL);
}
else
{
pthread_t tid_recv, tid_send;
close(write_pipes[0]);
close(read_pipes[1]);
/* Create data recieving thread */
if (pthread_create(&tid_recv, NULL, (void *(*)(void *))recv_data, (void *)write_pipes[1]))
{
perror("Thread creation failed: recv_data");
close(write_pipes[1]);
close(read_pipes[0]);
wait(NULL);
return;
}
/* Create data sending thread */
if (pthread_create(&tid_send, NULL, (void *(*)(void *))send_data, (void *)read_pipes[0]))
{
perror("Thread creation failed: send_data");
close(read_pipes[0]);
wait(NULL);
return;
}
/* Wait for child process ending*/
wait(NULL);
quit_thread=1;
pthread_join(tid_recv, NULL);
pthread_join(tid_send, NULL);
}
}
void *recv_data(int file_pipe)
{
char buf[BUF_SIZE];
int datalen;
while (!quit_thread)
{
/* write pipe */
datalen=read(sock_client, buf, sizeof(buf));
if (datalen==0 || datalen==-1)
break;
write(file_pipe, buf, datalen);
}
quit_thread=1;
close(file_pipe);
pthread_exit(NULL);
}
void *send_data(int file_pipe)
{
char buf[BUF_SIZE];
int datalen;
while (!quit_thread)
{
/* read pipe */
datalen=read(file_pipe, buf, sizeof(buf));
if (datalen==0 || datalen==-1)
break;
write(sock_client, buf, datalen);
}
quit_thread=1;
close(file_pipe);
pthread_exit(NULL);
} |
|