From 39ff47ad79537df20baff65a90174f3b05229e2c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 6 Jun 2020 21:54:58 +0200 Subject: [PATCH 1/4] update minissdpd/asyncsendto.c --- minissdpd/asyncsendto.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/minissdpd/asyncsendto.c b/minissdpd/asyncsendto.c index aa2d75f..7abef30 100644 --- a/minissdpd/asyncsendto.c +++ b/minissdpd/asyncsendto.c @@ -1,11 +1,12 @@ -/* $Id: asyncsendto.c,v 1.10 2018/07/06 11:44:59 nanard Exp $ */ +/* $Id: asyncsendto.c,v 1.11 2019/09/24 11:46:01 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2006-2018 Thomas Bernard + * (c) 2006-2019 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #include +#include #include #include #include From 4a1b41c1cb0bd9bc40890349023e41a3ef49eae6 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 6 Jun 2020 21:55:44 +0200 Subject: [PATCH 2/4] minissdpd: NO_BACKGROUND_NO_PIDFILE option fixes #468 --- minissdpd/Changelog.txt | 5 ++++- minissdpd/config.h | 10 +++++++--- minissdpd/minissdpd.c | 29 +++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/minissdpd/Changelog.txt b/minissdpd/Changelog.txt index 57974d8..4dac627 100644 --- a/minissdpd/Changelog.txt +++ b/minissdpd/Changelog.txt @@ -1,4 +1,7 @@ -$Id: Changelog.txt,v 1.50 2019/02/10 13:45:24 nanard Exp $ +$Id: Changelog.txt,v 1.51 2020/06/06 19:53:10 nanard Exp $ + +2020/06/06: + define NO_BACKGROUND_NO_PIDFILE for a systemd friendly mode 2018/12/18: updateDevice() remove element from the list when realloc fails diff --git a/minissdpd/config.h b/minissdpd/config.h index 5288426..5f4bf6a 100644 --- a/minissdpd/config.h +++ b/minissdpd/config.h @@ -1,7 +1,7 @@ -/* $Id: config.h,v 1.10 2018/02/23 13:58:14 nanard Exp $ */ +/* $Id: config.h,v 1.11 2020/06/06 19:53:10 nanard Exp $ */ /* MiniUPnP project - * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2006-2018 Thomas Bernard + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ + * (c) 2006-2020 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #ifndef CONFIG_H_INCLUDED @@ -30,4 +30,8 @@ * and unicasted, we cannot bind to 239.255.255.250 neither */ /*#define SSDP_LISTEN_ON_SPECIFIC_ADDR*/ +/* When NO_BACKGROUND_NO_PIDFILE is defined, minissdpd does not go to + * background and does not create any pidfile */ +/*#define NO_BACKGROUND_NO_PIDFILE*/ + #endif diff --git a/minissdpd/minissdpd.c b/minissdpd/minissdpd.c index 5c9c5d6..0b61e23 100644 --- a/minissdpd/minissdpd.c +++ b/minissdpd/minissdpd.c @@ -1,7 +1,7 @@ -/* $Id: minissdpd.c,v 1.53 2016/03/01 18:06:46 nanard Exp $ */ +/* $Id: minissdpd.c,v 1.57 2020/06/06 19:53:10 nanard Exp $ */ /* vim: tabstop=4 shiftwidth=4 noexpandtab * MiniUPnP project - * (c) 2007-2018 Thomas Bernard + * (c) 2007-2020 Thomas Bernard * website : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -1224,7 +1224,9 @@ static void ssdpDiscover(int s, int ipv6, const char * search) int main(int argc, char * * argv) { int ret = 0; +#ifndef NO_BACKGROUND_NO_PIDFILE int pid; +#endif struct sigaction sa; char buf[1500]; ssize_t n; @@ -1245,7 +1247,9 @@ int main(int argc, char * * argv) struct lan_addr_s * lan_addr; int i; const char * sockpath = "/var/run/minissdpd.sock"; +#ifndef NO_BACKGROUND_NO_PIDFILE const char * pidfilename = "/var/run/minissdpd.pid"; +#endif int debug_flag = 0; #ifdef ENABLE_IPV6 int ipv6 = 0; @@ -1291,8 +1295,10 @@ int main(int argc, char * * argv) } } else if(0==strcmp(argv[i], "-s")) sockpath = argv[++i]; +#ifndef NO_BACKGROUND_NO_PIDFILE else if(0==strcmp(argv[i], "-p")) pidfilename = argv[++i]; +#endif else if(0==strcmp(argv[i], "-t")) ttl = (unsigned char)atoi(argv[++i]); else if(0==strcmp(argv[i], "-f")) @@ -1308,7 +1314,11 @@ int main(int argc, char * * argv) #ifdef ENABLE_IPV6 "[-6] " #endif /* ENABLE_IPV6 */ - "[-s socket] [-p pidfile] [-t TTL] " + "[-s socket] " +#ifndef NO_BACKGROUND_NO_PIDFILE + "[-p pidfile] " +#endif + "[-t TTL] " "[-f device] " "-i [-i ] ...\n", argv[0]); @@ -1317,8 +1327,13 @@ int main(int argc, char * * argv) " 192.168.1.42/255.255.255.0, or an interface name such as eth0.\n"); fprintf(stderr, "\n By default, socket will be open as %s\n" +#ifndef NO_BACKGROUND_NO_PIDFILE " and pid written to file %s\n", - sockpath, pidfilename); + sockpath, pidfilename +#else + ,sockpath +#endif + ); return 1; } @@ -1329,11 +1344,13 @@ int main(int argc, char * * argv) if(!debug_flag) /* speed things up and ignore LOG_INFO and LOG_DEBUG */ setlogmask(LOG_UPTO(LOG_NOTICE)); +#ifndef NO_BACKGROUND_NO_PIDFILE if(checkforrunning(pidfilename) < 0) { syslog(LOG_ERR, "MiniSSDPd is already running. EXITING"); return 1; } +#endif upnp_bootid = (unsigned int)time(NULL); @@ -1414,6 +1431,7 @@ int main(int argc, char * * argv) } #endif +#ifndef NO_BACKGROUND_NO_PIDFILE /* daemonize or in any case get pid ! */ if(debug_flag) pid = getpid(); @@ -1428,6 +1446,7 @@ int main(int argc, char * * argv) } writepidfile(pidfilename, pid); +#endif /* send M-SEARCH ssdp:all Requests */ if(s_ssdp >= 0) @@ -1639,8 +1658,10 @@ quit: free(serv->location); free(serv); } +#ifndef NO_BACKGROUND_NO_PIDFILE if(unlink(pidfilename) < 0) syslog(LOG_ERR, "unlink(%s): %m", pidfilename); +#endif closelog(); return ret; } From e39549d6bb4f7389c898c7af59db18ec575cae3c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 6 Jun 2020 22:21:43 +0200 Subject: [PATCH 3/4] send the startup M-SEARCH message on the right interface(s) fixes #464 --- minissdpd/Changelog.txt | 3 ++- minissdpd/config.h | 7 ++++++- minissdpd/minissdpd.c | 28 +++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/minissdpd/Changelog.txt b/minissdpd/Changelog.txt index 4dac627..cc78f08 100644 --- a/minissdpd/Changelog.txt +++ b/minissdpd/Changelog.txt @@ -1,7 +1,8 @@ -$Id: Changelog.txt,v 1.51 2020/06/06 19:53:10 nanard Exp $ +$Id: Changelog.txt,v 1.52 2020/06/06 20:20:47 nanard Exp $ 2020/06/06: define NO_BACKGROUND_NO_PIDFILE for a systemd friendly mode + send the startup M-SEARCH message on the right interface(s) 2018/12/18: updateDevice() remove element from the list when realloc fails diff --git a/minissdpd/config.h b/minissdpd/config.h index 5f4bf6a..117e478 100644 --- a/minissdpd/config.h +++ b/minissdpd/config.h @@ -1,4 +1,4 @@ -/* $Id: config.h,v 1.11 2020/06/06 19:53:10 nanard Exp $ */ +/* $Id: config.h,v 1.12 2020/06/06 20:20:47 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ * (c) 2006-2020 Thomas Bernard @@ -34,4 +34,9 @@ * background and does not create any pidfile */ /*#define NO_BACKGROUND_NO_PIDFILE*/ +/* define HAVE_IP_MREQN to use struct ip_mreqn instead of struct ip_mreq + * for setsockopt(IP_MULTICAST_IF). Available with Linux 2.4+, + * FreeBSD, etc. */ +#define HAVE_IP_MREQN + #endif diff --git a/minissdpd/minissdpd.c b/minissdpd/minissdpd.c index 0b61e23..1c035f0 100644 --- a/minissdpd/minissdpd.c +++ b/minissdpd/minissdpd.c @@ -1,4 +1,4 @@ -/* $Id: minissdpd.c,v 1.57 2020/06/06 19:53:10 nanard Exp $ */ +/* $Id: minissdpd.c,v 1.58 2020/06/06 20:20:47 nanard Exp $ */ /* vim: tabstop=4 shiftwidth=4 noexpandtab * MiniUPnP project * (c) 2007-2020 Thomas Bernard @@ -1449,8 +1449,30 @@ int main(int argc, char * * argv) #endif /* send M-SEARCH ssdp:all Requests */ - if(s_ssdp >= 0) - ssdpDiscover(s_ssdp, 0, searched_device); + if(s_ssdp >= 0) { + for(lan_addr = lan_addrs.lh_first; lan_addr != NULL; lan_addr = lan_addr->list.le_next) { +#ifndef HAVE_IP_MREQN + struct in_addr mc_if; + + mc_if.s_addr = lan_addr->addr.s_addr; /*inet_addr(addr);*/ +#else + struct ip_mreqn mc_if; + + mc_if.imr_address.s_addr = lan_addr->addr.s_addr; /*inet_addr(addr);*/ +#ifdef ENABLE_IPV6 + mc_if.imr_ifindex = lan_addr->index; +#else /* ENABLE_IPV6 */ + mc_if.imr_ifindex = if_nametoindex(lan_addr->ifname); +#endif /* ENABLE_IPV6 */ +#endif /* HAVE_IP_MREQN */ + if(setsockopt(s_ssdp, IPPROTO_IP, IP_MULTICAST_IF, &mc_if, sizeof(mc_if)) < 0) { + syslog(LOG_WARNING, "setsockopt(IP_MULTICAST_IF): %m"); + } + ssdpDiscover(s_ssdp, 0, searched_device); + /* XXX if ssdpDiscover() doesn't send the SSDP packet at once, + * we should wait here */ + } + } if(s_ssdp6 >= 0) ssdpDiscover(s_ssdp6, 1, searched_device); From d2ee52e4d990b2c7b912da5c58cae3cf0dab1ccb Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 8 Jun 2020 13:17:16 +0200 Subject: [PATCH 4/4] improve syslog --- minissdpd/minissdpd.c | 2 +- minissdpd/openssdpsocket.c | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/minissdpd/minissdpd.c b/minissdpd/minissdpd.c index 1c035f0..44dab31 100644 --- a/minissdpd/minissdpd.c +++ b/minissdpd/minissdpd.c @@ -1215,7 +1215,7 @@ static void ssdpDiscover(int s, int ipv6, const char * search) n = sendto_or_schedule(s, bufr, n, 0, (const struct sockaddr *)&sockudp_w, ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); if (n < 0) { - syslog(LOG_ERR, "%s: sendto: %m", __func__); + syslog(LOG_ERR, "%s: sendto(s=%d, ipv6=%d): %m", __func__, s, ipv6); } } } diff --git a/minissdpd/openssdpsocket.c b/minissdpd/openssdpsocket.c index 702856d..7f09fb6 100644 --- a/minissdpd/openssdpsocket.c +++ b/minissdpd/openssdpsocket.c @@ -38,6 +38,7 @@ extern struct lan_addr_list lan_addrs; int AddDropMulticastMembership(int s, struct lan_addr_s * lan_addr, int ipv6, int drop) { + int ret = 0; struct ip_mreq imr; /* Ip multicast membership */ #ifdef ENABLE_IPV6 struct ipv6_mreq mr; @@ -56,21 +57,21 @@ AddDropMulticastMembership(int s, struct lan_addr_s * lan_addr, int ipv6, int dr if(setsockopt(s, IPPROTO_IPV6, drop ? IPV6_LEAVE_GROUP : IPV6_JOIN_GROUP, &mr, sizeof(struct ipv6_mreq)) < 0) { - syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m", - drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP", + syslog(LOG_ERR, "setsockopt(s=%d, %s)(%s, %s): %m", + s, drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP", LL_SSDP_MCAST_ADDR, lan_addr->ifname); - return -1; + ret = -1; } inet_pton(AF_INET6, SL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr); if(setsockopt(s, IPPROTO_IPV6, drop ? IPV6_LEAVE_GROUP : IPV6_JOIN_GROUP, &mr, sizeof(struct ipv6_mreq)) < 0) { - syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m", - drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP", + syslog(LOG_ERR, "setsockopt(s=%d, %s)(%s, %s): %m", + s, drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP", SL_SSDP_MCAST_ADDR, lan_addr->ifname); - return -1; + ret = -1; } } else @@ -89,8 +90,8 @@ AddDropMulticastMembership(int s, struct lan_addr_s * lan_addr, int ipv6, int dr if (setsockopt(s, IPPROTO_IP, drop ? IP_DROP_MEMBERSHIP : IP_ADD_MEMBERSHIP, (void *)&imr, sizeof(struct ip_mreq)) < 0) { - syslog(LOG_ERR, "setsockopt(udp, %s)(%s): %m", - drop ? "IP_DROP_MEMBERSHIP" : "IP_ADD_MEMBERSHIP", + syslog(LOG_ERR, "setsockopt(s=%d, %s)(%s): %m", + s, drop ? "IP_DROP_MEMBERSHIP" : "IP_ADD_MEMBERSHIP", lan_addr->ifname); return -1; } @@ -98,7 +99,7 @@ AddDropMulticastMembership(int s, struct lan_addr_s * lan_addr, int ipv6, int dr } #endif /* ENABLE_IPV6 */ - return 0; + return ret; } int