Fixed memory leaks.

This commit is contained in:
Jan Vidar Krey 2012-10-25 04:13:45 +02:00
parent 38b19f633d
commit d4763e54db
2 changed files with 83 additions and 10 deletions

View File

@ -508,6 +508,7 @@ void ADC_client_send_info(struct ADC_client* client)
ADC_TRACE; ADC_TRACE;
client->info = adc_msg_construct_source(ADC_CMD_BINF, client->sid, 96); client->info = adc_msg_construct_source(ADC_CMD_BINF, client->sid, 96);
adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_NICK, client->nick); adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_NICK, client->nick);
if (client->desc) if (client->desc)
@ -528,6 +529,7 @@ void ADC_client_send_info(struct ADC_client* client)
adc_msg_add_named_argument_int(client->info, ADC_INF_FLAG_UPLOAD_SPEED, 10 * 1024 * 1024); adc_msg_add_named_argument_int(client->info, ADC_INF_FLAG_UPLOAD_SPEED, 10 * 1024 * 1024);
adc_cid_pid(client); adc_cid_pid(client);
ADC_client_send(client, client->info); ADC_client_send(client, client->info);
} }
@ -565,6 +567,7 @@ void ADC_client_destroy(struct ADC_client* client)
hub_free(client->nick); hub_free(client->nick);
hub_free(client->desc); hub_free(client->desc);
hub_free(client->address.hostname); hub_free(client->address.hostname);
hub_free(client);
} }
int ADC_client_connect(struct ADC_client* client, const char* address) int ADC_client_connect(struct ADC_client* client, const char* address)
@ -577,19 +580,20 @@ int ADC_client_connect(struct ADC_client* client, const char* address)
return 0; return 0;
return 1; return 1;
} }
else if (client->state == ps_dns) return ADC_client_connect_internal(client);
}
int ADC_client_connect_internal(struct ADC_client* client)
{
int ret;
if (client->state == ps_dns)
{ {
// Done name resolving! // Done name resolving!
client->callback(client, ADC_CLIENT_CONNECTING, 0); client->callback(client, ADC_CLIENT_CONNECTING, 0);
ADC_client_set_state(client, ps_conn); ADC_client_set_state(client, ps_conn);
} }
return ADC_client_connect_internal(client); ret = net_connect(net_con_get_sd(client->con), (struct sockaddr*) &client->addr, sizeof(struct sockaddr_in));
}
int ADC_client_connect_internal(struct ADC_client* client)
{
int ret = net_connect(net_con_get_sd(client->con), (struct sockaddr*) &client->addr, sizeof(struct sockaddr_in));
if (ret == 0 || (ret == -1 && net_error() == EISCONN)) if (ret == 0 || (ret == -1 && net_error() == EISCONN))
{ {
ADC_client_on_connected(client); ADC_client_on_connected(client);
@ -622,10 +626,12 @@ static void ADC_client_on_connected(struct ADC_client* client)
else else
#endif #endif
{ {
struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE);
net_con_update(client->con, NET_EVENT_READ); net_con_update(client->con, NET_EVENT_READ);
client->callback(client, ADC_CLIENT_CONNECTED, 0); client->callback(client, ADC_CLIENT_CONNECTED, 0);
ADC_client_send(client, adc_msg_create(ADC_HANDSHAKE)); ADC_client_send(client, handshake);
ADC_client_set_state(client, ps_protocol); ADC_client_set_state(client, ps_protocol);
adc_msg_free(handshake);
} }
} }
@ -633,11 +639,13 @@ static void ADC_client_on_connected(struct ADC_client* client)
static void ADC_client_on_connected_ssl(struct ADC_client* client) static void ADC_client_on_connected_ssl(struct ADC_client* client)
{ {
ADC_TRACE; ADC_TRACE;
struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE);
client->callback(client, ADC_CLIENT_SSL_OK, 0); client->callback(client, ADC_CLIENT_SSL_OK, 0);
client->callback(client, ADC_CLIENT_CONNECTED, 0); client->callback(client, ADC_CLIENT_CONNECTED, 0);
net_con_update(client->con, NET_EVENT_READ); net_con_update(client->con, NET_EVENT_READ);
ADC_client_send(client, adc_msg_create(ADC_HANDSHAKE)); ADC_client_send(client, handshake);
ADC_client_set_state(client, ps_protocol); ADC_client_set_state(client, ps_protocol);
adc_msg_free(handshake);
} }
#endif #endif

View File

@ -35,7 +35,6 @@ static struct ADC_user* user_get(sid_t sid)
return user; return user;
} }
static void user_remove(const struct ADC_client_quit_reason* quit) static void user_remove(const struct ADC_client_quit_reason* quit)
{ {
struct ADC_user* user = user_get(quit->sid); struct ADC_user* user = user_get(quit->sid);
@ -162,6 +161,68 @@ static int handle(struct ADC_client* client, enum ADC_client_callback_type type,
static int running = 1; static int running = 1;
#if !defined(WIN32)
void adm_handle_signal(int sig)
{
switch (sig)
{
case SIGINT:
LOG_INFO("Interrupted. Shutting down...");
running = 0;
break;
case SIGTERM:
LOG_INFO("Terminated. Shutting down...");
running = 0;
break;
case SIGPIPE:
break;
case SIGHUP:
break;
default:
LOG_TRACE("hub_handle_signal(): caught unknown signal: %d", signal);
running = 0;
break;
}
}
static int signals[] =
{
SIGINT, /* Interrupt the application */
SIGTERM, /* Terminate the application */
SIGPIPE, /* prevent sigpipe from kills the application */
SIGHUP, /* reload configuration */
0
};
void adm_setup_signal_handlers()
{
sigset_t sig_set;
struct sigaction act;
int i;
sigemptyset(&sig_set);
act.sa_mask = sig_set;
act.sa_flags = SA_ONSTACK | SA_RESTART;
act.sa_handler = adm_handle_signal;
for (i = 0; signals[i]; i++)
{
if (sigaction(signals[i], &act, 0) != 0)
{
LOG_ERROR("Error setting signal handler %d", signals[i]);
}
}
}
void adm_shutdown_signal_handlers()
{
}
#endif /* !WIN32 */
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc < 2) if (argc < 2)
@ -170,6 +231,9 @@ int main(int argc, char** argv)
return 1; return 1;
} }
hub_set_log_verbosity(5);
adm_setup_signal_handlers();
struct ADC_client* client; struct ADC_client* client;
net_initialize(); net_initialize();
@ -183,6 +247,7 @@ int main(int argc, char** argv)
ADC_client_destroy(client); ADC_client_destroy(client);
net_destroy(); net_destroy();
adm_shutdown_signal_handlers();
return 0; return 0;
} }