owntone-server/src/rend-posix.c

573 lines
18 KiB
C
Raw Normal View History

2003-10-30 17:41:56 -05:00
/*
* $Id$
*
* Do the zeroconf/mdns/rendezvous (tm) thing. This is a hacked version
* of Apple's Responder.c from the Rendezvous (tm) POSIX implementation
2003-12-29 15:41:08 -05:00
*
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2003-10-30 17:41:56 -05:00
*/
/*
* Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.2 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
File: responder.c
Contains: Code to implement an mDNS responder on the Posix platform.
Written by: Quinn
Copyright: Copyright (c) 2002 by Apple Computer, Inc., All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
$Log$
Revision 1.25 2005/08/16 02:26:32 rpedde
Add interface directive to config file -- fix stderr logging on rendezvous child
2005-08-14 23:16:54 -04:00
Revision 1.24 2005/08/15 03:16:54 rpedde
specify interface to register
2005-01-07 01:57:59 -05:00
Revision 1.23 2005/01/07 06:57:59 rpedde
fix minor errno problem
2004-12-09 00:05:54 -05:00
Revision 1.22 2004/12/09 05:05:54 rpedde
Logging fixes
Revision 1.21 2004/11/30 04:17:32 rpedde
use pascal packed string to avoid invalid rdata error
Revision 1.20 2004/11/30 04:04:17 rpedde
database id txt record to store settings
2004-11-13 02:14:26 -05:00
Revision 1.19 2004/11/13 07:14:26 rpedde
modularize debugging statements
2004-04-19 02:19:46 -04:00
Revision 1.18 2004/04/19 06:19:46 rpedde
Starting to fix signal stuff
Revision 1.17 2004/03/29 19:44:58 rpedde
Move mdns stuff out of mdns subdir to help compile on older automakes
2004-03-08 14:21:03 -05:00
Revision 1.16 2004/03/08 19:21:03 rpedde
start of background scanning
2004-03-01 20:35:31 -05:00
Revision 1.15 2004/03/02 01:35:31 rpedde
fix domain
2004-03-01 19:03:37 -05:00
Revision 1.14 2004/03/02 00:03:37 rpedde
Merge new rendezvous code
2004-03-01 11:29:42 -05:00
Revision 1.13 2004/03/01 16:29:42 rpedde
Fix logging
2004-02-25 11:13:37 -05:00
Revision 1.12 2004/02/25 16:13:37 rpedde
More -Wall cleanups
2004-02-09 13:33:59 -05:00
Revision 1.11 2004/02/09 18:33:59 rpedde
Pretty up
2004-01-19 23:41:20 -05:00
Revision 1.10 2004/01/20 04:41:20 rpedde
merge new-rend-branch
Revision 1.9.2.1 2004/01/16 20:51:01 rpedde
Convert rend-posix to message-based system
2004-01-04 00:02:23 -05:00
Revision 1.9 2004/01/04 05:02:23 rpedde
fix segfault on dropping privs
2003-12-29 18:39:18 -05:00
Revision 1.8 2003/12/29 23:39:18 ron
add priv dropping
2003-12-29 15:41:08 -05:00
Revision 1.7 2003/12/29 20:41:08 ron
Make sure all files have GPL notice
2003-11-26 01:12:53 -05:00
Revision 1.6 2003/11/26 06:12:53 ron
Exclude from memory checks
Revision 1.5 2003/11/23 18:13:15 ron
Exit rather than returning... shouldn't make a difference, but does. ?
Revision 1.4 2003/11/20 21:58:22 ron
More diag logging, move WS_PRIVATE into the WS_CONNINFO
2003-11-17 11:40:09 -05:00
Revision 1.3 2003/11/17 16:40:09 ron
add support for named db
2003-11-13 23:54:55 -05:00
Revision 1.2 2003/11/14 04:54:55 ron
Use port 53
2003-10-30 17:41:56 -05:00
Revision 1.1 2003/10/30 22:41:56 ron
Initial checkin
Revision 1.3 2002/09/21 20:44:53 zarzycki
Added APSL info
Revision 1.2 2002/09/19 04:20:44 cheshire
Remove high-ascii characters that confuse some systems
Revision 1.1 2002/09/17 06:24:35 cheshire
First checkin
*/
#include "mDNSClientAPI.h"// Defines the interface to the client layer above
#include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
2003-10-30 17:41:56 -05:00
#include <assert.h>
#include <stdio.h> // For printf()
#include <stdlib.h> // For exit() etc.
#include <string.h> // For strlen() etc.
#include <unistd.h> // For select()
#include <errno.h> // For errno, EINTR
#include <signal.h>
#include <fcntl.h>
2003-12-29 18:39:18 -05:00
#include <pwd.h>
#include <sys/types.h>
2003-10-30 17:41:56 -05:00
2003-11-26 01:12:53 -05:00
#define __IN_ERR__
2004-02-25 11:13:37 -05:00
#include "daapd.h"
2003-10-30 17:41:56 -05:00
#include "err.h"
2004-01-19 23:41:20 -05:00
#include "rend.h"
#include "rend-unix.h"
2003-10-30 17:41:56 -05:00
/*
* I'll take some extra hackishness, please...
*/
typedef struct PosixNetworkInterface PosixNetworkInterface;
struct PosixNetworkInterface {
NetworkInterfaceInfo coreIntf;
const char * intfName;
PosixNetworkInterface * aliasIntf;
int index;
int multicastSocket;
int multicastSocketv6;
};
2003-10-30 17:41:56 -05:00
static mDNS mDNSStorage; // mDNS core uses this to store its globals
static mDNS_PlatformSupport PlatformStorage; // Stores this platform's globals
static volatile mDNSBool gStopNow;
/* modified signal handling code - rep 21 Oct 2k3 */
// o SIGINT causes an orderly shutdown of the program.
// o SIGQUIT causes a somewhat orderly shutdown (direct but dangerous)
//
// There are fatal race conditions in our signal handling, but there's not much
// we can do about them while remaining within the Posix space. Specifically,
// if a signal arrives after we test the globals its sets but before we call
// select, the signal will be dropped. The user will have to send the signal
// again. Unfortunately, Posix does not have a "sigselect" to atomically
// modify the signal mask and start a select.
static void HandleSigInt(int sigraised)
// A handler for SIGINT that causes us to break out of the
// main event loop when the user types ^C. This has the
// effect of quitting the program.
{
assert(sigraised == SIGINT);
2004-11-13 02:14:26 -05:00
DPRINTF(E_INF,L_REND,"SIGINT\n");
2003-10-30 17:41:56 -05:00
gStopNow = mDNStrue;
}
static void HandleSigQuit(int sigraised)
// If we get a SIGQUIT the user is desperate and we
// just call mDNS_Close directly. This is definitely
// not safe (because it could reenter mDNS), but
// we presume that the user has already tried the safe
// alternatives.
{
assert(sigraised == SIGQUIT);
2004-11-13 02:14:26 -05:00
DPRINTF(E_INF,L_REND,"SIGQUIT\n");
2003-10-30 17:41:56 -05:00
mDNS_Close(&mDNSStorage);
exit(0);
}
/* get rid of pidfile handling - rep - 21 Oct 2k3 */
static const char kDefaultServiceType[] = "_http._tcp.";
enum {
kDefaultPortNumber = 80
};
typedef struct PosixService PosixService;
struct PosixService {
ServiceRecordSet coreServ;
PosixService *next;
int serviceID;
};
static PosixService *gServiceList = NULL;
static void RegistrationCallback(mDNS *const m, ServiceRecordSet *const thisRegistration, mStatus status)
// mDNS core calls this routine to tell us about the status of
// our registration. The appropriate action to take depends
// entirely on the value of status.
{
switch (status) {
case mStatus_NoError:
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Callback: Name Registered\n");
2003-10-30 17:41:56 -05:00
// Do nothing; our name was successfully registered. We may
// get more call backs in the future.
break;
case mStatus_NameConflict:
2004-11-13 02:14:26 -05:00
DPRINTF(E_WARN,L_REND,"Callback: Name Conflict\n");
2003-10-30 17:41:56 -05:00
// In the event of a conflict, this sample RegistrationCallback
// just calls mDNS_RenameAndReregisterService to automatically
// pick a new unique name for the service. For a device such as a
// printer, this may be appropriate. For a device with a user
// interface, and a screen, and a keyboard, the appropriate response
// may be to prompt the user and ask them to choose a new name for
// the service.
//
// Also, what do we do if mDNS_RenameAndReregisterService returns an
// error. Right now I have no place to send that error to.
2004-03-01 19:03:37 -05:00
status = mDNS_RenameAndReregisterService(m, thisRegistration, mDNSNULL);
2003-10-30 17:41:56 -05:00
assert(status == mStatus_NoError);
break;
case mStatus_MemFree:
2004-11-13 02:14:26 -05:00
DPRINTF(E_WARN,L_REND,"Callback: Memory Free\n");
2003-10-30 17:41:56 -05:00
// When debugging is enabled, make sure that thisRegistration
// is not on our gServiceList.
#if defined(DEBUG)
{
PosixService *cursor;
cursor = gServiceList;
while (cursor != NULL) {
assert(&cursor->coreServ != thisRegistration);
cursor = cursor->next;
}
}
#endif
free(thisRegistration);
break;
default:
2004-11-13 02:14:26 -05:00
DPRINTF(E_WARN,L_REND,"Callback: Unknown Status %d\n",status);
2003-10-30 17:41:56 -05:00
break;
}
}
static int gServiceID = 0;
static mStatus RegisterOneService(const char * richTextHostName,
const char * serviceType,
2004-03-01 19:03:37 -05:00
const char * serviceDomain,
2003-10-30 17:41:56 -05:00
const mDNSu8 text[],
mDNSu16 textLen,
2005-08-14 23:16:54 -04:00
long portNumber,
mDNSInterfaceID id)
2003-10-30 17:41:56 -05:00
{
mStatus status;
PosixService * thisServ;
mDNSOpaque16 port;
domainlabel name;
domainname type;
domainname domain;
status = mStatus_NoError;
thisServ = (PosixService *) malloc(sizeof(*thisServ));
if (thisServ == NULL) {
status = mStatus_NoMemoryErr;
}
if (status == mStatus_NoError) {
2004-03-01 19:03:37 -05:00
MakeDomainLabelFromLiteralString(&name, richTextHostName);
MakeDomainNameFromDNSNameString(&type, serviceType);
MakeDomainNameFromDNSNameString(&domain, serviceDomain);
2003-10-30 17:41:56 -05:00
port.b[0] = (portNumber >> 8) & 0x0FF;
port.b[1] = (portNumber >> 0) & 0x0FF;;
status = mDNS_RegisterService(&mDNSStorage, &thisServ->coreServ,
&name, &type, &domain,
NULL,
port,
text, textLen,
2004-03-01 19:03:37 -05:00
NULL, 0,
2005-08-14 23:16:54 -04:00
id,
2003-10-30 17:41:56 -05:00
RegistrationCallback, thisServ);
}
if (status == mStatus_NoError) {
thisServ->serviceID = gServiceID;
gServiceID += 1;
thisServ->next = gServiceList;
gServiceList = thisServ;
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,
2004-03-01 20:35:31 -05:00
"Registered service %d, name '%s', type '%s', domain '%s', port %ld\n",
2003-10-30 17:41:56 -05:00
thisServ->serviceID,
richTextHostName,
serviceType,
2004-03-01 20:35:31 -05:00
serviceDomain,
2003-10-30 17:41:56 -05:00
portNumber);
} else {
if (thisServ != NULL) {
free(thisServ);
}
}
return status;
}
static void DeregisterOurServices(void)
{
PosixService *thisServ;
int thisServID;
while (gServiceList != NULL) {
thisServ = gServiceList;
gServiceList = thisServ->next;
thisServID = thisServ->serviceID;
mDNS_DeregisterService(&mDNSStorage, &thisServ->coreServ);
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Deregistered service %d\n",
2003-10-30 17:41:56 -05:00
thisServ->serviceID);
}
}
mDNSInterfaceID rend_get_interface_id(char *iface) {
2005-08-14 23:16:54 -04:00
PosixNetworkInterface *pni;
if(!iface)
return mDNSInterface_Any;
if(!strlen(iface))
2005-08-14 23:16:54 -04:00
return mDNSInterface_Any;
DPRINTF(E_LOG,L_REND,"Searching for interface %s\n",iface);
pni=(PosixNetworkInterface*)mDNSStorage.HostInterfaces;
while(pni) {
DPRINTF(E_INF,L_REND,"Found interface %s, index %d\n",pni->intfName,
pni->index);
if(strcasecmp(pni->intfName,iface) == 0) {
DPRINTF(E_INF,L_REND,"Found interface id: %d\n",pni->coreIntf.InterfaceID);
return pni->coreIntf.InterfaceID;
}
pni=(PosixNetworkInterface*)(pni->coreIntf.next);
2005-08-14 23:16:54 -04:00
}
DPRINTF(E_INF,L_REND,"Could not find interface.\n");
return mDNSInterface_Any;
2005-08-14 23:16:54 -04:00
}
2004-01-19 23:41:20 -05:00
/*
* rend_callback
*
* This is borrowed from the OSX rend client
*/
void rend_callback(void) {
REND_MESSAGE msg;
int result;
2004-02-09 13:33:59 -05:00
int err;
2005-08-14 23:16:54 -04:00
mDNSInterfaceID id;
2004-01-19 23:41:20 -05:00
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Processing rendezvous message\n");
2004-01-19 23:41:20 -05:00
/* here, we've seen the message, now we have to process it */
if((result=rend_read_message(&msg)) != sizeof(msg)) {
2004-02-09 13:33:59 -05:00
err=errno;
2004-12-09 00:05:54 -05:00
DPRINTF(E_FATAL,L_REND,"Rendezvous socket closed (daap server crashed?) Aborting.\n");
2004-02-09 13:33:59 -05:00
gStopNow=mDNStrue;
return;
2004-01-19 23:41:20 -05:00
}
switch(msg.cmd) {
case REND_MSG_TYPE_REGISTER:
2005-08-14 23:16:54 -04:00
id=rend_get_interface_id(msg.interface);
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Registering %s.%s (%d)\n",msg.name,msg.type,msg.port);
RegisterOneService(msg.name,msg.type,"local.","\034Database ID=beddab1edeadbea7",29,
2005-08-14 23:16:54 -04:00
msg.port,id);
2004-01-19 23:41:20 -05:00
rend_send_response(0); /* success */
break;
case REND_MSG_TYPE_UNREGISTER:
rend_send_response(1); /* error */
break;
case REND_MSG_TYPE_STOP:
2004-11-13 02:14:26 -05:00
DPRINTF(E_INF,L_REND,"Stopping mDNS\n");
2004-01-19 23:41:20 -05:00
gStopNow = mDNStrue;
rend_send_response(0);
break;
case REND_MSG_TYPE_STATUS:
rend_send_response(1);
break;
default:
break;
}
}
2003-10-30 17:41:56 -05:00
2004-01-19 23:41:20 -05:00
int rend_private_init(char *user) {
2003-10-30 17:41:56 -05:00
mStatus status;
mDNSBool result;
fprintf(stderr,"w00t\n");
2003-10-30 17:41:56 -05:00
status = mDNS_Init(&mDNSStorage, &PlatformStorage,
mDNS_Init_NoCache, mDNS_Init_ZeroCacheSize,
mDNS_Init_AdvertiseLocalAddresses,
mDNS_Init_NoInitCallback, mDNS_Init_NoInitCallbackContext);
2004-01-19 23:41:20 -05:00
2003-10-30 17:41:56 -05:00
if (status != mStatus_NoError) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_FATAL,L_REND,"mDNS Error %d\n",status);
2003-10-30 17:41:56 -05:00
return(-1);
}
2004-01-19 23:41:20 -05:00
if(drop_privs(user))
return -1;
2003-10-30 17:41:56 -05:00
signal(SIGINT, HandleSigInt); // SIGINT is what you get for a Ctrl-C
signal(SIGQUIT, HandleSigQuit); // SIGQUIT is what you get for a Ctrl-\ (indeed)
2004-04-19 02:19:46 -04:00
signal(SIGHUP, SIG_IGN); // SIGHUP might happen from a request to reload the daap server
2004-01-19 23:41:20 -05:00
2003-10-30 17:41:56 -05:00
while (!gStopNow) {
2004-01-19 23:41:20 -05:00
int nfds = 1;
2003-10-30 17:41:56 -05:00
fd_set readfds;
struct timeval timeout;
int result;
// 1. Set up the fd_set as usual here.
// This example client has no file descriptors of its own,
// but a real application would call FD_SET to add them to the set here
FD_ZERO(&readfds);
2004-01-19 23:41:20 -05:00
FD_SET(rend_pipe_to[RD_SIDE],&readfds);
2003-10-30 17:41:56 -05:00
// 2. Set up the timeout.
// This example client has no other work it needs to be doing,
// so we set an effectively infinite timeout
timeout.tv_sec = 0x3FFFFFFF;
timeout.tv_usec = 0;
// 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
mDNSPosixGetFDSet(&mDNSStorage, &nfds, &readfds, &timeout);
// 4. Call select as normal
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"select(%d, %d.%06d)\n", nfds,
2003-10-30 17:41:56 -05:00
timeout.tv_sec, timeout.tv_usec);
result = select(nfds, &readfds, NULL, NULL, &timeout);
if (result < 0) {
if (errno != EINTR) gStopNow = mDNStrue;
2005-01-07 01:57:59 -05:00
DPRINTF(E_WARN,L_REND,"select() returned %d errno %d\n", result, errno);
2003-10-30 17:41:56 -05:00
} else {
// 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
2004-03-01 19:03:37 -05:00
mDNSPosixProcessFDSet(&mDNSStorage, &readfds);
2003-10-30 17:41:56 -05:00
// 6. This example client has no other work it needs to be doing,
// but a real client would do its work here
// ... (do work) ...
2004-01-19 23:41:20 -05:00
if(FD_ISSET(rend_pipe_to[RD_SIDE],&readfds)) {
rend_callback();
}
2003-10-30 17:41:56 -05:00
}
}
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Exiting\n");
2003-10-30 17:41:56 -05:00
DeregisterOurServices();
mDNS_Close(&mDNSStorage);
if (status == mStatus_NoError) {
result = 0;
} else {
result = 2;
}
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND, "Finished with status %ld, result %d\n",
2003-10-30 17:41:56 -05:00
status, result);
exit(result);
2003-10-30 17:41:56 -05:00
}
2004-01-19 23:41:20 -05:00