summaryrefslogtreecommitdiffstats
path: root/util.c
blob: 0fcedc71c0fee844ba99015c4b5a6fcd421bb883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include "util.h"

gchar *flx_get_host_name(void) {
    char t[256];
    gethostname(t, sizeof(t));
    return g_strndup(t, sizeof(t));
}

gchar *flx_normalize_name(const gchar *s) {
    size_t l;
    g_assert(s);

    l = strlen(s);

    if (!l)
        return g_strdup(".");

    if (s[l-1] == '.')
        return g_strdup(s);
    
    return g_strdup_printf("%s.", s);
}

gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
    g_assert(a);
    g_assert(b);

    if (a->tv_sec < b->tv_sec)
        return -1;

    if (a->tv_sec > b->tv_sec)
        return 1;

    if (a->tv_usec < b->tv_usec)
        return -1;

    if (a->tv_usec > b->tv_usec)
        return 1;

    return 0;
}

glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
    g_assert(a);
    g_assert(b);

    if (flx_timeval_compare(a, b) < 0)
        return flx_timeval_diff(b, a);

    return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
}


gint flx_set_cloexec(gint fd) {
    gint n;

    g_assert(fd >= 0);
    
    if ((n = fcntl(fd, F_GETFD)) < 0)
        return -1;

    if (n & FD_CLOEXEC)
        return 0;

    return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
}

gint flx_set_nonblock(gint fd) {
    gint n;

    g_assert(fd >= 0);

    if ((n = fcntl(fd, F_GETFL)) < 0)
        return -1;

    if (n & O_NONBLOCK)
        return 0;

    return fcntl(fd, F_SETFL, n|O_NONBLOCK);
}

gint flx_wait_for_write(gint fd) {
    fd_set fds;
    gint r;
    
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    
    if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
        g_message("select() failed: %s", strerror(errno));

        return -1;
    }
    
    g_assert(r > 0);

    return 0;
}

GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
    g_assert(tv);

    g_get_current_time(tv);

    if (msec)
        g_time_val_add(tv, msec*1000);

    if (jitter)
        g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
        
    return tv;
}

gint flx_age(const GTimeVal *a) {
    GTimeVal now;
    
    g_assert(a);

    g_get_current_time(&now);

    return flx_timeval_diff(&now, a);
}

gboolean flx_domain_equal(const gchar *a, const gchar *b) {
    g_assert(a);
    g_assert(b);

    for (;;) {
        if (*a == 0 && *b == 0)
            return TRUE;
        
        if (*a == 0 && *b == '.' && *(b+1) == 0)
            return TRUE;

        if (*a == '.' && *(a+1) == 0 && *b == 0)
            return TRUE;

        if (*a != *b)
            return FALSE;

        a++;
        b++;
    }
}

guint flx_domain_hash(const gchar *p) {
    char t[256];
    strncpy(t, p, sizeof(t)-1);
    t[sizeof(t)-1] = 0;

    return g_int_hash(t);
}