English 中文(简体)
Avahi dns_sd兼容层不能运行Browse 背靠
原标题:Avahi dns_sd compatibility layer fails to run Browse callback

Background

I m working on a cross-platform Zeroconf/Bonjour/DNS-SD library for Haskell, and figured my best bet would bet would be to target the dns_sd.h API. Under Linux, the implementation of this interface is provided by Avahi, which claims to support a subset of the Bonjour API.

Problem

作为我的图书馆的疗效检查,我在C撰写了一个小的测试方案,仅使用APIC光带。 http://www.tcp。

#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!
");
}

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
", err1);
  DNSServiceErrorType err2 = DNSServiceProcessResult(sd);
  printf("err2=%d
", err2);
  return 0;
}

在我的Mac,这一试验方案在C和等同的Haskell(发现我的打印机;引诱!)两处都进行了罚款。

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

但是,在我的无赖机器上,该方案在我离开之前就向我发出警告:

$ 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

Questions

  1. Is the Avahi dns_sd compatibility layer still a suitable target for a cross-platform binding? Or is that warning message serious enough about using the native Avahi API that I should consider retargeting?
  2. What is the state of the art for cross-platform Zeroconf in C?
最佳回答

由于我所不知的原因,它只用非锁定电话开展工作。 下面是经过改进的法典。 从Avahi向非锁定模式排入,然后使用<条码>选择(3)等待现有数据。DNSserviceProcessResult(sd),每当备有数据时,都必须称呼,因此,你的例子可能纯粹是在其他平台上工作的。

#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!
", 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
", 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
", err2);
        if(err2 != 0)
            return 2;
    }
    return 0;
}
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签