#include <stdlib.h>
#include <string.h>
#include "dns_sd.h"
#if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
#pragma export on
#endif
#if defined(_WIN32)
#pragma warning(disable:4244)
#endif
#define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
static int DomainEndsInDot(const char *dom)
{
while (dom[0] && dom[1])
{
if (dom[0] == '\\') {
if (mdnsIsDigit(dom[1]) && mdnsIsDigit(dom[2]) && mdnsIsDigit(dom[3]))
dom += 4; else dom += 2; }
else dom++; }
return (dom[0] == '.');
}
static uint8_t *InternalTXTRecordSearch
(
uint16_t txtLen,
const void *txtRecord,
const char *key,
unsigned long *keylen
)
{
uint8_t *p = (uint8_t*)txtRecord;
uint8_t *e = p + txtLen;
*keylen = (unsigned long) strlen(key);
while (p<e)
{
uint8_t *x = p;
p += 1 + p[0];
if (p <= e && *keylen <= x[0] && !strncmp(key, (char*)x+1, *keylen))
if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
}
return(NULL);
}
int DNSSD_API DNSServiceConstructFullName
(
char *fullName,
const char *service,
const char *regtype,
const char *domain
)
{
unsigned long len;
unsigned char c;
char *fn = fullName;
const char *s = service;
const char *r = regtype;
const char *d = domain;
if (service)
{
while(*s)
{
c = (unsigned char)*s++;
if (c == '.' || (c == '\\')) *fn++ = '\\'; else if (c <= ' ') {
*fn++ = '\\';
*fn++ = (char) ('0' + (c / 100));
*fn++ = (char) ('0' + (c / 10) % 10);
c = (unsigned char)('0' + (c % 10));
}
*fn++ = (char)c;
}
*fn++ = '.';
}
if (!regtype) return -1;
len = (unsigned long) strlen(regtype);
if (DomainEndsInDot(regtype)) len--;
if (len < 6) return -1; if (strncmp((regtype + len - 4), "_tcp", 4) && strncmp((regtype + len - 4), "_udp", 4)) return -1;
while(*r) *fn++ = *r++;
if (!DomainEndsInDot(regtype)) *fn++ = '.';
if (!domain || !domain[0]) return -1;
while(*d) *fn++ = *d++;
if (!DomainEndsInDot(domain)) *fn++ = '.';
*fn = '\0';
return 0;
}
typedef struct _TXTRecordRefRealType
{
uint8_t *buffer; uint16_t buflen; uint16_t datalen; uint16_t malloced; } TXTRecordRefRealType;
#define txtRec ((TXTRecordRefRealType*)txtRecord)
struct dnssd_clientlib_CompileTimeAssertionCheck
{
char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
};
void TXTRecordCreate
(
TXTRecordRef *txtRecord,
uint16_t bufferLen,
void *buffer
)
{
txtRec->buffer = buffer;
txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
txtRec->datalen = 0;
txtRec->malloced = 0;
}
void TXTRecordDeallocate(TXTRecordRef *txtRecord)
{
if (txtRec->malloced) free(txtRec->buffer);
}
DNSServiceErrorType TXTRecordSetValue
(
TXTRecordRef *txtRecord,
const char *key,
uint8_t valueSize,
const void *value
)
{
uint8_t *start, *p;
const char *k;
unsigned long keysize, keyvalsize;
for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
keysize = (unsigned long)(k - key);
keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
(void)TXTRecordRemoveValue(txtRecord, key);
if (txtRec->datalen + keyvalsize > txtRec->buflen)
{
unsigned char *newbuf;
unsigned long newlen = txtRec->datalen + keyvalsize;
if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
newbuf = malloc((size_t)newlen);
if (!newbuf) return(kDNSServiceErr_NoMemory);
memcpy(newbuf, txtRec->buffer, txtRec->datalen);
if (txtRec->malloced) free(txtRec->buffer);
txtRec->buffer = newbuf;
txtRec->buflen = (uint16_t)(newlen);
txtRec->malloced = 1;
}
start = txtRec->buffer + txtRec->datalen;
p = start + 1;
memcpy(p, key, keysize);
p += keysize;
if (value)
{
*p++ = '=';
memcpy(p, value, valueSize);
p += valueSize;
}
*start = (uint8_t)(p - start - 1);
txtRec->datalen += p - start;
return(kDNSServiceErr_NoError);
}
DNSServiceErrorType TXTRecordRemoveValue
(
TXTRecordRef *txtRecord,
const char *key
)
{
unsigned long keylen, itemlen, remainder;
uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
if (!item) return(kDNSServiceErr_NoSuchKey);
itemlen = (unsigned long)(1 + item[0]);
remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
memmove(item, item + itemlen, remainder);
txtRec->datalen -= itemlen;
return(kDNSServiceErr_NoError);
}
uint16_t TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
const void * TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
int TXTRecordContainsKey
(
uint16_t txtLen,
const void *txtRecord,
const char *key
)
{
unsigned long keylen;
return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
}
const void * TXTRecordGetValuePtr
(
uint16_t txtLen,
const void *txtRecord,
const char *key,
uint8_t *valueLen
)
{
unsigned long keylen;
uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
if (!item || item[0] <= keylen) return(NULL); *valueLen = (uint8_t)(item[0] - (keylen + 1));
return (item + 1 + keylen + 1);
}
uint16_t TXTRecordGetCount
(
uint16_t txtLen,
const void *txtRecord
)
{
uint16_t count = 0;
uint8_t *p = (uint8_t*)txtRecord;
uint8_t *e = p + txtLen;
while (p<e) { p += 1 + p[0]; count++; }
return((p>e) ? (uint16_t)0 : count);
}
DNSServiceErrorType TXTRecordGetItemAtIndex
(
uint16_t txtLen,
const void *txtRecord,
uint16_t index,
uint16_t keyBufLen,
char *key,
uint8_t *valueLen,
const void **value
)
{
uint16_t count = 0;
uint8_t *p = (uint8_t*)txtRecord;
uint8_t *e = p + txtLen;
while (p<e && count<index) { p += 1 + p[0]; count++; } if (p<e && p + 1 + p[0] <= e) {
uint8_t *x = p+1;
unsigned long len = 0;
e = p + 1 + p[0];
while (x+len<e && x[len] != '=') len++;
if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
memcpy(key, x, len);
key[len] = 0;
if (x+len<e) {
*value = x + len + 1;
*valueLen = (uint8_t)(p[0] - (len + 1));
}
else
{
*value = NULL;
*valueLen = 0;
}
return(kDNSServiceErr_NoError);
}
return(kDNSServiceErr_Invalid);
}