Changeset 253

Show
Ignore:
Timestamp:
12/18/04 22:45:57 (4 years ago)
Author:
morris
Message:

Link with libzanka

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trackerd/trunk/Makefile.in

    r221 r253  
    1 SUBDIRS                                 = trackerd 
    2 DISTFILES                               = INSTALL LICENSE README Makefile Makefile.in config.guess config.h.in config.sub configure configure.in install-sh man run trackerd 
     1SUBDIRS                                 = libzanka trackerd 
     2DISTFILES                               = INSTALL LICENSE README Makefile Makefile.in config.guess config.h.in config.sub configure configure.in install-sh libzanka man run trackerd 
    33 
    44WT_PACKAGE_VERSION              = @WT_PACKAGE_VERSION@ 
  • trackerd/trunk/configure.in

    r236 r253  
    371371AC_CONFIG_FILES([Makefile]) 
    372372AC_CONFIG_FILES([trackerd/Makefile]) 
     373AC_CONFIG_SUBDIRS([libzanka]) 
    373374 
    374375AC_OUTPUT 
  • trackerd/trunk/trackerd/Makefile.in

    r252 r253  
    1 OBJECTS                                 = banlist.o commands.o main.o settings.o tracker.o utility.o 
     1OBJECTS                                 = banlist.o clients.o commands.o main.o servers.o settings.o tracker.o 
    22 
    33WT_PACKAGE_VERSION              = @WT_PACKAGE_VERSION@ 
     
    3535CFLAGS                                  = @CFLAGS@ 
    3636CPPFLAGS                                = @CPPFLAGS@ 
    37 LDFLAGS                                 = @LDFLAGS@ 
    38 LIBS                                    = @LIBS@ 
    39 INCLUDES                                = -I$(top_srcdir) -I$(srcdir) 
     37LDFLAGS                                 = -L$(top_srcdir)/libzanka/lib @LDFLAGS@ 
     38LIBS                                    = -lzanka @LIBS@ 
     39INCLUDES                                = -I$(top_srcdir)/libzanka/include -I$(top_srcdir) -I$(srcdir) 
    4040 
    4141COMPILE                                 = $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) 
     
    4747all: trackerd trackerdctl trackerd.conf 
    4848 
    49 trackerd: $(OBJECTS) 
    50         $(RM) -f $@ 
     49trackerd: $(OBJECTS) $(top_srcdir)/libzanka/lib/libzanka.a 
    5150        $(LINK) $(OBJECTS) $(LIBS) 
    52         $(CP) trackerd ../run/ 
     51        $(MKDIR) -p $(top_srcdir)/run/ 
     52        $(CP) $@ $(top_srcdir)/run/ 
    5353 
    54 trackerdctl: trackerd 
    55         $(SED) -e 's,@prefix\@,$(prefix),g' trackerdctl.in > trackerdctl 
    56         $(CHMOD) +x trackerdctl 
    57         $(CP) trackerdctl ../run/ 
     54trackerdctl: 
     55        $(SED) -e 's,@prefix\@,$(prefix),g' $@.in > $@ 
     56        $(CHMOD) +x $@ 
     57        $(MKDIR) -p $(top_srcdir)/run/ 
     58        $(CP) $@ $(top_srcdir)/run/ 
    5859 
    5960trackerd.conf: 
    60         $(SED) -e 's,@WT_USER\@,$(WT_USER),g' -e 's,@WT_GROUP\@,$(WT_GROUP),g' trackerd.conf.in > trackerd.conf 
    61         $(MKDIR) -p ../run/etc/ 
    62         $(CP) trackerd.conf ../run/etc/ 
     61        $(SED) -e 's,@WT_USER\@,$(WT_USER),g' -e 's,@WT_GROUP\@,$(WT_GROUP),g' $@.in > $@ 
     62        $(MKDIR) -p $(top_srcdir)/run/etc/ 
     63        $(CP) $@ $(top_srcdir)/run/etc/ 
    6364         
    6465.c.o: 
     
    7071distclean: clean 
    7172        $(RM) -f Makefile config.* 
    72         $(RM) -f ../run/trackerd ../run/trackerdctl ../run/etc/trackerd.conf 
     73        $(RM) -f $(top_srcdir)/run/trackerd $(top_srcdir)/run/trackerdctl $(top_srcdir)/run/etc/trackerd.conf 
  • trackerd/trunk/trackerd/banlist.c

    r231 r253  
    1 /* $Id: banlist.c,v 1.4 2004/09/09 09:37:56 morris Exp $ */ 
     1/* $Id: banlist.c,v 1.5 2004/12/18 21:45:32 morris Exp $ */ 
    22 
    33/* 
     
    2929#include "config.h" 
    3030 
    31 #include <sys/param.h> 
    32 #include <sys/types.h> 
    3331#include <stdio.h> 
    34 #include <stdlib.h> 
    3532#include <string.h> 
    36 #include <unistd.h> 
    37 #include <syslog.h> 
    38 #include <errno.h> 
    39 #include <math.h> 
     33#include <pthread.h> 
     34#include <zanka/za-ip.h> 
     35#include <zanka/za-log.h> 
    4036 
    4137#include "banlist.h" 
    4238#include "settings.h" 
    43 #include "utility.h" 
     39 
     40 
     41pthread_mutex_t                 wt_banlist_mutex = PTHREAD_MUTEX_INITIALIZER; 
    4442 
    4543 
     
    4947        bool            banned = false; 
    5048 
    51         /* check the banlist if we're configured to do so */ 
     49        pthread_mutex_lock(&wt_banlist_mutex); 
     50 
    5251        if(strlen(wt_settings.banlist) > 0) { 
    53  
    5452                /* open the banlist file */ 
    5553                fp = fopen(wt_settings.banlist, "r"); 
    5654 
    5755                if(!fp) { 
    58                         wt_log_err("Could not open %s: %s", 
     56                        za_log_err("Could not open %s: %s", 
    5957                                wt_settings.banlist, strerror(errno)); 
    6058 
     
    6361                 
    6462                while(fgets(buffer, sizeof(buffer), fp) != NULL) { 
    65                         /* remove the linebreak if any */ 
    6663                        if((p = strchr(buffer, '\n')) != NULL) 
    6764                                *p = '\0'; 
    6865         
    69                         /* ignore comments */ 
    7066                        if(buffer[0] == '#' || buffer[0] == '\0') 
    7167                                continue; 
    7268                         
    73                         if(strchr(buffer, '*')) { 
    74                                 /* test wildcard string */ 
    75                                 if(wt_ip_matches_wildcard(ip, buffer)) { 
    76                                         banned = true; 
    77                                          
    78                                         break; 
    79                                 } 
    80                         } 
    81                         else if(strchr(buffer, '/')) { 
    82                                 /* test netmask string */ 
    83                                 if(wt_ip_matches_netmask(ip, buffer)) { 
    84                                         banned = true; 
    85                                                  
    86                                         break; 
    87                                 } 
    88                         } else { 
    89                                 /* test absolute string */ 
    90                                 if(strcmp(ip, buffer) == 0) { 
    91                                         banned = true; 
    92                                          
    93                                         break; 
    94                                 } 
     69                        if(za_ip_match(ip, buffer)) { 
     70                                banned = true; 
     71                                 
     72                                break; 
    9573                        } 
    9674                } 
     
    10078 
    10179end: 
     80        /* clean up */ 
     81        pthread_mutex_unlock(&wt_banlist_mutex); 
     82 
    10283        return banned; 
    10384} 
    104  
    105  
    106  
    107 #pragma mark - 
    108  
    109 bool wt_ip_matches_wildcard(char *ip, char *match) { 
    110         char    *i, *ii, *m, *mm, *p1, *p2; 
    111         int             matches = 0; 
    112          
    113         ii = i = strdup(ip); 
    114         mm = m = strdup(match); 
    115          
    116         while((p1 = strsep(&ii, ".")) && (p2 = strsep(&mm, "."))) { 
    117                 if(strcmp(p1, p2) == 0 || strcmp(p2, "*") == 0) 
    118                         matches++; 
    119         } 
    120  
    121         free(i); 
    122         free(m); 
    123  
    124         return matches == 4 ? true : false; 
    125 } 
    126  
    127  
    128  
    129 bool wt_ip_matches_netmask(char *ip, char *match) { 
    130         char            *m, *mm, *block, *netmask; 
    131         unsigned int    ip_u, block_u, netmask_u; 
    132         unsigned long   mask; 
    133  
    134         ip_u = wt_iptou(ip); 
    135         mm = m = strdup(match); 
    136         block = strsep(&mm, "/"); 
    137         block_u  = wt_iptou(block); 
    138         netmask = strsep(&mm, "/"); 
    139  
    140         if(wt_strtoul(netmask, &mask) > 0) 
    141                 netmask_u = pow(2.0, 32.0) - pow(2.0, 32.0 - mask); 
    142         else 
    143                 netmask_u = wt_iptou(netmask); 
    144  
    145         free(m); 
    146  
    147         return ((ip_u & netmask_u) == (block_u & netmask_u)); 
    148 } 
  • trackerd/trunk/trackerd/banlist.h

    r231 r253  
    1 /* $Id: banlist.h,v 1.4 2004/09/09 09:37:56 morris Exp $ */ 
     1/* $Id: banlist.h,v 1.5 2004/12/18 21:45:32 morris Exp $ */ 
    22 
    33/* 
     
    3030#define WT_BANLIST_H 1 
    3131 
    32 #include <time.h> 
    33  
    34 #include "main.h" 
    35  
     32#include <stdbool.h> 
    3633 
    3734bool                                                    wt_ip_is_banned(char *); 
    3835 
    39 bool                                                    wt_ip_matches_wildcard(char *, char *); 
    40 bool                                                    wt_ip_matches_netmask(char *, char *); 
    41  
    4236#endif /* WT_BANLIST_H */ 
  • trackerd/trunk/trackerd/commands.c

    r248 r253  
    1 /* $Id: commands.c,v 1.24 2004/11/24 15:46:22 morris Exp $ */ 
     1/* $Id: commands.c,v 1.25 2004/12/18 21:45:32 morris Exp $ */ 
    22 
    33/* 
     
    3636#include <string.h> 
    3737#include <unistd.h> 
    38 #include <syslog.h> 
    3938#include <errno.h> 
    4039#include <ctype.h> 
     
    4847#include <openssl/rand.h> 
    4948#include <openssl/sha.h> 
    50 #include <openssl/err.h> 
     49#include <openssl/ssl.h> 
     50#include <zanka/za-argv.h> 
     51#include <zanka/za-functions.h> 
     52#include <zanka/za-macros.h> 
     53#include <zanka/za-log.h> 
     54#include <zanka/za-socket.h> 
     55#include <zanka/za-string.h> 
    5156 
    5257#include "banlist.h" 
    5358#include "commands.h" 
    5459#include "main.h" 
     60#include "servers.h" 
    5561#include "settings.h" 
    5662#include "tracker.h" 
    57 #include "utility.h" 
    58  
    59  
    60 wt_commands_t           wt_commands[] = { 
    61         { "CATEGORIES",         WT_CLIENT_STATE_SAID_HELLO,             0,              wt_cmd_categories }, 
    62         { "CLIENT",                     WT_CLIENT_STATE_SAID_HELLO,             1,              wt_cmd_client }, 
    63         { "HELLO",                      WT_CLIENT_STATE_CONNECTED,              0,              wt_cmd_hello }, 
    64         { "REGISTER",           WT_CLIENT_STATE_SAID_HELLO,             5,              wt_cmd_register }, 
    65         { "SERVERS",            WT_CLIENT_STATE_SAID_HELLO,             0,              wt_cmd_servers }, 
     63 
     64 
     65pthread_mutex_t                 wt_categories_mutex = PTHREAD_MUTEX_INITIALIZER; 
     66 
     67wt_commands_t                   wt_commands[] = { 
     68        { "CATEGORIES",  
     69          WT_CLIENT_STATE_SAID_HELLO,           0,              wt_cmd_categories }, 
     70        { "CLIENT",              
     71          WT_CLIENT_STATE_SAID_HELLO,           1,              wt_cmd_client }, 
     72        { "HELLO",               
     73          WT_CLIENT_STATE_CONNECTED,            0,              wt_cmd_hello }, 
     74        { "REGISTER",    
     75          WT_CLIENT_STATE_SAID_HELLO,           5,              wt_cmd_register }, 
     76        { "SERVERS",     
     77          WT_CLIENT_STATE_SAID_HELLO,           0,              wt_cmd_servers }, 
    6678}; 
    6779 
     
    7587        bool                            error = false; 
    7688 
    77         /* associate the struct with this thread */ 
    78         pthread_setspecific(wt_client_key, client); 
    79  
    80         /* go client */ 
     89        wt_client_set(client); 
     90 
    8191        while(client->state <= WT_CLIENT_STATE_SAID_HELLO) { 
    8292                if(client->buffer_offset == 0) { 
     
    8494                                FD_ZERO(&rfds); 
    8595                                FD_SET(client->sd, &rfds); 
    86                                 wt_dtotv(0.1, &tv); 
     96                                tv = za_dtotv(0.1); 
    8797                                state = select(client->sd + 1, &rfds, NULL, NULL, &tv); 
    8898                        } while(state == 0 && client->state <= WT_CLIENT_STATE_SAID_HELLO); 
     
    99109                                } else { 
    100110                                        /* error in TCP communication */ 
    101                                         wt_log_err("Could not read from %s: %s", 
     111                                        za_log_err("Could not read from %s: %s", 
    102112                                                client->ip, strerror(errno)); 
    103113         
     
    108118                } 
    109119                 
    110                 /* read from SSL */ 
    111120                bytes = SSL_read(client->ssl, buf, sizeof(buf)); 
    112121 
     
    117126                else if(bytes < 0) { 
    118127                        /* error in SSL communication */ 
    119                         wt_log_err("Could not read from %s: %s", 
    120                                 client->ip, wt_ssl_strerror()); 
     128                        za_log_err("Could not read from %s: %s", 
     129                                client->ip, za_socket_error()); 
    121130 
    122131                        error = true; 
     
    138147                 
    139148                if(client->buffer[client->buffer_offset - 1] == WT_MESSAGE_SEPARATOR) { 
    140                         /* chomp separator */ 
    141149                        client->buffer[client->buffer_offset - 1] = '\0'; 
    142150                 
    143                         /* parse buffer */ 
    144151                        wt_parse_command(client->buffer); 
    145152                         
    146                         /* reset buffer */ 
    147153                        memset(client->buffer, 0, client->buffer_size); 
    148154                        client->buffer_offset = 0; 
     
    152158        /* close SSL socket */ 
    153159        if(client->ssl) 
    154                 wt_close(client->ssl); 
     160                za_socket_close(client->ssl); 
    155161 
    156162        /* delete client */ 
    157163        if(client) { 
    158                 wt_log_info("Disconnect from %s", client->ip); 
    159  
    160                 free(client->buffer); 
    161                 free(client); 
     164                za_log_info("Disconnect from %s", client->ip); 
     165 
     166                wt_client_dealloc(client); 
    162167        } 
    163168 
     
    170175 
    171176void wt_parse_command(char *buffer) { 
    172         wt_client_t             *client = (wt_client_t *) pthread_getspecific(wt_client_key); 
     177        wt_client_t             *client = wt_client(); 
     178        za_argv_t               *argv; 
    173179        char                    *start, *command = NULL, *arg = NULL; 
    174         char                    **argv = NULL; 
    175         int                             index, argc = 0; 
    176          
    177         /* loop over the command */ 
     180        int                             index; 
     181 
     182        argv = za_argv_alloc(); 
     183         
     184        /* get command */ 
    178185        start = buffer; 
    179186 
     
    181188                buffer++; 
    182189         
    183         /* get command */ 
    184190        command = (char *) malloc((size_t) (buffer - start + 1)); 
    185191        memcpy(command, start, (size_t) (buffer - start)); 
     
    195201        } 
    196202         
    197         /* loop over argument string */ 
     203        /* get argument */ 
    198204        start = buffer; 
    199205         
     
    202208 
    203209        if(isspace(*start)) { 
    204                 /* get argument */ 
    205210                arg = (char *) malloc(buffer - start + 2); 
    206211                memcpy(arg, start + 1, buffer - start); 
    207212                arg[buffer - start + 1] = '\0'; 
    208213         
    209                 /* get argument vector */ 
    210                 wt_argv_create_wired(arg, &argc, &argv); 
     214                za_argv_init_with_wired_string(argv, arg); 
    211215        } 
    212216 
     
    216220 
    217221        /* verify arg */ 
    218         if(argc < wt_commands[index].args) { 
     222        if(ZA_ARGV_ARGC(argv) < wt_commands[index].args) { 
    219223                wt_reply(503, "Syntax Error"); 
    220224         
     
    223227         
    224228        /* go server command */ 
    225         ((*wt_commands[index].action) (argc, argv)); 
     229        ((*wt_commands[index].action) (ZA_ARGV_ARGC(argv), ZA_ARGV_ARGV(argv))); 
    226230 
    227231end: 
     
    233237                free(arg); 
    234238         
    235         if(argc > 0) 
    236                wt_argv_free(argc, argv); 
     239        za_argv_release(argv); 
     240        za_argv_dealloc(argv); 
    237241} 
    238242 
     
    243247         
    244248        min = 0; 
    245         max = ARRAY_SIZE(wt_commands) - 1; 
     249        max = ZA_ARRAY_SIZE(wt_commands) - 1; 
    246250         
    247251        do { 
     
    273277         
    274278        /* open categories file */ 
     279        pthread_mutex_lock(&wt_categories_mutex); 
    275280        fp = fopen(wt_settings.categories, "r"); 
    276281         
    277282        if(!fp) { 
    278283                wt_reply(500, "Command Failed"); 
    279                 wt_log_err("Could not open %s: %s", 
     284                za_log_err("Could not open %s: %s", 
    280285                        wt_settings.categories, strerror(errno)); 
    281286                 
     
    283288        } 
    284289 
    285         /* read file */ 
    286290        while(fgets(buffer, sizeof(buffer), fp) != NULL) { 
    287                 /* remove the linebreak if any */ 
    288291                if((p = strchr(buffer, '\n')) != NULL) 
    289292                        *p = '\0'; 
    290293                 
    291                 /* ignore comments */ 
    292294                if(buffer[0] == '#' || buffer[0] == '\0') 
    293295                        continue; 
    294296                         
    295                 /* reply 710 */ 
    296297                wt_reply(710, "%s", buffer); 
    297298        } 
    298299         
    299         /* reply 711 */ 
    300300        wt_reply(711, "Done"); 
    301301 
    302302end: 
     303        /* clean up */ 
    303304        if(fp) 
    304305                fclose(fp); 
     306 
     307        pthread_mutex_unlock(&wt_categories_mutex); 
    305308} 
    306309 
     
    312315 
    313316void wt_cmd_client(int argc, char **argv) { 
    314         wt_client_t             *client = (wt_client_t *) pthread_getspecific(wt_client_key); 
    315  
    316         /* copy version string */ 
     317        wt_client_t             *client = wt_client(); 
     318 
    317319        strlcpy(client->version, argv[0], sizeof(client->version)); 
    318320} 
     
    325327 
    326328void wt_cmd_hello(int argc, char **argv) { 
    327         wt_client_t             *client = (wt_client_t *) pthread_getspecific(wt_client_key); 
    328         char                    start_time[26]; 
    329          
    330         /* check state */ 
     329        wt_client_t             *client = wt_client(); 
     330        char                    date[ZA_TIME_SIZE]; 
     331         
    331332        if(client->state != WT_CLIENT_STATE_CONNECTED) 
    332333                return; 
     
    335336        if(wt_ip_is_banned(client->ip)) { 
    336337                wt_reply(511, "Banned"); 
    337                 wt_log_err("Connection from %s denied, host is banned", 
     338                za_log_err("Connection from %s denied, host is banned", 
    338339                        client->ip); 
    339340                 
    340341                client->state = WT_CLIENT_STATE_DISCONNECTED; 
    341342 
    342                 if(SSL_shutdown(client->ssl) == 0) 
    343                         SSL_shutdown(client->ssl); 
    344          
    345                 SSL_free(client->ssl); 
    346                 client->ssl = NULL; 
    347          
    348                 close(client->sd); 
    349                 client->sd = -1; 
    350                  
    351343                return; 
    352344        } 
    353345 
    354         /* format time string */ 
    355         wt_time_to_iso8601(wt_start_time, start_time, sizeof(start_time)); 
    356                  
    357346        /* reply a 200 */ 
    358347        wt_reply(200, "%s%c%s%c%s%c%s%c%s", 
     
    365354                         wt_settings.description, 
    366355                         WT_FIELD_SEPARATOR, 
    367                          start_time); 
     356                         za_time_to_iso8601_string(wt_start_time, date)); 
    368357 
    369358        /* elevate state */ 
     
    378367 
    379368void wt_cmd_register(int argc, char **argv) { 
    380         wt_client_t                             *client = (wt_client_t *) pthread_getspecific(wt_client_key); 
    381         wt_server_t                             *server; 
    382         wt_address_t                    *address = NULL, *address_each, *address_next; 
    383         struct sockaddr_storage ss; 
    384         struct addrinfo                 *ai0, *ai; 
    385         struct sockaddr_in              *sa_in; 
    386         struct sockaddr_in6             *sa_in6; 
    387         SHA_CTX                                 c; 
    388         static unsigned char    hex[] = "0123456789abcdef"; 
    389         unsigned char                   buffer[BUFSIZ], sha[SHA_DIGEST_LENGTH]; 
    390         unsigned int                    bandwidth; 
    391         char                                    host[WT_HOST_SIZE], url[WT_URL_SIZE], name[WT_IP_SIZE]; 
    392         int                                             i, length, port = 0, err; 
    393         bool                                    failed = false, lookup, reverselookup; 
     369        wt_client_t                                     *client = wt_client(); 
     370        wt_server_t                                     *server; 
     371        struct sockaddr_storage         ss; 
     372        struct addrinfo                         *aiap, *aip; 
     373        struct sockaddr_in                      *sa_in; 
     374        struct sockaddr_in6                     *sa_in6; 
     375        unsigned char                           buffer[BUFSIZ]; 
     376        unsigned long                           bandwidth; 
     377        char                                            host[ZA_HOST_SIZE], url[ZA_URL_SIZE], name[ZA_ADDRESS_SIZE]; 
     378        int                                                     length, port = 0, err; 
     379        bool                                            failed = false, lookup, reverselookup; 
    394380 
    395381        /* extract host:port from url */ 
    396         if(wt_extract_url(argv[1], host, &port) < 0) { 
     382        if(za_url_get_host_port(argv[1], host, &port) < 0) { 
    397383                if(wt_settings.strictlookup) { 
    398384                        wt_reply(503, "Syntax Error"); 
    399                         wt_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
     385                        za_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
    400386                                client->ip, argv[2], argv[1], "URL parse failed"); 
    401387                         
     
    420406                if(wt_settings.strictlookup) { 
    421407                        wt_reply(500, "Command Failed"); 
    422                         wt_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
     408                        za_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
    423409                                client->ip, argv[2], url, strerror(errno)); 
    424410 
     
    445431                        if(wt_settings.strictlookup) { 
    446432                                wt_reply(516, "Permission Denied"); 
    447                                 wt_log_warn("Register from %s as \"%s\" URL %s denied: %s", 
     433                                za_log_warn("Register from %s as \"%s\" URL %s denied: %s", 
    448434                                        client->ip, argv[2], url, "IP mismatch"); 
    449435                                 
     
    460446                        if(wt_settings.strictlookup) { 
    461447                                wt_reply(503, "Syntax Error"); 
    462                                 wt_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
     448                                za_log_warn("Register from %s as \"%s\" URL %s aborted: %s", 
    463449                                        client->ip, argv[2], url, "Syntax error"); 
    464450                                 
     
    474460                        lookup = false; 
    475461 
    476                         if(getaddrinfo(host, NULL, NULL, &ai0) == 0) { 
     462                        if(getaddrinfo(host, NULL, NULL, &aiap) == 0) { 
    477463                                /* may get several addresses back, okay if any matches */ 
    478                                 for(ai = ai0; ai; ai = ai->ai_next) { 
    479                                         if(getnameinfo(ai->ai_addr, ai->ai_addrlen, 
    480                                                                    name, sizeof(name), NULL, 0, NI_NUMERICHOST) == 0) { 
     464                                for(aip = aiap; aip; aip = aip->ai_next) { 
     465                                        if(getnameinfo(aip->ai_addr, aip->ai_addrlen, 
     466                                                                   name, sizeof(name), NULL, 0, 
     467                                                                   NI_NUMERICHOST) == 0) { 
    481468                                                if(strcmp(name, client->ip) == 0) { 
    482469                                                        /* pass test */ 
     
    488475                                } 
    489476 
     477                                freeaddrinfo(aiap); 
     478 
    490479                                if(!lookup) { 
    491480                                        /* test failed */ 
    492481                                        if(wt_settings.strictlookup) { 
    493482                                                wt_reply(516, "Permission Denied"); 
    494                                                 wt_log_warn("Register from %s as \"%s\" URL %s denied: %s", 
     483                                                za_log_warn("Register from %s as \"%s\" URL %s denied: %s", 
    495484                                                        client->ip, argv[2], url, "Lookup failed"); 
    496485                                                 
     
    501490                                        goto failed; 
    502491                                } 
    503  
    504                                 freeaddrinfo(ai0); 
    505492                        } 
    506493                } 
     
    522509                                if(wt_settings.strictlookup) { 
    523510                                        wt_reply(516, "Permission Denied"); 
    524                                         wt_log_warn("Register from %s as \"%s\" URL % denied: %s", 
     511                                        za_log_warn("Register from %s as \"%s\" URL % denied: %s", 
    525512                                                client->ip, argv[2], url, "Reverse lookup failed"); 
    526513                                                 
     
    541528 
    542529                if(port != WT_SERVER_PORT) { 
    543                         wt_log_info("Adjusting URL from %s to wired://%s:%d/", 
     530                        za_log_info("Adjusting URL from %s to wired://%s:%d/", 
    544531                                url, client->ip, port); 
    545532                        snprintf(url, sizeof(url), "wired://%s:%d/", client->ip, port); 
    546533                } else { 
    547                         wt_log_info("Adjusting URL from %s to wired://%s/", 
     534                        za_log_info("Adjusting URL from %s to wired://%s/", 
    548535                                url, client->ip); 
    549536                        snprintf(url, sizeof(url), "wired://%s/", client->ip); 
     
    552539 
    553540        /* get bandwidth */ 
    554         if(wt_strtoul(argv[3], (unsigned long *) &bandwidth) < 0) { 
     541        if(za_strtoul(argv[3], &bandwidth) < 0) { 
    555542                wt_reply(503, "Syntax Error"); 
    556543 
    557                 goto end
     544                return
    558545        } 
    559546         
     
    561548        if(wt_settings.minbandwidth > 0 && bandwidth < wt_settings.minbandwidth) { 
    562549                wt_reply(516, "Permission Denied"); 
    563                 wt_log_warn("Register from %s as \"%s\" URL %s denied: Bandwidth %.0f Kbps considered too low",  
     550                za_log_warn("Register from %s as \"%s\" URL %s denied: Bandwidth %.0f Kbps considered too low",  
    564551                        client->ip, argv[2], url, bandwidth / 128.0); 
    565552                 
    566                 goto end
     553                return
    567554        } 
    568555         
     
    570557        if(wt_settings.maxbandwidth > 0 && bandwidth > wt_settings.maxbandwidth) { 
    571558                wt_reply(516, "Permission Denied"); 
    572                 wt_log_warn("Register from %s as \"%s\" URL %s denied: Bandwidth %.0f Kbps considered too high", 
     559                za_log_warn("Register from %s as \"%s\" URL %s denied: Bandwidth %.0f Kbps considered too high", 
    573560                        client->ip, argv[2], url, bandwidth / 128.0); 
    574561                 
    575                 goto end
     562                return
    576563        } 
    577564 
    578565        /* is there an existing server from this host? */ 
    579         server = wt_get_server(NULL, client->ip); 
     566        server = wt_server_with_ip(client->ip); 
    580567         
    581568        if(server) { 
     
    583570                if(server->port != port && !wt_settings.allowmultiple) { 
    584571                        wt_reply(516, "Permission Denied"); 
    585                         wt_log_warn("Register from %s as \"%s\" URL %s denied: A server from the same address is already registered",  
     572                        za_log_warn("Register from %s as \"%s\" URL %s denied: A server from the same address is already registered",  
    586573                                client->ip, argv[2], url); 
    587574                                 
     
    589576                } 
    590577        } else { 
    591                 /* create a new server */ 
    592                 server = (wt_server_t *) malloc(sizeof(wt_server_t)); 
    593                 memset(server, 0, sizeof(wt_server_t)); 
    594  
    595                 /* add it to the server list */ 
    596                 WT_LIST_LOCK(wt_servers); 
    597                 wt_list_add(&wt_servers, server); 
    598                 WT_LIST_UNLOCK(wt_servers); 
    599  
    600                 /* update status */ 
     578                /* add new server to list */ 
     579                server = wt_server_alloc(); 
     580                ZA_LIST_LOCK(&wt_servers); 
     581                za_list_add_data(&wt_servers, server, (za_dealloc_func_t *) wt_server_dealloc); 
     582                ZA_LIST_UNLOCK(&wt_servers); 
     583 
    601584                pthread_mutex_lock(&wt_status_mutex); 
    602585                wt_current_servers++; 
     
    608591        server->port = port; 
    609592        server->bandwidth = bandwidth; 
    610          
    611         /* copy strings */ 
     593        server->register_time = time(NULL); 
     594         
    612595        strlcpy(server->ip, client->ip, sizeof(server->ip)); 
    613596        strlcpy(server->url, url, sizeof(server->url)); 
     
    615598        strlcpy(server->description, argv[4], sizeof(server->description)); 
    616599         
    617         if(wt_get_category(argv[0])) 
     600        if(wt_category_is_valid(argv[0])) 
    618601                strlcpy(server->category, argv[0], sizeof(server->category)); 
    619602 
    620603        /* compute random hash for transfer */ 
    621604        RAND_bytes(buffer, sizeof(buffer)); 
    622         SHA1_Init(&c); 
    623         SHA1_Update(&c, buffer, sizeof(buffer)); 
    624         SHA1_Final(sha, &c); 
    625          
    626         for(i = 0; i < SHA_DIGEST_LENGTH; i++) { 
    627                 server->hash[i+i]               = hex[sha[i] >> 4]; 
    628                 server->hash[i+i+1]             = hex[sha[i] & 0x0F]; 
    629         } 
    630          
    631         server->hash[i+i] = '\0'; 
    632          
    633         /* timestamp */ 
    634         server->register_time = time(NULL); 
    635  
     605        za_sha1(buffer, server->hash); 
     606         
    636607        /* reply 700 */ 
    637608        wt_reply(700, "%s", server->hash); 
    638          
    639         /* log */ 
    640         wt_log_info("Registered \"%s\" with URL %s", server->name, server->url); 
    641  
    642         /* update servers */ 
     609        za_log_info("Registered \"%s\" with URL %s", server->name, server->url); 
    643610        wt_write_servers(); 
    644  
    645 end: 
    646         /* clean up */ 
    647         if(address) { 
    648                 for(address_each = address; address_each; address_each = address_next) { 
    649                         address_next = address_each->next; 
    650  
    651                         free(address_each); 
    652                 } 
    653         } 
    654611} 
    655612 
     
    661618 
    662619void wt_cmd_servers(int argc, char **argv) { 
    663         wt_list_node_t        *node; 
     620        za_list_node_t        *node; 
    664621        wt_server_t             *server; 
    665622 
    666         /* loop over all servers */ 
    667         WT_LIST_LOCK(wt_servers); 
    668         WT_LIST_FOREACH(wt_servers, node, server) { 
    669                 /* reply 720 */ 
     623        /* reply all servers */ 
     624        ZA_LIST_LOCK(&wt_servers); 
     625        ZA_LIST_FOREACH(&wt_servers, node, server) { 
    670626                wt_reply(720, "%s%c%s%c%s%c%u%c%u%c%u%c%u%c%u%c%llu%c%s", 
    671627                        server->category, 
     
    689645                        server->description); 
    690646        } 
    691         WT_LIST_UNLOCK(wt_servers); 
    692  
    693         /* reply 721 */ 
     647        ZA_LIST_UNLOCK(&wt_servers); 
     648 
    694649        wt_reply(721, "Done"); 
    695650<