@@ -73,9 +73,9 @@ int tcp_rpcc_default_execute (struct connection *c, int op, raw_message_t *raw)
7373 tvkprintf (net_connections, 3 , " rpcc_execute: fd=%d, op=%d, len=%d\n " , c->fd , op, raw->total_bytes );
7474 if (op == TL_RPC_PING && raw->total_bytes == 12 ) {
7575 c->last_response_time = precise_now;
76- int Q[12 ];
76+ static int Q[12 ];
7777 assert (rwm_fetch_data (raw, Q, 12 ) == 12 );
78- int P[12 ];
78+ static int P[12 ];
7979 P[0 ] = TL_RPC_PONG;
8080 P[1 ] = Q[1 ];
8181 P[2 ] = Q[2 ];
@@ -90,24 +90,18 @@ int tcp_rpcc_default_execute (struct connection *c, int op, raw_message_t *raw)
9090
9191static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *msg) {
9292 struct tcp_rpc_data *D = TCP_RPC_DATA (c);
93- struct tcp_rpc_nonce_packet P{} ;
93+ static struct tcp_rpc_nonce_packet P;
9494 int res;
9595
9696 if (D->packet_num != -2 || D->packet_type != RPC_NONCE) {
9797 return -2 ;
9898 }
99- if (D->packet_len < sizeof (P) || D-> packet_len >= 1024 ) {
99+ if (D->packet_len != sizeof (struct tcp_rpc_nonce_packet ) ) {
100100 return -3 ;
101101 }
102- int excess_data_size = D->packet_len - sizeof (P); // fields from newer protocol version
103102
104- assert (rwm_fetch_data (msg, &P, sizeof (P)) == sizeof (P));
105- assert (rwm_fetch_data (msg, 0 , excess_data_size) == excess_data_size);
106- tvkprintf (net_connections, 4 , " Processing nonce packet, crypto schema: %d, version: %d, excess_data: %d, key select: %d\n " , P.crypto_schema , P.protocol_version , excess_data_size, P.key_select );
107-
108- if (P.protocol_version > 1 ) { // server must not reply with version > client
109- return -3 ;
110- }
103+ assert (rwm_fetch_data (msg, &P, D->packet_len ) == D->packet_len );
104+ tvkprintf (net_connections, 4 , " Processing nonce packet, crypto schema: %d, key select: %d\n " , P.crypto_schema , P.key_select );
111105
112106 switch (P.crypto_schema ) {
113107 case RPC_CRYPTO_NONE:
@@ -133,7 +127,7 @@ static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *m
133127 if (abs (P.crypto_ts - D->nonce_time ) > 30 ) {
134128 return -6 ; // less'om
135129 }
136- res = TCP_RPCC_FUNC (c)->rpc_start_crypto (c, &P );
130+ res = TCP_RPCC_FUNC (c)->rpc_start_crypto (c, P. crypto_nonce , P. key_select );
137131 if (res < 0 ) {
138132 return -6 ;
139133 }
@@ -148,13 +142,14 @@ static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *m
148142static int tcp_rpcc_send_handshake_packet (struct connection *c) {
149143 tvkprintf (net_connections, 4 , " tcp_rpcc_send_handshake_packet\n " );
150144 struct tcp_rpc_data *D = TCP_RPC_DATA (c);
151- struct tcp_rpc_handshake_packet P{} ;
145+ static struct tcp_rpc_handshake_packet P;
152146 if (!PID.ip ) {
153147 init_client_PID (c->local_endpoint .ss_family == AF_INET ? inet_sockaddr_address (&c->local_endpoint ): 0 );
154148 if (!PID.ip ) {
155149 PID.ip = get_my_ipv4 ();
156150 }
157151 }
152+ memset (&P, 0 , sizeof (P));
158153 P.type = RPC_HANDSHAKE;
159154 P.flags = default_rpc_flags & RPC_CRYPTO_USE_CRC32C;
160155 if (!D->remote_pid .port ) {
@@ -172,10 +167,11 @@ static int tcp_rpcc_send_handshake_packet (struct connection *c) {
172167}
173168
174169static int tcp_rpcc_send_handshake_error_packet (struct connection *c, int error_code) {
175- struct tcp_rpc_handshake_error_packet P{} ;
170+ static struct tcp_rpc_handshake_error_packet P;
176171 if (!PID.pid ) {
177172 init_client_PID (inet_sockaddr_address (&c->local_endpoint ));
178173 }
174+ memset (&P, 0 , sizeof (P));
179175 P.type = RPC_HANDSHAKE_ERROR;
180176 P.error_code = error_code;
181177 memcpy (&P.sender_pid , &PID, sizeof (PID));
@@ -189,11 +185,11 @@ static int tcp_rpcc_process_handshake_packet (struct connection *c, raw_message_
189185 tvkprintf (net_connections, 4 , " tcp_rpcc_process_handshake_packet\n " );
190186
191187 struct tcp_rpc_data *D = TCP_RPC_DATA (c);
192- struct tcp_rpc_handshake_packet P{} ;
188+ static struct tcp_rpc_handshake_packet P;
193189 if (D->packet_num != -1 || D->packet_type != RPC_HANDSHAKE) {
194190 return -2 ;
195191 }
196- if (D->packet_len != sizeof (P )) {
192+ if (D->packet_len != sizeof (struct tcp_rpc_handshake_packet )) {
197193 tcp_rpcc_send_handshake_error_packet (c, -3 );
198194 return -3 ;
199195 }
@@ -231,23 +227,17 @@ int tcp_rpcc_parse_execute (struct connection *c) {
231227 int len;
232228
233229 while (true ) {
234- len = c->in .total_bytes ;
230+ len = c->in .total_bytes ;
235231 if (len <= 0 ) {
236232 break ;
237233 }
238234 if (!D->packet_len ) {
239- if (len < D-> packet_v1_padding + 4 ) {
235+ if (len < 4 ) {
240236 c->status = conn_reading_answer;
241- return D->packet_v1_padding + 4 - len;
242- }
243- if (D->packet_v1_padding ) {
244- assert (D->packet_v1_padding < 4 );
245- assert (rwm_fetch_data (&c->in , 0 , D->packet_v1_padding ) == D->packet_v1_padding );
246- D->packet_v1_padding = 0 ;
237+ return 4 - len;
247238 }
248239 assert (rwm_fetch_lookup (&c->in , &D->packet_len , 4 ) == 4 );
249- // We skip checks for len&3 == 0 for protocol version 0, because there is little value in it.
250- if (D->packet_len > TCP_RPCC_FUNC (c)->max_packet_len && TCP_RPCC_FUNC (c)->max_packet_len > 0 ) {
240+ if (D->packet_len <= 0 || (D->packet_len & 3 ) || (D->packet_len > TCP_RPCC_FUNC (c)->max_packet_len && TCP_RPCC_FUNC (c)->max_packet_len > 0 )) {
251241 tvkprintf (net_connections, 1 , " error while parsing packet: bad packet length %d\n " , D->packet_len );
252242 c->status = conn_error;
253243 c->error = -1 ;
@@ -269,6 +259,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
269259 c->status = conn_reading_answer;
270260 return D->packet_len - len;
271261 }
262+
272263
273264 raw_message_t msg;
274265 if (c->in .total_bytes == D->packet_len ) {
@@ -348,9 +339,6 @@ int tcp_rpcc_parse_execute (struct connection *c) {
348339 }
349340
350341 D->in_packet_num ++;
351- if (c->crypto ) {
352- D->packet_v1_padding = (-D->packet_len ) & 3 ;
353- }
354342 D->packet_len = 0 ;
355343 if (c->status == conn_running) {
356344 c->status = conn_wait_answer;
@@ -433,10 +421,10 @@ int tcp_rpcc_init_fake_crypto (struct connection *c) {
433421 return -1 ;
434422 }
435423
436- struct tcp_rpc_nonce_packet buf{};
424+ static struct tcp_rpc_nonce_packet buf;
425+ memset (&buf, 0 , sizeof (buf));
437426 buf.type = RPC_NONCE;
438427 buf.crypto_schema = RPC_CRYPTO_NONE;
439- buf.protocol_version = 1 ; // ask for latest version we support
440428
441429 tcp_rpc_conn_send_data (c, sizeof (buf), &buf);
442430
@@ -489,13 +477,13 @@ int tcp_rpcc_init_crypto (struct connection *c) {
489477
490478 aes_generate_nonce (TCP_RPC_DATA (c)->nonce );
491479
492- struct tcp_rpc_nonce_packet buf{};
480+ static struct tcp_rpc_nonce_packet buf;
481+ memset (&buf, 0 , sizeof (buf));
493482 memcpy (buf.crypto_nonce , TCP_RPC_DATA (c)->nonce , 16 );
494483 buf.crypto_ts = TCP_RPC_DATA (c)->nonce_time ;
495484 buf.type = RPC_NONCE;
496485 buf.key_select = get_crypto_key_id (default_aes_key);
497486 buf.crypto_schema = (TCP_RPC_DATA (c)->crypto_flags & RPC_CRYPTO_ALLOW_UNENCRYPTED) ? RPC_CRYPTO_NONE_OR_AES : RPC_CRYPTO_AES;
498- buf.protocol_version = 1 ; // ask for latest version we support
499487
500488 tcp_rpc_conn_send_data (c, sizeof (buf), &buf);
501489
@@ -506,9 +494,9 @@ int tcp_rpcc_init_crypto (struct connection *c) {
506494 return 1 ;
507495}
508496
509- int tcp_rpcc_start_crypto (struct connection *c, struct tcp_rpc_nonce_packet *P ) {
497+ int tcp_rpcc_start_crypto (struct connection *c, char *nonce, int key_select ) {
510498 struct tcp_rpc_data *D = TCP_RPC_DATA (c);
511- tvkprintf (net_connections, 4 , " rpcc_start_crypto: key_select = %d\n " , P-> key_select );
499+ tvkprintf (net_connections, 4 , " rpcc_start_crypto: key_select = %d\n " , key_select);
512500
513501 if (c->crypto ) {
514502 return -1 ;
@@ -518,13 +506,13 @@ int tcp_rpcc_start_crypto (struct connection *c, struct tcp_rpc_nonce_packet *P)
518506 return -1 ;
519507 }
520508
521- if (!P-> key_select || P-> key_select != get_crypto_key_id (default_aes_key)) {
509+ if (!key_select || key_select != get_crypto_key_id (default_aes_key)) {
522510 return -1 ;
523511 }
524512
525513 struct aes_session_key aes_keys;
526514
527- if (aes_create_connection_keys (P-> protocol_version , default_aes_key, &aes_keys, 1 , P-> crypto_nonce , D->nonce , P-> crypto_ts , D->nonce_time , c) < 0 ) {
515+ if (aes_create_connection_keys (default_aes_key, &aes_keys, 1 , nonce , D->nonce , D->nonce_time , c) < 0 ) {
528516 return -1 ;
529517 }
530518
@@ -541,7 +529,7 @@ void tcp_rpcc_flush_crypto (struct connection *c) {
541529 tvkprintf (net_connections, 4 , " rpcc_flush_packet: padding with %d bytes\n " , pad_bytes);
542530 if (pad_bytes > 0 ) {
543531 assert (!(pad_bytes & 3 ));
544- int pad_str[3 ] = {4 , 4 , 4 };
532+ static int pad_str[3 ] = {4 , 4 , 4 };
545533 assert (pad_bytes <= 12 );
546534 assert (rwm_push_data (&c->out , pad_str, pad_bytes) == pad_bytes);
547535 }
@@ -564,7 +552,7 @@ int tcp_rpcc_flush (struct connection *c) {
564552 tvkprintf (net_connections, 4 , " rpcs_flush: padding with %d bytes\n " , pad_bytes);
565553 if (pad_bytes > 0 ) {
566554 assert (!(pad_bytes & 3 ));
567- int pad_str[3 ] = {4 , 4 , 4 };
555+ static int pad_str[3 ] = {4 , 4 , 4 };
568556 assert (pad_bytes <= 12 );
569557 assert (rwm_push_data (&c->out , pad_str, pad_bytes) == pad_bytes);
570558 }
0 commit comments