static VALUE dnssd_service_resolve(VALUE self, VALUE _flags, VALUE _interface, VALUE _name, VALUE _type, VALUE _domain) { const char *name, *type, *domain; DNSServiceFlags flags = 0; uint32_t interface = 0; DNSServiceErrorType e; DNSServiceRef *client; dnssd_utf8_cstr(_name, name); dnssd_utf8_cstr(_type, type); dnssd_utf8_cstr(_domain, domain); if (!NIL_P(_flags)) flags = (uint32_t)NUM2ULONG(_flags); if (!NIL_P(_interface)) interface = (uint32_t)NUM2ULONG(_interface); get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceResolve(client, flags, interface, name, type, domain, dnssd_service_resolve_reply, (void *)self); dnssd_check_error_code(e); return self; }
static VALUE dnssd_service_query_record(VALUE self, VALUE _flags, VALUE _interface, VALUE _fullname, VALUE _rrtype, VALUE _rrclass) { DNSServiceRef *client; DNSServiceFlags flags; DNSServiceErrorType e; char *fullname; uint32_t interface; uint16_t rrtype; uint16_t rrclass; flags = (DNSServiceFlags)NUM2ULONG(_flags); interface = (uint32_t)NUM2ULONG(_interface); dnssd_utf8_cstr(_fullname, fullname); rrtype = NUM2UINT(_rrtype); rrclass = NUM2UINT(_rrclass); get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceQueryRecord(client, flags, interface, fullname, rrtype, rrclass, dnssd_service_query_record_reply, (void *)self); dnssd_check_error_code(e); return self; }
static void DNSSD_API dnssd_service_resolve_reply(DNSServiceRef client, DNSServiceFlags flags, uint32_t interface, DNSServiceErrorType e, const char *name, const char *target, uint16_t port, uint16_t txt_len, const unsigned char *txt_rec, void *context) { VALUE service, reply, argv[7]; dnssd_check_error_code(e); service = (VALUE)context; argv[0] = service; argv[1] = ULONG2NUM(flags); argv[2] = ULONG2NUM(interface); argv[3] = rb_str_new2(name); rb_enc_associate(argv[3], rb_utf8_encoding()); argv[4] = rb_str_new2(target); rb_enc_associate(argv[4], rb_utf8_encoding()); argv[5] = UINT2NUM(ntohs(port)); argv[6] = rb_str_new((char *)txt_rec, txt_len); rb_enc_associate(argv[6], rb_utf8_encoding()); reply = rb_class_new_instance(7, argv, cDNSSDReplyResolve); dnssd_service_callback(service, reply); }
static VALUE dnssd_service_getaddrinfo(VALUE self, VALUE _flags, VALUE _interface, VALUE _protocol, VALUE _host) { DNSServiceFlags flags = 0; uint32_t interface = 0; DNSServiceProtocol protocol = 0; const char *host; DNSServiceErrorType e; DNSServiceRef *client; dnssd_utf8_cstr(_host, host); protocol = (DNSServiceProtocol)NUM2ULONG(_protocol); if (!NIL_P(_flags)) flags = (DNSServiceFlags)NUM2ULONG(_flags); if (!NIL_P(_interface)) interface = (uint32_t)NUM2ULONG(_interface); get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceGetAddrInfo(client, flags, interface, protocol, host, dnssd_service_getaddrinfo_reply, (void *)self); dnssd_check_error_code(e); return self; }
static void DNSSD_API dnssd_service_query_record_reply(DNSServiceRef client, DNSServiceFlags flags, uint32_t interface, DNSServiceErrorType e, const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *context) { VALUE service, reply, argv[8]; dnssd_check_error_code(e); service = (VALUE)context; argv[0] = service; argv[1] = ULONG2NUM(flags); argv[2] = ULONG2NUM(interface); argv[3] = rb_str_new2(fullname); rb_enc_associate(argv[3], rb_utf8_encoding()); argv[4] = UINT2NUM(rrtype); argv[5] = UINT2NUM(rrclass); argv[6] = rb_str_new((char *)rdata, rdlen); rb_enc_associate(argv[6], rb_utf8_encoding()); argv[7] = ULONG2NUM(ttl); reply = rb_class_new_instance(8, argv, cDNSSDReplyQueryRecord); dnssd_service_callback(service, reply); }
static VALUE dnssd_service_update_record(VALUE self, VALUE _record, VALUE _flags, VALUE _rdata, VALUE _ttl) { DNSServiceRef *client; DNSRecordRef primaryRec = NULL; DNSRecordRef *record; DNSServiceFlags flags; DNSServiceErrorType e; uint16_t rdlen; const void *rdata; uint32_t ttl; _rdata = rb_str_to_str(_rdata); flags = (DNSServiceFlags)NUM2ULONG(_flags); rdlen = RSTRING_LEN(_rdata); rdata = (void *)RSTRING_PTR(_rdata); ttl = (uint32_t)NUM2ULONG(_ttl); if (!NIL_P(_record)) { get(cDNSSDRecord, _record, DNSRecordRef, record); } else { record = &primaryRec; } get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceUpdateRecord(*client, *record, flags, rdlen, rdata, ttl); dnssd_check_error_code(e); return self; }
static VALUE dnssd_service_browse(VALUE self, VALUE _flags, VALUE _iface, VALUE _type, VALUE _domain) { const char *type; const char *domain = NULL; DNSServiceFlags flags = 0; uint32_t iface = 0; DNSServiceErrorType e; DNSServiceRef *client; dnssd_utf8_cstr(_type, type); if (!NIL_P(_domain)) dnssd_utf8_cstr(_domain, domain); if (!NIL_P(_flags)) flags = (DNSServiceFlags)NUM2ULONG(_flags); if (!NIL_P(_iface)) iface = (uint32_t)NUM2ULONG(_iface); get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceBrowse(client, flags, iface, type, domain, dnssd_service_browse_reply, (void *)self); dnssd_check_error_code(e); return self; }
static VALUE dnssd_service_register(VALUE self, VALUE _flags, VALUE _interface, VALUE _name, VALUE _type, VALUE _domain, VALUE _host, VALUE _port, VALUE _text_record) { const char *name, *type, *host = NULL, *domain = NULL; uint16_t port; uint16_t txt_len = 0; char *txt_rec = NULL; DNSServiceFlags flags = 0; uint32_t interface = 0; DNSServiceRegisterReply callback = NULL; DNSServiceErrorType e; DNSServiceRef *client; dnssd_utf8_cstr(_name, name); dnssd_utf8_cstr(_type, type); if (!NIL_P(_host)) dnssd_utf8_cstr(_host, host); if (!NIL_P(_domain)) dnssd_utf8_cstr(_domain, domain); port = htons((uint16_t)NUM2UINT(_port)); if (!NIL_P(_text_record)) { txt_rec = RSTRING_PTR(_text_record); txt_len = RSTRING_LEN(_text_record); } if (!NIL_P(_flags)) flags = (DNSServiceFlags)NUM2ULONG(_flags); if (!NIL_P(_interface)) interface = (uint32_t)NUM2ULONG(_interface); if (rb_block_given_p()) callback = dnssd_service_register_reply; get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceRegister(client, flags, interface, name, type, domain, host, port, txt_len, txt_rec, callback, (void*)self); dnssd_check_error_code(e); return self; }
/* * call-seq: * service.get_property(property) * * Binding for DNSServiceGetProperty. The only property currently supported in * DNSSD is DaemonVersion */ static VALUE dnssd_service_s_get_property(VALUE klass, VALUE _property) { char * property; uint32_t result = 0; uint32_t size = sizeof(result); DNSServiceErrorType e; dnssd_utf8_cstr(_property, property); e = DNSServiceGetProperty(property, (void *)&result, &size); dnssd_check_error_code(e); /* as of this writing only a uint32_t will be returned */ return ULONG2NUM(result); }
static VALUE dnssd_service_process(VALUE self) { DNSServiceRef *client; int dnssd_fd, result; rb_fdset_t read; struct timeval timeout; get(cDNSSDService, self, DNSServiceRef, client); if (client == NULL) { /* looks like this thread has already been stopped */ return Qnil; } dnssd_fd = DNSServiceRefSockFD(*client); if (-1 == dnssd_fd) rb_raise(eDNSSDError, "unable to get DNSSD FD for result processing"); timeout.tv_sec = 0; timeout.tv_usec = 10000; rb_fd_init(&read); retry: rb_fd_zero(&read); rb_fd_set(dnssd_fd, &read); result = rb_thread_fd_select(dnssd_fd + 1, &read, NULL, NULL, &timeout); if (result == -1) rb_sys_fail("select"); if (rb_ivar_get(self, dnssd_iv_continue) == Qfalse) return Qnil; /* timeout */ if (result == 0) goto retry; DNSServiceErrorType e = DNSServiceProcessResult(*client); dnssd_check_error_code(e); return self; }
static void DNSSD_API dnssd_service_enumerate_domains_reply(DNSServiceRef client, DNSServiceFlags flags, uint32_t interface, DNSServiceErrorType e, const char *domain, void *context) { VALUE service, reply, argv[4]; dnssd_check_error_code(e); service = (VALUE)context; argv[0] = service; argv[1] = ULONG2NUM(flags); argv[2] = ULONG2NUM(interface); argv[3] = rb_str_new2(domain); rb_enc_associate(argv[3], rb_utf8_encoding()); reply = rb_class_new_instance(4, argv, cDNSSDReplyDomain); dnssd_service_callback(service, reply); }
static VALUE dnssd_service_process(VALUE self) { DNSServiceRef *client; get(cDNSSDService, self, DNSServiceRef, client); if (client == NULL) { /* looks like this thread has already been stopped */ return Qnil; } rb_thread_wait_fd(DNSServiceRefSockFD(*client)); if (rb_ivar_get(self, dnssd_iv_continue) == Qfalse) return Qnil; DNSServiceErrorType e = DNSServiceProcessResult(*client); dnssd_check_error_code(e); return self; }
static void DNSSD_API dnssd_service_register_reply(DNSServiceRef client, DNSServiceFlags flags, DNSServiceErrorType e, const char *name, const char *type, const char *domain, void *context) { VALUE service, reply, argv[5]; dnssd_check_error_code(e); service = (VALUE)context; argv[0] = service; argv[1] = ULONG2NUM(flags); argv[2] = rb_str_new2(name); rb_enc_associate(argv[2], rb_utf8_encoding()); argv[3] = rb_str_new2(type); rb_enc_associate(argv[3], rb_utf8_encoding()); argv[4] = rb_str_new2(domain); rb_enc_associate(argv[4], rb_utf8_encoding()); reply = rb_class_new_instance(5, argv, cDNSSDReplyRegister); dnssd_service_callback(service, reply); }
static void DNSSD_API dnssd_service_getaddrinfo_reply(DNSServiceRef client, DNSServiceFlags flags, uint32_t interface, DNSServiceErrorType e, const char *host, const struct sockaddr *address, uint32_t ttl, void *context) { VALUE service, reply, argv[6]; dnssd_check_error_code(e); service = (VALUE)context; argv[0] = service; argv[1] = ULONG2NUM(flags); argv[2] = ULONG2NUM(interface); argv[3] = rb_str_new2(host); rb_enc_associate(argv[3], rb_utf8_encoding()); argv[4] = rb_str_new((char *)address, SIN_LEN((struct sockaddr_in*)address)); rb_enc_associate(argv[4], rb_utf8_encoding()); argv[5] = ULONG2NUM(ttl); reply = rb_class_new_instance(6, argv, cDNSSDReplyAddrInfo); dnssd_service_callback(service, reply); }
static VALUE dnssd_service_enumerate_domains(VALUE self, VALUE _flags, VALUE _interface) { DNSServiceFlags flags = 0; uint32_t interface = 0; DNSServiceErrorType e; DNSServiceRef *client; if (!NIL_P(_flags)) flags = (DNSServiceFlags)NUM2ULONG(_flags); if (!NIL_P(_interface)) interface = (uint32_t)NUM2ULONG(_interface); get(cDNSSDService, self, DNSServiceRef, client); e = DNSServiceEnumerateDomains(client, flags, interface, dnssd_service_enumerate_domains_reply, (void *)self); dnssd_check_error_code(e); return self; }
static VALUE dnssd_service_add_record(VALUE self, VALUE _flags, VALUE _rrtype, VALUE _rdata, VALUE _ttl) { VALUE _record = Qnil; DNSServiceRef *client; DNSRecordRef *record; DNSServiceFlags flags; DNSServiceErrorType e; uint16_t rrtype; uint16_t rdlen; const void *rdata; uint32_t ttl; _rdata = rb_str_to_str(_rdata); flags = (DNSServiceFlags)NUM2ULONG(_flags); rrtype = NUM2UINT(_rrtype); rdlen = RSTRING_LEN(_rdata); rdata = (void *)RSTRING_PTR(_rdata); ttl = (uint32_t)NUM2ULONG(_ttl); get(cDNSSDService, self, DNSServiceRef, client); _record = rb_class_new_instance(0, NULL, cDNSSDRecord); get(cDNSSDRecord, _record, DNSRecordRef, record); e = DNSServiceAddRecord(*client, record, flags, rrtype, rdlen, rdata, ttl); dnssd_check_error_code(e); /* record will become invalid when this service is destroyed */ rb_ivar_set(_record, dnssd_iv_service, self); rb_ary_push(rb_ivar_get(self, dnssd_iv_records), _record); return _record; }