Esempio n. 1
0
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;
}
Esempio n. 2
0
static int
mdns_register_record(uint16_t rrtype, const char *name, uint16_t rdlen,
                     const void *rdata)
{
  struct mdns_record *r;
  DNSServiceErrorType err;

  DPRINTF(E_DBG, L_MDNS, "Adding mDNS record %s/%u\n", name, rrtype);

  r = calloc(1, sizeof(*r));
  if (!r)
    {
      DPRINTF(E_LOG, L_MDNS, "Out of memory adding record.\n");
      return -1;
    }
  r->name = strdup(name);
  if (!(r->name))
    {
      DPRINTF(E_LOG, L_MDNS, "Out of memory adding record.\n");
      return mdns_record_free(r);
    }

  r->rrtype = rrtype;

  err = DNSServiceRegisterRecord(mdns_sdref_main, &r->recRef,
                                 kDNSServiceFlagsShared, 0, r->name,
                                 r->rrtype, kDNSServiceClass_IN, rdlen, rdata,
                                 0, mdns_record_callback, r);

  if (err != kDNSServiceErr_NoError)
    {
      DPRINTF(E_LOG, L_MDNS, "Error registering record %s, error %d\n", name,
              err);
      return mdns_record_free(r);
    }

  /* keep these around so we can display r->name in the callback */
  r->next = mdns_records;
  mdns_records = r;

  return 0;
}