TFTP服務(wù)器代碼_第1頁
TFTP服務(wù)器代碼_第2頁
TFTP服務(wù)器代碼_第3頁
TFTP服務(wù)器代碼_第4頁
TFTP服務(wù)器代碼_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、#define _VC#include #include #include #include #include #ifndef MAKEWORD#define MAKEWORD(l,h) (WORD)(BYTE)(l)|(WORD)(BYTE)(h)8)#endif#define WSA_MAJOR_VERSION 1#define WSA_MINOR_VERSION 1#define WSA_VERSION MAKEWORD(WSA_MAJOR_VERSION, WSA_MINOR_VERSION)/* read/write request packet format2 bytes stri

2、ng 1 byte string 1 byte-| Opcode | Filename | 0 | Mode | 0 |-*/#define TFTP_RRQ 1 /*Read request (RRQ)*/#define TFTP_WRQ 2 /*Write request (WRQ) */* DATA packet format2 bytes 2 bytes n bytes-| Opcode | block # | Data |-*/#define TFTP_DATA 3 /*Data (DA TA)*/* ACK packet format2 bytes 2 bytes-| Opcode

3、 | block # |-*/#define TFTP_ACK 4 /*Acknowledgment (ACK)*/*ERROR packet format2 bytes 2 bytes string 1 byte-| Opcode | ErrorCode | ErrMsg | 0 |-*/#define TFTP_ERROR 5 /*Error (ERROR)*/#define TFTP_NETASCII 0#define TFTP_OCTET 1#define TFTP_WSTA T_FIRSTACKE 0#define TFTP_WSTA T_NEXTACK 1#define TFTP_

4、WSTA T_LASTACK 2#define MAX_RETRY 3#define TFTP_NOTEND_DATALEN 512+2+2#ifdef _VC#pragma comment(lib,Wsock32.lib)#endif/*typedef void (* CMDFUNC)(char 256,int pcout);typedef struct _cmdnumchar *cmd;int num;int paramcount;CMDFUNC callback;CMDNUM,*PCMDNUM;*/typedef struct DATAint socknum;char *filename

5、;unsigned short remoteport;char *remoteaddr;DA TA passdata6;ofstream logfile(log.txt,ios:app);int makeack(unsigned short num,char *buffer,int size);void showsysinfo();void getfile(DATA *needdata);void putfile(DATA *needdata);int makeerror(int errcode,char errstring256,char *buffer,int bufsize);SOCKE

6、Tsock6=INV ALID_SOCKET,INV ALID_SOCKET,INV ALID_SOCKET,INV ALID_SOCKET,IN V ALID_SOCKET,INV ALID_SOCKET;int filemode =TFTP_OCTET;#define MYPORT 69void main(int argc,char *argv)WSADATA stWSAData;int ret=0;sockaddr_in addr;char recvbuf1024=0;char sendbuf1024=0;sockaddr_in from;sockaddr_in newaddr;int

