?? tftpd.c
字號:
//printf("validate_access 1\n"); file = fdopen(fd, (mode == RRQ)? "r":"w");//printf("validate_access 2 file =%p\n",file); if (file == NULL) { return errno+100; }//printf("validate_access 3\n"); return (0);}int timeout;jmp_buf timeoutbuf;//timer()void timer(void){ timeout += rexmtval; if (timeout >= maxtimeout) _exit(1);#ifdef EMBED printf("%s(%d): no longjmp()\n", __FILE__, __LINE__);#else longjmp(timeoutbuf, 1);#endif}/* * Send the requested file. */sendfile(pf) struct formats *pf;{ unsigned char *dp, *r_init(); unsigned char *ap; /* ack packet */ register int block = 1, size, n; short th_opcode,th_opcode2; unsigned short th_block,th_block2;//printf(" ===== sendfile 1\n"); //signal(SIGALRM, (void *)timer); dp = r_init(); ap = (unsigned char *)ackbuf; do {//printf(" ===== sendfile 2 file = %p \n",file); size = readit(file, &dp, pf->f_convert);printf(" **** size %d , block = %d\n",size,block); if (size < 0) { nak(errno + 100); goto abort; } //th_opcode = htons((u_short)DATA); //th_block = htons((u_short)block); //dp[0] = th_opcode && 0xff; //dp[1] = (th_opcode >> 8)&& 0xff; //dp[2] = th_block && 0xff; //dp[3] = (th_block >> 8)&& 0xff; dp[0] = 0; dp[1] = DATA; dp[2] = (block >> 8) & 0xff; dp[3] = block & 0xff;//printf("th_opcode = %d , th_block = %d\n",th_opcode,th_block);printf("th_opcode = %d , th_block = %d\n",*(unsigned short*)(&dp[0]),*(unsigned short*)(&dp[2])); timeout = 0;#ifndef EMBED (void) setjmp(timeoutbuf);#endifsend_data://printf(" ===== sendfile 3\n"); if (send(peer, dp, size + 4, 0) != size + 4) { //syslog(LOG_ERR, "tftpd: write: %m\n"); goto abort; }//printf(" ===== sendfile 4 f_convert = %d \n", pf->f_convert); read_ahead(file, pf->f_convert); for ( ; ; ) {//printf(" ===== sendfile 5\n"); // alarm(rexmtval); /* read the ack */ n = recv(peer, ackbuf, sizeof (ackbuf), 0); // alarm(0); if (n < 0) { //syslog(LOG_ERR, "tftpd: read: %m\n"); goto abort; } th_opcode2 = (ap[0] << 8 | ap[1]); th_block2 = (ap[2] << 8 | ap[3]);// ap->th_opcode = ntohs((u_short)ap->th_opcode);// ap->th_block = ntohs((u_short)ap->th_block);printf("th_opcode2 = %d , th_block2 = %d , block = %d\n",th_opcode2, th_block2,block); if (th_opcode2 == ERR) goto abort; if (th_opcode2 == ACK) { if (th_block2 == block) { break; } /* Re-synchronize with the other side */ (void) synchnet(peer); if (th_block2 == (block -1)) { goto send_data; } } } block++; } while (size == SEGSIZE);abort: (void) fclose(file);}justquit(){ _exit(0);}/* * Receive a file. */recvfile(pf) struct formats *pf;{ unsigned char *dp, *w_init(); unsigned char *ap; /* ack buffer */ register int block = 0, n, size; short th_opcode,th_opcode2; unsigned short th_block,th_block2;// signal(SIGALRM, (void *)timer); dp = w_init(); ap = (unsigned char *)ackbuf; do { timeout = 0;// th_opcode = htons((u_short)ACK);// th_block = htons((u_short)block); ap[0] = 0; ap[1] = ACK; ap[2] = (block >> 8) & 0xff; ap[3] = block & 0xff; block++;#ifndef EMBED (void) setjmp(timeoutbuf);#endifsend_ack: if (send(peer, ackbuf, 4, 0) != 4) { //syslog(LOG_ERR, "tftpd: write: %m\n"); goto abort; }//printf("-------- recvfile 1\n"); write_behind(file, pf->f_convert); for ( ; ; ) {printf("-------- recvfile 2\n");// alarm(rexmtval);#if 1{ int i; struct sockaddr_in from; socklen_t fromlen; (void)ioctl(peer, FIONREAD, &i); fromlen = sizeof(from); printf("FIONREAD=%d fromlen=%d\n",i,fromlen); n = recvfrom(peer,dp,PKTSIZE,0,(struct sockaddr*)&from,&fromlen); printf("n=%d\n",n);}#else n = recv(peer, dp, PKTSIZE, 0);#endif// alarm(0);printf("-------- recvefile 2-1 n = %d\n",n); if (n < 0) { /* really? */ //syslog(LOG_ERR, "tftpd: read: %m\n"); goto abort; }//printf("-------- recvfile 3\n"); th_opcode2 = (dp[0] << 8 | dp[1]); th_block2 = (dp[2] << 8 | dp[3]);// th_opcode2 = ntohs((u_short)th_opcode2);// th_block2 = ntohs((u_short)th_block2); if (th_opcode2 == ERR) goto abort; if (th_opcode2 == DATA) { if (th_block2 == block) { break; /* normal */ }#if 0327 * 0 /* Re-synchronize with the other side */ (void) synchnet(peer);#endif if (th_block2 == (block-1)) goto send_ack; /* rexmit */ } } /* size = write(file, dp->th_data, n - 4); */ size = writeit(file, &dp, n - 4, pf->f_convert);//printf("--------- recvfile 4size %d \n",size); if (size != (n-4)) { /* ahem */ if (size < 0) nak(errno + 100); else nak(ENOSPACE); goto abort; }//printf("-------- recvfile 5\n"); } while (size == SEGSIZE); write_behind(file, pf->f_convert);//printf("-------- recvfile 6\n");// (void) fclose(file); /* close data file */// th_opcode = htons((u_short)ACK); /* send the "final" ack */// th_block = htons((u_short)(block)); ap[0] = 0; ap[1] = ACK; ap[2] = (block >> 8) & 0xff; ap[3] = block & 0xff; (void) send(peer, ackbuf, 4, 0);#if 0// signal(SIGALRM, (void *)justquit); /* just quit on timeout */// alarm(rexmtval); n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */// alarm(0); if (n >= 4 && /* if read some data */ th_opcode2 == DATA && /* and got a data block */ block == th_block2) { /* then my last ack was lost */ (void) send(peer, ackbuf, 4, 0); /* resend final ack */ }#endifabort: (void) fclose(file); /* close data file */}struct errmsg { int e_code;// char *e_msg; char e_msg[40];} errmsgs[] = { { EUNDEF, "Undefined error code" }, { ENOTFOUND, "File not found" }, { EACCESS, "Access violation" }, { ENOSPACE, "Disk full or allocation exceeded" }, { EBADOP, "Illegal TFTP operation" }, { EBADID, "Unknown transfer ID" }, { EEXISTS, "File already exists" }, { ENOUSER, "No such user" }, { -1, 0 }};/* * Send a nak packet (error message). * Error code passed in is one of the * standard TFTP codes, or a UNIX errno * offset by 100. */nak(error) int error;{ unsigned char *tp; int length, i; register struct errmsg *pe; short th_opcode,th_code; char *th_msg;/* extern char *sys_errlist[]; */ tp = (unsigned char *)buf; tp[0] = 0; tp[1] = ERR; tp[2] = (error >> 8) & 0xff; tp[3] = error & 0xff; //th_opcode = htons((u_short)ERR); //th_code = htons((u_short)error);#if 0326 for (pe = errmsgs, i = sizeof(errmsgs)/sizeof(errmsgs[0]);; pe++){ if (--i < 0){ pe = errmsgs; break; } if (pe->e_code == error){ break; } }#else for (pe = errmsgs; pe->e_code >= 0; pe++) if (pe->e_code == error) break;#endif#if 0 if (pe->e_code < 0) {#ifdef EMBED strcpy(pe->e_msg,"ERR");// pe->e_msg = "ERROR";#else pe->e_msg = sys_errlist[error - 100];#endif th_code = EUNDEF; /* set 'undef' errorcode */ }#endif strcpy(&tp[4], pe->e_msg); length = strlen(pe->e_msg);#if 0 th_msg[length] = '\0';#endif length += 5; if (send(peer, buf, length, 0) != length) //syslog(LOG_ERR, "nak: %m\n"); ;}#ifdef NAOSI_ABS32void naosi(void *pp, int *type);static char* naosi_tab[] = { (char*)naosi, (char*)naosi_tab,};void naosi(void *ptr, int *type){ int t, dt, dd; char *p, **pp; dt = (char*)naosi - naosi_tab[0]; dd = (char*)naosi_tab - naosi_tab[1]; for(pp = (char**)ptr; (t = *type++) >= 0; ++pp){ if((p = *pp) != 0){ if(t == 1){ p += dt; }else if(t == 2){ p += dd; } *pp = p; } }}int type_func[] = { 1, -1 };int type_data[] = { 2, -1 };#endif /* NAOSI_ABS32 */#ifdef NAOSI_ABS32naosi_tftpd(){ int i; for( i=0; i<sizeof(formats)/sizeof(struct formats); i++ ){ naosi(&formats[i].f_validate, type_func); naosi(&formats[i].f_send, type_func); naosi(&formats[i].f_recv, type_func); }}#endif /* NAOSI_ABS32 */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -