tftpd.c 35 KB

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