fixed some issues

This commit is contained in:
notsecure 2014-07-15 18:22:04 -04:00
parent 22d28ddf36
commit 96249e8b02

View File

@ -288,7 +288,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length,
/* return 0 if pending data was sent completely /* return 0 if pending data was sent completely
* return -1 if it wasn't * return -1 if it wasn't
*/ */
static int send_pending_data(TCP_Secure_Connection *con) static int send_pending_data_nonpriority(TCP_Secure_Connection *con)
{ {
if (con->last_packet_length == 0) { if (con->last_packet_length == 0) {
return 0; return 0;
@ -314,8 +314,13 @@ static int send_pending_data(TCP_Secure_Connection *con)
/* return 0 if pending data was sent completely /* return 0 if pending data was sent completely
* return -1 if it wasn't * return -1 if it wasn't
*/ */
static int send_pending_data_priority(TCP_Secure_Connection *con) static int send_pending_data(TCP_Secure_Connection *con)
{ {
/* finish sending current non-priority packet */
if(send_pending_data_nonpriority(con) == -1) {
return -1;
}
TCP_Priority_List *p = con->priority_queue_start; TCP_Priority_List *p = con->priority_queue_start;
while(p) { while(p) {
@ -346,16 +351,8 @@ static int send_pending_data_priority(TCP_Secure_Connection *con)
/* return 0 on failure (only if malloc fails) /* return 0 on failure (only if malloc fails)
* return 1 on success * return 1 on success
*/ */
static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, int sent) static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent)
{ {
if(sent == size) {
return 1;
}
if(sent <= 0) {
sent = 0;
}
TCP_Priority_List *p = con->priority_queue_end, *new; TCP_Priority_List *p = con->priority_queue_end, *new;
new = malloc(sizeof(TCP_Priority_List) + size); new = malloc(sizeof(TCP_Priority_List) + size);
if(!new) { if(!new) {
@ -387,7 +384,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
return -1; return -1;
_Bool sendpriority = 1; _Bool sendpriority = 1;
if (send_pending_data_priority(con) == -1) { if (send_pending_data(con) == -1) {
if (priority) { if (priority) {
sendpriority = 0; sendpriority = 0;
} else { } else {
@ -401,16 +398,24 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
memcpy(packet, &c_length, sizeof(uint16_t)); memcpy(packet, &c_length, sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
if (priority) {
return add_priority(con, packet, sizeof(packet), sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0);
}
if (send_pending_data(con) == -1)
return 0;
if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t)))
return -1; return -1;
if (priority) {
len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0;
if(len <= 0) {
len = 0;
} else {
increment_nonce(con->sent_nonce);
}
if(len == sizeof(packet)) {
return 1;
}
return add_priority(con, packet, sizeof(packet), len);
}
len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL);
if (len <= 0) if (len <= 0)
@ -1152,10 +1157,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
continue; continue;
} }
/* try sending queued priority packets first */ send_pending_data(conn);
if(send_pending_data_priority(conn) == 0) {
send_pending_data(conn);
}
#ifndef TCP_SERVER_USE_EPOLL #ifndef TCP_SERVER_USE_EPOLL