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
35 gchar *avahi_get_host_name(void) {
37 char t[HOST_NAME_MAX];
41 gethostname(t, sizeof(t));
43 return avahi_normalize_name(t);
46 gchar *avahi_normalize_name(const gchar *s) {
58 return g_strdup_printf("%s.", s);
61 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
65 if (a->tv_sec < b->tv_sec)
68 if (a->tv_sec > b->tv_sec)
71 if (a->tv_usec < b->tv_usec)
74 if (a->tv_usec > b->tv_usec)
80 glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
84 if (avahi_timeval_compare(a, b) < 0)
85 return avahi_timeval_diff(b, a);
87 return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
91 gint avahi_set_cloexec(gint fd) {
96 if ((n = fcntl(fd, F_GETFD)) < 0)
102 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
105 gint avahi_set_nonblock(gint fd) {
110 if ((n = fcntl(fd, F_GETFL)) < 0)
116 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
119 gint avahi_wait_for_write(gint fd) {
126 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
127 g_message("select() failed: %s", strerror(errno));
137 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
140 g_get_current_time(tv);
143 g_time_val_add(tv, msec*1000);
146 g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
151 gint avahi_age(const GTimeVal *a) {
156 g_get_current_time(&now);
158 return avahi_timeval_diff(&now, a);
161 gboolean avahi_domain_cmp(const gchar *a, const gchar *b) {
162 int escaped_a = 0, escaped_b = 0;
167 /* Check for escape characters "\" */
168 if ((escaped_a = *a == '\\'))
171 if ((escaped_b = *b == '\\'))
174 /* Check for string end */
175 if (*a == 0 && *b == 0)
178 if (*a == 0 && !escaped_b && *b == '.' && *(b+1) == 0)
181 if (!escaped_a && *a == '.' && *(a+1) == 0 && *b == 0)
184 /* Compare characters */
185 if (escaped_a == escaped_b && *a != *b)
186 return *a < *b ? -1 : 1;
188 /* Next characters */
195 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
196 return avahi_domain_cmp(a, b) == 0;
199 guint avahi_domain_hash(const gchar *p) {
201 strncpy(t, p, sizeof(t)-1);
204 return g_int_hash(t);
207 void avahi_hexdump(gconstpointer p, guint size) {
211 printf("Dumping %u bytes from %p:\n", size, p);
216 for (i = 0; i < 16; i++) {
218 printf("%02x ", c[i]);
223 for (i = 0; i < 16; i++) {
225 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');