JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleRegistration_BeginRegister( JNIEnv *pEnv, jobject pThis, jint ifIndex, jint flags, jstring serviceName, jstring regType, jstring domain, jstring host, jint port, jbyteArray txtRecord) { //syslog(LOG_ERR, "BR"); jclass cls = (*pEnv)->GetObjectClass( pEnv, pThis); jfieldID contextField = (*pEnv)->GetFieldID( pEnv, cls, "fNativeContext", "J"); OpContext *pContext = NULL; DNSServiceErrorType err = kDNSServiceErr_NoError; jbyte *pBytes; jsize numBytes; //syslog(LOG_ERR, "BR: contextField %d", contextField); if ( contextField != 0) pContext = NewContext( pEnv, pThis, "serviceRegistered", "(Lcom/apple/dnssd/DNSSDRegistration;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); else err = kDNSServiceErr_BadParam; if ( pContext != NULL) { const char *servStr = SafeGetUTFChars( pEnv, serviceName); const char *regStr = SafeGetUTFChars( pEnv, regType); const char *domainStr = SafeGetUTFChars( pEnv, domain); const char *hostStr = SafeGetUTFChars( pEnv, host); //syslog(LOG_ERR, "BR: regStr %s", regStr); // Since Java ints are defined to be big-endian, we de-canonicalize 'port' from a // big-endian number into a 16-bit pattern here. uint16_t portBits = port; portBits = ( ((unsigned char*) &portBits)[0] << 8) | ((unsigned char*) &portBits)[1]; pBytes = txtRecord ? (*pEnv)->GetByteArrayElements( pEnv, txtRecord, NULL) : NULL; numBytes = txtRecord ? (*pEnv)->GetArrayLength( pEnv, txtRecord) : 0; err = DNSServiceRegister( &pContext->ServiceRef, flags, ifIndex, servStr, regStr, domainStr, hostStr, portBits, numBytes, pBytes, ServiceRegisterReply, pContext); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, pThis, contextField, (long) pContext); } if ( pBytes != NULL) (*pEnv)->ReleaseByteArrayElements( pEnv, txtRecord, pBytes, 0); SafeReleaseUTFChars( pEnv, serviceName, servStr); SafeReleaseUTFChars( pEnv, regType, regStr); SafeReleaseUTFChars( pEnv, domain, domainStr); SafeReleaseUTFChars( pEnv, host, hostStr); } else err = kDNSServiceErr_NoMemory; return err; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleQuery_CreateQuery( JNIEnv *pEnv, jobject pThis, jint flags, jint ifIndex, jstring serviceName, jint rrtype, jint rrclass) { 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, "queryAnswered", "(Lcom/apple/dnssd/DNSSDService;IILjava/lang/String;II[BI)V"); else err = kDNSServiceErr_BadParam; if ( pContext != NULL) { const char *servStr = SafeGetUTFChars( pEnv, serviceName); err = DNSServiceQueryRecord( &pContext->ServiceRef, flags, ifIndex, servStr, rrtype, rrclass, ServiceQueryReply, pContext); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, pThis, contextField, (long) pContext); } SafeReleaseUTFChars( pEnv, serviceName, servStr); } else err = kDNSServiceErr_NoMemory; return err; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleRecordRegistrar_RegisterRecord( JNIEnv *pEnv, jobject pThis, jint flags, jint ifIndex, jstring fullname, jint rrType, jint rrClass, jbyteArray rData, jint ttl, jobject destObj) { jclass cls = (*pEnv)->GetObjectClass( pEnv, pThis); jfieldID contextField = (*pEnv)->GetFieldID( pEnv, cls, "fNativeContext", "J"); jclass destCls = (*pEnv)->GetObjectClass( pEnv, destObj); jfieldID recField = (*pEnv)->GetFieldID( pEnv, destCls, "fRecord", "J"); const char *nameStr = SafeGetUTFChars( pEnv, fullname); OpContext *pContext = NULL; DNSServiceErrorType err = kDNSServiceErr_NoError; jbyte *pBytes; jsize numBytes; DNSRecordRef recRef; RecordRegistrationRef *regEnvelope; if ( contextField != 0) pContext = (OpContext*) (long) (*pEnv)->GetLongField(pEnv, pThis, contextField); if ( pContext == NULL || pContext->ServiceRef == NULL || nameStr == NULL) return kDNSServiceErr_BadParam; regEnvelope = calloc( 1, sizeof *regEnvelope); if ( regEnvelope == NULL) return kDNSServiceErr_NoMemory; regEnvelope->Context = pContext; regEnvelope->RecordObj = (*pEnv)->NewWeakGlobalRef( pEnv, destObj); // must convert local ref to global to cache pBytes = (*pEnv)->GetByteArrayElements( pEnv, rData, NULL); numBytes = (*pEnv)->GetArrayLength( pEnv, rData); err = DNSServiceRegisterRecord( pContext->ServiceRef, &recRef, flags, ifIndex, nameStr, rrType, rrClass, numBytes, pBytes, ttl, RegisterRecordReply, regEnvelope); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, destObj, recField, (long) recRef); } else { if ( regEnvelope->RecordObj != NULL) (*pEnv)->DeleteWeakGlobalRef( pEnv, regEnvelope->RecordObj); free( regEnvelope); } if ( pBytes != NULL) (*pEnv)->ReleaseByteArrayElements( pEnv, rData, pBytes, 0); SafeReleaseUTFChars( pEnv, fullname, nameStr); return err; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleDNSSD_GetIfIndexForName( JNIEnv *pEnv, jobject pThis _UNUSED, jstring ifName) { uint32_t ifIndex = kDNSServiceInterfaceIndexLocalOnly; const char *nameStr = SafeGetUTFChars( pEnv, ifName); if (strcmp(nameStr, LOCAL_ONLY_NAME)) ifIndex = if_nametoindex( nameStr); SafeReleaseUTFChars( pEnv, ifName, nameStr); return ifIndex; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleDNSSD_ConstructName( JNIEnv *pEnv, jobject pThis _UNUSED, jstring serviceName, jstring regtype, jstring domain, jobjectArray pOut) { DNSServiceErrorType err = kDNSServiceErr_NoError; const char *nameStr = SafeGetUTFChars( pEnv, serviceName); const char *regStr = SafeGetUTFChars( pEnv, regtype); const char *domStr = SafeGetUTFChars( pEnv, domain); char buff[ kDNSServiceMaxDomainName + 1]; err = DNSServiceConstructFullName( buff, nameStr, regStr, domStr); if ( err == kDNSServiceErr_NoError) { // pOut is expected to be a String[1] array. (*pEnv)->SetObjectArrayElement( pEnv, pOut, 0, (*pEnv)->NewStringUTF( pEnv, buff)); } SafeReleaseUTFChars( pEnv, serviceName, nameStr); SafeReleaseUTFChars( pEnv, regtype, regStr); SafeReleaseUTFChars( pEnv, domain, domStr); return err; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleBrowser_CreateBrowser( JNIEnv *pEnv, jobject pThis, jint flags, jint ifIndex, jstring regType, jstring domain) { 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, "serviceFound", "(Lcom/apple/dnssd/DNSSDService;IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); else err = kDNSServiceErr_BadParam; if ( pContext != NULL) { const char *regStr = SafeGetUTFChars( pEnv, regType); const char *domainStr = SafeGetUTFChars( pEnv, domain); pContext->Callback2 = (*pEnv)->GetMethodID( pEnv, (*pEnv)->GetObjectClass( pEnv, pContext->ClientObj), "serviceLost", "(Lcom/apple/dnssd/DNSSDService;IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); err = DNSServiceBrowse( &pContext->ServiceRef, flags, ifIndex, regStr, domainStr, ServiceBrowseReply, pContext); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, pThis, contextField, (long) pContext); } SafeReleaseUTFChars( pEnv, regType, regStr); SafeReleaseUTFChars( pEnv, domain, domainStr); } else err = kDNSServiceErr_NoMemory; return err; }
JNIEXPORT jint JNICALL Java_com_apple_dnssd_AppleResolver_CreateResolver( JNIEnv *pEnv, jobject pThis, jint flags, jint ifIndex, jstring serviceName, jstring regType, jstring domain) { 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, "serviceResolved", "(Lcom/apple/dnssd/DNSSDService;II[B[BILcom/apple/dnssd/TXTRecord;)V"); else err = kDNSServiceErr_BadParam; if ( pContext != NULL) { const char *servStr = SafeGetUTFChars( pEnv, serviceName); const char *regStr = SafeGetUTFChars( pEnv, regType); const char *domainStr = SafeGetUTFChars( pEnv, domain); err = DNSServiceResolve( &pContext->ServiceRef, flags, ifIndex, servStr, regStr, domainStr, ServiceResolveReply, pContext); if ( err == kDNSServiceErr_NoError) { (*pEnv)->SetLongField(pEnv, pThis, contextField, (long) pContext); } SafeReleaseUTFChars( pEnv, serviceName, servStr); SafeReleaseUTFChars( pEnv, regType, regStr); SafeReleaseUTFChars( pEnv, domain, domainStr); } else err = kDNSServiceErr_NoMemory; return err; }
JNIEXPORT void JNICALL Java_com_apple_dnssd_AppleDNSSD_ReconfirmRecord( JNIEnv *pEnv, jobject pThis _UNUSED, jint flags, jint ifIndex, jstring fullName, jint rrtype, jint rrclass, jbyteArray rdata) { jbyte *pBytes; jsize numBytes; const char *nameStr = SafeGetUTFChars( pEnv, fullName); pBytes = (*pEnv)->GetByteArrayElements( pEnv, rdata, NULL); numBytes = (*pEnv)->GetArrayLength( pEnv, rdata); DNSServiceReconfirmRecord( flags, ifIndex, nameStr, rrtype, rrclass, numBytes, pBytes); if ( pBytes != NULL) (*pEnv)->ReleaseByteArrayElements( pEnv, rdata, pBytes, 0); SafeReleaseUTFChars( pEnv, fullName, nameStr); }