summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2011-05-31 13:45:57 (GMT)
committer Jaromil <jaromil@dyne.org>2011-05-31 13:45:57 (GMT)
commita5a478f909d416a19ead6ba7e6699f2c9d0475c7 (patch)
treefb2e66f69387997c35bec55a7fbdde37993b0806
parent207b505fb92f53abe0bd3a28c4e15b90385cd56d (diff)
act intuitively and stop requiring server and port at commandline
if no network address is specified, then performs autodiscovery if discovery finds only one, uses that one specifiying address on commandline is only necessary when more than one upnp 'rootdevice' is present on the same local network
-rw-r--r--.gitignore12
-rw-r--r--src/avremote.c17
-rw-r--r--src/avremote.h13
-rw-r--r--src/cmdline.c40
-rw-r--r--src/discover.c19
-rw-r--r--src/discover.h22
6 files changed, 64 insertions, 59 deletions
diff --git a/.gitignore b/.gitignore
index da016c9..52135db 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,15 @@ autom4te.cache
config.h
config.log
config.status
+Makefile.in
+aclocal.m4
+config.guess
+config.h.in
+config.sub
+configure
+depcomp
+install-sh
+missing
+src/Makefile.in
+src/upnp/Makefile.in
+stamp-h1
diff --git a/src/avremote.c b/src/avremote.c
index 6b4e2be..c21285a 100644
--- a/src/avremote.c
+++ b/src/avremote.c
@@ -49,13 +49,6 @@
#include <avremote.h>
-/* Buffer Boundaries
- the following defines set the maximum size we allow for buffers used */
-#define MAX_HOSTNAME_SIZE 256
-#define MAX_MSG_SIZE 2048
-#define MAX_HDR_SIZE 512
-#define MAX_RES_SIZE 1401
-#define MAX_META_SIZE 2048
upnp_t *create_upnp() {
@@ -90,7 +83,7 @@ void free_upnp(upnp_t *upnp) {
free(upnp);
}
-int connect_upnp(upnp_t *upnp, char *hostname, int port) {
+int connect_upnp(upnp_t *upnp) {
struct sockaddr_in serveraddr;
// const struct sockaddr *serveraddr;
struct hostent *host;
@@ -109,9 +102,9 @@ int connect_upnp(upnp_t *upnp, char *hostname, int port) {
}
/* gethostbyname: get the server's DNS entry */
- host = gethostbyname(hostname);
+ host = gethostbyname(upnp->hostname);
if (host == NULL) {
- fprintf(stderr,"error: no such host as %s (%s)\n", hostname, strerror(errno));
+ fprintf(stderr,"error: no such host as %s (%s)\n", upnp->hostname, strerror(errno));
return(-1);
}
@@ -120,7 +113,7 @@ int connect_upnp(upnp_t *upnp, char *hostname, int port) {
serveraddr.sin_family = AF_INET;
bcopy((char *)host->h_addr,
(char *)&serveraddr.sin_addr.s_addr, host->h_length);
- serveraddr.sin_port = htons(port);
+ serveraddr.sin_port = htons(upnp->port);
/* connect: create a connection with the server */
if (connect(sockfd, (const struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) {
@@ -128,8 +121,6 @@ int connect_upnp(upnp_t *upnp, char *hostname, int port) {
return(-1);
}
- snprintf(upnp->hostname, 255, "%s",hostname);
- upnp->port = port;
upnp->sockfd = sockfd;
return(sockfd);
diff --git a/src/avremote.h b/src/avremote.h
index e1e80e6..cee5155 100644
--- a/src/avremote.h
+++ b/src/avremote.h
@@ -21,6 +21,14 @@
#ifndef __AVREMOTE_H__
#define __AVREMOTE_H__
+/* Buffer Boundaries
+ the following defines set the maximum size we allow for buffers used */
+#define MAX_HOSTNAME_SIZE 256
+#define MAX_MSG_SIZE 2048
+#define MAX_HDR_SIZE 512
+#define MAX_RES_SIZE 1401
+#define MAX_META_SIZE 2048
+
// messages get rendered in this structure
// allocated and freed with create/free_upnp
typedef struct {
@@ -72,7 +80,10 @@ typedef struct {
upnp_t *create_upnp();
void free_upnp(upnp_t *upnp);
-int connect_upnp(upnp_t *upnp, char *hostname, int port);
+int upnp_discover(upnp_t *upnp);
+
+// should set upnp-> hostname and port before calling this
+int connect_upnp(upnp_t *upnp);
/*
Available AVTransport actions:
diff --git a/src/cmdline.c b/src/cmdline.c
index dadab6a..0c57f9f 100644
--- a/src/cmdline.c
+++ b/src/cmdline.c
@@ -32,7 +32,6 @@
#include <errno.h>
#include <avremote.h>
-#include <discover.h>
#include <parsers.h>
// our exit codes are shell style: 1 is error, 0 is success
@@ -143,41 +142,46 @@ void cmdline(int argc, char **argv) {
fprintf(stderr,"command not specified, see %s -h for help\n",argv[0]);
exit(1);
}
-
-
- // not in dry run nor discovery, check for necessary options
- if(!port) {
- fprintf(stderr,"port not specified, use -p\n");
- exit(1);
- }
-
- if(!server[0]) {
- fprintf(stderr,"server not specified, using localhost\n");
- sprintf(server,"%s","localhost");
- }
+
}
}
int main(int argc, char **argv) {
upnp_t *upnp;
+ int found;
cmdline(argc, argv);
+ upnp = create_upnp();
+
+ // no server specified, force discovery
+ if(!server[0] || !port) discover = 1;
if (discover)
{
fprintf(stderr,"Performing upnp discovery...\n");
- upnp_discover(dry_run);
- exit(0);
+ found = upnp_discover(upnp);
+
+ // we exit in case none or more than one found and no manual
+ // server:port was specified
+ if(!server[0] || !port)
+ {
+ if(found != 1)
+ exit(0);
+ }
+ else
+ { // commandline specified explicit addresses
+ snprintf(upnp->hostname, MAX_HOSTNAME_SIZE-1,"%s",server);
+ upnp->port = port;
+ }
+
}
- upnp = create_upnp();
if(!dry_run)
{
-
- if ( connect_upnp (upnp, server, port) < 0 )
+ if ( connect_upnp (upnp) < 0 )
{
fprintf(stderr,"can't connect to %s:%u: operation aborted.\n", server, port);
exit(ERR);
diff --git a/src/discover.c b/src/discover.c
index ae0e3fa..ecce8d4 100644
--- a/src/discover.c
+++ b/src/discover.c
@@ -28,8 +28,10 @@
#include <upnpcommands.h>
#include <upnperrors.h>
+#include <avremote.h>
-int upnp_discover()
+
+int upnp_discover(upnp_t *upnp)
{
const char * rootdescurl = 0;
const char * multicastif = 0;
@@ -40,6 +42,7 @@ int upnp_discover()
struct UPNPUrls urls;
struct IGDdatas data;
int r, err;
+ int num = 0;
// damn programmers who change API prototypes in headers
// without versioning.
@@ -57,10 +60,10 @@ int upnp_discover()
} else if (r == 3) { // 3 = an UPnP root device has been found (not an IGD)
dev = devlist;
- for( dev = devlist; dev; dev = dev->pNext) {
+ for( dev = devlist; dev; dev = dev->pNext, num++) {
// parse out ip and port from url
- char ip[256];
+ char ip[MAX_HOSTNAME_SIZE];
char port[64];
char tmp[512];
char *p, *pp;
@@ -71,7 +74,7 @@ int upnp_discover()
// ip
do p+=2; while(*p != '/'); p++;
pp = p; do pp++; while(*pp != ':'); *pp = 0;
- snprintf(ip,255,"%s",p);
+ snprintf(ip,MAX_HOSTNAME_SIZE-1,"%s",p);
// port
p = pp+1; pp = p;
@@ -80,12 +83,18 @@ int upnp_discover()
fprintf(stderr,"%s\t%s\t%s\t%s\n", dev->st, dev->descURL, ip, port);
+ if(!num) { // first found
+ sscanf(port, "%u", &upnp->port);
+ snprintf(upnp->hostname, MAX_HOSTNAME_SIZE-1, "%s", ip);
+ }
+
}
FreeUPNPUrls(&urls);
+
}
freeUPNPDevlist(devlist); devlist = 0;
- return(r);
+ return(num);
}
diff --git a/src/discover.h b/src/discover.h
deleted file mode 100644
index caa18a1..0000000
--- a/src/discover.h
+++ /dev/null
@@ -1,22 +0,0 @@
-/* AVRemote
-
- (c) 2011 Nederlands Instituut voor Mediakunst (NIMk)
- 2011 Denis Roio <jaromil@nimk.nl>
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>
-
-*/
-
-int upnp_discover();
-