Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90424
  • 博文数量: 44
  • 博客积分: 1920
  • 博客等级: 上尉
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-06 09:13
文章分类

全部博文(44)

文章存档

2011年(1)

2009年(43)

我的朋友

分类: LINUX

2009-07-20 13:24:14

程序理解帮助示意图:


//server.h文件

#ifndef __SERVER_H__
#define __SERVER_H__

#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>

typedef int (*P_FUN)(int fd);

struct cmd_info_t
{
    char *name;
    P_FUN fun;
};

int creat_server(int port);
int exe_telnet_fun(int fd, char *str);

#endif


//server.c文件

#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include "server.h"

int creat_server(int port)
{
    int server_fd, client_fd;
    struct sockaddr_in server_addr, client_addr;
    int len = sizeof(client_addr);
    
    bzero(&client_addr, sizeof(client_addr));
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if((server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        perror("socket");    
        exit(1);
    }
    if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("bind");    
        exit(1);
    }
    listen(server_fd,1);
    printf("TCP Shell start at port %d !\n\r", port);
    client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &len);
    printf("connected form %s\n\r", inet_ntoa(client_addr.sin_addr.s_addr));
    return client_fd;
}

static int do_cls(int connfd)
{
    const char clscode[] = "\x1b[H\x1b[J";
    write(connfd, clscode, strlen(clscode));
    return 0;
}

static int do_exit(int connfd)
{
    return -1;
}

struct cmd_info_t TelnetList[] = {
    {"cls", do_cls},
    {"clear", do_cls},
    {"exit", do_exit},
};

int exe_telnet_fun(int fd, char *str)
{
    char buf[10]="";
    int i = 0;
    while( (!((*str>64) && (*str<91))) && (!((*str>96) && (*str<123))) )
    {        str++;    }
    
    while(((*str>64) && (*str<91)) || ((*str>96) && (*str<123)))
    {
        buf[i++] = *str;
        str++;
    }
    for(i=0;i<(sizeof(TelnetList)/sizeof(TelnetList[0]));i++)
    {
        if(strcmp(buf, TelnetList[i].name)==0)
        {
                TelnetList[i].fun(fd);
                return 1;
        }
    }
    return 0;
}


//main.c文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "server.h"

int main()
{
    int sockfd;
    int fd_in[2];
    int fd_out[2];
    pipe(fd_in);
    pipe(fd_out);
    sockfd = creat_server(5556);
    if(fork()==0)        //run shell

    {
        close(fd_out[0]);
        close(fd_in[1]);
        
        dup2(fd_in[0],0);
        dup2(fd_out[1],1);
        dup2(fd_out[1],2);
        execl("/bin/bash","bash",NULL);
        perror("bash");
        while(1);
    }
    
    close(fd_out[1]);
    close(fd_in[0]);
    
    if(fork()==0) //read cmd from socket

    {
        close(fd_out[0]);    
        while(1)
        {
            char cmd_buf[100];
            int i = 0;
            char ch;
            while(1)
            {    
                i = 0;
                memset(cmd_buf, 0, 100);
                write(sockfd, "Telnet>#", 9);
                read(sockfd, &ch, 1);
                while(ch!='\n')
                {
                    if(ch==8)
                    {
                        cmd_buf[i-1]='\0';
                        i--;
                    }
                    else
                    {
                        cmd_buf[i++]=ch;    
                    }
                    read(sockfd, &ch, 1);                    
                }
                cmd_buf[i-1]='\0';
                printf("get cmd: #%s#\n",cmd_buf);
                if(cmd_buf[0]=='1')
                    close(sockfd);
                if(!exe_telnet_fun(sockfd, cmd_buf))
                {
                    write(fd_in[1], cmd_buf, strlen(cmd_buf));
                    write(fd_in[1], "\n", 2);
                }
                usleep(300*1000);    
            }
        }
    }

    close(fd_in[1]);            
    while(1)            //send cmd result to socket

    {        
        char buf[200]="";        
        dup2(fd_out[0],0);
        fgets(buf, 100, stdin);
        printf("gets buf=%s\n",buf);
        buf[strlen(buf)+1]='\r';
        write(sockfd, buf, strlen(buf)+2);
    }
    return 0;
}

阅读(595) | 评论(0) | 转发(0) |
0

上一篇:Rip详解

下一篇:a complicated tcp shell

给主人留下些什么吧!~~