andrei@versabanq.com: wvdbusd: name connections :%s.0 instead of :%s.
[wvapps.git] / wvtftp / wvtftpbase.h
blob6bfebb2b4c2233d075436e4d0a3dc3393957cdaf
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
5 * WvTFTPBase, the WvTFTP base class. This class holds functions that are
6 * common to both server and client (if one is ever written).
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __WVTFTPBASE_H
24 #define __WVTFTPBASE_H
26 #include "wvstring.h"
27 #include "wvhashtable.h"
28 #include "wvudp.h"
29 #include "wvlog.h"
30 #include "wvtimestream.h"
31 #include "wvstringlist.h"
32 #include "uniconf.h"
33 #include <stdio.h>
34 #include <time.h>
35 #include <sys/time.h>
37 const int MAX_PACKET_SIZE = 65535;
38 const bool WVTFTP_DEBUG = false;
40 class PktTime
42 public:
43 PktTime(int _pktclump);
44 ~PktTime();
46 void set(int pktnum, struct timeval &tv);
47 struct timeval *get(int pktnum);
49 private:
50 int idx;
51 int pktclump;
52 struct timeval *times;
55 class WvTFTPBase : public WvUDPStream
57 public:
58 enum TFTPDir {tftpread = 0, tftpwrite = 1};
59 enum TFTPOpcode {RRQ = 1, WRQ = 2, DATA = 3, ACK = 4, ERROR = 5};
60 enum TFTPMode {netascii = 0, octet, mail};
62 // _tftp_tick is in ms.
63 WvTFTPBase(int _tftp_tick, int port = 0);
64 virtual ~WvTFTPBase();
66 struct TFTPConn
68 WvIPPortAddr remote; // remote's address and port
69 WvString filename; // filename of this connection
70 TFTPDir direction; // reading or writing?
71 TFTPMode mode; // mode (netascii or octet)
72 FILE *tftpfile; // the file being transferred
73 size_t blksize; // blocksize (RFC 2348)
74 int tsize; // transfer size (RFC 2349)
75 int pktclump; // number of packets to send at once
76 int unack; // first unacked packet for writing data
77 int lastsent; // block number of last packet sent
78 bool donefile; // done reading from the file?
79 bool send_oack; // do we need to or did we send an OACK?
80 char oack[512]; // Holds the OACK packet in case we need
81 size_t oacklen; // to resend it.
82 int numtimeouts;
83 int rtt; // "Total" round-trip time accumulator.
84 int mult; // base of the multiplier for timeout
85 // backoffs. The actual multiplier
86 // is mult squared.
87 PktTime *pkttimes;
88 int total_packets; // Number of correct packets used to
89 // calculate average rtt.
91 int timed_out_ignore; // block number of largest packet that
92 // has been retransmitted due to
93 // timeout, and should thus be ignored
94 // in rtt calculations.
95 struct timeval last_received; // Time the last packet was received.
96 bool alias_once;
97 UniConf alias;
99 TFTPConn():
100 tftpfile(NULL),
101 pkttimes(NULL),
102 alias_once(false)
105 ~TFTPConn()
107 if (tftpfile)
108 fclose(tftpfile);
110 if (pkttimes)
111 delete pkttimes;
115 DeclareWvDict(TFTPConn, WvIPPortAddr, remote);
117 protected:
118 TFTPConnDict conns;
119 WvLog log;
120 int tftp_tick;
121 char packet[MAX_PACKET_SIZE];
122 size_t packetsize;
123 int def_timeout;
125 virtual void new_connection() = 0;
126 virtual void handle_packet();
127 void send_data(TFTPConn *c, bool resend = false);
128 void send_ack(TFTPConn *c, bool resend = false);
129 void send_err(char errcode, WvString errmsg = "");
131 void dump_pkt();
134 #endif // __WVTFTP_H