⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 telnet.c

📁 嵌入式TCP/IP协议栈应用主机端程序(VC6源码) 一个专为嵌入式系统编写的小型TCP/IP协议栈TCP/IPLean
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Read network config file to get IP address netmask and gateway
** Return driver type, 0 if error */
WORD read_netconfig(char *fname, NODE *np)
{
    char temps[31];
    WORD dtype=0;
    BYTE b;

    if (read_cfgstr(fname, "net", netcfg, MAXNETCFG))
    {                                               /* Get IP address */
        if (!read_cfgstr(fname, "ip", temps, 30) || (np->ip=atoip(temps))==0)
            printf("No IP address\n");
        else if (!(dtype = open_net(netcfg)))       /* Open net driver */
            printf("Can't open net driver '%s'\n", netcfg);
        else
        {                                           /* Save ether address */
            memcpy(np->mac, ether_addr(dtype), MACLEN);
            np->dtype = dtype;                      /* ..and driver type */
            b = (BYTE)(np->ip >> 24);
            if (read_cfgstr(fname, "mask", temps, 30))
                np->mask = atoip(temps);            /* Get netmask */
            else
                np->mask = b<128 ? 0xff000000L: b<192 ? 0xffff0000L:0xffffff00L;
            if (read_cfgstr(fname, "gate", temps, 30))
                np->gate = atoip(temps);            /* Get gateway IP addr */
            else
                np->gate = 0;
        }
    }
    return(dtype);
}

/* Return ptr to local node 'n' (n=0 for first), return 0 if doesn't exist
** Used by IP functions to get my netmask & gateway addresses */
NODE *locnode_n(int n)
{
    return(n==0 ? &locnode : 0);
}

/* Upcall from TCP stack to server when opening, connecting, receiving data
** or closing. Return 0 to prevent connection opening, or close if connected */
int server_action(TSOCK *ts, CONN_STATE conn)
{
    int ok=1;
    WORD port, len;
    BYTE temps[30];
    time_t t;

    port = ts->loc.port;                    /* Connection being opened */
    if (conn == TCP_OPEN)
    {
        ok = port==ECHOPORT || port==DAYPORT || port==HTTPORT;
        if (port == DAYPORT)                    /* Daytime server? */
        {
            time(&t);                           /* ..send date & time string */
            buff_in(&ts->txb, (BYTE *)ctime(&t), 24);
            buff_in(&ts->txb, (BYTE *)"\r\n", 2);
        }
        else if (port == ECHOPORT)              /* Echo server? */
            ts->connflags = TPUSH;              /* ..use PUSH flag */
        else if (port == HTTPORT)               /* HTTP server ? */
        {                                       /* ..load my page */
            buff_in(&ts->txb, (BYTE *)MYPAGE, sizeof(MYPAGE));
        }
    }
    else if (conn == TCP_CONN)              /* Connected */
    {
        if (port==DAYPORT || port==HTTPORT)     /* If daytime or HTTP */
            ok = 0;                             /* ..close straight away */
    }
    else if (conn == TCP_DATA)              /* Received data */
    {
        if (port == ECHOPORT)                   /* If Echo */
        {                                       /* ..echo it back! */
            while ((len=buff_out(&ts->rxb, temps, sizeof(temps)))!=0)
                buff_in(&ts->txb, temps, len);
        }
    }
    return(ok);
}

/* Upcall from TCP stack to client when opening, connecting, receiving data
** or closing. Return 0 to close if connected */
int client_action(TSOCK *ts, CONN_STATE conn)
{
    if (conn == TCP_OPEN)
    {
        if (ts->rem.port == TELPORT)        /* If login, send Telnet opts */
            buff_in(&ts->txb, telopts, sizeof(telopts));
        if (clientcmd)
        {
            buff_instr(&ts->txb, clientcmd);/* Send command-line string */
            buff_instr(&ts->txb, "\r\n");
        }
    }
    return(1);
}

