First bugfixes

Now we can at least successfully register.
This commit is contained in:
Přemysl Eric Janouch 2014-07-15 23:28:00 +02:00
parent c419946729
commit 996e0912c2
2 changed files with 14 additions and 9 deletions

View File

@ -519,7 +519,7 @@ error_set (struct error **e, const char *message, ...)
va_list ap; va_list ap;
va_start (ap, message); va_start (ap, message);
int size = snprintf (NULL, 0, message, ap); int size = vsnprintf (NULL, 0, message, ap);
va_end (ap); va_end (ap);
hard_assert (size >= 0); hard_assert (size >= 0);
@ -528,7 +528,7 @@ error_set (struct error **e, const char *message, ...)
tmp->message = xmalloc (size + 1); tmp->message = xmalloc (size + 1);
va_start (ap, message); va_start (ap, message);
size = snprintf (tmp->message, size + 1, message, ap); size = vsnprintf (tmp->message, size + 1, message, ap);
va_end (ap); va_end (ap);
hard_assert (size >= 0); hard_assert (size >= 0);
@ -579,6 +579,8 @@ struct str_map
void (*free) (void *); ///< Callback to destruct the payload void (*free) (void *); ///< Callback to destruct the payload
/// Callback to compare keys for equivalence /// Callback to compare keys for equivalence
// FIXME: they may still end up on a different index, and actually should;
// delete this callback and put strxfrm() in its place
int (*key_cmp) (const char *, const char *); int (*key_cmp) (const char *, const char *);
}; };
@ -1088,6 +1090,7 @@ poller_set (struct poller *self, int fd, short int events,
struct poller_info *info = self->info[index]; struct poller_info *info = self->info[index];
info->fd = fd; info->fd = fd;
info->events = events;
info->dispatcher = dispatcher; info->dispatcher = dispatcher;
info->user_data = data; info->user_data = data;

View File

@ -148,7 +148,7 @@ irc_validate_to_str (enum validation_result result)
#define N6 "[0-9ABCDEFabcdef]{1,}" #define N6 "[0-9ABCDEFabcdef]{1,}"
#define LE "A-Za-z" #define LE "A-Za-z"
#define SP "\\[\\]\\\\`_^{|}" #define SP "][\\\\`_^{|}"
static enum validation_result static enum validation_result
irc_validate_hostname (const char *hostname) irc_validate_hostname (const char *hostname)
@ -192,7 +192,7 @@ irc_validate_nickname (const char *nickname)
{ {
if (!*nickname) if (!*nickname)
return VALIDATION_ERROR_EMPTY; return VALIDATION_ERROR_EMPTY;
if (!irc_regex_match ("^[" LE SP "][-0-9" LE SP "]*$", nickname)) if (!irc_regex_match ("^[" SP LE "][" SP LE "0-9-]*$", nickname))
return VALIDATION_ERROR_INVALID; return VALIDATION_ERROR_INVALID;
if (strlen (nickname) > IRC_MAX_NICKNAME) if (strlen (nickname) > IRC_MAX_NICKNAME)
return VALIDATION_ERROR_TOO_LONG; return VALIDATION_ERROR_TOO_LONG;
@ -706,7 +706,7 @@ irc_register_handlers (struct server_context *ctx)
for (size_t i = 0; i < N_ELEMENTS (message_handlers); i++) for (size_t i = 0; i < N_ELEMENTS (message_handlers); i++)
{ {
const struct irc_command *cmd = &message_handlers[i]; const struct irc_command *cmd = &message_handlers[i];
str_map_set (&ctx->handlers, cmd->name, (void *) cmd->handler); str_map_set (&ctx->handlers, cmd->name, (void *) cmd);
} }
} }
@ -723,8 +723,7 @@ irc_process_message (const struct irc_message *msg,
struct client *c = user_data; struct client *c = user_data;
struct irc_command *cmd = str_map_find (&c->ctx->handlers, msg->command); struct irc_command *cmd = str_map_find (&c->ctx->handlers, msg->command);
if (!cmd) if (!cmd)
irc_send_reply (c, IRC_ERR_UNKNOWNCOMMAND, irc_send_reply (c, IRC_ERR_UNKNOWNCOMMAND, msg->command);
"%s: Unknown command", msg->command);
else if (cmd->requires_registration && !c->registered) else if (cmd->requires_registration && !c->registered)
irc_send_reply (c, IRC_ERR_NOTREGISTERED); irc_send_reply (c, IRC_ERR_NOTREGISTERED);
else else
@ -1020,20 +1019,22 @@ on_irc_client_available (const struct pollfd *pfd, void *user_data)
char host[NI_MAXHOST] = "unknown", port[NI_MAXSERV] = "unknown"; char host[NI_MAXHOST] = "unknown", port[NI_MAXSERV] = "unknown";
int err = getnameinfo ((struct sockaddr *) &peer, peer_len, int err = getnameinfo ((struct sockaddr *) &peer, peer_len,
host, sizeof host, port, sizeof port, AI_NUMERICSERV); host, sizeof host, port, sizeof port, NI_NUMERICSERV);
if (err) if (err)
print_debug ("%s: %s", "getnameinfo", gai_strerror (err)); print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
print_debug ("accepted connection from %s:%s", host, port); print_debug ("accepted connection from %s:%s", host, port);
struct client *c = xmalloc (sizeof *c); struct client *c = xmalloc (sizeof *c);
client_init (c); client_init (c);
c->ctx = ctx;
c->socket_fd = fd; c->socket_fd = fd;
c->hostname = xstrdup (host); c->hostname = xstrdup (host);
LIST_PREPEND (ctx->clients, c); LIST_PREPEND (ctx->clients, c);
// TODO: set a timeout on the socket, something like 3 minutes, then we // TODO: set a timeout on the socket, something like 3 minutes, then we
// should terminate the connection. // should terminate the connection.
poller_set (&ctx->poller, c->socket_fd, POLLIN, set_blocking (fd, false);
poller_set (&ctx->poller, fd, POLLIN,
(poller_dispatcher_func) on_irc_client_ready, c); (poller_dispatcher_func) on_irc_client_ready, c);
} }
} }
@ -1283,6 +1284,7 @@ irc_listen (struct server_context *ctx, struct error **e)
return false; return false;
} }
set_blocking (sockfd, false);
ctx->listen_fd = sockfd; ctx->listen_fd = sockfd;
poller_set (&ctx->poller, ctx->listen_fd, POLLIN, poller_set (&ctx->poller, ctx->listen_fd, POLLIN,
(poller_dispatcher_func) on_irc_client_available, ctx); (poller_dispatcher_func) on_irc_client_available, ctx);