tftpd.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. *
  9. * Trivial file transfer protocol server.
  10. *
  11. * This code includes many modifications by Jim Guyton <guyton@rand-unix>
  12. *
  13. * This source file was started based on netkit-tftpd 0.17
  14. * Heavily modified for curl's test suite
  15. */
  16. /*
  17. * Copyright (c) 1983 Regents of the University of California.
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. All advertising materials mentioning features or use of this software
  29. * must display the following acknowledgement:
  30. * This product includes software developed by the University of
  31. * California, Berkeley and its contributors.
  32. * 4. Neither the name of the University nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. */
  48. #include "server_setup.h"
  49. #ifdef HAVE_SYS_IOCTL_H
  50. #include <sys/ioctl.h>
  51. #endif
  52. #ifdef HAVE_SIGNAL_H
  53. #include <signal.h>
  54. #endif
  55. #ifdef HAVE_FCNTL_H
  56. #include <fcntl.h>
  57. #endif
  58. #ifdef HAVE_NETINET_IN_H
  59. #include <netinet/in.h>
  60. #endif
  61. #ifdef HAVE_ARPA_INET_H
  62. #include <arpa/inet.h>
  63. #endif
  64. #ifdef HAVE_ARPA_TFTP_H
  65. #include <arpa/tftp.h>
  66. #else
  67. #include "tftp.h"
  68. #endif
  69. #ifdef HAVE_NETDB_H
  70. #include <netdb.h>
  71. #endif
  72. #ifdef HAVE_SYS_FILIO_H
  73. /* FIONREAD on Solaris 7 */
  74. #include <sys/filio.h>
  75. #endif
  76. #include <setjmp.h>
  77. #ifdef HAVE_PWD_H
  78. #include <pwd.h>
  79. #endif
  80. #define ENABLE_CURLX_PRINTF
  81. /* make the curlx header define all printf() functions to use the curlx_*
  82. versions instead */
  83. #include "curlx.h" /* from the private lib dir */
  84. #include "getpart.h"
  85. #include "util.h"
  86. #include "server_sockaddr.h"
  87. /* include memdebug.h last */
  88. #include "memdebug.h"
  89. /*****************************************************************************
  90. * STRUCT DECLARATIONS AND DEFINES *
  91. *****************************************************************************/
  92. #ifndef PKTSIZE
  93. #define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */
  94. #endif
  95. struct testcase {
  96. char *buffer; /* holds the file data to send to the client */
  97. size_t bufsize; /* size of the data in buffer */
  98. char *rptr; /* read pointer into the buffer */
  99. size_t rcount; /* amount of data left to read of the file */
  100. long testno; /* test case number */
  101. int ofile; /* file descriptor for output file when uploading to us */
  102. int writedelay; /* number of seconds between each packet */
  103. };
  104. struct formats {
  105. const char *f_mode;
  106. int f_convert;
  107. };
  108. struct errmsg {
  109. int e_code;
  110. const char *e_msg;
  111. };
  112. typedef union {
  113. struct tftphdr hdr;
  114. char storage[PKTSIZE];
  115. } tftphdr_storage_t;
  116. /*
  117. * bf.counter values in range [-1 .. SEGSIZE] represents size of data in the
  118. * bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE.
  119. */
  120. struct bf {
  121. int counter; /* size of data in buffer, or flag */
  122. tftphdr_storage_t buf; /* room for data packet */
  123. };
  124. #define BF_ALLOC -3 /* alloc'd but not yet filled */
  125. #define BF_FREE -2 /* free */
  126. #define opcode_RRQ 1
  127. #define opcode_WRQ 2
  128. #define opcode_DATA 3
  129. #define opcode_ACK 4
  130. #define opcode_ERROR 5
  131. #define TIMEOUT 5
  132. #undef MIN
  133. #define MIN(x,y) ((x)<(y)?(x):(y))
  134. #ifndef DEFAULT_LOGFILE
  135. #define DEFAULT_LOGFILE "log/tftpd.log"
  136. #endif
  137. #define REQUEST_DUMP "log/server.input"
  138. #define DEFAULT_PORT 8999 /* UDP */
  139. /*****************************************************************************
  140. * GLOBAL VARIABLES *
  141. *****************************************************************************/
  142. static struct errmsg errmsgs[] = {
  143. { EUNDEF, "Undefined error code" },
  144. { ENOTFOUND, "File not found" },
  145. { EACCESS, "Access violation" },
  146. { ENOSPACE, "Disk full or allocation exceeded" },
  147. { EBADOP, "Illegal TFTP operation" },
  148. { EBADID, "Unknown transfer ID" },
  149. { EEXISTS, "File already exists" },
  150. { ENOUSER, "No such user" },
  151. { -1, 0 }
  152. };
  153. static struct formats formata[] = {
  154. { "netascii", 1 },
  155. { "octet", 0 },
  156. { NULL, 0 }
  157. };
  158. static struct bf bfs[2];
  159. static int nextone; /* index of next buffer to use */
  160. static int current; /* index of buffer in use */
  161. /* control flags for crlf conversions */
  162. static int newline = 0; /* fillbuf: in middle of newline expansion */
  163. static int prevchar = -1; /* putbuf: previous char (cr check) */
  164. static tftphdr_storage_t buf;
  165. static tftphdr_storage_t ackbuf;
  166. static srvr_sockaddr_union_t from;
  167. static curl_socklen_t fromlen;
  168. static curl_socket_t peer = CURL_SOCKET_BAD;
  169. static int timeout;
  170. static int maxtimeout = 5 * TIMEOUT;
  171. #ifdef ENABLE_IPV6
  172. static bool use_ipv6 = FALSE;
  173. #endif
  174. static const char *ipv_inuse = "IPv4";
  175. const char *serverlogfile = DEFAULT_LOGFILE;
  176. static char *pidname= (char *)".tftpd.pid";
  177. static int serverlogslocked = 0;
  178. static int wrotepidfile = 0;
  179. #ifdef HAVE_SIGSETJMP
  180. static sigjmp_buf timeoutbuf;
  181. #endif
  182. #if defined(HAVE_ALARM) && defined(SIGALRM)
  183. static int rexmtval = TIMEOUT;
  184. #endif
  185. /* do-nothing macro replacement for systems which lack siginterrupt() */
  186. #ifndef HAVE_SIGINTERRUPT
  187. #define siginterrupt(x,y) do {} while(0)
  188. #endif
  189. /* vars used to keep around previous signal handlers */
  190. typedef RETSIGTYPE (*SIGHANDLER_T)(int);
  191. #ifdef SIGHUP
  192. static SIGHANDLER_T old_sighup_handler = SIG_ERR;
  193. #endif
  194. #ifdef SIGPIPE
  195. static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
  196. #endif
  197. #ifdef SIGINT
  198. static SIGHANDLER_T old_sigint_handler = SIG_ERR;
  199. #endif
  200. #ifdef SIGTERM
  201. static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
  202. #endif
  203. #if defined(SIGBREAK) && defined(WIN32)
  204. static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
  205. #endif
  206. /* var which if set indicates that the program should finish execution */
  207. SIG_ATOMIC_T got_exit_signal = 0;
  208. /* if next is set indicates the first signal handled in exit_signal_handler */
  209. static volatile int exit_signal = 0;
  210. /*****************************************************************************
  211. * FUNCTION PROTOTYPES *
  212. *****************************************************************************/
  213. static struct tftphdr *rw_init(int);
  214. static struct tftphdr *w_init(void);
  215. static struct tftphdr *r_init(void);
  216. static void read_ahead(struct testcase *test, int convert);
  217. static ssize_t write_behind(struct testcase *test, int convert);
  218. static int synchnet(curl_socket_t);
  219. static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size);
  220. static int validate_access(struct testcase *test, const char *fname, int mode);
  221. static void sendtftp(struct testcase *test, struct formats *pf);
  222. static void recvtftp(struct testcase *test, struct formats *pf);
  223. static void nak(int error);
  224. #if defined(HAVE_ALARM) && defined(SIGALRM)
  225. static void mysignal(int sig, void (*handler)(int));
  226. static void timer(int signum);
  227. static void justtimeout(int signum);
  228. #endif /* HAVE_ALARM && SIGALRM */
  229. static RETSIGTYPE exit_signal_handler(int signum);
  230. static void install_signal_handlers(void);
  231. static void restore_signal_handlers(void);
  232. /*****************************************************************************
  233. * FUNCTION IMPLEMENTATIONS *
  234. *****************************************************************************/
  235. #if defined(HAVE_ALARM) && defined(SIGALRM)
  236. /*
  237. * Like signal(), but with well-defined semantics.
  238. */
  239. static void mysignal(int sig, void (*handler)(int))
  240. {
  241. struct sigaction sa;
  242. memset(&sa, 0, sizeof(sa));
  243. sa.sa_handler = handler;
  244. sigaction(sig, &sa, NULL);
  245. }
  246. static void timer(int signum)
  247. {
  248. (void)signum;
  249. logmsg("alarm!");
  250. timeout += rexmtval;
  251. if(timeout >= maxtimeout) {
  252. if(wrotepidfile) {
  253. wrotepidfile = 0;
  254. unlink(pidname);
  255. }
  256. if(serverlogslocked) {
  257. serverlogslocked = 0;
  258. clear_advisor_read_lock(SERVERLOGS_LOCK);
  259. }
  260. exit(1);
  261. }
  262. #ifdef HAVE_SIGSETJMP
  263. siglongjmp(timeoutbuf, 1);
  264. #endif
  265. }
  266. static void justtimeout(int signum)
  267. {
  268. (void)signum;
  269. }
  270. #endif /* HAVE_ALARM && SIGALRM */
  271. /* signal handler that will be triggered to indicate that the program
  272. should finish its execution in a controlled manner as soon as possible.
  273. The first time this is called it will set got_exit_signal to one and
  274. store in exit_signal the signal that triggered its execution. */
  275. static RETSIGTYPE exit_signal_handler(int signum)
  276. {
  277. int old_errno = errno;
  278. if(got_exit_signal == 0) {
  279. got_exit_signal = 1;
  280. exit_signal = signum;
  281. }
  282. (void)signal(signum, exit_signal_handler);
  283. errno = old_errno;
  284. }
  285. static void install_signal_handlers(void)
  286. {
  287. #ifdef SIGHUP
  288. /* ignore SIGHUP signal */
  289. if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
  290. logmsg("cannot install SIGHUP handler: %s", strerror(errno));
  291. #endif
  292. #ifdef SIGPIPE
  293. /* ignore SIGPIPE signal */
  294. if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
  295. logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
  296. #endif
  297. #ifdef SIGINT
  298. /* handle SIGINT signal with our exit_signal_handler */
  299. if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
  300. logmsg("cannot install SIGINT handler: %s", strerror(errno));
  301. else
  302. siginterrupt(SIGINT, 1);
  303. #endif
  304. #ifdef SIGTERM
  305. /* handle SIGTERM signal with our exit_signal_handler */
  306. if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
  307. logmsg("cannot install SIGTERM handler: %s", strerror(errno));
  308. else
  309. siginterrupt(SIGTERM, 1);
  310. #endif
  311. #if defined(SIGBREAK) && defined(WIN32)
  312. /* handle SIGBREAK signal with our exit_signal_handler */
  313. if((old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler)) == SIG_ERR)
  314. logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
  315. else
  316. siginterrupt(SIGBREAK, 1);
  317. #endif
  318. }
  319. static void restore_signal_handlers(void)
  320. {
  321. #ifdef SIGHUP
  322. if(SIG_ERR != old_sighup_handler)
  323. (void)signal(SIGHUP, old_sighup_handler);
  324. #endif
  325. #ifdef SIGPIPE
  326. if(SIG_ERR != old_sigpipe_handler)
  327. (void)signal(SIGPIPE, old_sigpipe_handler);
  328. #endif
  329. #ifdef SIGINT
  330. if(SIG_ERR != old_sigint_handler)
  331. (void)signal(SIGINT, old_sigint_handler);
  332. #endif
  333. #ifdef SIGTERM
  334. if(SIG_ERR != old_sigterm_handler)
  335. (void)signal(SIGTERM, old_sigterm_handler);
  336. #endif
  337. #if defined(SIGBREAK) && defined(WIN32)
  338. if(SIG_ERR != old_sigbreak_handler)
  339. (void)signal(SIGBREAK, old_sigbreak_handler);
  340. #endif
  341. }
  342. /*
  343. * init for either read-ahead or write-behind.
  344. * zero for write-behind, one for read-head.
  345. */
  346. static struct tftphdr *rw_init(int x)
  347. {
  348. newline = 0; /* init crlf flag */
  349. prevchar = -1;
  350. bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
  351. current = 0;
  352. bfs[1].counter = BF_FREE;
  353. nextone = x; /* ahead or behind? */
  354. return &bfs[0].buf.hdr;
  355. }
  356. static struct tftphdr *w_init(void)
  357. {
  358. return rw_init(0); /* write-behind */
  359. }
  360. static struct tftphdr *r_init(void)
  361. {
  362. return rw_init(1); /* read-ahead */
  363. }
  364. /* Have emptied current buffer by sending to net and getting ack.
  365. Free it and return next buffer filled with data.
  366. */
  367. static int readit(struct testcase *test, struct tftphdr **dpp,
  368. int convert /* if true, convert to ascii */)
  369. {
  370. struct bf *b;
  371. bfs[current].counter = BF_FREE; /* free old one */
  372. current = !current; /* "incr" current */
  373. b = &bfs[current]; /* look at new buffer */
  374. if (b->counter == BF_FREE) /* if it's empty */
  375. read_ahead(test, convert); /* fill it */
  376. *dpp = &b->buf.hdr; /* set caller's ptr */
  377. return b->counter;
  378. }
  379. /*
  380. * fill the input buffer, doing ascii conversions if requested
  381. * conversions are lf -> cr,lf and cr -> cr, nul
  382. */
  383. static void read_ahead(struct testcase *test,
  384. int convert /* if true, convert to ascii */)
  385. {
  386. int i;
  387. char *p;
  388. int c;
  389. struct bf *b;
  390. struct tftphdr *dp;
  391. b = &bfs[nextone]; /* look at "next" buffer */
  392. if (b->counter != BF_FREE) /* nop if not free */
  393. return;
  394. nextone = !nextone; /* "incr" next buffer ptr */
  395. dp = &b->buf.hdr;
  396. if (convert == 0) {
  397. /* The former file reading code did this:
  398. b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
  399. size_t copy_n = MIN(SEGSIZE, test->rcount);
  400. memcpy(dp->th_data, test->rptr, copy_n);
  401. /* decrease amount, advance pointer */
  402. test->rcount -= copy_n;
  403. test->rptr += copy_n;
  404. b->counter = (int)copy_n;
  405. return;
  406. }
  407. p = dp->th_data;
  408. for (i = 0 ; i < SEGSIZE; i++) {
  409. if (newline) {
  410. if (prevchar == '\n')
  411. c = '\n'; /* lf to cr,lf */
  412. else
  413. c = '\0'; /* cr to cr,nul */
  414. newline = 0;
  415. }
  416. else {
  417. if(test->rcount) {
  418. c=test->rptr[0];
  419. test->rptr++;
  420. test->rcount--;
  421. }
  422. else
  423. break;
  424. if (c == '\n' || c == '\r') {
  425. prevchar = c;
  426. c = '\r';
  427. newline = 1;
  428. }
  429. }
  430. *p++ = (char)c;
  431. }
  432. b->counter = (int)(p - dp->th_data);
  433. }
  434. /* Update count associated with the buffer, get new buffer from the queue.
  435. Calls write_behind only if next buffer not available.
  436. */
  437. static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
  438. int ct, int convert)
  439. {
  440. bfs[current].counter = ct; /* set size of data to write */
  441. current = !current; /* switch to other buffer */
  442. if (bfs[current].counter != BF_FREE) /* if not free */
  443. write_behind(test, convert); /* flush it */
  444. bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
  445. *dpp = &bfs[current].buf.hdr;
  446. return ct; /* this is a lie of course */
  447. }
  448. /*
  449. * Output a buffer to a file, converting from netascii if requested.
  450. * CR,NUL -> CR and CR,LF => LF.
  451. * Note spec is undefined if we get CR as last byte of file or a
  452. * CR followed by anything else. In this case we leave it alone.
  453. */
  454. static ssize_t write_behind(struct testcase *test, int convert)
  455. {
  456. char *writebuf;
  457. int count;
  458. int ct;
  459. char *p;
  460. int c; /* current character */
  461. struct bf *b;
  462. struct tftphdr *dp;
  463. b = &bfs[nextone];
  464. if (b->counter < -1) /* anything to flush? */
  465. return 0; /* just nop if nothing to do */
  466. if(!test->ofile) {
  467. char outfile[256];
  468. snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
  469. test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
  470. if(test->ofile == -1) {
  471. logmsg("Couldn't create and/or open file %s for upload!", outfile);
  472. return -1; /* failure! */
  473. }
  474. }
  475. count = b->counter; /* remember byte count */
  476. b->counter = BF_FREE; /* reset flag */
  477. dp = &b->buf.hdr;
  478. nextone = !nextone; /* incr for next time */
  479. writebuf = dp->th_data;
  480. if (count <= 0)
  481. return -1; /* nak logic? */
  482. if (convert == 0)
  483. return write(test->ofile, writebuf, count);
  484. p = writebuf;
  485. ct = count;
  486. while (ct--) { /* loop over the buffer */
  487. c = *p++; /* pick up a character */
  488. if (prevchar == '\r') { /* if prev char was cr */
  489. if (c == '\n') /* if have cr,lf then just */
  490. lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
  491. else
  492. if (c == '\0') /* if have cr,nul then */
  493. goto skipit; /* just skip over the putc */
  494. /* else just fall through and allow it */
  495. }
  496. /* formerly
  497. putc(c, file); */
  498. if(1 != write(test->ofile, &c, 1))
  499. break;
  500. skipit:
  501. prevchar = c;
  502. }
  503. return count;
  504. }
  505. /* When an error has occurred, it is possible that the two sides are out of
  506. * synch. Ie: that what I think is the other side's response to packet N is
  507. * really their response to packet N-1.
  508. *
  509. * So, to try to prevent that, we flush all the input queued up for us on the
  510. * network connection on our host.
  511. *
  512. * We return the number of packets we flushed (mostly for reporting when trace
  513. * is active).
  514. */
  515. static int synchnet(curl_socket_t f /* socket to flush */)
  516. {
  517. #if defined(HAVE_IOCTLSOCKET)
  518. unsigned long i;
  519. #else
  520. int i;
  521. #endif
  522. int j = 0;
  523. char rbuf[PKTSIZE];
  524. srvr_sockaddr_union_t fromaddr;
  525. curl_socklen_t fromaddrlen;
  526. for (;;) {
  527. #if defined(HAVE_IOCTLSOCKET)
  528. (void) ioctlsocket(f, FIONREAD, &i);
  529. #else
  530. (void) ioctl(f, FIONREAD, &i);
  531. #endif
  532. if (i) {
  533. j++;
  534. #ifdef ENABLE_IPV6
  535. if(!use_ipv6)
  536. #endif
  537. fromaddrlen = sizeof(fromaddr.sa4);
  538. #ifdef ENABLE_IPV6
  539. else
  540. fromaddrlen = sizeof(fromaddr.sa6);
  541. #endif
  542. (void) recvfrom(f, rbuf, sizeof(rbuf), 0,
  543. &fromaddr.sa, &fromaddrlen);
  544. }
  545. else
  546. break;
  547. }
  548. return j;
  549. }
  550. int main(int argc, char **argv)
  551. {
  552. srvr_sockaddr_union_t me;
  553. struct tftphdr *tp;
  554. ssize_t n = 0;
  555. int arg = 1;
  556. unsigned short port = DEFAULT_PORT;
  557. curl_socket_t sock = CURL_SOCKET_BAD;
  558. int flag;
  559. int rc;
  560. int error;
  561. long pid;
  562. struct testcase test;
  563. int result = 0;
  564. memset(&test, 0, sizeof(test));
  565. while(argc>arg) {
  566. if(!strcmp("--version", argv[arg])) {
  567. printf("tftpd IPv4%s\n",
  568. #ifdef ENABLE_IPV6
  569. "/IPv6"
  570. #else
  571. ""
  572. #endif
  573. );
  574. return 0;
  575. }
  576. else if(!strcmp("--pidfile", argv[arg])) {
  577. arg++;
  578. if(argc>arg)
  579. pidname = argv[arg++];
  580. }
  581. else if(!strcmp("--logfile", argv[arg])) {
  582. arg++;
  583. if(argc>arg)
  584. serverlogfile = argv[arg++];
  585. }
  586. else if(!strcmp("--ipv4", argv[arg])) {
  587. #ifdef ENABLE_IPV6
  588. ipv_inuse = "IPv4";
  589. use_ipv6 = FALSE;
  590. #endif
  591. arg++;
  592. }
  593. else if(!strcmp("--ipv6", argv[arg])) {
  594. #ifdef ENABLE_IPV6
  595. ipv_inuse = "IPv6";
  596. use_ipv6 = TRUE;
  597. #endif
  598. arg++;
  599. }
  600. else if(!strcmp("--port", argv[arg])) {
  601. arg++;
  602. if(argc>arg) {
  603. char *endptr;
  604. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  605. if((endptr != argv[arg] + strlen(argv[arg])) ||
  606. (ulnum < 1025UL) || (ulnum > 65535UL)) {
  607. fprintf(stderr, "tftpd: invalid --port argument (%s)\n",
  608. argv[arg]);
  609. return 0;
  610. }
  611. port = curlx_ultous(ulnum);
  612. arg++;
  613. }
  614. }
  615. else if(!strcmp("--srcdir", argv[arg])) {
  616. arg++;
  617. if(argc>arg) {
  618. path = argv[arg];
  619. arg++;
  620. }
  621. }
  622. else {
  623. puts("Usage: tftpd [option]\n"
  624. " --version\n"
  625. " --logfile [file]\n"
  626. " --pidfile [file]\n"
  627. " --ipv4\n"
  628. " --ipv6\n"
  629. " --port [port]\n"
  630. " --srcdir [path]");
  631. return 0;
  632. }
  633. }
  634. #ifdef WIN32
  635. win32_init();
  636. atexit(win32_cleanup);
  637. #endif
  638. install_signal_handlers();
  639. pid = (long)getpid();
  640. #ifdef ENABLE_IPV6
  641. if(!use_ipv6)
  642. #endif
  643. sock = socket(AF_INET, SOCK_DGRAM, 0);
  644. #ifdef ENABLE_IPV6
  645. else
  646. sock = socket(AF_INET6, SOCK_DGRAM, 0);
  647. #endif
  648. if(CURL_SOCKET_BAD == sock) {
  649. error = SOCKERRNO;
  650. logmsg("Error creating socket: (%d) %s",
  651. error, strerror(error));
  652. result = 1;
  653. goto tftpd_cleanup;
  654. }
  655. flag = 1;
  656. if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  657. (void *)&flag, sizeof(flag))) {
  658. error = SOCKERRNO;
  659. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  660. error, strerror(error));
  661. result = 1;
  662. goto tftpd_cleanup;
  663. }
  664. #ifdef ENABLE_IPV6
  665. if(!use_ipv6) {
  666. #endif
  667. memset(&me.sa4, 0, sizeof(me.sa4));
  668. me.sa4.sin_family = AF_INET;
  669. me.sa4.sin_addr.s_addr = INADDR_ANY;
  670. me.sa4.sin_port = htons(port);
  671. rc = bind(sock, &me.sa, sizeof(me.sa4));
  672. #ifdef ENABLE_IPV6
  673. }
  674. else {
  675. memset(&me.sa6, 0, sizeof(me.sa6));
  676. me.sa6.sin6_family = AF_INET6;
  677. me.sa6.sin6_addr = in6addr_any;
  678. me.sa6.sin6_port = htons(port);
  679. rc = bind(sock, &me.sa, sizeof(me.sa6));
  680. }
  681. #endif /* ENABLE_IPV6 */
  682. if(0 != rc) {
  683. error = SOCKERRNO;
  684. logmsg("Error binding socket on port %hu: (%d) %s",
  685. port, error, strerror(error));
  686. result = 1;
  687. goto tftpd_cleanup;
  688. }
  689. wrotepidfile = write_pidfile(pidname);
  690. if(!wrotepidfile) {
  691. result = 1;
  692. goto tftpd_cleanup;
  693. }
  694. logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
  695. for (;;) {
  696. fromlen = sizeof(from);
  697. #ifdef ENABLE_IPV6
  698. if(!use_ipv6)
  699. #endif
  700. fromlen = sizeof(from.sa4);
  701. #ifdef ENABLE_IPV6
  702. else
  703. fromlen = sizeof(from.sa6);
  704. #endif
  705. n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0,
  706. &from.sa, &fromlen);
  707. if(got_exit_signal)
  708. break;
  709. if (n < 0) {
  710. logmsg("recvfrom");
  711. result = 3;
  712. break;
  713. }
  714. set_advisor_read_lock(SERVERLOGS_LOCK);
  715. serverlogslocked = 1;
  716. #ifdef ENABLE_IPV6
  717. if(!use_ipv6) {
  718. #endif
  719. from.sa4.sin_family = AF_INET;
  720. peer = socket(AF_INET, SOCK_DGRAM, 0);
  721. if(CURL_SOCKET_BAD == peer) {
  722. logmsg("socket");
  723. result = 2;
  724. break;
  725. }
  726. if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) {
  727. logmsg("connect: fail");
  728. result = 1;
  729. break;
  730. }
  731. #ifdef ENABLE_IPV6
  732. }
  733. else {
  734. from.sa6.sin6_family = AF_INET6;
  735. peer = socket(AF_INET6, SOCK_DGRAM, 0);
  736. if(CURL_SOCKET_BAD == peer) {
  737. logmsg("socket");
  738. result = 2;
  739. break;
  740. }
  741. if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
  742. logmsg("connect: fail");
  743. result = 1;
  744. break;
  745. }
  746. }
  747. #endif
  748. maxtimeout = 5*TIMEOUT;
  749. tp = &buf.hdr;
  750. tp->th_opcode = ntohs(tp->th_opcode);
  751. if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
  752. memset(&test, 0, sizeof(test));
  753. if (do_tftp(&test, tp, n) < 0)
  754. break;
  755. if(test.buffer)
  756. free(test.buffer);
  757. }
  758. sclose(peer);
  759. peer = CURL_SOCKET_BAD;
  760. if(test.ofile > 0) {
  761. close(test.ofile);
  762. test.ofile = 0;
  763. }
  764. if(got_exit_signal)
  765. break;
  766. if(serverlogslocked) {
  767. serverlogslocked = 0;
  768. clear_advisor_read_lock(SERVERLOGS_LOCK);
  769. }
  770. logmsg("end of one transfer");
  771. }
  772. tftpd_cleanup:
  773. if(test.ofile > 0)
  774. close(test.ofile);
  775. if((peer != sock) && (peer != CURL_SOCKET_BAD))
  776. sclose(peer);
  777. if(sock != CURL_SOCKET_BAD)
  778. sclose(sock);
  779. if(got_exit_signal)
  780. logmsg("signalled to die");
  781. if(wrotepidfile)
  782. unlink(pidname);
  783. if(serverlogslocked) {
  784. serverlogslocked = 0;
  785. clear_advisor_read_lock(SERVERLOGS_LOCK);
  786. }
  787. restore_signal_handlers();
  788. if(got_exit_signal) {
  789. logmsg("========> %s tftpd (port: %d pid: %ld) exits with signal (%d)",
  790. ipv_inuse, (int)port, pid, exit_signal);
  791. /*
  792. * To properly set the return status of the process we
  793. * must raise the same signal SIGINT or SIGTERM that we
  794. * caught and let the old handler take care of it.
  795. */
  796. raise(exit_signal);
  797. }
  798. logmsg("========> tftpd quits");
  799. return result;
  800. }
  801. /*
  802. * Handle initial connection protocol.
  803. */
  804. static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
  805. {
  806. char *cp;
  807. int first = 1, ecode;
  808. struct formats *pf;
  809. char *filename, *mode = NULL;
  810. int error;
  811. FILE *server;
  812. #ifdef USE_WINSOCK
  813. DWORD recvtimeout, recvtimeoutbak;
  814. #endif
  815. /* Open request dump file. */
  816. server = fopen(REQUEST_DUMP, "ab");
  817. if(!server) {
  818. error = errno;
  819. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  820. logmsg("Error opening file: %s", REQUEST_DUMP);
  821. return -1;
  822. }
  823. /* store input protocol */
  824. fprintf(server, "opcode: %x\n", tp->th_opcode);
  825. cp = (char *)&tp->th_stuff;
  826. filename = cp;
  827. again:
  828. while (cp < &buf.storage[size]) {
  829. if (*cp == '\0')
  830. break;
  831. cp++;
  832. }
  833. if (*cp) {
  834. nak(EBADOP);
  835. fclose(server);
  836. return 3;
  837. }
  838. if (first) {
  839. mode = ++cp;
  840. first = 0;
  841. goto again;
  842. }
  843. /* store input protocol */
  844. fprintf(server, "filename: %s\n", filename);
  845. for (cp = mode; cp && *cp; cp++)
  846. if(ISUPPER(*cp))
  847. *cp = (char)tolower((int)*cp);
  848. /* store input protocol */
  849. fprintf(server, "mode: %s\n", mode);
  850. fclose(server);
  851. for (pf = formata; pf->f_mode; pf++)
  852. if (strcmp(pf->f_mode, mode) == 0)
  853. break;
  854. if (!pf->f_mode) {
  855. nak(EBADOP);
  856. return 2;
  857. }
  858. ecode = validate_access(test, filename, tp->th_opcode);
  859. if (ecode) {
  860. nak(ecode);
  861. return 1;
  862. }
  863. #ifdef USE_WINSOCK
  864. recvtimeout = sizeof(recvtimeoutbak);
  865. getsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
  866. (char*)&recvtimeoutbak, (int*)&recvtimeout);
  867. recvtimeout = TIMEOUT*1000;
  868. setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
  869. (const char*)&recvtimeout, sizeof(recvtimeout));
  870. #endif
  871. if (tp->th_opcode == opcode_WRQ)
  872. recvtftp(test, pf);
  873. else
  874. sendtftp(test, pf);
  875. #ifdef USE_WINSOCK
  876. recvtimeout = recvtimeoutbak;
  877. setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
  878. (const char*)&recvtimeout, sizeof(recvtimeout));
  879. #endif
  880. return 0;
  881. }
  882. /* Based on the testno, parse the correct server commands. */
  883. static int parse_servercmd(struct testcase *req)
  884. {
  885. FILE *stream;
  886. char *filename;
  887. int error;
  888. filename = test2file(req->testno);
  889. stream=fopen(filename, "rb");
  890. if(!stream) {
  891. error = errno;
  892. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  893. logmsg(" [1] Error opening file: %s", filename);
  894. logmsg(" Couldn't open test file %ld", req->testno);
  895. return 1; /* done */
  896. }
  897. else {
  898. char *orgcmd = NULL;
  899. char *cmd = NULL;
  900. size_t cmdsize = 0;
  901. int num=0;
  902. /* get the custom server control "commands" */
  903. error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
  904. fclose(stream);
  905. if(error) {
  906. logmsg("getpart() failed with error: %d", error);
  907. return 1; /* done */
  908. }
  909. cmd = orgcmd;
  910. while(cmd && cmdsize) {
  911. char *check;
  912. if(1 == sscanf(cmd, "writedelay: %d", &num)) {
  913. logmsg("instructed to delay %d secs between packets", num);
  914. req->writedelay = num;
  915. }
  916. else {
  917. logmsg("Unknown <servercmd> instruction found: %s", cmd);
  918. }
  919. /* try to deal with CRLF or just LF */
  920. check = strchr(cmd, '\r');
  921. if(!check)
  922. check = strchr(cmd, '\n');
  923. if(check) {
  924. /* get to the letter following the newline */
  925. while((*check == '\r') || (*check == '\n'))
  926. check++;
  927. if(!*check)
  928. /* if we reached a zero, get out */
  929. break;
  930. cmd = check;
  931. }
  932. else
  933. break;
  934. }
  935. if(orgcmd)
  936. free(orgcmd);
  937. }
  938. return 0; /* OK! */
  939. }
  940. /*
  941. * Validate file access.
  942. */
  943. static int validate_access(struct testcase *test,
  944. const char *filename, int mode)
  945. {
  946. char *ptr;
  947. long testno, partno;
  948. int error;
  949. char partbuf[80]="data";
  950. logmsg("trying to get file: %s mode %x", filename, mode);
  951. if(!strncmp("verifiedserver", filename, 14)) {
  952. char weare[128];
  953. size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid());
  954. logmsg("Are-we-friendly question received");
  955. test->buffer = strdup(weare);
  956. test->rptr = test->buffer; /* set read pointer */
  957. test->bufsize = count; /* set total count */
  958. test->rcount = count; /* set data left to read */
  959. return 0; /* fine */
  960. }
  961. /* find the last slash */
  962. ptr = strrchr(filename, '/');
  963. if(ptr) {
  964. char *file;
  965. ptr++; /* skip the slash */
  966. /* skip all non-numericals following the slash */
  967. while(*ptr && !ISDIGIT(*ptr))
  968. ptr++;
  969. /* get the number */
  970. testno = strtol(ptr, &ptr, 10);
  971. if(testno > 10000) {
  972. partno = testno % 10000;
  973. testno /= 10000;
  974. }
  975. else
  976. partno = 0;
  977. logmsg("requested test number %ld part %ld", testno, partno);
  978. test->testno = testno;
  979. (void)parse_servercmd(test);
  980. file = test2file(testno);
  981. if(0 != partno)
  982. sprintf(partbuf, "data%ld", partno);
  983. if(file) {
  984. FILE *stream=fopen(file, "rb");
  985. if(!stream) {
  986. error = errno;
  987. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  988. logmsg("Error opening file: %s", file);
  989. logmsg("Couldn't open test file: %s", file);
  990. return EACCESS;
  991. }
  992. else {
  993. size_t count;
  994. error = getpart(&test->buffer, &count, "reply", partbuf, stream);
  995. fclose(stream);
  996. if(error) {
  997. logmsg("getpart() failed with error: %d", error);
  998. return EACCESS;
  999. }
  1000. if(test->buffer) {
  1001. test->rptr = test->buffer; /* set read pointer */
  1002. test->bufsize = count; /* set total count */
  1003. test->rcount = count; /* set data left to read */
  1004. }
  1005. else
  1006. return EACCESS;
  1007. }
  1008. }
  1009. else
  1010. return EACCESS;
  1011. }
  1012. else {
  1013. logmsg("no slash found in path");
  1014. return EACCESS; /* failure */
  1015. }
  1016. logmsg("file opened and all is good");
  1017. return 0;
  1018. }
  1019. /*
  1020. * Send the requested file.
  1021. */
  1022. static void sendtftp(struct testcase *test, struct formats *pf)
  1023. {
  1024. int size;
  1025. ssize_t n;
  1026. /* This is volatile to live through a siglongjmp */
  1027. volatile unsigned short sendblock; /* block count */
  1028. struct tftphdr *sdp; /* data buffer */
  1029. struct tftphdr *sap; /* ack buffer */
  1030. sendblock = 1;
  1031. #if defined(HAVE_ALARM) && defined(SIGALRM)
  1032. mysignal(SIGALRM, timer);
  1033. #endif
  1034. sdp = r_init();
  1035. sap = &ackbuf.hdr;
  1036. do {
  1037. size = readit(test, &sdp, pf->f_convert);
  1038. if (size < 0) {
  1039. nak(errno + 100);
  1040. return;
  1041. }
  1042. sdp->th_opcode = htons((unsigned short)opcode_DATA);
  1043. sdp->th_block = htons(sendblock);
  1044. timeout = 0;
  1045. #ifdef HAVE_SIGSETJMP
  1046. (void) sigsetjmp(timeoutbuf, 1);
  1047. #endif
  1048. if(test->writedelay) {
  1049. logmsg("Pausing %d seconds before %d bytes", test->writedelay,
  1050. size);
  1051. wait_ms(1000*test->writedelay);
  1052. }
  1053. send_data:
  1054. if (swrite(peer, sdp, size + 4) != size + 4) {
  1055. logmsg("write");
  1056. return;
  1057. }
  1058. read_ahead(test, pf->f_convert);
  1059. for ( ; ; ) {
  1060. #ifdef HAVE_ALARM
  1061. alarm(rexmtval); /* read the ack */
  1062. #endif
  1063. n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
  1064. #ifdef HAVE_ALARM
  1065. alarm(0);
  1066. #endif
  1067. if(got_exit_signal)
  1068. return;
  1069. if (n < 0) {
  1070. logmsg("read: fail");
  1071. return;
  1072. }
  1073. sap->th_opcode = ntohs((unsigned short)sap->th_opcode);
  1074. sap->th_block = ntohs(sap->th_block);
  1075. if (sap->th_opcode == opcode_ERROR) {
  1076. logmsg("got ERROR");
  1077. return;
  1078. }
  1079. if (sap->th_opcode == opcode_ACK) {
  1080. if (sap->th_block == sendblock) {
  1081. break;
  1082. }
  1083. /* Re-synchronize with the other side */
  1084. (void) synchnet(peer);
  1085. if (sap->th_block == (sendblock-1)) {
  1086. goto send_data;
  1087. }
  1088. }
  1089. }
  1090. sendblock++;
  1091. } while (size == SEGSIZE);
  1092. }
  1093. /*
  1094. * Receive a file.
  1095. */
  1096. static void recvtftp(struct testcase *test, struct formats *pf)
  1097. {
  1098. ssize_t n, size;
  1099. /* These are volatile to live through a siglongjmp */
  1100. volatile unsigned short recvblock; /* block count */
  1101. struct tftphdr * volatile rdp; /* data buffer */
  1102. struct tftphdr *rap; /* ack buffer */
  1103. recvblock = 0;
  1104. rdp = w_init();
  1105. #if defined(HAVE_ALARM) && defined(SIGALRM)
  1106. mysignal(SIGALRM, timer);
  1107. #endif
  1108. rap = &ackbuf.hdr;
  1109. do {
  1110. timeout = 0;
  1111. rap->th_opcode = htons((unsigned short)opcode_ACK);
  1112. rap->th_block = htons(recvblock);
  1113. recvblock++;
  1114. #ifdef HAVE_SIGSETJMP
  1115. (void) sigsetjmp(timeoutbuf, 1);
  1116. #endif
  1117. send_ack:
  1118. if (swrite(peer, &ackbuf.storage[0], 4) != 4) {
  1119. logmsg("write: fail\n");
  1120. goto abort;
  1121. }
  1122. write_behind(test, pf->f_convert);
  1123. for ( ; ; ) {
  1124. #ifdef HAVE_ALARM
  1125. alarm(rexmtval);
  1126. #endif
  1127. n = sread(peer, rdp, PKTSIZE);
  1128. #ifdef HAVE_ALARM
  1129. alarm(0);
  1130. #endif
  1131. if(got_exit_signal)
  1132. goto abort;
  1133. if (n < 0) { /* really? */
  1134. logmsg("read: fail\n");
  1135. goto abort;
  1136. }
  1137. rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode);
  1138. rdp->th_block = ntohs(rdp->th_block);
  1139. if (rdp->th_opcode == opcode_ERROR)
  1140. goto abort;
  1141. if (rdp->th_opcode == opcode_DATA) {
  1142. if (rdp->th_block == recvblock) {
  1143. break; /* normal */
  1144. }
  1145. /* Re-synchronize with the other side */
  1146. (void) synchnet(peer);
  1147. if (rdp->th_block == (recvblock-1))
  1148. goto send_ack; /* rexmit */
  1149. }
  1150. }
  1151. size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
  1152. if (size != (n-4)) { /* ahem */
  1153. if (size < 0)
  1154. nak(errno + 100);
  1155. else
  1156. nak(ENOSPACE);
  1157. goto abort;
  1158. }
  1159. } while (size == SEGSIZE);
  1160. write_behind(test, pf->f_convert);
  1161. rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final" ack */
  1162. rap->th_block = htons(recvblock);
  1163. (void) swrite(peer, &ackbuf.storage[0], 4);
  1164. #if defined(HAVE_ALARM) && defined(SIGALRM)
  1165. mysignal(SIGALRM, justtimeout); /* just abort read on timeout */
  1166. alarm(rexmtval);
  1167. #endif
  1168. /* normally times out and quits */
  1169. n = sread(peer, &buf.storage[0], sizeof(buf.storage));
  1170. #ifdef HAVE_ALARM
  1171. alarm(0);
  1172. #endif
  1173. if(got_exit_signal)
  1174. goto abort;
  1175. if (n >= 4 && /* if read some data */
  1176. rdp->th_opcode == opcode_DATA && /* and got a data block */
  1177. recvblock == rdp->th_block) { /* then my last ack was lost */
  1178. (void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
  1179. }
  1180. abort:
  1181. return;
  1182. }
  1183. /*
  1184. * Send a nak packet (error message). Error code passed in is one of the
  1185. * standard TFTP codes, or a UNIX errno offset by 100.
  1186. */
  1187. static void nak(int error)
  1188. {
  1189. struct tftphdr *tp;
  1190. int length;
  1191. struct errmsg *pe;
  1192. tp = &buf.hdr;
  1193. tp->th_opcode = htons((unsigned short)opcode_ERROR);
  1194. tp->th_code = htons((unsigned short)error);
  1195. for (pe = errmsgs; pe->e_code >= 0; pe++)
  1196. if (pe->e_code == error)
  1197. break;
  1198. if (pe->e_code < 0) {
  1199. pe->e_msg = strerror(error - 100);
  1200. tp->th_code = EUNDEF; /* set 'undef' errorcode */
  1201. }
  1202. length = (int)strlen(pe->e_msg);
  1203. /* we use memcpy() instead of strcpy() in order to avoid buffer overflow
  1204. * report from glibc with FORTIFY_SOURCE */
  1205. memcpy(tp->th_msg, pe->e_msg, length + 1);
  1206. length += 5;
  1207. if (swrite(peer, &buf.storage[0], length) != length)
  1208. logmsg("nak: fail\n");
  1209. }