www.pudn.com > TcpIpOn8051.rar > telnet.C
/*------------------------------------------------------------------------------ main.C Copyright 1995-1999 Keil Software, Inc. ------------------------------------------------------------------------------*/ #include "public.h" #include "ether.h" #include "arp.h" #include "ip.h" #include "tcp.h" #includestatic WORD III; char clientcmd[30]; // Pointer to command-line command BYTE telopts[] = { TEL_IAC, TEL_DO, TEL_SGA, // Do suppress go-ahead TEL_IAC, TEL_WONT, TEL_ECHO, // Won't echo TEL_IAC, TEL_WONT, TEL_AUTH // Won't authenticate }; extern mycomp xdata mypara; extern TCP_SOCK xdata tsock[nsocks]; extern get_current(); extern delay_ms(BYTE sec); extern initcpu(); //WORD server_action(TCP_SOCK *ts, CONN_STATE conn); WORD client_action(TCP_SOCK *ts, CONN_STATE conn); extern void Task_attemper(void); extern void ip_arpreq(ip destip); extern void icmp_send(netcard *netbuf,ip destip,BYTE type,BYTE icode,WORD ident,WORD seq,WORD len); extern void print(char str[]); extern BYTE tcp_open(TCP_SOCK *ts,ip destip, WORD destport); //extern WORD (*server_upcall)(TCP_SOCK *ts, CONN_STATE conn);// TCP server action //extern WORD (*client_upcall)(TCP_SOCK *ts, CONN_STATE conn);// TCP client action extern WORD buff_in(CBUFF *bp, BYTE *tdata, WORD len); extern WORD buff_out(CBUFF *bp, BYTE *tdata, WORD maxlen); //extern WORD buff_instr(CBUFF *bp, char *str); //extern WORD tcp_check(tcppacket *tcp,DWORD srcip,DWORD destip,WORD tlen); void ping(ip destip) { netcard xdata netbuf;//缓冲区,注意:可能需要修改 print("pinging 202.118.19.174!\n\r"); icmp_send(&netbuf,destip,8,0x00,0x0200,III,60); III++; } void main() { //ip xdata destip,srcip; //WORD destport,tlen; //tcppacket tcp; WORD numnum; initcpu(); numnum=0; print("start!\n\r"); ip_arpreq(mypara.mygate); //server_upcall = server_action; //client_upcall = client_action; /* srcip=0xca7613ae; destip=0xca7613ad; tlen=0x0028-sizeof(ipheader); tcp.t.sport=1848; // Source port tcp.t.dport=1024; // Destination port tcp.t.seq=0x99d9f4bd; // Sequence number tcp.t.ack=0x00f80400; // Ack number tcp.t.hlen=0x05; // TCP header len (num of bytes << 2) tcp.t.flags=0x18; // Option flags tcp.t.window=0x4000; // Flow control credit (num of bytes) tcp.t.check=0; // Checksum tcp.t.urgent=0; // Urgent data pointer tcp.t.sport=0x0740; // Source port tcp.t.dport=0x4000; // Destination port tcp.t.seq=0xf9d4fa4b; // Sequence number tcp.t.ack=0x00000000; // Ack number tcp.t.hlen=0x70; // TCP header len (num of bytes << 2) tcp.t.flags=0x02; // Option flags tcp.t.window=0x4000; // Flow control credit (num of bytes) tcp.t.check=0x8776; // Checksum tcp.t.urgent=0; // Urgent data pointer tcp.tcpdata[0]=2; tcp.tcpdata[1]=4; tcp.tcpdata[2]=0x05; tcp.tcpdata[3]=0xb4; tlen=0x1c; numnum=tcp_check(&tcp,srcip,destip,tlen); destip=0xca7613ae; destport=1024; */ //ping(destip); //delay_ms(10); //tcp_open(&tsock[0],destip,destport); while(1) { numnum++; Task_attemper(); } } /* //server upcall WORD server_action(TCP_SOCK *ts, CONN_STATE conn) { WORD ok=1; WORD port, len; BYTE temps[30]; // time_t t; // temps[1]='\0'; port = ts->locport; // Connection being opened if (conn == TCP_OPEN) { ok = port==echoport || port==httpport || port==1024; //if (port == echoport || port==1024) // 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 || port==1024) // If Echo { // ..echo it back! len=buff_out(&ts->rxbuf, temps, sizeof(temps)); temps[1]='\0'; if(len!=0) print(&temps[0]); //temps[0]=0x41; buff_in(&ts->txbuf, &temps[0], 1); } } return(ok); } */ //client upcall WORD client_action(TCP_SOCK *ts, CONN_STATE conn) { char ret[]="\r\n"; if (conn == TCP_OPEN) { if (ts->remport == telport || ts->remport == 1024) // If login, send Telnet opts buff_in(&ts->txbuf, telopts, sizeof(telopts)); if (clientcmd) { buff_in(&ts->txbuf, clientcmd,sizeof(clientcmd));// Send command-line string buff_in(&ts->txbuf, ret,sizeof(ret)); } } return(1); } /*EOF*/