Skip to content

Commit b9eb8b3

Browse files
committed
Merge pull request #184 from acv/reorg-util-c
Split util.c into generic and gateway specific parts
2 parents 1e65f1b + 71b5072 commit b9eb8b3

File tree

10 files changed

+322
-250
lines changed

10 files changed

+322
-250
lines changed

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ libgateway_a_SOURCES = commandline.c \
3333
safe.c \
3434
httpd_thread.c \
3535
simple_http.c \
36-
pstring.c
36+
pstring.c \
37+
wd_util.c
3738

3839
noinst_HEADERS = commandline.h \
3940
common.h \
@@ -53,7 +54,8 @@ noinst_HEADERS = commandline.h \
5354
safe.h \
5455
httpd_thread.h \
5556
simple_http.h \
56-
pstring.h
57+
pstring.h \
58+
wd_util.h
5759

5860
wdctl_LDADD = libgateway.a
5961

src/auth.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "firewall.h"
4949
#include "client_list.h"
5050
#include "util.h"
51+
#include "wd_util.h"
5152

5253
/** Launches a thread that periodically checks if any of the connections has timed out
5354
@param arg Must contain a pointer to a string containing the IP adress of the client to check to check

src/centralserver.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "common.h"
4444
#include "safe.h"
4545
#include "util.h"
46+
#include "wd_util.h"
4647
#include "auth.h"
4748
#include "conf.h"
4849
#include "debug.h"
@@ -258,6 +259,7 @@ _connect_auth_server(int level)
258259
/*
259260
* DNS resolving was successful
260261
*/
262+
mark_online();
261263
ip = safe_strdup(inet_ntoa(*h_addr));
262264
debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] succeeded = [%s]", level, hostname, ip);
263265

src/conf.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030

3131
/*@{*/
3232
/** Defines */
33-
/** How many times should we try detecting the interface with the default route
34-
* (in seconds). If set to 0, it will keep retrying forever */
35-
#define NUM_EXT_INTERFACE_DETECT_RETRY 0
36-
/** How often should we try to detect the interface with the default route
37-
* if it isn't up yet (interval in seconds) */
38-
#define EXT_INTERFACE_DETECT_RETRY_INTERVAL 1
3933

4034
/** Defaults configuration values */
4135
#ifndef SYSCONFDIR

src/http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
#include "client_list.h"
5353
#include "common.h"
5454
#include "centralserver.h"
55-
5655
#include "util.h"
56+
#include "wd_util.h"
5757

5858
#include "../config.h"
5959

src/util.c

Lines changed: 13 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
* *
2020
\********************************************************************/
2121

22-
/*
23-
* $Id$
24-
*/
2522
/**
2623
@file util.c
2724
@brief Misc utility functions
@@ -38,12 +35,12 @@
3835
#include <pthread.h>
3936
#include <sys/wait.h>
4037
#include <sys/types.h>
38+
#include <sys/time.h>
4139
#include <sys/unistd.h>
4240
#include <netinet/in.h>
4341
#include <sys/ioctl.h>
4442
#include <arpa/inet.h>
4543

46-
#include <netinet/in.h>
4744
#include <net/if.h>
4845

4946
#include <fcntl.h>
@@ -53,35 +50,34 @@
5350
#include <netpacket/packet.h>
5451

5552
#include <string.h>
56-
#include <pthread.h>
5753
#include <netdb.h>
5854

5955
#include "common.h"
60-
#include "client_list.h"
6156
#include "safe.h"
6257
#include "util.h"
63-
#include "conf.h"
6458
#include "debug.h"
6559
#include "pstring.h"
66-
#include "gateway.h"
67-
#include "commandline.h"
6860

6961
#include "../config.h"
7062

63+
#define LOCK_GHBN() do { \
64+
debug(LOG_DEBUG, "Locking wd_gethostbyname()"); \
65+
pthread_mutex_lock(&ghbn_mutex); \
66+
debug(LOG_DEBUG, "wd_gethostbyname() locked"); \
67+
} while (0)
68+
69+
#define UNLOCK_GHBN() do { \
70+
debug(LOG_DEBUG, "Unlocking wd_gethostbyname()"); \
71+
pthread_mutex_unlock(&ghbn_mutex); \
72+
debug(LOG_DEBUG, "wd_gethostbyname() unlocked"); \
73+
} while (0)
74+
7175
/** @brief FD for icmp raw socket */
7276
static int icmp_fd;
7377

7478
/** @brief Mutex to protect gethostbyname since not reentrant */
7579
static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
7680

77-
/* XXX Do these need to be locked ? */
78-
static time_t last_online_time = 0;
79-
static time_t last_offline_time = 0;
80-
static time_t last_auth_online_time = 0;
81-
static time_t last_auth_offline_time = 0;
82-
83-
long served_this_session = 0;
84-
8581
static unsigned short rand16(void);
8682

