OSStatus CConfigPropertySheet::SetupBrowsing() { OSStatus err; // Start browsing for browse domains err = DNSServiceEnumerateDomains( &m_browseDomainsRef, kDNSServiceFlagsBrowseDomains, 0, BrowseDomainsReply, this ); require_noerr( err, exit ); err = WSAAsyncSelect( DNSServiceRefSockFD( m_browseDomainsRef ), m_hWnd, WM_DATAREADY, FD_READ|FD_CLOSE ); require_noerr( err, exit ); // Start browsing for registration domains err = DNSServiceEnumerateDomains( &m_regDomainsRef, kDNSServiceFlagsRegistrationDomains, 0, RegDomainsReply, this ); require_noerr( err, exit ); err = WSAAsyncSelect( DNSServiceRefSockFD( m_regDomainsRef ), m_hWnd, WM_DATAREADY, FD_READ|FD_CLOSE ); require_noerr( err, exit ); exit: if ( err ) { TearDownBrowsing(); } return err; }
/* void enumerate (); */ NS_IMETHODIMP CBFENUMERATE::Enumerate() { Log(ToNewUnicode(NS_LITERAL_STRING("Enumerate Started"))); DNSServiceErrorType err = kDNSServiceErr_Unknown; if (mType == PR_TRUE) { err = DNSServiceEnumerateDomains(&mSdRef, kDNSServiceFlagsRegistrationDomains, mInterfaceIndex, (DNSServiceDomainEnumReply) Callback, this); } else { err = DNSServiceEnumerateDomains(&mSdRef, kDNSServiceFlagsBrowseDomains, mInterfaceIndex, (DNSServiceDomainEnumReply) Callback, this); } if (err != kDNSServiceErr_NoError) return NS_ERROR_FAILURE; StartTimer(); return NS_OK; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleDomainEnum_BeginEnum( JNIEnv *pEnv, jobject pThis, jint flags, jint ifIndex) { jclass cls = (*pEnv)->GetObjectClass( pEnv, pThis); jfieldID contextField = (*pEnv)->GetFieldID( pEnv, cls, "fNativeContext", "J"); OpContext *pContext = NULL; DNSServiceErrorType err = kDNSServiceErr_NoError; if ( contextField != 0) pContext = NewContext( pEnv, pThis, "domainFound", "(Lcom/apple/dnssd/DNSSDService;IILjava/lang/String;)V"); else err = kDNSServiceErr_BadParam; if ( pContext != NULL) { pContext->Callback2 = (*pEnv)->GetMethodID( pEnv, (*pEnv)->GetObjectClass( pEnv, pContext->ClientObj), "domainLost", "(Lcom/apple/dnssd/DNSSDService;IILjava/lang/String;)V"); err = DNSServiceEnumerateDomains( &pContext->ServiceRef, flags, ifIndex, DomainEnumReply, pContext); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, pThis, contextField, (long) pContext); } } else err = kDNSServiceErr_NoMemory; return err; }
/* IBFDNSSDService enumerate (in long interfaceIndex, in PRBool domainType, in IBFDNSSDEnumerateListener listener); */ NS_IMETHODIMP CBFDNSSDService::Enumerate(PRInt32 interfaceIndex, PRBool domainType, IBFDNSSDEnumerateListener *listener, IBFDNSSDService **_retval) { CBFDNSSDService * service = NULL; DNSServiceErrorType dnsErr = 0; nsresult err = 0; *_retval = NULL; try { service = new CBFDNSSDService( listener ); } catch ( ... ) { service = NULL; } if ( service == NULL ) { err = NS_ERROR_FAILURE; goto exit; } service->m_enuDomainType = domainType; dnsErr = DNSServiceEnumerateDomains( &service->m_sdRef, domainType ? kDNSServiceFlagsBrowseDomains : kDNSServiceFlagsRegistrationDomains, interfaceIndex, ( DNSServiceDomainEnumReply ) EnumerateReply, service ); if ( dnsErr != kDNSServiceErr_NoError ) { err = NS_ERROR_FAILURE; goto exit; } if ( ( service->m_fileDesc = PR_ImportTCPSocket( DNSServiceRefSockFD( service->m_sdRef ) ) ) == NULL ) { err = NS_ERROR_FAILURE; goto exit; } if ( ( service->m_threadPool = PR_CreateThreadPool( 1, 1, 8192 ) ) == NULL ) { err = NS_ERROR_FAILURE; goto exit; } err = service->SetupNotifications(); if ( err != NS_OK) { goto exit; } listener->AddRef(); service->AddRef(); *_retval = service; err = NS_OK; exit: if ( err && service ) { delete service; service = NULL; } return err; }
Handle<Value> dnsServiceEnumerateDomains(Arguments const& args) { HandleScope scope; if (argumentCountMismatch(args, 5)) { return throwArgumentCountMismatchException(args, 5); } if ( ! ServiceRef::HasInstance(args[0])) { return throwTypeError("argument 1 must be a DNSServiceRef (sdRef)"); } ServiceRef * serviceRef = ObjectWrap::Unwrap<ServiceRef>(args[0]->ToObject()); if (serviceRef->IsInitialized()) { return throwError("DNSServiceRef is already initialized"); } if ( ! args[1]->IsInt32()) { return throwError("argument 2 must be an integer (DNSServiceFlags)"); } DNSServiceFlags flags = args[1]->ToInteger()->Int32Value(); if ( ! args[2]->IsInt32()) { return throwTypeError("argument 3 must be an integer (interfaceIndex)"); } uint32_t interfaceIndex = args[2]->ToInteger()->Int32Value(); if ( ! args[3]->IsFunction()) { return throwTypeError("argument 4 must be a function (callBack)"); } serviceRef->SetCallback(Local<Function>::Cast(args[3])); if ( ! args[4]->IsNull() && ! args[4]->IsUndefined()) { serviceRef->SetContext(args[4]); } DNSServiceErrorType error = DNSServiceEnumerateDomains( & serviceRef->GetServiceRef(), flags, interfaceIndex, OnEnumeration, serviceRef); if (error != kDNSServiceErr_NoError) { return throwMdnsError("dnsServiceEnumerateDomains()", error); } if ( ! serviceRef->SetSocketFlags()) { return throwError("Failed to set socket flags (O_NONBLOCK, FD_CLOEXEC)"); } return Undefined(); }
STDMETHODIMP CDNSSD::EnumerateDomains(DNSSDFlags flags, ULONG ifIndex, IDomainListener *listener, IDNSSDService **service) { CComObject<CDNSSDService> * object = NULL; DNSServiceRef sref = NULL; DNSServiceErrorType err = 0; HRESULT hr = 0; // Initialize *service = NULL; try { object = new CComObject<CDNSSDService>(); } catch ( ... ) { object = NULL; } require_action( object != NULL, exit, err = kDNSServiceErr_NoMemory ); hr = object->FinalConstruct(); require_action( hr == S_OK, exit, err = kDNSServiceErr_Unknown ); object->AddRef(); err = DNSServiceEnumerateDomains( &sref, flags, ifIndex, ( DNSServiceDomainEnumReply ) &DomainEnumReply, object ); require_noerr( err, exit ); object->SetServiceRef( sref ); object->SetListener( listener ); err = object->Run(); require_noerr( err, exit ); *service = object; exit: if ( err && object ) { object->Release(); } return err; }
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 ErlDrvSSizeT call(ErlDrvData edd, unsigned int cmd, char *buf, ErlDrvSizeT len, char **rbuf, ErlDrvSizeT rlen, unsigned int *flags) { dnssd_drv_t* dd = (dnssd_drv_t*) edd; int version, out_len, index, rindex, local_only; DNSServiceErrorType err; char* out_atom_text; ei_term arg, name, type, domain, txt, host, hostport; char *name_tmp, *type_tmp, *domain_tmp, *txt_tmp, *host_tmp; /* don't allow reuse */ if (dd->sd_ref) return -1; index = 0; dd->sd_ref = NULL; ei_decode_version(buf, &index, &version); ei_decode_ei_term(buf, &index, &arg); if (cmd == DNSSD_CMD_ENUM) { if (arg.ei_type == ERL_ATOM_EXT) { if (strncmp(arg.value.atom_name, "browse", 6) == 0) { // init for enum browse err = DNSServiceEnumerateDomains(&dd->sd_ref, kDNSServiceFlagsBrowseDomains, kDNSServiceInterfaceIndexAny, (DNSServiceDomainEnumReply) EnumReply, dd); } else if (strncmp(arg.value.atom_name,"reg", 3) == 0) { // init for enum reg err = DNSServiceEnumerateDomains(&dd->sd_ref, kDNSServiceFlagsRegistrationDomains, kDNSServiceInterfaceIndexAny, (DNSServiceDomainEnumReply) EnumReply, dd); } else { goto badarg; } } else { goto badarg; } } else if (cmd == DNSSD_CMD_BROWSE) { if (!arg.ei_type == ERL_TUPLE || arg.arity != 2) goto badarg; /* decode type */ ei_decode_ei_term(buf, &index, &type); if (type.ei_type != ERL_BINARY_EXT) goto badarg; index += 5; // skip tag + 4 byte size type_tmp = (char*)driver_alloc(type.size + 1); memset(type_tmp, 0, type.size + 1); memcpy(type_tmp, buf + index, type.size); index += type.size; /* decode domain */ ei_decode_ei_term(buf, &index, &domain); if (domain.ei_type != ERL_BINARY_EXT) { driver_free(type_tmp); goto badarg; } index += 5; // skip tag + 4 byte size domain_tmp = (char *) driver_alloc(domain.size + 1); memset(domain_tmp, 0, domain.size + 1); memcpy(domain_tmp, buf + index, domain.size); err = DNSServiceBrowse(&dd->sd_ref, 0, // Flags kDNSServiceInterfaceIndexAny, type_tmp, domain_tmp, (DNSServiceBrowseReply) BrowseReply, dd); driver_free(type_tmp); driver_free(domain_tmp); } else if (cmd == DNSSD_CMD_RESOLVE) { if (!arg.ei_type == ERL_TUPLE || arg.arity != 3) goto badarg; /* decode name */ ei_decode_ei_term(buf, &index, &name); if (name.ei_type != ERL_BINARY_EXT) goto badarg; index += 5; // skip tag + 4 byte size name_tmp = (char *) driver_alloc(name.size + 1); memset(name_tmp, 0, name.size + 1); memcpy(name_tmp, buf + index, name.size); index += name.size; /* decode type */ ei_decode_ei_term(buf, &index, &type); if (type.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); goto badarg; } index += 5; // skip tag + 4 byte size type_tmp = (char *) driver_alloc(type.size + 1); memset(type_tmp, 0, type.size + 1); memcpy(type_tmp, buf + index, type.size); index += type.size; /* decode domain */ ei_decode_ei_term(buf, &index, &domain); if (domain.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); driver_free(type_tmp); goto badarg; } index += 5; // skip tag + 4 byte size domain_tmp = (char *) driver_alloc(domain.size + 1); memset(domain_tmp, 0, domain.size + 1); memcpy(domain_tmp, buf + index, domain.size); /* start op */ err = DNSServiceResolve(&dd->sd_ref, 0, // Flags kDNSServiceInterfaceIndexAny, name_tmp, type_tmp, domain_tmp, (DNSServiceResolveReply) ResolveReply, dd); driver_free(name_tmp); driver_free(type_tmp); driver_free(domain_tmp); } else if (cmd == DNSSD_CMD_REGISTER) { if (!arg.ei_type == ERL_TUPLE || arg.arity != 6) goto badarg; /* decode name */ ei_decode_ei_term(buf, &index, &name); if (name.ei_type != ERL_BINARY_EXT) goto badarg; index += 5; // skip tag + 4 byte size name_tmp = (char *) driver_alloc(name.size + 1); memset(name_tmp, 0, name.size + 1); memcpy(name_tmp, buf + index, name.size); index += name.size; /* decode type */ ei_decode_ei_term(buf, &index, &type); if (type.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); goto badarg; } index += 5; // skip tag + 4 byte size type_tmp = (char *) driver_alloc(type.size + 1); memset(type_tmp, 0, type.size + 1); memcpy(type_tmp, buf + index, type.size); index += type.size; /* decode domain */ ei_decode_ei_term(buf, &index, &domain); if (domain.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); driver_free(type_tmp); goto badarg; } index += 5; // skip tag + 4 byte size domain_tmp = (char *) driver_alloc(domain.size + 1); memset(domain_tmp, 0, domain.size + 1); memcpy(domain_tmp, buf + index, domain.size); index += domain.size; /* decode host */ ei_decode_ei_term(buf, &index, &host); if (host.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); driver_free(type_tmp); driver_free(domain_tmp); goto badarg; } index += 5; // skip tag + 4 byte size host_tmp = (char *) driver_alloc(host.size + 1); memset(host_tmp, 0, host.size + 1); memcpy(host_tmp, buf + index, host.size); index += host.size; /* decode port */ ei_decode_ei_term(buf, &index, &hostport); if (hostport.ei_type != ERL_INTEGER_EXT && hostport.ei_type != ERL_SMALL_INTEGER_EXT) { driver_free(name_tmp); driver_free(type_tmp); driver_free(domain_tmp); driver_free(host_tmp); goto badarg; } /* decode txt */ ei_decode_ei_term(buf, &index, &txt); if (txt.ei_type != ERL_BINARY_EXT) { driver_free(name_tmp); driver_free(type_tmp); driver_free(domain_tmp); driver_free(host_tmp); goto badarg; } index += 5; // skip tag + 4 byte size txt_tmp = (char *) driver_alloc(txt.size + 1); memset(txt_tmp, 0, txt.size + 1); memcpy(txt_tmp, buf + index, txt.size); local_only = (0 == strcmp("localhost", host_tmp)); err = DNSServiceRegister(&dd->sd_ref, 0, // Flags local_only ? -1 : 0, // Interface: local / any name_tmp, type_tmp, local_only ? "local" : domain_tmp, host_tmp, htons(hostport.value.i_val), txt.size, txt_tmp, (DNSServiceRegisterReply) RegisterReply, dd); driver_free(name_tmp); driver_free(type_tmp); driver_free(domain_tmp); driver_free(host_tmp); driver_free(txt_tmp); } else { goto badarg; } rindex = 0; out_len = 0; ei_encode_version(NULL, &out_len); if (err == kDNSServiceErr_NoError) { #ifdef __WIN32__ dd->event = WSACreateEvent(); WSAEventSelect(DNSServiceRefSockFD(dd->sd_ref), dd->event, FD_READ); driver_select(dd->erl_port, dd->event, ERL_DRV_READ, 1); #else driver_select(dd->erl_port, (ErlDrvEvent)(size_t) DNSServiceRefSockFD(dd->sd_ref), ERL_DRV_READ, 1); #endif out_atom_text = "ok"; ei_encode_atom(NULL, &out_len, out_atom_text); if(rlen < out_len) { *rbuf = driver_alloc(out_len); rlen = out_len; } ei_encode_version(*rbuf, &rindex); ei_encode_atom(*rbuf, &rindex, out_atom_text); return out_len; } else { out_atom_text = "error"; ei_encode_tuple_header(NULL, &out_len, 2); ei_encode_atom(NULL, &out_len, out_atom_text); ei_encode_long(NULL, &out_len, 1337); if(rlen < out_len) { *rbuf = driver_alloc(out_len); rlen = out_len; } ei_encode_version(*rbuf, &rindex); ei_encode_tuple_header(*rbuf, &rindex, 2); ei_encode_atom(*rbuf, &rindex, out_atom_text); ei_encode_long(*rbuf, &rindex, (long) err); return out_len; } badarg: return -1; }
/* CF_EXPORT */ Boolean CFNetServiceBrowserSearchForDomains(CFNetServiceBrowserRef b, Boolean registrationDomains, CFStreamError* error) { __CFNetServiceBrowser* browser = (__CFNetServiceBrowser*)b; CFStreamError extra; Boolean result = FALSE; if (!error) error = &extra; memset(error, 0, sizeof(error[0])); // Retain so it doesn't go away underneath in the case of a callout. This is really // no worry for async, but makes the memmove for the error more difficult to place // for synchronous without it being here. CFRetain(browser); // Lock down the browser to start search __CFSpinLock(&(browser->_lock)); do { CFSocketContext ctxt = {0, browser, CFRetain, CFRelease, NULL}; if (!browser->_callback) { browser->_error.error = kCFNetServicesErrorInvalid; browser->_error.domain = kCFStreamErrorDomainNetServices; break; } // Check to see if there is an ongoing search already if (browser->_trigger) { // If it's a mdns search, don't allow another. if (CFGetTypeID(browser->_trigger) == CFSocketGetTypeID()) { browser->_error.error = kCFNetServicesErrorInProgress; browser->_error.domain = kCFStreamErrorDomainNetServices; break; } // It's just the cancel that hasn't fired yet, so cancel it. else { // Remove the trigger from run loops and modes _CFTypeUnscheduleFromMultipleRunLoops(browser->_trigger, browser->_schedules); // Invalidate the run loop source CFRunLoopSourceInvalidate((CFRunLoopSourceRef)(browser->_trigger)); // Release the trigger now. CFRelease(browser->_trigger); browser->_trigger = NULL; } } browser->_domainSearch = TRUE; // Create the domain search at the service discovery level browser->_error.error = DNSServiceEnumerateDomains(&browser->_browse, registrationDomains ? kDNSServiceFlagsRegistrationDomains : kDNSServiceFlagsBrowseDomains, 0, _DomainEnumReply, browser); // Fail if it did. if (browser->_error.error) { browser->_error.error = _DNSServiceErrorToCFNetServiceError(browser->_error.error); browser->_error.domain = kCFStreamErrorDomainNetServices; break; } // Create the trigger for the browse browser->_trigger = CFSocketCreateWithNative(CFGetAllocator(browser), DNSServiceRefSockFD(browser->_browse), kCFSocketReadCallBack, _SocketCallBack, &ctxt); // Make sure the CFSocket wrapper succeeded if (!browser->_trigger) { // Try to use errno for the error browser->_error.error = errno; // If it has no error in it, assume no memory if (!browser->_error.error) browser->_error.error = ENOMEM; // Correct domain and bail. browser->_error.domain = kCFStreamErrorDomainPOSIX; DNSServiceRefDeallocate(browser->_browse); browser->_browse = NULL; break; } // Tell CFSocket not to close the native socket on invalidation. CFSocketSetSocketFlags((CFSocketRef)browser->_trigger, CFSocketGetSocketFlags((CFSocketRef)browser->_trigger) & ~kCFSocketCloseOnInvalidate); // Async mode is complete at this point if (CFArrayGetCount(browser->_schedules)) { // Schedule the trigger on the run loops and modes. _CFTypeScheduleOnMultipleRunLoops(browser->_trigger, browser->_schedules); // It's now succeeded. result = TRUE; } // If there is no callback, go into synchronous mode. else { // Unlock the browser __CFSpinUnlock(&(browser->_lock)); // Wait for synchronous return result = _BrowserBlockUntilComplete(browser); // Lock down the browser __CFSpinLock(&(browser->_lock)); } } while (0); // Copy the error. memmove(error, &browser->_error, sizeof(error[0])); // Unlock the browser __CFSpinUnlock(&(browser->_lock)); // Release the earlier retain. CFRelease(browser); return result; }