Domanda

Sfondo

Sto lavorando a una libreria zeroconf/bonjour/dns-sd multipiattaforma per Haskell, e ho pensato che la mia scommessa migliore sarebbe scommessa sarebbe quella di colpire il dns_sd.h API. Sotto Linux, l'implementazione di questa interfaccia è fornita da Avahi, che afferma di sostenere un sottoinsieme dell'API Bonjour.

Problema

Come controllo di sanità mentale per la mia biblioteca, ho scritto un piccolo programma di test in C che utilizza solo le ossa nude dell'API. Sfoglia per qualsiasi servizio sulla rete di tipo _http._tcp, stampa un messaggio non appena ne vede uno e poi muore:

#include <dns_sd.h>
#include <stdio.h>
#include <stdlib.h>

void cb(DNSServiceRef sdRef,
        DNSServiceFlags flags,
        uint32_t interfaceIndex,
        DNSServiceErrorType errorCode,
        const char *serviceName,
        const char *regtype,
        const char *replyDomain,
        void *context) {
  printf("called!\n");
}

int main() {
  DNSServiceRef sd = malloc(sizeof(DNSServiceRef));
  const char *regtype = "_http._tcp";
  DNSServiceErrorType err1 = DNSServiceBrowse(&sd, 0, 0, regtype, NULL, &cb, NULL);
  printf("err1=%d\n", err1);
  DNSServiceErrorType err2 = DNSServiceProcessResult(sd);
  printf("err2=%d\n", err2);
  return 0;
}

Sul mio Mac, questo programma di test funziona bene sia in C che in Haskell equivalente (trova la mia stampante; eccitante!):

$ gcc test.c -o test
$ ./test
err1=0
called!
err2=0

Ma sulla mia macchina Linux, il programma mi rimprovera prima di uscire senza invocare il callback:

$ gcc test.c -o test -ldns_sd
$ ./test
*** WARNING *** The program 'test' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=test>
err1=0
err2=0

Domande

  1. È l'Avahi dns_sd Livello di compatibilità ancora un obiettivo adatto per un legame multipiattaforma? O è quel messaggio di avvertimento abbastanza serio sull'uso dell'API Avahi nativa che dovrei prendere in considerazione il retargeting?
  2. Qual è lo stato dell'arte per Zeroconf multipiattaforma in C?
È stato utile?

Soluzione

Per ragione a me sconosciuta, funziona solo con chiamate non bloccanti. Di seguito è riportato il codice migliorato. La presa da Avahi è impostata su una modalità non bloccante e quindi select (3) viene utilizzato per attendere i dati disponibili. DNSServiceProcessResult(sd) Deve essere chiamato ogni volta che ci sono dati sulla presa, quindi potrebbe essere stata pura fortuna che il tuo esempio abbia funzionato su altre piattaforme.

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dns_sd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

static int set_nonblocking(int fd)
{
    int flags;
    /* If they have O_NONBLOCK, use the Posix way to do it */
#if defined(O_NONBLOCK)
    /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
    if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
        flags = 0;
    return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#else
    /* Otherwise, use the old way of doing it */
    flags = 1;
    return ioctl(fd, FIOBIO, &flags);
#endif
}     

void cb(DNSServiceRef sdRef,
        DNSServiceFlags flags,
        uint32_t interfaceIndex,
        DNSServiceErrorType errorCode,
        const char *serviceName,
        const char *regtype,
        const char *replyDomain,
        void *context) {
    printf("called %s %s!\n", serviceName, regtype);
}

int main() {
    DNSServiceRef sd = malloc(sizeof(DNSServiceRef));
    const char *regtype = "_http._tcp";
    DNSServiceErrorType err1 = DNSServiceBrowse(&sd, 0, 0, regtype, NULL, &cb, NULL);
    printf("err1=%d\n", err1);
    int socket = DNSServiceRefSockFD(sd);
    set_nonblocking(socket);

    fd_set read_fds;
    FD_ZERO(&read_fds);
    FD_SET(socket, &read_fds);

    while(1) {
        if(select(socket+1, &read_fds, NULL, NULL, NULL)  < 0) {
            perror("select");  
        }
        DNSServiceErrorType err2 = DNSServiceProcessResult(sd);
        printf("err2=%d\n", err2);
        if(err2 != 0)
            return 2;
    }
    return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top