8783
/** Fork a child and execute a shell command, the parent
@@ -153,8 +149,6 @@ wd_gethostbyname(const char *name)
153149
return NULL;
154150
}
155151

156-
mark_online();
157-
158152
in_addr_temp = (struct in_addr *)he->h_addr_list[0];
159153
addr->s_addr = in_addr_temp->s_addr;
160154

@@ -285,190 +279,6 @@ get_ext_iface(void)
285279
return NULL;
286280
}
287281

288-
void
289-
mark_online()
290-
{
291-
int before;
292-
int after;
293-
294-
before = is_online();
295-
time(&last_online_time);
296-
after = is_online(); /* XXX is_online() looks at last_online_time... */
297-
298-
if (before != after) {
299-
debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
300-
}
301-
302-
}
303-
304-
void
305-
mark_offline()
306-
{
307-
int before;
308-
int after;
309-
310-
before = is_online();
311-
time(&last_offline_time);
312-
after = is_online();
313-
314-
if (before != after) {
315-
debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
316-
}
317-
318-
/* If we're offline it definately means the auth server is offline */
319-
mark_auth_offline();
320-
321-
}
322-
323-
int
324-
is_online()
325-
{
326-
if (last_online_time == 0 || (last_offline_time - last_online_time) >= (config_get_config()->checkinterval * 2)) {
327-
/* We're probably offline */
328-
return (0);
329-
} else {
330-
/* We're probably online */
331-
return (1);
332-
}
333-
}
334-
335-
void
336-
mark_auth_online()
337-
{
338-
int before;
339-
int after;
340-
341-
before = is_auth_online();
342-
time(&last_auth_online_time);
343-
after = is_auth_online();
344-
345-
if (before != after) {
346-
debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
347-
}
348-
349-
/* If auth server is online it means we're definately online */
350-
mark_online();
351-
352-
}
353-
354-
void
355-
mark_auth_offline()
356-
{
357-
int before;
358-
int after;
359-
360-
before = is_auth_online();
361-
time(&last_auth_offline_time);
362-
after = is_auth_online();
363-
364-
if (before != after) {
365-
debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
366-
}
367-
368-
}
369-
370-
int
371-
is_auth_online()
372-
{
373-
if (!is_online()) {
374-
/* If we're not online auth is definately not online :) */
375-
return (0);
376-
} else if (last_auth_online_time == 0
377-
|| (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2)) {
378-
/* Auth is probably offline */
379-
return (0);
380-
} else {
381-
/* Auth is probably online */
382-
return (1);
383-
}
384-
}
385-
386-
/*
387-
* @return A string containing human-readable status text. MUST BE free()d by caller
388-
*/
389-
char *
390-
get_status_text()
391-
{
392-
pstr_t *pstr = pstr_new();
393-
s_config *config;
394-
t_auth_serv *auth_server;
395-
t_client *sublist, *current;
396-
int count;
397-
time_t uptime = 0;
398-
unsigned int days = 0, hours = 0, minutes = 0, seconds = 0;
399-
t_trusted_mac *p;
400-
401-
pstr_cat(pstr, "WiFiDog status\n\n");
402-
403-
uptime = time(NULL) - started_time;
404-
days = (unsigned int)uptime / (24 * 60 * 60);
405-
uptime -= days * (24 * 60 * 60);
406-
hours = (unsigned int)uptime / (60 * 60);
407-
uptime -= hours * (60 * 60);
408-
minutes = (unsigned int)uptime / 60;
409-
uptime -= minutes * 60;
410-
seconds = (unsigned int)uptime;
411-
412-
pstr_cat(pstr, "Version: " VERSION "\n");
413-
pstr_append_sprintf(pstr, "Uptime: %ud %uh %um %us\n", days, hours, minutes, seconds);
414-
pstr_cat(pstr, "Has been restarted: ");
415-
416-
if (restart_orig_pid) {
417-
pstr_append_sprintf(pstr, "yes (from PID %d)\n", restart_orig_pid);
418-
} else {
419-
pstr_cat(pstr, "no\n");
420-
}
421-
422-
pstr_append_sprintf(pstr, "Internet Connectivity: %s\n", (is_online()? "yes" : "no"));
423-
pstr_append_sprintf(pstr, "Auth server reachable: %s\n", (is_auth_online()? "yes" : "no"));
424-
pstr_append_sprintf(pstr, "Clients served this session: %lu\n\n", served_this_session);
425-
426-
LOCK_CLIENT_LIST();
427-
428-
count = client_list_dup(&sublist);
429-
430-
UNLOCK_CLIENT_LIST();
431-
432-
current = sublist;
433-
434-
pstr_append_sprintf(pstr, "%d clients " "connected.\n", count);
435-
436-
count = 1;
437-
while (current != NULL) {
438-
pstr_append_sprintf(pstr, "\nClient %d\n", count);
439-
pstr_append_sprintf(pstr, " IP: %s MAC: %s\n", current->ip, current->mac);
440-
pstr_append_sprintf(pstr, " Token: %s\n", current->token);
441-
pstr_append_sprintf(pstr, " Downloaded: %llu\n Uploaded: %llu\n", current->counters.incoming,
442-
current->counters.outgoing);
443-
count++;
444-
current = current->next;
445-
}
446-
447-
client_list_destroy(sublist);
448-
449-
config = config_get_config();
450-
451-
if (config->trustedmaclist != NULL) {
452-
pstr_cat(pstr, "\nTrusted MAC addresses:\n");
453-
454-
for (p = config->trustedmaclist; p != NULL; p = p->next) {
455-
pstr_append_sprintf(pstr, " %s\n", p->mac);
456-
}
457-
}
458-
459-
pstr_cat(pstr, "\nAuthentication servers:\n");
460-
461-
LOCK_CONFIG();
462-
463-
for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {
464-
pstr_append_sprintf(pstr, " Host: %s (%s)\n", auth_server->authserv_hostname, auth_server->last_ip);
465-
}
466-
467-
UNLOCK_CONFIG();
468-
469-
return pstr_to_string(pstr);
470-
}
471-
472282
/** Initialize the ICMP socket
473283
* @return A boolean of the success
474284
*/

0 commit comments

Comments
 (0)