/*
* Its easier to use asprintf to set the TXT record values
*/
-#define TXTRecordPrintf(rec, key, args...) { \
- char *str; \
- asprintf(&str, args); \
- TXTRecordSetValue(rec, key, strlen(str), str); \
- free(str); \
+
+int TXTRecordPrintf(TXTRecordRef * rec, const char * key, const char * fmt, ... )
+{
+ int ret = 0;
+ char *str;
+ va_list ap;
+ va_start( ap, fmt );
+
+ if( 0 > vasprintf(&str, fmt, ap ) ) {
+ va_end(ap);
+ return -1;
+ }
+ va_end(ap);
+
+ if( kDNSServiceErr_NoError != TXTRecordSetValue(rec, key, strlen(str), str) ) {
+ ret = -1;
}
-#define TXTRecordKeyPrintf(rec, k, var, args...) { \
- char *key, *str; \
- asprintf(&key, k, var); \
- asprintf(&str, args); \
- TXTRecordSetValue(rec, key, strlen(str), str); \
- free(str); free(key); \
+
+ free(str);
+ return ret;
+}
+
+int TXTRecordKeyPrintf(TXTRecordRef * rec, const char * key_fmt, int key_var, const char * fmt, ...)
+{
+ int ret = 0;
+ char *key = NULL, *str = NULL;
+ va_list ap;
+
+ if( 0 > asprintf(&key, key_fmt, key_var))
+ return -1;
+
+ va_start( ap, fmt );
+ if( 0 > vasprintf(&str, fmt, ap )) {
+ va_end(ap);
+ ret = -1;
+ goto exit;
+ }
+ va_end(ap);
+
+ if( kDNSServiceErr_NoError != TXTRecordSetValue(rec, key, strlen(str), str) ) {
+ ret = -1;
+ goto exit;
}
+exit:
+ if (str)
+ free(str);
+ if (key)
+ free(key);
+ return ret;
+}
+
+static struct pollfd *fds;
/*
* This is the thread that polls the filehandles
*/
-void *polling_thread(void *arg) {
+static void *polling_thread(void *arg) {
// First we loop through getting the filehandles and adding them to our poll, we
// need to allocate our pollfd's
DNSServiceErrorType error;
- struct pollfd *fds = calloc(svc_ref_count, sizeof(struct pollfd));
+ fds = calloc(svc_ref_count, sizeof(struct pollfd));
assert(fds);
for(int i=0; i < svc_ref_count; i++) {
return(NULL);
}
-
/*
* This is the callback for the service register function ... actually there isn't a lot
* we can do if we get problems, so we don't really need to do anything other than report
* the issue.
*/
-void RegisterReply(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode,
- const char *name, const char *regtype, const char *domain, void *context) {
-
- if(errorCode != kDNSServiceErr_NoError) {
+static void RegisterReply(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode,
+ const char *name, const char *regtype, const char *domain, void *context)
+{
+ if (errorCode != kDNSServiceErr_NoError) {
LOG(log_error, logtype_afpd, "Failed to register mDNS service: %s%s%s: code=%d",
name, regtype, domain, errorCode);
}
}
-
/*
* This function unregisters anything we have already
* registered and frees associated memory
*/
static void unregister_stuff() {
- pthread_kill(poller, SIGKILL);
+ pthread_cancel(poller);
+
+ for (int i = 0; i < svc_ref_count; i++)
+ close(fds[i].fd);
+ free(fds);
+ fds = NULL;
+
if(svc_refs) {
for(int i=0; i < svc_ref_count; i++) {
DNSServiceRefDeallocate(svc_refs[i]);
/* Register our service, prepare the TXT record */
TXTRecordCreate(&txt_adisk, 0, NULL);
- TXTRecordPrintf(&txt_adisk, "sys", "waMa=0,adVF=0x100");
+ if( 0 > TXTRecordPrintf(&txt_adisk, "sys", "waMa=0,adVF=0x100") ) {
+ LOG ( log_error, logtype_afpd, "Could not create Zeroconf TXTRecord for sys");
+ goto fail;
+ }
/* Build AFP volumes list */
int i = 0;
for (volume = getvolumes(); volume; volume = volume->v_next) {
- if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0) {
+ if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, -1, tmpname, 255) <= 0) {
LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
goto fail;
}
if (volume->v_uuid) {
LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
volume->v_localname, volume->v_uuid);
- TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1,adVU=%s",
- tmpname, volume->v_uuid);
+ if( 0 > TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1,adVU=%s",
+ tmpname, volume->v_uuid) ) {
+ LOG ( log_error, logtype_afpd, "Could not set Zeroconf TXTRecord for dk%u", i);
+ goto fail;
+ }
} else {
LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
volume->v_localname);
- TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1", tmpname);
+ if( 0 > TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1", tmpname) ) {
+ LOG ( log_error, logtype_afpd, "Could not set Zeroconf TXTRecord for dk%u", i);
+ goto fail;
+ }
}
}
}
// Allocate the memory to store our service refs
svc_refs = calloc(svc_ref_count, sizeof(DNSServiceRef));
- assert(svc_ref);
+ assert(svc_refs);
svc_ref_count = 0;
/* AFP server */
LOG(log_info, logtype_afpd, "Registering server '%s' with model '%s'",
dsi->bonjourname, obj->options.mimicmodel);
TXTRecordCreate(&txt_devinfo, 0, NULL);
- TXTRecordPrintf(&txt_devinfo, "model=%s", obj->options.mimicmodel);
+ if( 0 > TXTRecordPrintf(&txt_devinfo, "model", obj->options.mimicmodel) ) {
+ LOG ( log_error, logtype_afpd, "Could not create Zeroconf TXTRecord for model");
+ goto fail;
+ }
+
error = DNSServiceRegister(&svc_refs[svc_ref_count++],
0, // no flags
0, // all network interfaces
DEV_INFO_SERVICE_TYPE,
"", // default domains
NULL, // default host name
- 0,
+ /*
+ * We would probably use port 0 zero, but we can't, from man DNSServiceRegister:
+ * "A value of 0 for a port is passed to register placeholder services.
+ * Place holder services are not found when browsing, but other
+ * clients cannot register with the same name as the placeholder service."
+ * We therefor use port 9 which is used by the adisk service type.
+ */
+ htons(9),
TXTRecordGetLength(&txt_devinfo),
TXTRecordGetBytesPtr(&txt_devinfo),
RegisterReply, // callback