2
0

tftpd.c 35 KB

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