10 gchar *flx_get_host_name(void) {
12 char t[HOST_NAME_MAX];
16 gethostname(t, sizeof(t));
18 return flx_normalize_name(t);
21 gchar *flx_normalize_name(const gchar *s) {
33 return g_strdup_printf("%s.", s);
36 gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
40 if (a->tv_sec < b->tv_sec)
43 if (a->tv_sec > b->tv_sec)
46 if (a->tv_usec < b->tv_usec)
49 if (a->tv_usec > b->tv_usec)
55 glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
59 if (flx_timeval_compare(a, b) < 0)
60 return flx_timeval_diff(b, a);
62 return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
66 gint flx_set_cloexec(gint fd) {
71 if ((n = fcntl(fd, F_GETFD)) < 0)
77 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
80 gint flx_set_nonblock(gint fd) {
85 if ((n = fcntl(fd, F_GETFL)) < 0)
91 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
94 gint flx_wait_for_write(gint fd) {
101 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
102 g_message("select() failed: %s", strerror(errno));
112 GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
115 g_get_current_time(tv);
118 g_time_val_add(tv, msec*1000);
121 g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
126 gint flx_age(const GTimeVal *a) {
131 g_get_current_time(&now);
133 return flx_timeval_diff(&now, a);
136 gboolean flx_domain_equal(const gchar *a, const gchar *b) {
137 int escaped_a = 0, escaped_b = 0;
142 /* Check for escape characters "\" */
143 if ((escaped_a = *a == '\\'))
146 if ((escaped_b = *b == '\\'))
149 /* Check for string end */
150 if (*a == 0 && *b == 0)
153 if (*a == 0 && !escaped_b && *b == '.' && *(b+1) == 0)
156 if (!escaped_a && *a == '.' && *(a+1) == 0 && *b == 0)
159 /* Compare characters */
160 if (escaped_a == escaped_b && *a != *b)
164 /* Next characters */
171 guint flx_domain_hash(const gchar *p) {
173 strncpy(t, p, sizeof(t)-1);
176 return g_int_hash(t);
179 void flx_hexdump(gconstpointer p, guint size) {
183 printf("Dumping %u bytes from %p:\n", size, p);
188 for (i = 0; i < 16; i++) {
190 printf("%02x ", c[i]);
195 for (i = 0; i < 16; i++) {
197 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');