2015-07-23 20:44:37 +00:00
|
|
|
/* $Id: listdevices.c,v 1.6 2015/07/23 20:40:08 nanard Exp $ */
|
2013-02-16 09:25:10 +00:00
|
|
|
/* Project : miniupnp
|
|
|
|
* Author : Thomas Bernard
|
2015-07-22 21:37:40 +00:00
|
|
|
* Copyright (c) 2013-2015 Thomas Bernard
|
2013-02-16 09:25:10 +00:00
|
|
|
* This software is subject to the conditions detailed in the
|
|
|
|
* LICENCE file provided in this distribution. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-02-20 16:04:53 +00:00
|
|
|
#include <string.h>
|
2015-07-23 20:44:37 +00:00
|
|
|
#include <stdlib.h>
|
2015-07-15 12:52:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif /* _WIN32 */
|
2013-02-16 09:25:10 +00:00
|
|
|
#include "miniupnpc.h"
|
|
|
|
|
2018-04-29 23:59:38 +00:00
|
|
|
struct upnp_dev_list {
|
|
|
|
struct upnp_dev_list * next;
|
|
|
|
char * descURL;
|
|
|
|
struct UPNPDev * * array;
|
|
|
|
size_t count;
|
|
|
|
size_t allocated_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ADD_DEVICE_COUNT_STEP 16
|
|
|
|
|
|
|
|
void add_device(struct upnp_dev_list * * list_head, struct UPNPDev * dev)
|
|
|
|
{
|
|
|
|
struct upnp_dev_list * elt;
|
2018-04-30 14:26:29 +00:00
|
|
|
size_t i;
|
|
|
|
|
2018-04-29 23:59:38 +00:00
|
|
|
if(dev == NULL)
|
|
|
|
return;
|
|
|
|
for(elt = *list_head; elt != NULL; elt = elt->next) {
|
|
|
|
if(strcmp(elt->descURL, dev->descURL) == 0) {
|
2018-04-30 14:26:29 +00:00
|
|
|
for(i = 0; i < elt->count; i++) {
|
|
|
|
if (strcmp(elt->array[i]->st, dev->st) == 0 && strcmp(elt->array[i]->usn, dev->usn) == 0) {
|
|
|
|
return; /* already found */
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 23:59:38 +00:00
|
|
|
if(elt->count >= elt->allocated_count) {
|
|
|
|
struct UPNPDev * * tmp;
|
|
|
|
elt->allocated_count += ADD_DEVICE_COUNT_STEP;
|
|
|
|
tmp = realloc(elt->array, elt->allocated_count * sizeof(struct UPNPDev *));
|
|
|
|
if(tmp == NULL) {
|
|
|
|
fprintf(stderr, "Failed to realloc(%p, %lu)\n", elt->array, elt->allocated_count * sizeof(struct UPNPDev *));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elt->array = tmp;
|
|
|
|
}
|
|
|
|
elt->array[elt->count++] = dev;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elt = malloc(sizeof(struct upnp_dev_list));
|
|
|
|
if(elt == NULL) {
|
|
|
|
fprintf(stderr, "Failed to malloc(%lu)\n", sizeof(struct upnp_dev_list));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elt->next = *list_head;
|
|
|
|
elt->descURL = strdup(dev->descURL);
|
|
|
|
elt->allocated_count = ADD_DEVICE_COUNT_STEP;
|
|
|
|
elt->array = malloc(ADD_DEVICE_COUNT_STEP * sizeof(struct UPNPDev *));
|
|
|
|
if(elt->array == NULL) {
|
|
|
|
fprintf(stderr, "Failed to malloc(%lu)\n", ADD_DEVICE_COUNT_STEP * sizeof(struct UPNPDev *));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elt->array[0] = dev;
|
|
|
|
elt->count = 1;
|
|
|
|
*list_head = elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_device(struct upnp_dev_list * elt)
|
|
|
|
{
|
|
|
|
free(elt->descURL);
|
|
|
|
free(elt->array);
|
|
|
|
free(elt);
|
|
|
|
}
|
|
|
|
|
2013-02-16 09:25:10 +00:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
2014-01-31 13:59:21 +00:00
|
|
|
const char * searched_device = NULL;
|
2015-05-22 10:26:54 +00:00
|
|
|
const char * * searched_devices = NULL;
|
2013-02-16 09:25:10 +00:00
|
|
|
const char * multicastif = 0;
|
|
|
|
const char * minissdpdpath = 0;
|
|
|
|
int ipv6 = 0;
|
2015-07-23 20:44:37 +00:00
|
|
|
unsigned char ttl = 2;
|
2013-02-16 09:25:10 +00:00
|
|
|
int error = 0;
|
|
|
|
struct UPNPDev * devlist = 0;
|
|
|
|
struct UPNPDev * dev;
|
2018-04-29 23:59:38 +00:00
|
|
|
struct upnp_dev_list * sorted_list = NULL;
|
|
|
|
struct upnp_dev_list * dev_array;
|
2013-02-20 16:04:53 +00:00
|
|
|
int i;
|
2013-02-16 09:25:10 +00:00
|
|
|
|
2015-07-15 12:52:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
|
|
|
int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
|
|
if(nResult != NO_ERROR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WSAStartup() failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-20 16:04:53 +00:00
|
|
|
for(i = 1; i < argc; i++) {
|
|
|
|
if(strcmp(argv[i], "-6") == 0)
|
|
|
|
ipv6 = 1;
|
2014-01-31 13:59:21 +00:00
|
|
|
else if(strcmp(argv[i], "-d") == 0) {
|
|
|
|
if(++i >= argc) {
|
2015-07-23 20:44:37 +00:00
|
|
|
fprintf(stderr, "%s option needs one argument\n", "-d");
|
2014-01-31 13:59:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
searched_device = argv[i];
|
2015-07-23 20:44:37 +00:00
|
|
|
} else if(strcmp(argv[i], "-t") == 0) {
|
|
|
|
if(++i >= argc) {
|
|
|
|
fprintf(stderr, "%s option needs one argument\n", "-t");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ttl = (unsigned char)atoi(argv[i]);
|
2015-05-22 10:26:54 +00:00
|
|
|
} else if(strcmp(argv[i], "-l") == 0) {
|
|
|
|
if(++i >= argc) {
|
|
|
|
fprintf(stderr, "-l option needs at least one argument\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
searched_devices = (const char * *)(argv + i);
|
|
|
|
break;
|
2014-11-17 09:54:09 +00:00
|
|
|
} else if(strcmp(argv[i], "-m") == 0) {
|
|
|
|
if(++i >= argc) {
|
|
|
|
fprintf(stderr, "-m option needs one argument\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
multicastif = argv[i];
|
2014-01-31 13:59:21 +00:00
|
|
|
} else {
|
2015-05-22 10:26:54 +00:00
|
|
|
printf("usage : %s [options] [-l <device1> <device2> ...]\n", argv[0]);
|
2014-01-31 13:59:21 +00:00
|
|
|
printf("options :\n");
|
2013-02-20 16:04:53 +00:00
|
|
|
printf(" -6 : use IPv6\n");
|
2014-11-17 09:54:09 +00:00
|
|
|
printf(" -m address/ifname : network interface to use for multicast\n");
|
2015-05-22 10:26:54 +00:00
|
|
|
printf(" -d <device string> : search only for this type of device\n");
|
|
|
|
printf(" -l <device1> <device2> ... : search only for theses types of device\n");
|
2015-07-23 20:44:37 +00:00
|
|
|
printf(" -t ttl : set multicast TTL. Default value is 2.\n");
|
2014-01-31 13:59:21 +00:00
|
|
|
printf(" -h : this help\n");
|
2013-02-20 16:04:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 13:59:21 +00:00
|
|
|
if(searched_device) {
|
|
|
|
printf("searching UPnP device type %s\n", searched_device);
|
|
|
|
devlist = upnpDiscoverDevice(searched_device,
|
|
|
|
2000, multicastif, minissdpdpath,
|
2015-10-08 15:42:45 +00:00
|
|
|
0/*localport*/, ipv6, ttl, &error);
|
2015-05-22 10:26:54 +00:00
|
|
|
} else if(searched_devices) {
|
|
|
|
printf("searching UPnP device types :\n");
|
|
|
|
for(i = 0; searched_devices[i]; i++)
|
|
|
|
printf("\t%s\n", searched_devices[i]);
|
|
|
|
devlist = upnpDiscoverDevices(searched_devices,
|
|
|
|
2000, multicastif, minissdpdpath,
|
2015-10-08 15:42:45 +00:00
|
|
|
0/*localport*/, ipv6, ttl, &error, 1);
|
2014-01-31 13:59:21 +00:00
|
|
|
} else {
|
|
|
|
printf("searching all UPnP devices\n");
|
|
|
|
devlist = upnpDiscoverAll(2000, multicastif, minissdpdpath,
|
2015-10-08 15:42:45 +00:00
|
|
|
0/*localport*/, ipv6, ttl, &error);
|
2014-01-31 13:59:21 +00:00
|
|
|
}
|
2013-02-16 09:25:10 +00:00
|
|
|
if(devlist) {
|
2015-07-22 21:37:40 +00:00
|
|
|
for(dev = devlist, i = 1; dev != NULL; dev = dev->pNext, i++) {
|
|
|
|
printf("%3d: %-48s\n", i, dev->st);
|
|
|
|
printf(" %s\n", dev->descURL);
|
|
|
|
printf(" %s\n", dev->usn);
|
2018-04-29 23:59:38 +00:00
|
|
|
add_device(&sorted_list, dev);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
for (dev_array = sorted_list; dev_array != NULL ; dev_array = dev_array->next) {
|
|
|
|
printf("%s :\n", dev_array->descURL);
|
2018-04-30 14:26:29 +00:00
|
|
|
for(i = 0; (unsigned)i < dev_array->count; i++) {
|
2018-04-29 23:59:38 +00:00
|
|
|
printf("%2d: %s\n", i+1, dev_array->array[i]->st);
|
|
|
|
printf(" %s\n", dev_array->array[i]->usn);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
2013-02-16 09:25:10 +00:00
|
|
|
}
|
|
|
|
freeUPNPDevlist(devlist);
|
2018-04-29 23:59:38 +00:00
|
|
|
while(sorted_list != NULL) {
|
|
|
|
dev_array = sorted_list;
|
|
|
|
sorted_list = sorted_list->next;
|
|
|
|
free_device(dev_array);
|
|
|
|
}
|
2013-02-16 09:25:10 +00:00
|
|
|
} else {
|
|
|
|
printf("no device found.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|