Changeset 247

Show
Ignore:
Timestamp:
10/22/04 21:52:20 (9 years ago)
Author:
alexcv
Message:

Thread safe version of gethostbyname()

Location:
trunk/wifidog
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r246 r247  
    11# $Header$ 
     22004-10-22 Alexandre Carmel-Veilleux <acv@acv.ca> 
     3        * src/various: Added wd_gethostbyname, a thread-safe (serialized) 
     4          version of gethostbyname. 
     5 
    262004-10-15 Alexandre Carmel-Veilleux <acv@acv.ca> 
    37        * src/auth.c: Fixed hard coded port. 
  • trunk/wifidog/src/centralserver.c

    r241 r247  
    3434#include <errno.h> 
    3535#include <unistd.h> 
    36 #include <netdb.h> 
    3736#include <string.h> 
    3837#include <syslog.h> 
     
    4039#include "common.h" 
    4140 
     41#include "util.h" 
    4242#include "auth.h" 
    4343#include "conf.h" 
     
    6161        size_t  numbytes, totalbytes; 
    6262        char buf[MAX_BUF]; 
    63         struct hostent *he; 
     63        struct in_addr *h_addr; 
    6464        struct sockaddr_in their_addr; 
    6565        char *tmp; 
     
    7878        done = 0; 
    7979        while (!done && ((auth_server = get_auth_server()) != NULL)) { 
    80                 if ((he = gethostbyname(auth_server->authserv_hostname)) == NULL) { 
     80                if ((h_addr = wd_gethostbyname(auth_server->authserv_hostname)) == NULL) { 
    8181                        debug(LOG_ERR, "Failed to resolve %s via gethostbyname" 
    8282                                "(): %s", auth_server->authserv_hostname,  
     
    8888                their_addr.sin_family = AF_INET; 
    8989                their_addr.sin_port = htons(auth_server->authserv_http_port); 
    90                 their_addr.sin_addr = *((struct in_addr *)he->h_addr); 
     90                their_addr.sin_addr = *h_addr; 
    9191                memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
    9292 
     
    104104                                mark_auth_server_bad(auth_server); 
    105105                                debug(LOG_ERR, "Aborting request"); 
     106                                free(h_addr); 
    106107                                close(sockfd); 
    107108                                return(-1); /* non-fatal */ 
     
    110111                        done = 1; 
    111112                } 
     113                free(h_addr); 
    112114        } 
    113115        /** 
  • trunk/wifidog/src/ping_thread.c

    r239 r247  
    9393        t_auth_serv             *auth_server; 
    9494        char                    request[MAX_BUF]; 
    95         struct hostent          *he; 
     95        struct in_addr          *h_addr; 
    9696        struct sockaddr_in      their_addr; 
    9797 
     
    108108                        auth_server->authserv_hostname); 
    109109         
    110         if ((he = gethostbyname(auth_server->authserv_hostname)) == NULL) { 
     110        if ((h_addr = wd_gethostbyname(auth_server->authserv_hostname)) == NULL) { 
    111111                debug(LOG_ERR, "Failed to resolve %s via gethostbyname" 
    112112                                "(): %s", auth_server->authserv_hostname,  
     
    120120        their_addr.sin_family = AF_INET; 
    121121        their_addr.sin_port = htons(auth_server->authserv_http_port); 
    122         their_addr.sin_addr = *((struct in_addr *)he->h_addr); 
     122        their_addr.sin_addr = *h_addr; 
    123123        memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
    124124 
     
    133133                mark_auth_server_bad(auth_server); 
    134134                close(sockfd); 
     135                free(h_addr); 
    135136                return; 
    136137        } 
     138        free(h_addr); 
    137139                 
    138140        snprintf(request, sizeof(request) - 1, "GET %sping/?gw_id=%s HTTP/1.0\n" 
  • trunk/wifidog/src/util.c

    r242 r247  
    2020 
    2121/* 
    22  * $Header: /cvsroot/wifidog/wifidog/src/firewall.c,v 1.32 2004/04/23 
    23  * 11:37:43 aprilp Exp $ 
     22 * $Header$ 
    2423 */ 
    2524/** 
     
    4140 
    4241#include <string.h> 
     42#include <pthread.h> 
     43#include <netdb.h> 
    4344 
    4445#include "util.h" 
    4546#include "conf.h" 
    4647#include "debug.h" 
     48 
     49static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER; 
    4750 
    4851/** Fork a child and execute a shell command, the parent 
     
    8285    return (WEXITSTATUS(status)); 
    8386} 
     87 
     88struct in_addr * 
     89wd_gethostbyname(const char *name) 
     90{ 
     91        struct hostent *he; 
     92        struct in_addr *h_addr, *in_addr_temp; 
     93 
     94        /* XXX Calling function is reponsible for free() */ 
     95 
     96        h_addr = (struct in_addr *)malloc(sizeof(struct in_addr)); 
     97         
     98        if (h_addr == NULL) 
     99                return NULL; 
     100         
     101        pthread_mutex_lock(&ghbn_mutex); 
     102 
     103        he = gethostbyname(name); 
     104 
     105        if (he == NULL) { 
     106                free(h_addr); 
     107                pthread_mutex_unlock(&ghbn_mutex); 
     108                return NULL; 
     109        } 
     110 
     111        in_addr_temp = (struct in_addr *)he->h_addr_list[0]; 
     112        h_addr->s_addr = in_addr_temp->s_addr; 
     113         
     114        pthread_mutex_unlock(&ghbn_mutex); 
     115 
     116        return h_addr; 
     117} 
  • trunk/wifidog/src/util.h

    r136 r247  
    3131 */ 
    3232int execute(char *cmd_line, int quiet); 
     33struct in_addr *wd_gethostbyname(const char *name); 
    3334 
    3435#endif /* _UTIL_H_ */