/* Telnet client display */
void do_teldisp(TSOCK *ts)
{
    static BYTE d[3]={0,0,0};

    while (buff_dlen(&ts->rxb))
    {
        if (d[0] == TEL_IAC)
        {
            if (!d[1] && buff_out(&ts->rxb, &d[1], 1))
            {
                if (d[1] == TEL_IAC)
                    d[0] = d[1] = 0;
                else if (!d[2] && buff_out(&ts->rxb, &d[2], 1))
                {
                    d[1] = TEL_WONT;
                    buff_in(&ts->txb, d, 3);
                    d[0] = d[1] = d[2] = 0;
                }

            }
        }
        else while (buff_out(&ts->rxb, d, 1) && d[0]!=TEL_IAC)
            putchar(d[0]);
    }
}

/* Check for incoming packets, send response if required */
void do_receive(GENFRAME *gfp)
{
    NODE node;
    ARPKT *arp;
    IPKT *ip;
    ICMPKT *icmp;
    int rxlen, txlen, len;

    if ((rxlen=get_frame(gfp)) > 0)                 /* Any incoming frames? */
    {
        ip = getframe_datap(gfp);
        if (is_arp(gfp, rxlen))
        {                                           /* ARP response? */
            arp = getframe_datap(gfp);
            if (arp->op==ARPREQ && arp->dip==locnode.ip)
            {                                       /* ARP request? */
                node.ip = arp->sip;                 /* Make ARP response */
                memcpy(node.mac, arp->smac, MACLEN);
                txlen = make_arp(gfp, &locnode, &node, ARPRESP);
                put_frame(gfp, txlen);              /* Send packet */
            }
            if (arp->op==ARPRESP && arp->dip==locnode.ip)
            {                                       /* ARP response? */
                arp_receive(tsocks, NSOCKS, gfp);
            }
        }
        else if ((rxlen=is_ip(gfp, rxlen))!=0 &&    /* IP datagram? */
                 ip->i.dip==locnode.ip || ip->i.dip==BCASTIP)
        {
            getip_srce(gfp, &node);
            if ((len=is_icmp(ip, rxlen))!=0)        /* ICMP? */
            {
                icmp = (ICMPKT *)ip;
                if (icmp->c.type==ICREQ)            /* Echo request? */
                {
                    len = (WORD)maxi(len, 0);       /* Make response */
                    txlen = make_icmp(gfp, &locnode, &node, ICREP,
                                      icmp->c.code, (WORD)len);
                    put_frame(gfp, txlen);          /* Send packet */
                }
            }
            else if ((len=is_tcp(ip, rxlen))!=0)    /* TCP? */
            {
                tcp_receive(tsocks, NSOCKS, gfp, len);
            }
        }
    }
}

/* Poll the network interface to keep it alive */
void do_poll(GENFRAME *gfp)
{
    tcp_poll(tsocks, NSOCKS, gfp);
    poll_net(gfp->g.dtype);
}

/* Convert string (numeric or alphabetic) into a port number, 0 if error */
WORD str2service(char *str)
{
    WORD port=0;

    if (isdigit(*str))
        port = atoi(str);
    else if (!stricmp(str, "echo"))
        port = ECHOPORT;
    else if (!stricmp(str, "daytime"))
        port = DAYPORT;
    else if (!stricmp(str, "http"))
        port = HTTPORT;
    else
        printf("Unrecognised port '%s'\n", str);
    return(port);
}

/* Display usage help */
void disp_usage(void)
{
    printf("Usage:    TELNET [ options ] [ IP_addr [ port [ command ]]]\n");
    printf("Options:  -c name   Config filename (default %s)\n", CFGFILE);
    printf("          -s        State display\n");
    printf("          -t        TCP segment display\n");
    printf("          -v        Verbose packet display\n");
    printf("          -x        Hex packet display\n");
    printf("Example:  TELNET 10.1.1.1 http \"GET /index.html\"\n");
}

/* Ctrl-break handler: set flag and return */
void break_handler(int sig)
{
    breakflag = sig;
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -