--- tftpd/src/srv.c 2014/02/14 15:38:37 1.1 +++ tftpd/src/srv.c 2014/02/20 01:31:50 1.2.2.3 @@ -1,4 +1,6 @@ #include "global.h" +#include "exec.h" +#include "srv.h" static void * @@ -30,16 +32,18 @@ txPkt(sched_task_t *task) else if (wlen != TASK_DATLEN(task)) { EERROR(EIO, "Sended %d bytes != packet %d bytes", wlen, TASK_DATLEN(task)); + schedEvent(TASK_ROOT(task), execProg, "error", 0, NULL, TFTP_OPC_ERROR); schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_CALL, timeoutSession, NULL); - memset(&cli, 0, sizeof(sockaddr_t)); + schedEvent(TASK_ROOT(task), timeoutSession, NULL, 0, NULL, 0); } else EVERBOSE(2, "Sended %d bytes", wlen); /* on error or argument, drop session */ if (TASK_ARG(task) == (void*) -1 || ntohs(tftp->tftp_opc) == TFTP_OPC_ERROR) { + schedEvent(TASK_ROOT(task), execProg, "error", 0, NULL, TFTP_OPC_ERROR); schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_CALL, timeoutSession, NULL); - memset(&cli, 0, sizeof cli); + schedEvent(TASK_ROOT(task), timeoutSession, NULL, 0, NULL, 0); } taskExit(task, NULL); @@ -104,6 +108,7 @@ txAck(sched_task_t *task) TASK_DATA(task), RPACK_OFF(pkt)); if (cli.close) { + schedEvent(TASK_ROOT(task), execProg, "complete", 0, NULL, TFTP_OPC_WRQ); schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_CALL, timeoutSession, NULL); schedEvent(TASK_ROOT(task), timeoutSession, NULL, 0, NULL, 0); EVERBOSE(2, "Finish WRQ request"); @@ -111,7 +116,86 @@ txAck(sched_task_t *task) taskExit(task, NULL); } +static int +getOpts(rpack_t * __restrict pkt) +{ + char *opt, *val; + int len; + + do { + /* option */ + len = str_getString(RPACK_NEXT(pkt), RPACK_REMAIN(pkt), NULL); + if (len == -1) + return -1; + opt = (char*) rpack_rnext(pkt, len); + if (!opt) + return -1; + /* value */ + len = str_getString(RPACK_NEXT(pkt), RPACK_REMAIN(pkt), NULL); + if (len == -1) + return -1; + val = (char*) rpack_rnext(pkt, len); + if (!val) + return -1; + + if (!strcasecmp(opt, TFTP_OPT_BLKSIZE)) { + len = strtol(val, NULL, 10); + if (len > TFTP_LOAD_MAX) + cli.siz = len; + } else if (!strcasecmp(opt, TFTP_OPT_TSIZE)) + cli.tsiz = strtoll(val, NULL, 10); + else if (!strcasecmp(opt, TFTP_OPT_TIMEOUT)) + cli.tout = strtol(val, NULL, 10); + else + return -1; + } while (!RPACK_ISEND(pkt)); + + EVERBOSE(4, "blksize=%u tsize=%llu timeout=%u", cli.siz, cli.tsiz, cli.tout); + return 0; +} + static void * +txOack(sched_task_t *task) +{ + rpack_t *pkt = TASK_DATA(task); + u_short n = htons(TFTP_OPC_OACK); + struct stat sb; + char szStr[STRSIZ]; + + ETRACE(); + + RPACK_REWIND(pkt); + rpack_uint16(pkt, &n, 0); + + /* if opcode is RRQ and tsize is 0 then we must return file size to client */ + if (cli.opc == TFTP_OPC_RRQ && !cli.tsiz && stat(cli.file, &sb) != -1) + cli.tsiz = sb.st_size; + + if (cli.siz > TFTP_LOAD_MAX) { + memset(szStr, 0, sizeof szStr); + snprintf(szStr, sizeof szStr, "%u", cli.siz); + rpack_rdata(pkt, TFTP_OPT_BLKSIZE, strlen(TFTP_OPT_BLKSIZE) + 1); + rpack_rdata(pkt, szStr, strlen(szStr) + 1); + } + if (cli.tsiz) { + memset(szStr, 0, sizeof szStr); + snprintf(szStr, sizeof szStr, "%llu", cli.tsiz); + rpack_rdata(pkt, TFTP_OPT_TSIZE, strlen(TFTP_OPT_TSIZE) + 1); + rpack_rdata(pkt, szStr, strlen(szStr) + 1); + } + if (cli.tout) { + memset(szStr, 0, sizeof szStr); + snprintf(szStr, sizeof szStr, "%u", cli.tout); + rpack_rdata(pkt, TFTP_OPT_TIMEOUT, strlen(TFTP_OPT_TIMEOUT) + 1); + rpack_rdata(pkt, szStr, strlen(szStr) + 1); + } + + schedEvent(TASK_ROOT(task), txPkt, NULL, TASK_FD(task), + TASK_DATA(task), RPACK_OFF(pkt)); + taskExit(task, NULL); +} + +static void * RQ(sched_task_t *task) { rpack_t *pkt = TASK_DATA(task); @@ -163,6 +247,11 @@ RQ(sched_task_t *task) EVERBOSE(2, "WRQ:: file=%s mode=%s\n", cli.file, cli.mode); break; } + + if (!RPACK_ISEND(pkt) && !getOpts(pkt)) + schedEvent(TASK_ROOT(task), txOack, NULL, TASK_FD(task), + TASK_DATA(task), 0); + cli.fd = open(cli.file, code, 0644); if (cli.fd == -1) { if (errno == EACCES) @@ -178,6 +267,8 @@ RQ(sched_task_t *task) } else cli.seq = 0; + schedEvent(TASK_ROOT(task), execProg, "request", 0, NULL, cli.opc); + if (cli.opc == TFTP_OPC_WRQ) { /* ack */ tftp->tftp_opc = htons(TFTP_OPC_ACK); @@ -234,6 +325,7 @@ ACK(sched_task_t *task) schedEvent(TASK_ROOT(task), txData, NULL, TASK_FD(task), TASK_DATA(task), 0); else { + schedEvent(TASK_ROOT(task), execProg, "complete", 0, NULL, TFTP_OPC_RRQ); schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_CALL, timeoutSession, NULL); schedEvent(TASK_ROOT(task), timeoutSession, NULL, 0, NULL, 0); EVERBOSE(2, "Finish RRQ request");