7、fromlen=0;char filename256;int i,len;DWORD ThreadID6;showsysinfo();if(WSAStartup(WSA_VERSION,&stWSAData)!=0)printf(cant start socket rn);exit(0);if (sock0=socket(AF_INET,SOCK_DGRAM,0)0)perror(socket);exit(1);addr.sin_family=AF_INET;addr.sin_port=htons(MYPORT);addr.sin_addr.s_addr=INADDR_ANY;memset(&

8、(addr.sin_zero),0,8);if (bind(sock0,(struct sockaddr *)&addr,sizeof(struct sockaddr)0) perror(bind);exit(1);for (;)fromlen=sizeof(from);recvfrom(sock0,recvbuf,sizeof(recvbuf),0,(sockaddr*)&from,&fromlen);if (recvbuf0=0 & recvbuf10)for (i=0;i+)filenamei=recvbufi+2;if (filenamei=0) break;for (i=1;i=5;

9、i+) if (socki=INV ALID_SOCKET) passdatai.socknum=i;if (socki=socket(AF_INET,SOCK_DGRAM,0)0)perror(socket);exit(1);newaddr.sin_family=AF_INET;newaddr.sin_port=0;newaddr.sin_addr.s_addr=INADDR_ANY;memset(&(newaddr.sin_zero),0,8);if (bind(socki,(struct sockaddr *)&newaddr,sizeof(struct sockaddr)8);pos+

10、;bufferpos=(char)num;pos+;return pos;int makedata(int num,char *data,int datasize,char *buffer,int bufsize) int pos=0;bufferpos=0;pos+;bufferpos=TFTP_DA TA;pos+;bufferpos=(char)(num8);pos+;bufferpos=(char)num;pos+;memcpy(&bufferpos,data,datasize);pos=pos+datasize;return pos;int makeerror(int errcode

11、,char *errstring,char *buffer,int bufsize)int pos=0;int i;bufferpos=0;pos+;bufferpos=TFTP_ERROR;pos+;bufferpos=(char)(errcode8);pos+;bufferpos=(char)errcode;pos+;for(i=0;i+)bufferpos=errstringi;pos+;if (errstringi=0) break;return pos;void getfile(DATA *needdata)char sendbuf1024=0;char recvbuf1024=0;

12、sockaddr_in addr;sockaddr_in from;int fromlen=0;int ret=0;int len=0;/ fd_set fdr; int retry=0;/ struct timeval timeout=5,0; int stat=0;int lastdata=0; long flen=0; FILE *file;addr.sin_family=AF_INET; from.sin_family=AF_INET; addr.sin_port=htons(needdata-remoteport); addr.sin_addr.s_addr=inet_addr(ne

13、eddata-remoteaddr);/ printf(received WRQ form %sn,needdata-remoteaddr); if(file=fopen(needdata-filename,rb)!=NULL) fclose(file);printf(file %s already existn,needdata-filename); len=makeerror(6,file already exist0,sendbuf,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,si

14、zeof(addr);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;if (file=fopen(needdata-filename,w+b)=NULL)printf(cant create file rn);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;len=makeack(lastdata,sendbuf,sizeof(sendbuf);ret=sendto(sockne

15、eddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send ack 0# to %sn,needdata-remoteaddr); while(true)/* FD_ZERO(&fdr);FD_SET(sockneeddata-socknum,&fdr); ret=select(sockneeddata-socknum,&fdr,NULL,NULL,NULL); if(SOCKET_ERROR=ret)printf(socket error rn); fclose(file);return; else if

16、 (0=ret)if (MAX_RETRY=retry)printf(Time outrn);fclose(file);return;sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);retry+; else if (FD_ISSET(sockneeddata-socknum,&fdr)retry=0;*/fromlen=sizeof(sockaddr);ret=recvfrom(sockneeddata-socknum,recvbuf,sizeof(recvbuf),0,(sockaddr *)&

17、from,&fromlen);if(0=stat)addr.sin_port=from.sin_port;stat=1;if(TFTP_DATA=recvbuf1)lastdata=(recvbuf2+256)%256)*256+(recvbuf3+256)%256;/ printf(received data block %d# from %sn,lastdata,needdata-remoteaddr);len=makeack(lastdata,sendbuf,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockad

18、dr*)&addr,sizeof(addr);/ printf(send ack %d# to %sn,lastdata,needdata-remoteaddr); if(retremoteaddr);logfile received filename from remoteaddrrnsocknum);sockneeddata-socknum=INVALID_SOCKET;return; else fwrite(&recvbuf4,1,512,file); flen=flen+512;/ printf(%d byte receivedr,flen);/ / void putfile(DATA

19、 *needdata)char sendbuf1024=0;char recvbuf1024=0;char databuf1024=0;sockaddr_in addr;sockaddr_in from;int fromlen=0;int ret=0;int len=0;fd_set fdr;int retry=0;struct timeval timeout=5,0;int stat=TFTP_WSTAT_NEXTACK;int lastack=0;FILE *file;int flen=0;int blocknum=0;size_t rlen=0;addr.sin_family=AF_IN

20、ET;from.sin_family=AF_INET;addr.sin_port=htons(needdata-remoteport);addr.sin_addr.s_addr=inet_addr(needdata-remoteaddr);/ printf(received RRQ form %sn,needdata-remoteaddr); if(file=fopen(needdata-filename,rb)=NULL) printf(file %s not foundrn,needdata-filename); len=makeerror(1,file not found0,sendbu

21、f,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;rlen=fread(databuf,1,512,file);if (rlensocknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send data block %d# to %sn,blo

22、cknum,needdata-remoteaddr);for(;)FD_ZERO(&fdr);FD_SET(sockneeddata-socknum,&fdr);ret=select(sockneeddata-socknum,&fdr,NULL,NULL,&timeout);if(SOCKET_ERROR=ret)printf(socket errorrn);fclose(file);return; else if (0=ret)if (MAX_RETRY=retry)printf(time outrn);fclose(file);closesocket(sockneeddata-socknu

23、m);sockneeddata-socknum=INV ALID_SOCKET;return;sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send data block %d# to %sn,blocknum,needdata-remoteaddr);retry+; else retry=0; fromlen=sizeof(sockaddr);ret=recvfrom(sockneeddata-socknum,recvbuf,sizeof(recvbuf),0,(sockaddr *)&from,&fromlen);if(TFTP_ACK=recvbuf1)lastack=(recvbuf2+256)%256)*256+(recvbuf3+256)%256;/ printf(received ack %d# from %sn,lastack,needdata-remoteaddr);switch(stat)case TFTP_WSTAT_NEXTACK:if (lastack=blocknum)/ print

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論