4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 gchar *avahi_get_host_name(void) {
33 char t[HOST_NAME_MAX];
37 gethostname(t, sizeof(t));
39 return avahi_normalize_name(t);
42 gchar *avahi_normalize_name(const gchar *s) {
54 return g_strdup_printf("%s.", s);
57 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
61 if (a->tv_sec < b->tv_sec)
64 if (a->tv_sec > b->tv_sec)
67 if (a->tv_usec < b->tv_usec)
70 if (a->tv_usec > b->tv_usec)
76 glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
80 if (avahi_timeval_compare(a, b) < 0)
81 return avahi_timeval_diff(b, a);
83 return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
87 gint avahi_set_cloexec(gint fd) {
92 if ((n = fcntl(fd, F_GETFD)) < 0)
98 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
101 gint avahi_set_nonblock(gint fd) {
106 if ((n = fcntl(fd, F_GETFL)) < 0)
112 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
115 gint avahi_wait_for_write(gint fd) {
122 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
123 g_message("select() failed: %s", strerror(errno));
133 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
136 g_get_current_time(tv);
139 g_time_val_add(tv, msec*1000);
142 g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
147 gint avahi_age(const GTimeVal *a) {
152 g_get_current_time(&now);
154 return avahi_timeval_diff(&now, a);
157 gboolean avahi_domain_cmp(const gchar *a, const gchar *b) {
158 int escaped_a = 0, escaped_b = 0;
163 /* Check for escape characters "\" */
164 if ((escaped_a = *a == '\\'))
167 if ((escaped_b = *b == '\\'))
170 /* Check for string end */
171 if (*a == 0 && *b == 0)
174 if (*a == 0 && !escaped_b && *b == '.' && *(b+1) == 0)
177 if (!escaped_a && *a == '.' && *(a+1) == 0 && *b == 0)
180 /* Compare characters */
181 if (escaped_a == escaped_b && *a != *b)
182 return *a < *b ? -1 : 1;
184 /* Next characters */
191 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
192 return avahi_domain_cmp(a, b) == 0;
195 guint avahi_domain_hash(const gchar *p) {
197 strncpy(t, p, sizeof(t)-1);
200 return g_int_hash(t);
203 void avahi_hexdump(gconstpointer p, guint size) {
207 printf("Dumping %u bytes from %p:\n", size, p);
212 for (i = 0; i < 16; i++) {
214 printf("%02x ", c[i]);
219 for (i = 0; i < 16; i++) {
221 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');