root/trunk/wifidog/src/util.c @ 422

Revision 422, 4.1 KB (checked in by aprilp, 8 years ago)

* Ping the users everytime we check their counters, that way we keep them alive
* Optional ExternalInterface?
* Optional GatewayAddress? (we discover it. finally.)
* We check for the traffic from the clients to the firewall, to catch the traffic the icmp ping is generating
* Bumped to alpha7

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/********************************************************************\
2 * This program is free software; you can redistribute it and/or    *
3 * modify it under the terms of the GNU General Public License as   *
4 * published by the Free Software Foundation; either version 2 of   *
5 * the License, or (at your option) any later version.              *
6 *                                                                  *
7 * This program is distributed in the hope that it will be useful,  *
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
10 * GNU General Public License for more details.                     *
11 *                                                                  *
12 * You should have received a copy of the GNU General Public License*
13 * along with this program; if not, contact:                        *
14 *                                                                  *
15 * Free Software Foundation           Voice:  +1-617-542-5942       *
16 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
17 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
18 *                                                                  *
19 \********************************************************************/
20
21/*
22 * $Header$
23 */
24/**
25  @file util.c
26  @brief Misc utility functions
27  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
28 */
29
30#define _GNU_SOURCE
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <syslog.h>
35#include <errno.h>
36#include <pthread.h>
37#include <sys/wait.h>
38#include <sys/types.h>
39#include <sys/unistd.h>
40#include <netinet/in.h>
41#include <sys/ioctl.h>
42#include <net/if.h>
43
44#include <string.h>
45#include <pthread.h>
46#include <netdb.h>
47
48#include "util.h"
49#include "conf.h"
50#include "debug.h"
51
52static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
53
54/** Fork a child and execute a shell command, the parent
55 * process waits for the child to return and returns the child's exit()
56 * value.
57 * @return Return code of the command
58 */
59int
60execute(char *cmd_line, int quiet)
61{
62    int pid,
63        status,
64        rc;
65
66    const char *new_argv[4];
67    new_argv[0] = "/bin/sh";
68    new_argv[1] = "-c";
69    new_argv[2] = cmd_line;
70    new_argv[3] = NULL;
71
72    if ((pid = fork()) < 0) {    /* fork a child process           */
73        debug(LOG_ERR, "fork(): %s", strerror(errno));
74        exit(1);
75    } else if (pid == 0) {    /* for the child process:         */
76        /* We don't want to see any errors if quiet flag is on */
77        if (quiet) close(2);
78        if (execvp("/bin/sh", (char *const *)new_argv) < 0) {    /* execute the command  */
79            debug(LOG_ERR, "execvp(): %s", strerror(errno));
80            exit(1);
81        }
82    } else {        /* for the parent:      */
83        do {
84            rc = wait(&status);
85        } while (rc != pid && rc != -1);    /* wait for completion  */
86    }
87
88    return (WEXITSTATUS(status));
89}
90
91struct in_addr *
92wd_gethostbyname(const char *name)
93{
94        struct hostent *he;
95        struct in_addr *h_addr, *in_addr_temp;
96
97        /* XXX Calling function is reponsible for free() */
98
99        h_addr = (struct in_addr *)malloc(sizeof(struct in_addr));
100       
101        if (h_addr == NULL)
102                return NULL;
103       
104        LOCK_GHBN();
105
106        he = gethostbyname(name);
107
108        if (he == NULL) {
109                free(h_addr);
110                UNLOCK_GHBN();
111                return NULL;
112        }
113
114        in_addr_temp = (struct in_addr *)he->h_addr_list[0];
115        h_addr->s_addr = in_addr_temp->s_addr;
116       
117        UNLOCK_GHBN();
118
119        return h_addr;
120}
121
122char *get_iface_ip(char *ifname) {
123    struct ifreq if_data;
124    struct in_addr in;
125    int sockd;
126    u_int32_t ip;
127
128    /* Create a socket */
129    if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) {
130        debug(LOG_ERR, "socket(): %s", strerror(errno));
131        return NULL;
132    }
133
134    /* Get IP of internal interface */
135    strcpy (if_data.ifr_name, ifname);
136
137    /* Get the IP address */
138    if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) {
139        debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno));
140        return NULL;
141    }
142    memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4);
143    in.s_addr = ip;
144
145    return (char *)inet_ntoa(in);
146}
Note: See TracBrowser for help on using the browser.