@@ -21,14 +21,14 @@ void wipe_priority_list(TCP_Priority_List *p)
2121/** return 0 if pending data was sent completely
2222 * return -1 if it wasn't
2323 */
24- int send_pending_data_nonpriority (TCP_Connection * con )
24+ int send_pending_data_nonpriority (const Logger * logger , TCP_Connection * con )
2525{
2626 if (con -> last_packet_length == 0 ) {
2727 return 0 ;
2828 }
2929
3030 const uint16_t left = con -> last_packet_length - con -> last_packet_sent ;
31- const int len = net_send (con -> sock , con -> last_packet + con -> last_packet_sent , left );
31+ const int len = net_send (logger , con -> sock , con -> last_packet + con -> last_packet_sent , left , con -> ip_port );
3232
3333 if (len <= 0 ) {
3434 return -1 ;
@@ -47,18 +47,18 @@ int send_pending_data_nonpriority(TCP_Connection *con)
4747/** return 0 if pending data was sent completely
4848 * return -1 if it wasn't
4949 */
50- int send_pending_data (TCP_Connection * con )
50+ int send_pending_data (const Logger * logger , TCP_Connection * con )
5151{
5252 /* finish sending current non-priority packet */
53- if (send_pending_data_nonpriority (con ) == -1 ) {
53+ if (send_pending_data_nonpriority (logger , con ) == -1 ) {
5454 return -1 ;
5555 }
5656
5757 TCP_Priority_List * p = con -> priority_queue_start ;
5858
5959 while (p ) {
6060 const uint16_t left = p -> size - p -> sent ;
61- const int len = net_send (con -> sock , p -> data + p -> sent , left );
61+ const int len = net_send (logger , con -> sock , p -> data + p -> sent , left , con -> ip_port );
6262
6363 if (len != left ) {
6464 if (len > 0 ) {
@@ -122,15 +122,16 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin
122122 * return 0 if could not send packet.
123123 * return -1 on failure (connection must be killed).
124124 */
125- int write_packet_TCP_secure_connection (TCP_Connection * con , const uint8_t * data , uint16_t length , bool priority )
125+ int write_packet_TCP_secure_connection (const Logger * logger , TCP_Connection * con , const uint8_t * data , uint16_t length ,
126+ bool priority )
126127{
127128 if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE ) {
128129 return -1 ;
129130 }
130131
131132 bool sendpriority = 1 ;
132133
133- if (send_pending_data (con ) == -1 ) {
134+ if (send_pending_data (logger , con ) == -1 ) {
134135 if (priority ) {
135136 sendpriority = 0 ;
136137 } else {
@@ -149,7 +150,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
149150 }
150151
151152 if (priority ) {
152- len = sendpriority ? net_send (con -> sock , packet , SIZEOF_VLA (packet )) : 0 ;
153+ len = sendpriority ? net_send (logger , con -> sock , packet , SIZEOF_VLA (packet ), con -> ip_port ) : 0 ;
153154
154155 if (len <= 0 ) {
155156 len = 0 ;
@@ -164,7 +165,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
164165 return add_priority (con , packet , SIZEOF_VLA (packet ), len );
165166 }
166167
167- len = net_send (con -> sock , packet , SIZEOF_VLA (packet ));
168+ len = net_send (logger , con -> sock , packet , SIZEOF_VLA (packet ), con -> ip_port );
168169
169170 if (len <= 0 ) {
170171 return 0 ;
@@ -187,22 +188,23 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
187188 * return length on success
188189 * return -1 on failure/no data in buffer.
189190 */
190- int read_TCP_packet (const Logger * logger , Socket sock , uint8_t * data , uint16_t length )
191+ int read_TCP_packet (const Logger * logger , Socket sock , uint8_t * data , uint16_t length , IP_Port ip_port )
191192{
192193 const uint16_t count = net_socket_data_recv_buffer (sock );
193194
194- if (count >= length ) {
195- const int len = net_recv (sock , data , length );
195+ if (count < length ) {
196+ LOGGER_INFO (logger , "recv buffer has %d bytes, but requested %d bytes" , count , length );
197+ return -1 ;
198+ }
196199
197- if (len != length ) {
198- LOGGER_ERROR (logger , "FAIL recv packet" );
199- return -1 ;
200- }
200+ const int len = net_recv (logger , sock , data , length , ip_port );
201201
202- return len ;
202+ if (len != length ) {
203+ LOGGER_ERROR (logger , "FAIL recv packet" );
204+ return -1 ;
203205 }
204206
205- return -1 ;
207+ return len ;
206208}
207209
208210/** Read the next two bytes in TCP stream then convert them to
@@ -212,22 +214,24 @@ int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t l
212214 * return 0 if nothing has been read from socket.
213215 * return -1 on failure.
214216 */
215- static uint16_t read_TCP_length (const Logger * logger , Socket sock )
217+ static uint16_t read_TCP_length (const Logger * logger , Socket sock , IP_Port ip_port )
216218{
217219 const uint16_t count = net_socket_data_recv_buffer (sock );
218220
219221 if (count >= sizeof (uint16_t )) {
220- uint16_t length ;
221- const int len = net_recv (sock , & length , sizeof (uint16_t ) );
222+ uint8_t length_buf [ sizeof ( uint16_t )] ;
223+ const int len = net_recv (logger , sock , length_buf , sizeof (length_buf ), ip_port );
222224
223225 if (len != sizeof (uint16_t )) {
224226 LOGGER_ERROR (logger , "FAIL recv packet" );
225227 return 0 ;
226228 }
227229
228- length = net_ntohs (length );
230+ uint16_t length ;
231+ net_unpack_u16 (length_buf , & length );
229232
230233 if (length > MAX_PACKET_SIZE ) {
234+ LOGGER_WARNING (logger , "TCP packet too large: %d > %d" , length , MAX_PACKET_SIZE );
231235 return -1 ;
232236 }
233237
@@ -242,10 +246,10 @@ static uint16_t read_TCP_length(const Logger *logger, Socket sock)
242246 * return -1 on failure (connection must be killed).
243247 */
244248int read_packet_TCP_secure_connection (const Logger * logger , Socket sock , uint16_t * next_packet_length ,
245- const uint8_t * shared_key , uint8_t * recv_nonce , uint8_t * data , uint16_t max_len )
249+ const uint8_t * shared_key , uint8_t * recv_nonce , uint8_t * data , uint16_t max_len , IP_Port ip_port )
246250{
247251 if (* next_packet_length == 0 ) {
248- uint16_t len = read_TCP_length (logger , sock );
252+ const uint16_t len = read_TCP_length (logger , sock , ip_port );
249253
250254 if (len == (uint16_t ) -1 ) {
251255 return -1 ;
@@ -259,21 +263,24 @@ int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_
259263 }
260264
261265 if (max_len + CRYPTO_MAC_SIZE < * next_packet_length ) {
266+ LOGGER_DEBUG (logger , "packet too large" );
262267 return -1 ;
263268 }
264269
265270 VLA (uint8_t , data_encrypted , * next_packet_length );
266- int len_packet = read_TCP_packet (logger , sock , data_encrypted , * next_packet_length );
271+ const int len_packet = read_TCP_packet (logger , sock , data_encrypted , * next_packet_length , ip_port );
267272
268273 if (len_packet != * next_packet_length ) {
274+ LOGGER_WARNING (logger , "invalid packet length: %d, expected %d" , len_packet , * next_packet_length );
269275 return 0 ;
270276 }
271277
272278 * next_packet_length = 0 ;
273279
274- int len = decrypt_data_symmetric (shared_key , recv_nonce , data_encrypted , len_packet , data );
280+ const int len = decrypt_data_symmetric (shared_key , recv_nonce , data_encrypted , len_packet , data );
275281
276282 if (len + CRYPTO_MAC_SIZE != len_packet ) {
283+ LOGGER_WARNING (logger , "decrypted length %d does not match expected length %d" , len + CRYPTO_MAC_SIZE , len_packet );
277284 return -1 ;
278285 }
279286
0 commit comments