Cleanup reference adc message reference counting somewhat.

This commit is contained in:
Jan Vidar Krey 2010-02-16 17:41:56 +01:00
parent 29c162727c
commit 963416ad73
8 changed files with 107 additions and 138 deletions

View File

@ -23,7 +23,7 @@ EXO_TEST(hub_net_startup, {
EXO_TEST(hub_config_initialize, { EXO_TEST(hub_config_initialize, {
config_defaults(&g_config); config_defaults(&g_config);
g_config.server_port = 15111; g_config.server_port = 65111;
return 1; return 1;
}); });

View File

@ -8,27 +8,27 @@ EXO_TEST(test_message_refc_1, {
}); });
EXO_TEST(test_message_refc_2, { EXO_TEST(test_message_refc_2, {
return g_msg->references == 0; // 0 return g_msg->references == 1;
}); });
EXO_TEST(test_message_refc_3, { EXO_TEST(test_message_refc_3, {
adc_msg_incref(g_msg); adc_msg_incref(g_msg);
return g_msg->references == 1; // 1 return g_msg->references == 2;
}); });
EXO_TEST(test_message_refc_4, { EXO_TEST(test_message_refc_4, {
adc_msg_incref(g_msg); adc_msg_incref(g_msg);
return g_msg->references == 2; // 2 return g_msg->references == 3;
}); });
EXO_TEST(test_message_refc_5, { EXO_TEST(test_message_refc_5, {
adc_msg_free(g_msg); adc_msg_free(g_msg);
return g_msg->references == 1; // 1 return g_msg->references == 2;
}); });
EXO_TEST(test_message_refc_6, { EXO_TEST(test_message_refc_6, {
adc_msg_free(g_msg); adc_msg_free(g_msg);
return g_msg->references == 0; // 0 return g_msg->references == 1;
}); });
EXO_TEST(test_message_refc_7, { EXO_TEST(test_message_refc_7, {

View File

@ -26,6 +26,7 @@
uhub_assert(X->capacity); \ uhub_assert(X->capacity); \
uhub_assert(X->length); \ uhub_assert(X->length); \
uhub_assert(X->length <= X->capacity); \ uhub_assert(X->length <= X->capacity); \
uhub_assert(X->references > 0); \
uhub_assert(X->length == strlen(X->cache)); uhub_assert(X->length == strlen(X->cache));
#define ADC_MSG_NULL_ON_FREE #define ADC_MSG_NULL_ON_FREE
#else #else
@ -65,7 +66,6 @@ static void msg_free(void* ptr)
struct adc_message* adc_msg_incref(struct adc_message* msg) struct adc_message* adc_msg_incref(struct adc_message* msg)
{ {
if (!msg) return 0;
#ifndef ADC_MESSAGE_INCREF #ifndef ADC_MESSAGE_INCREF
msg->references++; msg->references++;
#ifdef MSG_MEMORY_DEBUG #ifdef MSG_MEMORY_DEBUG
@ -184,11 +184,9 @@ void adc_msg_free(struct adc_message* msg)
ADC_MSG_ASSERT(msg); ADC_MSG_ASSERT(msg);
if (msg->references > 0) msg->references--;
{
msg->references--; if (msg->references == 0)
}
else
{ {
#ifdef ADC_MSG_NULL_ON_FREE #ifdef ADC_MSG_NULL_ON_FREE
if (msg->cache) if (msg->cache)
@ -233,7 +231,7 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
copy->length = cmd->length; copy->length = cmd->length;
copy->capacity = 0; copy->capacity = 0;
copy->priority = cmd->priority; copy->priority = cmd->priority;
copy->references = 0; copy->references = 1;
copy->feature_cast_include = 0; copy->feature_cast_include = 0;
copy->feature_cast_exclude = 0; copy->feature_cast_exclude = 0;
@ -306,7 +304,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
if (command == NULL) if (command == NULL)
return NULL; /* OOM */ return NULL; /* OOM */
if (!is_printable_utf8(line, length)) if (!is_printable_utf8(line, length))
{ {
LOG_DEBUG("Dropped message with non-printable UTF-8 characters."); LOG_DEBUG("Dropped message with non-printable UTF-8 characters.");
@ -324,17 +322,18 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
msg_free(command); msg_free(command);
return NULL; /* OOM */ return NULL; /* OOM */
} }
adc_msg_set_length(command, length + need_terminate); adc_msg_set_length(command, length + need_terminate);
memcpy(command->cache, line, length); memcpy(command->cache, line, length);
/* Ensure we are zero terminated */ /* Ensure we are zero terminated */
command->cache[length] = 0; command->cache[length] = 0;
command->cache[length+need_terminate] = 0; command->cache[length+need_terminate] = 0;
command->cmd = FOURCC(line[0], line[1], line[2], line[3]); command->cmd = FOURCC(line[0], line[1], line[2], line[3]);
command->priority = 0; command->priority = 0;
command->references = 1;
switch (prefix) switch (prefix)
{ {
case 'U': case 'U':
@ -342,12 +341,12 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
/* these should never be seen on a hub */ /* these should never be seen on a hub */
ok = 0; ok = 0;
break; break;
case 'I': case 'I':
case 'H': case 'H':
ok = (length > 3); ok = (length > 3);
break; break;
case 'B': case 'B':
ok = (length > 8 && ok = (length > 8 &&
is_space(line[4]) && is_space(line[4]) &&
@ -355,18 +354,18 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
is_valid_base32_char(line[6]) && is_valid_base32_char(line[6]) &&
is_valid_base32_char(line[7]) && is_valid_base32_char(line[7]) &&
is_valid_base32_char(line[8])); is_valid_base32_char(line[8]));
if (!ok) break; if (!ok) break;
temp_sid[0] = line[5]; temp_sid[0] = line[5];
temp_sid[1] = line[6]; temp_sid[1] = line[6];
temp_sid[2] = line[7]; temp_sid[2] = line[7];
temp_sid[3] = line[8]; temp_sid[3] = line[8];
temp_sid[4] = '\0'; temp_sid[4] = '\0';
command->source = string_to_sid(temp_sid); command->source = string_to_sid(temp_sid);
break; break;
case 'F': case 'F':
ok = (length > 8 && ok = (length > 8 &&
is_space(line[4]) && is_space(line[4]) &&
@ -374,21 +373,21 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
is_valid_base32_char(line[6]) && is_valid_base32_char(line[6]) &&
is_valid_base32_char(line[7]) && is_valid_base32_char(line[7]) &&
is_valid_base32_char(line[8])); is_valid_base32_char(line[8]));
if (!ok) break; if (!ok) break;
temp_sid[0] = line[5]; temp_sid[0] = line[5];
temp_sid[1] = line[6]; temp_sid[1] = line[6];
temp_sid[2] = line[7]; temp_sid[2] = line[7];
temp_sid[3] = line[8]; temp_sid[3] = line[8];
temp_sid[4] = '\0'; temp_sid[4] = '\0';
command->source = string_to_sid(temp_sid); command->source = string_to_sid(temp_sid);
/* Create feature cast lists */ /* Create feature cast lists */
command->feature_cast_include = list_create(); command->feature_cast_include = list_create();
command->feature_cast_exclude = list_create(); command->feature_cast_exclude = list_create();
if (!command->feature_cast_include || !command->feature_cast_exclude) if (!command->feature_cast_include || !command->feature_cast_exclude)
{ {
list_destroy(command->feature_cast_include); list_destroy(command->feature_cast_include);
@ -397,7 +396,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
msg_free(command); msg_free(command);
return NULL; /* OOM */ return NULL; /* OOM */
} }
n = 10; n = 10;
while (line[n] == '+' || line[n] == '-') while (line[n] == '+' || line[n] == '-')
{ {
@ -405,21 +404,20 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
feature_cast_list = command->feature_cast_include; feature_cast_list = command->feature_cast_include;
else else
feature_cast_list = command->feature_cast_exclude; feature_cast_list = command->feature_cast_exclude;
temp_sid[0] = line[n++]; temp_sid[0] = line[n++];
temp_sid[1] = line[n++]; temp_sid[1] = line[n++];
temp_sid[2] = line[n++]; temp_sid[2] = line[n++];
temp_sid[3] = line[n++]; temp_sid[3] = line[n++];
temp_sid[4] = '\0'; temp_sid[4] = '\0';
list_append(feature_cast_list, hub_strdup(temp_sid)); list_append(feature_cast_list, hub_strdup(temp_sid));
} }
if (n == 10) if (n == 10)
ok = 0; ok = 0;
break; break;
case 'D': case 'D':
case 'E': case 'E':
ok = (length > 13 && ok = (length > 13 &&
@ -433,41 +431,41 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
is_valid_base32_char(line[11]) && is_valid_base32_char(line[11]) &&
is_valid_base32_char(line[12]) && is_valid_base32_char(line[12]) &&
is_valid_base32_char(line[13])); is_valid_base32_char(line[13]));
if (!ok) break; if (!ok) break;
temp_sid[0] = line[5]; temp_sid[0] = line[5];
temp_sid[1] = line[6]; temp_sid[1] = line[6];
temp_sid[2] = line[7]; temp_sid[2] = line[7];
temp_sid[3] = line[8]; temp_sid[3] = line[8];
temp_sid[4] = '\0'; temp_sid[4] = '\0';
command->source = string_to_sid(temp_sid); command->source = string_to_sid(temp_sid);
temp_sid[0] = line[10]; temp_sid[0] = line[10];
temp_sid[1] = line[11]; temp_sid[1] = line[11];
temp_sid[2] = line[12]; temp_sid[2] = line[12];
temp_sid[3] = line[13]; temp_sid[3] = line[13];
temp_sid[4] = '\0'; temp_sid[4] = '\0';
command->target = string_to_sid(temp_sid); command->target = string_to_sid(temp_sid);
break; break;
default: default:
ok = 0; ok = 0;
} }
if (need_terminate) if (need_terminate)
{ {
command->cache[length] = '\n'; command->cache[length] = '\n';
} }
if (!ok) if (!ok)
{ {
adc_msg_free(command); adc_msg_free(command);
return NULL; return NULL;
} }
/* At this point the arg_offset should point to a space, or the end of message */ /* At this point the arg_offset should point to a space, or the end of message */
n = adc_msg_get_arg_offset(command); n = adc_msg_get_arg_offset(command);
if (command->cache[n] == ' ') if (command->cache[n] == ' ')
@ -476,13 +474,13 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
} }
else if (command->cache[n] == '\n') ok = 1; else if (command->cache[n] == '\n') ok = 1;
else ok = 0; else ok = 0;
if (!ok) if (!ok)
{ {
adc_msg_free(command); adc_msg_free(command);
return NULL; return NULL;
} }
ADC_MSG_ASSERT(command); ADC_MSG_ASSERT(command);
return command; return command;
} }
@ -497,7 +495,6 @@ struct adc_message* adc_msg_create(const char* line)
struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size) struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
{ {
struct adc_message* msg = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message)); struct adc_message* msg = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
if (!msg) if (!msg)
return NULL; /* OOM */ return NULL; /* OOM */
@ -508,7 +505,7 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
msg_free(msg); msg_free(msg);
return NULL; /* OOM */ return NULL; /* OOM */
} }
if (fourcc) if (fourcc)
{ {
msg->cache[0] = (char) ((fourcc >> 24) & 0xff); msg->cache[0] = (char) ((fourcc >> 24) & 0xff);
@ -516,15 +513,15 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
msg->cache[2] = (char) ((fourcc >> 8) & 0xff); msg->cache[2] = (char) ((fourcc >> 8) & 0xff);
msg->cache[3] = (char) ((fourcc ) & 0xff); msg->cache[3] = (char) ((fourcc ) & 0xff);
msg->cache[4] = '\n'; msg->cache[4] = '\n';
/* Ensure we are zero terminated */ /* Ensure we are zero terminated */
adc_msg_set_length(msg, 5); adc_msg_set_length(msg, 5);
msg->cache[msg->length] = 0; msg->cache[msg->length] = 0;
} }
msg->cmd = fourcc; msg->cmd = fourcc;
msg->priority = 0; msg->priority = 0;
msg->references = 1;
return msg; return msg;
} }
@ -591,7 +588,7 @@ int adc_msg_has_named_argument(struct adc_message* cmd, const char prefix_[2])
int arg_offset = adc_msg_get_arg_offset(cmd); int arg_offset = adc_msg_get_arg_offset(cmd);
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
start = memmem(&cmd->cache[arg_offset], (cmd->length - arg_offset), prefix, 3); start = memmem(&cmd->cache[arg_offset], (cmd->length - arg_offset), prefix, 3);
while (start) while (start)
{ {
@ -601,7 +598,7 @@ int adc_msg_has_named_argument(struct adc_message* cmd, const char prefix_[2])
else else
start = NULL; start = NULL;
} }
return count; return count;
} }
@ -614,25 +611,25 @@ char* adc_msg_get_named_argument(struct adc_message* cmd, const char prefix_[2])
size_t length; size_t length;
char prefix[4] = { ' ', prefix_[0], prefix_[1], '\0' }; char prefix[4] = { ' ', prefix_[0], prefix_[1], '\0' };
int arg_offset = adc_msg_get_arg_offset(cmd); int arg_offset = adc_msg_get_arg_offset(cmd);
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
start = memmem(&cmd->cache[arg_offset], cmd->length - arg_offset, prefix, 3); start = memmem(&cmd->cache[arg_offset], cmd->length - arg_offset, prefix, 3);
if (!start) if (!start)
return NULL; return NULL;
start = &start[3]; start = &start[3];
end = strchr(start, ' '); end = strchr(start, ' ');
if (!end) end = &cmd->cache[cmd->length]; if (!end) end = &cmd->cache[cmd->length];
length = &end[0] - &start[0]; length = &end[0] - &start[0];
argument = hub_strndup(start, length); argument = hub_strndup(start, length);
if (length > 0 && argument[length-1] == '\n') if (length > 0 && argument[length-1] == '\n')
{ {
argument[length-1] = 0; argument[length-1] = 0;
} }
return argument; return argument;
} }
@ -650,9 +647,9 @@ int adc_msg_replace_named_argument(struct adc_message* cmd, const char prefix[2]
{ {
return -1; return -1;
} }
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
return 0; return 0;
} }
@ -683,9 +680,9 @@ int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], co
int ret = 0; int ret = 0;
if (!string) if (!string)
return -1; return -1;
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
adc_msg_unterminate(cmd); adc_msg_unterminate(cmd);
adc_msg_cache_append(cmd, " ", 1); adc_msg_cache_append(cmd, " ", 1);
adc_msg_cache_append(cmd, prefix, 2); adc_msg_cache_append(cmd, prefix, 2);
@ -719,7 +716,7 @@ int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix
int adc_msg_add_argument(struct adc_message* cmd, const char* string) int adc_msg_add_argument(struct adc_message* cmd, const char* string)
{ {
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
adc_msg_unterminate(cmd); adc_msg_unterminate(cmd);
adc_msg_cache_append(cmd, " ", 1); adc_msg_cache_append(cmd, " ", 1);
adc_msg_cache_append(cmd, string, strlen(string)); adc_msg_cache_append(cmd, string, strlen(string));
@ -785,11 +782,11 @@ int adc_msg_get_argument_index(struct adc_message* cmd, const char prefix[2])
char* start; char* start;
char* end; char* end;
int count = 0; int count = 0;
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
adc_msg_unterminate(cmd); adc_msg_unterminate(cmd);
start = strchr(&cmd->cache[adc_msg_get_arg_offset(cmd)-1], ' '); start = strchr(&cmd->cache[adc_msg_get_arg_offset(cmd)-1], ' ');
while (start) while (start)
{ {
@ -842,16 +839,13 @@ int adc_msg_unescape_length(const char* str)
} }
char* adc_msg_unescape(const char* string) char* adc_msg_unescape(const char* string)
{ {
char* new_string = msg_malloc(adc_msg_unescape_length(string)+1); char* new_string = msg_malloc(adc_msg_unescape_length(string)+1);
char* ptr = (char*) new_string; char* ptr = (char*) new_string;
char* str = (char*) string; char* str = (char*) string;
int escaped = 0; int escaped = 0;
while (*str) while (*str)
{ {
if (escaped) { if (escaped) {
@ -863,14 +857,12 @@ char* adc_msg_unescape(const char* string)
*ptr++ = '\n'; *ptr++ = '\n';
else else
*ptr++ = *str; *ptr++ = *str;
escaped = 0; escaped = 0;
} else { } else {
if (*str == '\\') if (*str == '\\')
escaped = 1; escaped = 1;
else else
*ptr++ = *str; *ptr++ = *str;
} }
str++; str++;
} }

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey * Copyright (C) 2007-2010, Jan Vidar Krey
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -42,7 +42,7 @@ static void remove_server_restricted_flags(struct adc_message* cmd)
static int set_feature_cast_supports(struct hub_user* u, struct adc_message* cmd) static int set_feature_cast_supports(struct hub_user* u, struct adc_message* cmd)
{ {
char *it, *tmp; char *it, *tmp;
if (adc_msg_has_named_argument(cmd, ADC_INF_FLAG_SUPPORT)) if (adc_msg_has_named_argument(cmd, ADC_INF_FLAG_SUPPORT))
{ {
tmp = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SUPPORT); tmp = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SUPPORT);
@ -58,7 +58,7 @@ static int set_feature_cast_supports(struct hub_user* u, struct adc_message* cmd
user_set_feature_cast_support(u, it); user_set_feature_cast_support(u, it);
it = &it[5]; it = &it[5];
} }
if (*it) if (*it)
{ {
user_set_feature_cast_support(u, it); user_set_feature_cast_support(u, it);
@ -74,9 +74,9 @@ static int check_hash_tiger(const char* cid, const char* pid)
char x_pid[64]; char x_pid[64];
char raw_pid[64]; char raw_pid[64];
uint64_t tiger_res[3]; uint64_t tiger_res[3];
memset(x_pid, 0, MAX_CID_LEN+1); memset(x_pid, 0, MAX_CID_LEN+1);
base32_decode(pid, (unsigned char*) raw_pid, MAX_CID_LEN); base32_decode(pid, (unsigned char*) raw_pid, MAX_CID_LEN);
tiger((uint64_t*) raw_pid, TIGERSIZE, (uint64_t*) tiger_res); tiger((uint64_t*) raw_pid, TIGERSIZE, (uint64_t*) tiger_res);
base32_encode((unsigned char*) tiger_res, TIGERSIZE, x_pid); base32_encode((unsigned char*) tiger_res, TIGERSIZE, x_pid);
@ -102,7 +102,7 @@ static int check_cid(struct hub_info* hub, struct hub_user* user, struct adc_mes
hub_free(pid); hub_free(pid);
return status_msg_error_no_memory; return status_msg_error_no_memory;
} }
if (strlen(cid) != MAX_CID_LEN) if (strlen(cid) != MAX_CID_LEN)
{ {
hub_free(cid); hub_free(cid);
@ -125,7 +125,7 @@ static int check_cid(struct hub_info* hub, struct hub_user* user, struct adc_mes
hub_free(pid); hub_free(pid);
return status_msg_inf_error_cid_invalid; return status_msg_inf_error_cid_invalid;
} }
if (!is_valid_base32_char(pid[pos])) if (!is_valid_base32_char(pid[pos]))
{ {
hub_free(cid); hub_free(cid);
@ -415,7 +415,7 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
int64_t shared_size = atoll(arg); int64_t shared_size = atoll(arg);
if (shared_size < 0) if (shared_size < 0)
shared_size = 0; shared_size = 0;
if (user_is_logged_in(user)) if (user_is_logged_in(user))
{ {
hub->users->shared_size -= user->limits.shared_size; hub->users->shared_size -= user->limits.shared_size;
@ -425,14 +425,14 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
hub_free(arg); hub_free(arg);
arg = 0; arg = 0;
} }
arg = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SHARED_FILES); arg = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_SHARED_FILES);
if (arg) if (arg)
{ {
ssize_t shared_files = atoll(arg); ssize_t shared_files = atoll(arg);
if (shared_files < 0) if (shared_files < 0)
shared_files = 0; shared_files = 0;
if (user_is_logged_in(user)) if (user_is_logged_in(user))
{ {
hub->users->shared_files -= user->limits.shared_files; hub->users->shared_files -= user->limits.shared_files;
@ -442,7 +442,7 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
hub_free(arg); hub_free(arg);
arg = 0; arg = 0;
} }
arg = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_COUNT_HUB_NORMAL); arg = adc_msg_get_named_argument(cmd, ADC_INF_FLAG_COUNT_HUB_NORMAL);
if (arg) if (arg)
{ {
@ -497,7 +497,7 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
{ {
return status_msg_user_share_size_high; return status_msg_user_share_size_high;
} }
if ((user->limits.hub_count_user > hub_get_max_hubs_user(hub) && hub_get_max_hubs_user(hub)) || if ((user->limits.hub_count_user > hub_get_max_hubs_user(hub) && hub_get_max_hubs_user(hub)) ||
(user->limits.hub_count_registered > hub_get_max_hubs_reg(hub) && hub_get_max_hubs_reg(hub)) || (user->limits.hub_count_registered > hub_get_max_hubs_reg(hub) && hub_get_max_hubs_reg(hub)) ||
(user->limits.hub_count_operator > hub_get_max_hubs_op(hub) && hub_get_max_hubs_op(hub)) || (user->limits.hub_count_operator > hub_get_max_hubs_op(hub) && hub_get_max_hubs_op(hub)) ||
@ -505,14 +505,14 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
{ {
return status_msg_user_hub_limit_high; return status_msg_user_hub_limit_high;
} }
if ((user->limits.hub_count_user < hub_get_min_hubs_user(hub) && hub_get_min_hubs_user(hub)) || if ((user->limits.hub_count_user < hub_get_min_hubs_user(hub) && hub_get_min_hubs_user(hub)) ||
(user->limits.hub_count_registered < hub_get_min_hubs_reg(hub) && hub_get_min_hubs_reg(hub)) || (user->limits.hub_count_registered < hub_get_min_hubs_reg(hub) && hub_get_min_hubs_reg(hub)) ||
(user->limits.hub_count_operator < hub_get_min_hubs_op(hub) && hub_get_min_hubs_op(hub))) (user->limits.hub_count_operator < hub_get_min_hubs_op(hub) && hub_get_min_hubs_op(hub)))
{ {
return status_msg_user_hub_limit_low; return status_msg_user_hub_limit_low;
} }
if (user->limits.upload_slots < hub_get_min_slots(hub) && hub_get_min_slots(hub)) if (user->limits.upload_slots < hub_get_min_slots(hub) && hub_get_min_slots(hub))
{ {
return status_msg_user_slots_low; return status_msg_user_slots_low;
@ -559,11 +559,11 @@ static int set_credentials(struct hub_info* hub, struct hub_user* user, struct a
case cred_guest: case cred_guest:
/* Nothing to be added to the info message */ /* Nothing to be added to the info message */
break; break;
case cred_user: case cred_user:
adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_REGISTERED_USER); adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_REGISTERED_USER);
break; break;
case cred_operator: case cred_operator:
adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_OPERATOR); adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_OPERATOR);
break; break;
@ -571,11 +571,11 @@ static int set_credentials(struct hub_info* hub, struct hub_user* user, struct a
case cred_super: case cred_super:
adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_SUPER_USER); adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_SUPER_USER);
break; break;
case cred_admin: case cred_admin:
adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_ADMIN); adc_msg_add_argument(cmd, ADC_INF_FLAG_CLIENT_TYPE ADC_CLIENT_TYPE_ADMIN);
break; break;
case cred_link: case cred_link:
break; break;
} }
@ -670,13 +670,12 @@ int hub_handle_info_login(struct hub_info* hub, struct hub_user* user, struct ad
int code = 0; int code = 0;
INF_CHECK(hub_perform_login_checks, hub, user, cmd); INF_CHECK(hub_perform_login_checks, hub, user, cmd);
/* Private ID must never be broadcasted - drop it! */ /* Private ID must never be broadcasted - drop it! */
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_PRIVATE_ID); adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_PRIVATE_ID);
code = set_credentials(hub, user, cmd); code = set_credentials(hub, user, cmd);
/* Note: this must be done *after* set_credentials. */ /* Note: this must be done *after* set_credentials. */
if (check_is_hub_full(hub, user)) if (check_is_hub_full(hub, user))
{ {
@ -785,16 +784,18 @@ int hub_handle_info(struct hub_info* hub, struct hub_user* user, const struct ad
strip_network(user, cmd); strip_network(user, cmd);
hub_handle_info_low_bandwidth(hub, user, cmd); hub_handle_info_low_bandwidth(hub, user, cmd);
user_update_info(user, cmd); user_update_info(user, cmd);
if (!adc_msg_is_empty(cmd)) if (!adc_msg_is_empty(cmd))
{ {
route_message(hub, user, cmd); route_message(hub, user, cmd);
} }
adc_msg_free(cmd); adc_msg_free(cmd);
} }
return 0; return 0;
} }

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey * Copyright (C) 2007-2010, Jan Vidar Krey
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View File

@ -24,35 +24,6 @@
/* FIXME: This should not be needed! */ /* FIXME: This should not be needed! */
extern struct hub_info* g_hub; extern struct hub_info* g_hub;
#ifdef DEBUG_SENDQ
void debug_sendq_send(struct hub_user* user, int sent, int total)
{
LOG_DUMP("SEND: sd=%d, %d/%d bytes\n", user->connection->sd, sent, total);
if (sent == -1)
{
int err = net_error();
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
}
}
void debug_sendq_recv(struct hub_user* user, int received, int max, const char* buffer)
{
LOG_DUMP("RECV: %d/%d bytes\n", received, (int) max);
if (received == -1)
{
int err = net_error();
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
}
else if (received > 0)
{
char* data = hub_malloc_zero(received + 1);
memcpy(data, buffer, received);
LOG_DUMP("RECV: \"%s\"\n", data);
hub_free(data);
}
}
#endif
int handle_net_read(struct hub_user* user) int handle_net_read(struct hub_user* user)
{ {
static char buf[MAX_RECV_BUF]; static char buf[MAX_RECV_BUF];
@ -223,7 +194,7 @@ void net_on_accept(struct net_connection* con, int event, void *arg)
} }
} }
addr = ip_convert_to_string(&ipaddr); addr = ip_convert_to_string(&ipaddr);
/* FIXME: Should have a plugin log this */ /* FIXME: Should have a plugin log this */
LOG_TRACE("Got connection from %s", addr); LOG_TRACE("Got connection from %s", addr);

View File

@ -83,7 +83,14 @@ void user_set_state(struct hub_user* user, enum user_state state)
void user_set_info(struct hub_user* user, struct adc_message* cmd) void user_set_info(struct hub_user* user, struct adc_message* cmd)
{ {
adc_msg_free(user->info); adc_msg_free(user->info);
user->info = adc_msg_incref(cmd); if (cmd)
{
user->info = adc_msg_incref(cmd);
}
else
{
user->info = 0;
}
} }
void user_update_info(struct hub_user* u, struct adc_message* cmd) void user_update_info(struct hub_user* u, struct adc_message* cmd)
@ -97,7 +104,7 @@ void user_update_info(struct hub_user* u, struct adc_message* cmd)
/* FIXME: OOM! */ /* FIXME: OOM! */
return; return;
} }
/* /*
* FIXME: Optimization potential: * FIXME: Optimization potential:
* *

View File

@ -155,7 +155,7 @@ int uman_remove(struct hub_info* hub, struct hub_user* user)
return -1; return -1;
list_remove(hub->users->list, user); list_remove(hub->users->list, user);
if (hub->users->count > 0) if (hub->users->count > 0)
{ {
hub->users->count--; hub->users->count--;
@ -164,12 +164,12 @@ int uman_remove(struct hub_info* hub, struct hub_user* user)
{ {
assert(!"negative count!"); assert(!"negative count!");
} }
hub->users->shared_size -= user->limits.shared_size; hub->users->shared_size -= user->limits.shared_size;
hub->users->shared_files -= user->limits.shared_files; hub->users->shared_files -= user->limits.shared_files;
user->hub = 0; user->hub = 0;
return 0; return 0;
} }
@ -237,7 +237,7 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
} }
user = (struct hub_user*) list_get_next(hub->users->list); user = (struct hub_user*) list_get_next(hub->users->list);
} }
#if 0 #if 0
FIXME: FIXME FIXME handle send queue excess FIXME: FIXME FIXME handle send queue excess
if (!target->send_queue_size) if (!target->send_queue_size)
@ -248,12 +248,11 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
return ret; return ret;
} }
void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving) void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
{ {
struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6); struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6);
adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid)); adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid));
if (leaving->quit_reason == quit_banned || leaving->quit_reason == quit_kicked) if (leaving->quit_reason == quit_banned || leaving->quit_reason == quit_kicked)
{ {
adc_msg_add_argument(command, ADC_QUI_FLAG_DISCONNECT); adc_msg_add_argument(command, ADC_QUI_FLAG_DISCONNECT);
@ -262,7 +261,6 @@ void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
adc_msg_free(command); adc_msg_free(command);
} }
sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user) sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user)
{ {
sid_t sid = sid_alloc(hub->users->sids, user); sid_t sid = sid_alloc(hub->users->sids, user);