1 /*
2  *  UFTP - UDP based FTP with multicast
3  *
4  *  Copyright (C) 2001-2020   Dennis A. Bush, Jr.   bush@tcnj.edu
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  Additional permission under GNU GPL version 3 section 7
20  *
21  *  If you modify this program, or any covered work, by linking or
22  *  combining it with the OpenSSL project's OpenSSL library (or a
23  *  modified version of that library), containing parts covered by the
24  *  terms of the OpenSSL or SSLeay licenses, the copyright holder
25  *  grants you additional permission to convey the resulting work.
26  *  Corresponding Source for a non-source form of such a combination
27  *  shall include the source code for the parts of OpenSSL used as well
28  *  as that of the covered work.
29  */
30 
31 #ifndef _CLIENT_H
32 #define _CLIENT_H
33 
34 #include "uftp_common.h"
35 #include "encryption.h"
36 
37 #define MAXLIST 100
38 #define MAXMISORDER 5
39 #define KEY_REQ_INT 5
40 
41 /**
42  * Current state of client for a given group
43  */
44 enum client_phase {
45     PHASE_PREREGISTER = 1,      ///
46     PHASE_REGISTERED = 2,       /// Registered and awaiting KEYINFO or REG_CONF
47     PHASE_RECEIVING = 3,        /// Currently receiving a file
48     PHASE_COMPLETE = 4,         /// Completed group and awaiting DONE_CONF
49     PHASE_MIDGROUP = 5          /// Registered awaiting next file or group end
50 };
51 
52 /**
53  * Info pertaining to current file
54  */
55 struct file_t {
56     uint32_t blocks;            /// Total blocks
57     uint16_t sections;          /// Total sections
58     uint16_t big_sections;      /// Number of larger sized sections
59     uint32_t secsize_small, secsize_big;  /// Size of sections
60     int ftype;                  /// File type (regular, directory, symlink)
61     f_offset_t size;            /// Size in bytes
62     int64_t tstamp;             /// File timestamp
63     char filepath[MAXPATHNAME]; /// Local path to file
64     char temppath[MAXPATHNAME]; /// Local path to temp file
65     char name[MAXPATHNAME];     /// Path name
66     char linkname[MAXPATHNAME]; /// Link name (symlinks only)
67     uint8_t *naklist;           /// NAK list
68     uint8_t *section_done;      /// Array of done flags for each section
69     int fd;                     /// File descriptor for file
70     uint32_t last_block;        /// Block number of last block received
71     uint16_t last_section;      /// Section number of last block received
72     int got_data;               /// True if at least one data packet received
73     struct timeval nak_time;    /// Time to send out NAKs
74     uint16_t nak_section_first; /// First section number to send NAKs for
75     uint16_t nak_section_last;  /// Last section number to send NAKs for
76     int got_done;               /// A DONE was received for this client
77     f_offset_t curr_offset;     /// Current file pointer offset in fd
78     int restart;                /// True if restarting a prior session
79     int comp_status;            /// Value for status field of COMPLETE
80     int destdiridx;             /// Index of dest dir file is received in
81     char *cache;                /// Disk cache, consecutive packets
82     uint32_t cache_start;       /// First block in cache
83     uint32_t cache_end;         /// Last block in cache
84     int cache_len;              /// Length of cache in bytes
85     char *cache_status;         /// Receive status of cache entries
86 };
87 
88 /**
89  * Header of client save state file.
90  * Followed in the file by the NAK list and section_done list.
91  * The naklist and section_done fields are left blank when the struct is
92  * written to a file.  When read back in, memory is allocated and the
93  * NAK list and section_done list are written to them.
94  */
95 struct client_restart_t {
96     uint32_t blocks;            /// Total blocks
97     uint32_t sections;          /// Total sections
98     f_offset_t size;            /// Size in bytes
99     char name[MAXPATHNAME];     /// Path name
100     uint8_t *naklist;           /// NAK list
101     uint8_t *section_done;      /// Array of done flags for each section
102 };
103 
104 /**
105  * Loss history item.
106  * These are part of an array where the array index is the sequence number.
107  */
108 struct loss_history_t {
109     int found;                  /// True if this packet was received
110     struct timeval t;           /// Time received, either actual or inferred
111     int size;                   /// Size of received packet, including UDP/IP
112 };
113 
114 /**
115  * Loss event item.
116  */
117 struct loss_event_t {
118     uint32_t start_seq;         /// Seq num of event start, including wraparound
119     int len;                    /// Size of loss interval
120     struct timeval t;           /// Timestamp of event start
121 };
122 
123 /**
124  * Info for a particular group
125  */
126 struct group_list_t {
127     uint32_t group_id;              /// Group ID
128     uint8_t group_inst;             /// Group instance ID (restart number)
129     uint16_t file_id;               /// File ID of current file
130     uint8_t version;                /// Protocol version number of server
131     union sockaddr_u multi;         /// Private multicast address
132     int multi_join;                 /// True if we're listening on private addr
133     char start_date[10];            /// Date initial ANNOUNCE was received
134     char start_time[10];            /// Time initial ANNOUNCE was received
135     uint16_t send_seq;              /// Outgoing seq. number for loss detection
136     uint32_t src_id;                /// ID of server (network byte order)
137     uint32_t proxy_id;              /// ID of proxy (network byte order)
138     struct fp_list_t *server_fp;    /// Pointer to server's fingerprint struct
139     struct fp_list_t *proxy_fp;     /// Pointer to proxy's fingerprint struct
140     union sockaddr_u replyaddr;     /// IP to send responses to
141     int phase;                      /// Current client_phase of the group
142     int client_auth, restart, sync_mode, sync_preview; /// Flags from ANNOUNCE
143     struct client_restart_t *restartinfo; /// Restart file header
144     unsigned int blocksize;         /// Size of packet payload
145     unsigned int datapacketsize;    /// Max size of UFTP packet
146     struct timeval timeout_time, start_timeout_time, expire_time;
147     double rtt, grtt;               /// Client's RTT and server's GRTT
148     uint16_t start_txseq, max_txseq;  /// Server's starting, max sequence #
149     struct loss_history_t *loss_history;  /// Loss history
150     struct loss_event_t loss_events[9];   /// Loss event history
151     int seq_wrap;                   /// Number of times server seq wrapped
152     int ccseq;                      /// Current congestion control sequence #
153     int64_t initrate;               /// Cong. control rate at start of fb round
154     int isclr;                      /// True if this client is the CLR
155     int slowstart;                  /// True if we're in slowstart mode
156     uint8_t robust, cc_type;        /// Robust factor, congestion control type
157     uint32_t gsize;                 /// Group size estimate
158     struct timeval cc_time;         /// Timer for sending CC_ACK
159     struct timeval last_server_ts, last_server_rx_ts;
160     int keytype, hashtype;          /// Encryption parameters
161     union key_t server_pubkey;      /// Server's public key
162     union key_t server_dhkey;       /// Server ECDH public key for this group
163     union key_t proxy_pubkey;       /// Response proxy public key
164     union key_t proxy_dhkey;        /// Respose proxy ECDH key for this group
165     union key_t client_dhkey;       /// Client ECDH private key for this group
166     unsigned int server_keytype;    /// Type of server key
167     unsigned int proxy_keytype;     /// Type of response proxy key
168     uint8_t rand1[RAND_LEN];        /// Server's random number
169     uint8_t rand2[RAND_LEN];        /// Client's random number
170     uint8_t *s_context;             /// Server session context
171     uint8_t *p_context;             /// Proxy session context
172     uint8_t *c_context1;            /// Client session context 1
173     uint8_t *c_context2;            /// Client session context 2
174     int s_context_len;              /// Length of server context
175     int p_context_len;              /// Length of proxy context
176     int c_context1_len;             /// Length of client context 1
177     int c_context2_len;             /// Length of client context 2
178     uint8_t premaster[MASTER4_LEN]; /// Premaster secret resulting from ECDH
179     uint8_t groupmaster[MASTER4_LEN];/// Group master key from server
180     unsigned int premaster_len;     /// Length of premaster secret
181     uint8_t s_hs_key[MAXKEY];       /// Symmetric handshake key for server
182     uint8_t s_hs_iv[MAXIV];         /// Symmetric handshake IV for server
183     uint8_t c_hs_key[MAXKEY];       /// Symmetric handshake key for client
184     uint8_t c_hs_iv[MAXIV];         /// Symmetric handshake IV for client
185     uint8_t s_app_key[MAXKEY];      /// Symmetric application key for server
186     uint8_t s_app_iv[MAXIV];        /// Symmetric application IV for server
187     uint8_t c_app_key[MAXKEY];      /// Symmetric application key for client
188     uint8_t c_app_iv[MAXIV];        /// Symmetric application IV for client
189     uint8_t finished_key[HASH_LEN]; /// Key for client finished message
190     uint8_t verify_data[HASH_LEN];  /// Client's finished hash
191     uint64_t ivctr;                 /// Counter portion of the IV
192     int ivlen, keylen, hashlen;     /// Length of hash, symmetric key and iv
193     struct file_t fileinfo;         /// Info pertaining to current file
194 };
195 
196 /**
197  * Global command line values and sockets
198  */
199 extern SOCKET listener;
200 extern char tempdir[MAXDIRNAME], destdir[MAXDIR][MAXDIRNAME];
201 extern char pidfile[MAXPATHNAME];
202 extern char keyfile[MAXPATHNAME], keyinfo[MAXPATHNAME];
203 extern char backupdir[MAXDIR][MAXDIRNAME];
204 extern char statusfilename[MAXPATHNAME];
205 extern FILE *status_file;
206 extern int debug, encrypted_only, dscp, destdircnt, tempfile;
207 extern int interface_count, pub_multi_count, rcvbuf, backupcnt;
208 extern char postreceive[MAXPATHNAME], portname[PORTNAME_LEN];
209 extern int port, move_individual, cache_len, noname, user_abort, use_ssm;
210 extern uint32_t uid;
211 extern union sockaddr_u hb_hosts[MAXLIST];
212 extern struct iflist m_interface[MAX_INTERFACES];
213 extern union sockaddr_u pub_multi[MAX_INTERFACES];
214 extern struct group_list_t group_list[MAXLIST];
215 extern struct fp_list_t server_list[MAXLIST], proxy_list[MAXLIST];
216 extern struct iflist ifl[MAX_INTERFACES];
217 extern struct timeval next_keyreq_time, next_hb_time;
218 extern int ifl_len, server_count, proxy_count, has_v4_proxy, sys_keys, priority;
219 extern int hbhost_count, hb_interval;
220 extern union key_t privkey;
221 extern int privkey_type;
222 extern struct fp_list_t proxy4_info;
223 extern union key_t proxy4_pubkey, proxy4_dhkey;
224 extern int proxy4_pubkeytype;
225 
226 #endif  // _CLIENT_H
227 
228