owntone-server/src/rend-howl.c

188 lines
4.6 KiB
C
Raw Normal View History

2003-11-17 17:28:48 -05:00
/*
* $Id$
2003-12-29 15:41:08 -05:00
* Rendezvous for SwampWolf's Howl (http://www.swampwolf.com)
2003-11-17 17:28:48 -05:00
*
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-11-17 17:28:48 -05:00
*/
2004-03-13 20:06:15 -05:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-11-17 17:28:48 -05:00
#include <errno.h>
#include <stdio.h>
2003-12-29 18:39:18 -05:00
#include <pwd.h>
2004-04-28 14:55:22 -04:00
#include <signal.h>
2003-12-29 18:39:18 -05:00
#include <stdlib.h>
#include <sys/types.h>
#include <howl.h>
2004-01-19 23:41:20 -05:00
#include <pthread.h>
2003-12-29 18:39:18 -05:00
2003-11-17 17:28:48 -05:00
#include "err.h"
2004-01-19 23:41:20 -05:00
#include "rend-unix.h"
pthread_t rend_tid;
sw_discovery rend_handle;
2004-01-19 23:41:20 -05:00
/* Forwards */
void *rend_pipe_monitor(void* arg);
void rend_callback(void);
2003-11-17 17:28:48 -05:00
/*
* rend_howl_reply
*
* Callback function for mDNS stuff
*/
static sw_result rend_howl_reply(sw_discovery discovery,
sw_discovery_publish_status status,
sw_discovery_oid oid,
2003-11-17 17:28:48 -05:00
sw_opaque extra) {
static sw_string status_text[] = {
"started",
"stopped",
"name collision",
"invalid"
};
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Publish reply: %s\n",status_text[status]);
2003-11-17 17:28:48 -05:00
return SW_OKAY;
}
/*
2004-01-19 23:41:20 -05:00
* rend_private_init
*
* Initialize howl and start runloop
2003-11-17 17:28:48 -05:00
*/
2004-01-19 23:41:20 -05:00
int rend_private_init(char *user) {
2003-11-17 17:28:48 -05:00
sw_result result;
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Starting rendezvous services\n");
2004-04-28 14:55:22 -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
if(sw_discovery_init(&rend_handle) != SW_OKAY) {
DPRINTF(E_WARN,L_REND,"Error initializing howl\n");
2003-11-17 17:28:48 -05:00
errno=EINVAL;
return -1;
}
2004-01-19 23:41:20 -05:00
if(drop_privs(user))
2003-11-17 17:28:48 -05:00
return -1;
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Starting polling thread\n");
2003-11-17 17:28:48 -05:00
2004-01-19 23:41:20 -05:00
if(pthread_create(&rend_tid,NULL,rend_pipe_monitor,NULL)) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_FATAL,L_REND,"Could not start thread. Terminating\n");
2004-01-19 23:41:20 -05:00
/* should kill parent, too */
exit(EXIT_FAILURE);
2003-11-17 17:28:48 -05:00
}
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Entering runloop\n");
2004-01-19 23:41:20 -05:00
sw_discovery_run(rend_handle);
2004-01-19 23:41:20 -05:00
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Exiting runloop\n");
2004-01-19 23:41:20 -05:00
2003-11-17 17:28:48 -05:00
return 0;
}
2004-01-19 23:41:20 -05:00
/*
* rend_pipe_monitor
*/
void *rend_pipe_monitor(void* arg) {
fd_set rset;
struct timeval tv;
int result;
while(1) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Waiting for data\n");
2004-01-19 23:41:20 -05:00
FD_ZERO(&rset);
FD_SET(rend_pipe_to[RD_SIDE],&rset);
/* sit in a select spin until there is data on the to fd */
while(((result=select(rend_pipe_to[RD_SIDE] + 1,&rset,NULL,NULL,NULL)) != -1) &&
errno != EINTR) {
if(FD_ISSET(rend_pipe_to[RD_SIDE],&rset)) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Received a message from daap server\n");
2004-01-19 23:41:20 -05:00
rend_callback();
}
}
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Select error!\n");
2004-01-19 23:41:20 -05:00
/* should really bail here */
}
}
/*
* rend_callback
*
* This gets called from the main thread when there is a
* message waiting to be processed.
*/
void rend_callback(void) {
REND_MESSAGE msg;
sw_discovery_oid rend_oid;
2004-01-19 23:41:20 -05:00
sw_result result;
/* here, we've seen the message, now we have to process it */
if(rend_read_message(&msg) != sizeof(msg)) {
2004-12-09 00:05:54 -05:00
DPRINTF(E_FATAL,L_REND,"Rendezvous socket closed (daap server crashed?) Aborting.\n");
2004-01-19 23:41:20 -05:00
exit(EXIT_FAILURE);
}
switch(msg.cmd) {
case REND_MSG_TYPE_REGISTER:
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Registering %s.%s (%d)\n",msg.type,msg.name,msg.port);
if((result=sw_discovery_publish(rend_handle,
0, /* interface handle */
msg.name,
msg.type,
NULL, /* domain */
NULL, /* host */
msg.port,
"\034Database ID=beddab1edeadbea7", /* text record */
29, /* text record length */
rend_howl_reply,
NULL,
&rend_oid)) != SW_OKAY) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_WARN,L_REND,"Error registering name\n");
2004-01-19 23:41:20 -05:00
rend_send_response(-1);
} else {
rend_send_response(0); /* success */
}
break;
case REND_MSG_TYPE_UNREGISTER:
2004-11-13 02:14:26 -05:00
DPRINTF(E_WARN,L_REND,"Unsupported function: UNREGISTER\n");
2004-01-19 23:41:20 -05:00
rend_send_response(-1); /* error */
break;
case REND_MSG_TYPE_STOP:
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Stopping mDNS\n");
2004-01-19 23:41:20 -05:00
rend_send_response(0);
//sw_rendezvous_stop_publish(rend_handle);
sw_discovery_fina(rend_handle);
2004-01-19 23:41:20 -05:00
break;
case REND_MSG_TYPE_STATUS:
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Status inquiry -- returning 0\n");
2004-01-19 23:41:20 -05:00
rend_send_response(0); /* success */
break;
default:
break;
}
}