1 /* respond.c - twoftpd routines for responding to clients
2  * Copyright (C) 2008  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <errno.h>
19 #include <string.h>
20 #include <bglibs/iobuf.h>
21 #include <bglibs/systime.h>
22 #include "twoftpd.h"
23 #include "log.h"
24 
25 extern int errno;
26 int log_responses = 0;
27 
respond_start(unsigned code,int final)28 int respond_start(unsigned code, int final)
29 {
30   const char* sep = final ? " " : "-";
31   if (log_responses) {
32     log_start();
33     log_uint(code);
34     log_str(sep);
35   }
36   return obuf_putu(&outbuf, code) && obuf_puts(&outbuf, sep);
37 }
38 
respond_end(void)39 int respond_end(void)
40 {
41   if (log_responses) log_end();
42   return obuf_putsflush(&outbuf, "\r\n");
43 }
44 
respond_str(const char * str)45 int respond_str(const char* str)
46 {
47   if (log_responses) log_str(str);
48   /* Translate LF to NUL and \377 to \377\377 on output */
49   for (; *str; ++str)
50     switch (*str) {
51     case LF:
52       if (!obuf_putc(&outbuf, 0)) return 0;
53       break;
54     case '\377':
55       if (!obuf_putc(&outbuf, '\377')) return 0;
56       /* Fall through and output the \377 again */
57     default:
58       if (!obuf_putc(&outbuf, *str)) return 0;
59     }
60   return 1;
61 }
62 
respond_uint(unsigned long num)63 int respond_uint(unsigned long num)
64 {
65   if (log_responses) log_uint(num);
66   return obuf_putu(&outbuf, num);
67 }
68 
respond_syserr(unsigned code,const char * msg)69 int respond_syserr(unsigned code, const char *msg)
70 {
71   return respond_start(code, 1) &&
72     respond_str(msg) &&
73     respond_str(": ") &&
74     respond_str(strerror(errno)) &&
75     respond_end();
76 }
77 
respond(unsigned code,int final,const char * msg)78 int respond(unsigned code, int final, const char* msg)
79 {
80   return respond_start(code, final) &&
81     respond_str(msg) &&
82     respond_end();
83 }
84 
85 static struct timeval start;
86 
respond_start_xfer(void)87 void respond_start_xfer(void)
88 {
89   gettimeofday(&start, 0);
90 }
91 
92 static const char* scales[] = { "", "k", "M", "G", "T", 0 };
93 
respond_bytes(unsigned code,const char * msg,unsigned long bytes,int sent)94 static int respond_bytes(unsigned code,
95 			 const char* msg, unsigned long bytes, int sent)
96 {
97   struct timeval end;
98   unsigned long rate;
99   int scaleno;
100   gettimeofday(&end, 0);
101   rate = bytes / (end.tv_sec-start.tv_sec +
102 		  (end.tv_usec-start.tv_usec)/1000000.0);
103   for (scaleno = 0;
104        rate > 10000 && scales[scaleno+1] != 0;
105        ++scaleno, rate /= 1024)
106     ;
107   return respond_start(code, 1) &&
108     respond_str(msg) &&
109     respond_str(" (") &&
110     respond_uint(bytes) &&
111     respond_str(sent ? " bytes sent, " : " bytes received, ") &&
112     respond_uint(rate) &&
113     respond_str(scales[scaleno]) &&
114     respond_str("B/s).") &&
115     respond_end();
116 }
117 
respond_xferresult(unsigned result,unsigned long bytes,int sent)118 int respond_xferresult(unsigned result, unsigned long bytes, int sent)
119 {
120   switch (result) {
121   case 0:
122     return respond_bytes(226, "Transfer completed", bytes, sent);
123   case 1:
124     return respond_bytes(426, "Transfer timed out", bytes, sent);
125   case 2:
126     return respond_bytes(426, "Transfer interrupted", bytes, sent);
127   default:
128     return respond_bytes(451, "Transfer failed", bytes, sent);
129   }
130 }
131