"Fossies" - the Fresh Open Source Software Archive

Member "nss-mdns-0.10/src/avahi.c" (28 Apr 2006, 3597 Bytes) of package /linux/misc/dns/old/nss-mdns-0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* $Id: avahi.c 92 2006-04-28 21:14:09Z lennart $ */
    2 
    3 /***
    4   This file is part of nss-mdns.
    5  
    6   nss-mdns 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 of the
    9   License, or (at your option) any later version.
   10  
   11   nss-mdns is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   14   General Public License for more details.
   15  
   16   You should have received a copy of the GNU Lesser General Public
   17   License along with nss-mdns; if not, write to the Free Software
   18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   19   USA.
   20 ***/
   21 
   22 #ifdef HAVE_CONFIG_H
   23 #include <config.h>
   24 #endif
   25 
   26 #include <sys/socket.h>
   27 #include <string.h>
   28 #include <stdio.h>
   29 #include <sys/types.h>
   30 #include <arpa/inet.h>
   31 #include <sys/un.h>
   32 #include <assert.h>
   33 #include <unistd.h>
   34 
   35 #include "avahi.h"
   36 #include "util.h"
   37 
   38 #define WHITESPACE " \t"
   39 
   40 static FILE *open_socket(void) {
   41     int fd = -1;
   42     struct sockaddr_un sa;
   43     FILE *f = NULL;
   44 
   45     if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
   46         goto fail;
   47 
   48     set_cloexec(fd);
   49     
   50     memset(&sa, 0, sizeof(sa));
   51     sa.sun_family = AF_UNIX;
   52     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
   53     sa.sun_path[sizeof(sa.sun_path)-1] = 0;
   54 
   55     if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0)
   56         goto fail;
   57 
   58     if (!(f = fdopen(fd, "r+")))
   59         goto fail;
   60 
   61     return f;
   62     
   63 fail:
   64     if (fd >= 0)
   65         close(fd);
   66 
   67     return NULL;
   68 }
   69 
   70 int avahi_resolve_name(int af, const char* name, void* data) {
   71     FILE *f = NULL;
   72     char *p;
   73     int ret = -1;
   74     char ln[256];
   75 
   76     if (af != AF_INET && af != AF_INET6)
   77         goto finish;
   78     
   79     if (!(f = open_socket()))
   80         goto finish;
   81 
   82     fprintf(f, "RESOLVE-HOSTNAME%s %s\n", af == AF_INET ? "-IPV4" : "-IPV6", name);
   83     fflush(f);
   84 
   85     if (!(fgets(ln, sizeof(ln), f)))
   86         goto finish;
   87 
   88     if (ln[0] != '+') {
   89         ret = 1;
   90         goto finish;
   91     }
   92 
   93     p = ln+1;
   94     p += strspn(p, WHITESPACE);
   95 
   96     /* Skip interface */
   97     p += strcspn(p, WHITESPACE);
   98     p += strspn(p, WHITESPACE);
   99 
  100     /* Skip protocol */
  101     p += strcspn(p, WHITESPACE);
  102     p += strspn(p, WHITESPACE);
  103 
  104     /* Skip host name */
  105     p += strcspn(p, WHITESPACE);
  106     p += strspn(p, WHITESPACE);
  107 
  108     /* Cut off end of line */
  109     *(p + strcspn(p, "\n\r\t ")) = 0;
  110 
  111     if (inet_pton(af, p, data) <= 0)
  112         goto finish;
  113 
  114     ret = 0;
  115     
  116 finish:
  117 
  118     if (f)
  119         fclose(f);
  120 
  121     return ret;
  122 }
  123 
  124 int avahi_resolve_address(int af, const void *data, char* name, size_t name_len) {
  125     FILE *f = NULL;
  126     char *p;
  127     int ret = -1;
  128     char a[256], ln[256];
  129 
  130     if (af != AF_INET && af != AF_INET6)
  131         goto finish;
  132     
  133     if (!(f = open_socket()))
  134         goto finish;
  135 
  136     fprintf(f, "RESOLVE-ADDRESS %s\n", inet_ntop(af, data, a, sizeof(a)));
  137     
  138     if (!(fgets(ln, sizeof(ln), f)))
  139         goto finish;
  140 
  141     if (ln[0] != '+') {
  142         ret = 1;
  143         goto finish;
  144     }
  145 
  146     p = ln+1;
  147     p += strspn(p, WHITESPACE);
  148 
  149     /* Skip interface */
  150     p += strcspn(p, WHITESPACE);
  151     p += strspn(p, WHITESPACE);
  152 
  153     /* Skip protocol */
  154     p += strcspn(p, WHITESPACE);
  155     p += strspn(p, WHITESPACE);
  156     
  157     /* Cut off end of line */
  158     *(p + strcspn(p, "\n\r\t ")) = 0;
  159 
  160     strncpy(name, p, name_len-1);
  161     name[name_len-1] = 0;
  162 
  163     ret = 0;
  164      
  165 finish:
  166 
  167     if (f)
  168         fclose(f);
  169 
  170     return ret;
  171 }