公开TCP/IP 源代码
如果反响热烈的话我将贴出我我写的一个在TC 纯DOS 实现的一个TCP/IP 协议栈
这一个是是TFTP 协议的实现(下载),没反映的话就此打住。
请不要将此程序用于商业目的:)
此程序调用了其他文件的函数,以后会有说明。
本人现在找工作,如果那家公司对本人有 兴趣的话联系
hu_guozhong@yahoo.com.cn
QQ 34138008
int tftprequest(tftphdr *th, u_short request, u_char *name, u_char *mode)
{
u_char *cp;
th->th_opcode = htons(request);
cp = th->th_u.tu_stuff;
while(*name)
{
*cp++ = *name++;
}
*cp++ = '\0';
while(*mode)
{
*cp++ = *mode++;
}
*cp++ = '\0';
return cp - th->th_u.tu_stuff +2;
}
/*!
* \brief Download a file from a TFTP server and burn it into the flash ROM.
*
* \return 1 on success, 0 otherwise.
*/
int tftprev(void)
{
u_char retry;
int rlen = 0/*,rc=0*/;
int slen;
/* u_char *cp=tftpdata; ************************************/
int i;
u_short sport = 1024;
u_short dport = 69;
u_short block = 0;
tftphdr *thout,*threv;
/*
* Prepare the transmit buffer for a file request.
*/
thout=(tftphdr *)malloc(sizeof(tftphdr));
threv=(tftphdr *)malloc(sizeof(tftphdr));
printf("\n tftp request begin");
strcpy(bootpfile,"boot.txt");/****************************/
slen = tftprequest(thout, TFTP_RRQ, bootpfile, "octet");
/*
* Lopp until we receive a packet with less than 512 bytes of data.
*/
do {
/*
* Send file request or acknowledge and receive
* a data block.
*/
/* We destroy the nb in any case which is failed ,and free the memory */
for(retry = 0; retry < 1; retry++) {
if(sendto( dport, sport, thout,slen,server_ip) >=1) {
destroybuf();
delay(65500);
if((rlen = receive(dport,sport,threv,sizeof(tftphdr))) >= 4)
{
/* printf("\nthe times is=%d tftpdata length=%d\n",block+1,rlen); ***********************/
break;
}
else {
destroybuf();
continue;
}/* end if(receive())*/
} else
{
destroybuf();
continue;
}/* end if(sendto) */
} /* end for() */
/*
* Can't reach the TFTP server or got a malformed
* repsonse.
*/
if(rlen < 4)
{
free(thout);
free(threv);
return 0;
}
/*
* Accept data blocks only. Anything else will stop
* the transfer with an error.
*/
if(htons(threv->th_opcode) != TFTP_DATA)
{
if(htons(threv->th_opcode)==TFTP_ERROR)
printf("\nFILE NOT FIND OR TFTP SERVER NOT OPEN");
free(threv);
free(thout);
return 0;
}
/*
* If this was the first block we received, prepare
* the send buffer for sending ACKs.
*/
if(block == 0) {
dport=((udphdr *)nb.tp.vp)->sport;
dport=htons(dport);
/* printf("\nthe ACK de dport =%x",dport); */
thout->th_opcode = htons(TFTP_ACK);
slen = 4;
}
/*
* If this block is out of sequence, we ignore it.
* However, if we missed the first block, return
* with an error.
*/
if(htons(threv->th_u.tu_block) != block + 1) {
if(block == 0)
{
free(threv);
free(thout);
return 0;
}
continue;
}
/*
* Burn the received data into the flash ROM.
*/
if(rlen > 4)
{
/* memcpy(cp, threv->th_data, rlen - 4); *******************/
insertdata(block,threv->th_data,rlen-4); /*****************/
/* travelist(); */
/* cp=cp+rlen-4; ************************/
/* *cp='\0'; *************************************/
}
/*
* Update our block counter.
*/
/* printf("\nthe tftpdata1111=%s",tftpdata); */
block++;
thout->th_u.tu_block = htons(block);
destroybuf();
} while(rlen >= 516);/*end do? */
printf("\nBLOCK NUMBER IS:",block);
/*
* Send the last ACK.
*/
sendto(dport, sport,thout, slen,server_ip);
destroybuf();
free(thout);
free(threv);
return 1;
}
程序不完善的地方,欢迎大家提出修改意见!!!