* Code reformatting.

This commit is contained in:
Justin Maggard 2013-02-04 20:23:49 +00:00
parent 77c30bdfba
commit 11998204e2

View File

@ -83,16 +83,15 @@ OpenAndConfSSDPReceiveSocket(void)
int i = 1; int i = 1;
struct sockaddr_in sockname; struct sockaddr_in sockname;
if( (s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{ {
DPRINTF(E_ERROR, L_SSDP, "socket(udp): %s\n", strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "socket(udp): %s\n", strerror(errno));
return -1; return -1;
} }
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
{
DPRINTF(E_WARN, L_SSDP, "setsockopt(udp, SO_REUSEADDR): %s\n", strerror(errno)); DPRINTF(E_WARN, L_SSDP, "setsockopt(udp, SO_REUSEADDR): %s\n", strerror(errno));
}
memset(&sockname, 0, sizeof(struct sockaddr_in)); memset(&sockname, 0, sizeof(struct sockaddr_in));
sockname.sin_family = AF_INET; sockname.sin_family = AF_INET;
@ -134,7 +133,8 @@ OpenAndConfSSDPNotifySocket(in_addr_t addr)
struct in_addr mc_if; struct in_addr mc_if;
struct sockaddr_in sockname; struct sockaddr_in sockname;
if( (s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{ {
DPRINTF(E_ERROR, L_SSDP, "socket(udp_notify): %s\n", strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "socket(udp_notify): %s\n", strerror(errno));
return -1; return -1;
@ -199,27 +199,6 @@ OpenAndConfSSDPNotifySockets(int * sockets)
return 0; return 0;
} }
/*
* response from a LiveBox (Wanadoo)
HTTP/1.1 200 OK
CACHE-CONTROL: max-age=1800
DATE: Thu, 01 Jan 1970 04:03:23 GMT
EXT:
LOCATION: http://192.168.0.1:49152/gatedesc.xml
SERVER: Linux/2.4.17, UPnP/1.0, Intel SDK for UPnP devices /1.2
ST: upnp:rootdevice
USN: uuid:75802409-bccb-40e7-8e6c-fa095ecce13e::upnp:rootdevice
* response from a Linksys 802.11b :
HTTP/1.1 200 OK
Cache-Control:max-age=120
Location:http://192.168.5.1:5678/rootDesc.xml
Server:NT/5.0 UPnP/1.0
ST:upnp:rootdevice
USN:uuid:upnp-InternetGatewayDevice-1_0-0090a2777777::upnp:rootdevice
EXT:
*/
static const char * const known_service_types[] = static const char * const known_service_types[] =
{ {
uuidvalue, uuidvalue,
@ -249,6 +228,9 @@ SendSSDPAnnounce2(int s, struct sockaddr_in sockname, int st_no,
{ {
int l, n; int l, n;
char buf[512]; char buf[512];
char tmstr[30];
time_t tm = time(NULL);
/* /*
* follow guideline from document "UPnP Device Architecture 1.0" * follow guideline from document "UPnP Device Architecture 1.0"
* uppercase is recommended. * uppercase is recommended.
@ -256,10 +238,7 @@ SendSSDPAnnounce2(int s, struct sockaddr_in sockname, int st_no,
* SERVER: OS/ver UPnP/1.0 minidlna/1.0 * SERVER: OS/ver UPnP/1.0 minidlna/1.0
* - check what to put in the 'Cache-Control' header * - check what to put in the 'Cache-Control' header
* */ * */
char szTime[30]; strftime(tmstr, sizeof(tmstr), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tm));
time_t tTime = time(NULL);
strftime(szTime, 30,"%a, %d %b %Y %H:%M:%S GMT" , gmtime(&tTime));
l = snprintf(buf, sizeof(buf), "HTTP/1.1 200 OK\r\n" l = snprintf(buf, sizeof(buf), "HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=%u\r\n" "CACHE-CONTROL: max-age=%u\r\n"
"DATE: %s\r\n" "DATE: %s\r\n"
@ -271,18 +250,20 @@ SendSSDPAnnounce2(int s, struct sockaddr_in sockname, int st_no,
"Content-Length: 0\r\n" "Content-Length: 0\r\n"
"\r\n", "\r\n",
(runtime_vars.notify_interval<<1)+10, (runtime_vars.notify_interval<<1)+10,
szTime, tmstr,
known_service_types[st_no], (st_no>1?"1":""), known_service_types[st_no],
uuidvalue, (st_no>0?"::":""), (st_no>0?known_service_types[st_no]:""), (st_no>1?"1":""), (st_no>1?"1":""),
uuidvalue,
(st_no > 0 ? "::" : ""),
(st_no > 0 ? known_service_types[st_no] : ""),
(st_no > 1 ? "1" : ""),
host, (unsigned int)port); host, (unsigned int)port);
//DEBUG DPRINTF(E_DEBUG, L_SSDP, "Sending M-SEARCH response:\n%s", buf); //DEBUG DPRINTF(E_DEBUG, L_SSDP, "Sending M-SEARCH response:\n%s", buf);
n = sendto(s, buf, l, 0, n = sendto(s, buf, l, 0,
(struct sockaddr *)&sockname, sizeof(struct sockaddr_in) ); (struct sockaddr *)&sockname, sizeof(struct sockaddr_in) );
if (n < 0) if (n < 0)
{
DPRINTF(E_ERROR, L_SSDP, "sendto(udp): %s\n", strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "sendto(udp): %s\n", strerror(errno));
} }
}
static void static void
SendSSDPNotifies(int s, const char *host, unsigned short port, SendSSDPNotifies(int s, const char *host, unsigned short port,
@ -317,8 +298,12 @@ SendSSDPNotifies(int s, const char * host, unsigned short port,
SSDP_MCAST_ADDR, SSDP_PORT, SSDP_MCAST_ADDR, SSDP_PORT,
lifetime, lifetime,
host, port, host, port,
known_service_types[i], (i>1?"1":""), known_service_types[i],
uuidvalue, (i>0?"::":""), (i>0?known_service_types[i]:""), (i>1?"1":"") ); (i > 1 ? "1" : ""),
uuidvalue,
(i > 0 ? "::" : ""),
(i > 0 ? known_service_types[i] : ""),
(i > 1 ? "1" : ""));
if (l >= sizeof(bufr)) if (l >= sizeof(bufr))
{ {
DPRINTF(E_WARN, L_SSDP, "SendSSDPNotifies(): truncated output\n"); DPRINTF(E_WARN, L_SSDP, "SendSSDPNotifies(): truncated output\n");
@ -328,9 +313,7 @@ SendSSDPNotifies(int s, const char * host, unsigned short port,
n = sendto(s, bufr, l, 0, n = sendto(s, bufr, l, 0,
(struct sockaddr *)&sockname, sizeof(struct sockaddr_in)); (struct sockaddr *)&sockname, sizeof(struct sockaddr_in));
if (n < 0) if (n < 0)
{
DPRINTF(E_ERROR, L_SSDP, "sendto(udp_notify=%d, %s): %s\n", s, host, strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "sendto(udp_notify=%d, %s): %s\n", s, host, strerror(errno));
}
i++; i++;
} }
} }
@ -340,11 +323,9 @@ void
SendSSDPNotifies2(int *sockets, SendSSDPNotifies2(int *sockets,
unsigned short port, unsigned short port,
unsigned int lifetime) unsigned int lifetime)
/*SendSSDPNotifies2(int * sockets, struct lan_addr_s * lan_addr, int n_lan_addr,
unsigned short port,
unsigned int lifetime)*/
{ {
int i; int i;
DPRINTF(E_DEBUG, L_SSDP, "Sending SSDP notifies\n"); DPRINTF(E_DEBUG, L_SSDP, "Sending SSDP notifies\n");
for (i = 0; i < n_lan_addr; i++) for (i = 0; i < n_lan_addr; i++)
{ {
@ -415,7 +396,7 @@ ParseUPnPClient(char *location)
n = nread; n = nread;
p = buf; p = buf;
while( !off && n-- > 0 ) while (!off && (n-- > 0))
{ {
if (p[0] == '\r' && p[1] == '\n' && p[2] == '\r' && p[3] == '\n') if (p[0] == '\r' && p[1] == '\n' && p[2] == '\r' && p[3] == '\n')
{ {
@ -432,15 +413,17 @@ ParseUPnPClient(char *location)
p = buf; p = buf;
if (strncmp(p, "HTTP/", 5) != 0) if (strncmp(p, "HTTP/", 5) != 0)
goto close; goto close;
while(*p != ' ' && *p != '\t') p++; while (*p != ' ' && *p != '\t')
p++;
/* If we don't get a 200 status, ignore it */ /* If we don't get a 200 status, ignore it */
if (strtol(p, NULL, 10) != 200) if (strtol(p, NULL, 10) != 200)
goto close; goto close;
if( (p = strcasestr(p, "Content-Length:")) ) p = strcasestr(p, "Content-Length:");
if (p)
content_len = strtol(p+15, NULL, 10); content_len = strtol(p+15, NULL, 10);
do_headers = 0; do_headers = 0;
} }
if( buf + nread - off >= content_len ) if ((buf + nread - off) >= content_len)
break; break;
} }
close: close:
@ -455,14 +438,14 @@ close:
if (model) if (model)
{ {
DPRINTF(E_DEBUG, L_SSDP, "Model: %s\n", model); DPRINTF(E_DEBUG, L_SSDP, "Model: %s\n", model);
if( strstr(model, "Roku SoundBridge") ) if (strstr(model, "Roku SoundBridge") != NULL)
{ {
type = ERokuSoundBridge; type = ERokuSoundBridge;
flags |= FLAG_MS_PFS; flags |= FLAG_MS_PFS;
flags |= FLAG_AUDIO_ONLY; flags |= FLAG_AUDIO_ONLY;
flags |= FLAG_MIME_WAV_WAV; flags |= FLAG_MIME_WAV_WAV;
} }
else if( strcmp(model, "Samsung DTV DMR") == 0 && serial ) else if ((strcmp(model, "Samsung DTV DMR") == 0) && serial )
{ {
DPRINTF(E_DEBUG, L_SSDP, "Serial: %s\n", serial); DPRINTF(E_DEBUG, L_SSDP, "Serial: %s\n", serial);
/* The Series B I saw was 20081224DMR. Series A should be older than that. */ /* The Series B I saw was 20081224DMR. Series A should be older than that. */
@ -531,6 +514,7 @@ ProcessSSDPRequest(int s, unsigned short port)
return; return;
} }
bufr[n] = '\0'; bufr[n] = '\0';
n -= 2;
if (memcmp(bufr, "NOTIFY", 6) == 0) if (memcmp(bufr, "NOTIFY", 6) == 0)
{ {
@ -542,40 +526,43 @@ ProcessSSDPRequest(int s, unsigned short port)
if( bufr[i] == '*' ) if( bufr[i] == '*' )
break; break;
} }
if( !strcasestrc(bufr+i, "HTTP/1.1", '\r') ) if (strcasestrc(bufr+i, "HTTP/1.1", '\r') == NULL)
return; return;
while (i < n) while (i < n)
{ {
while((i < n - 2) && (bufr[i] != '\r' || bufr[i+1] != '\n')) while ((i < n) && (bufr[i] != '\r' || bufr[i+1] != '\n'))
i++; i++;
i += 2; i += 2;
if (strncasecmp(bufr+i, "SERVER:", 7) == 0) if (strncasecmp(bufr+i, "SERVER:", 7) == 0)
{ {
srv = bufr+i+7; srv = bufr+i+7;
while(*srv == ' ' || *srv == '\t') srv++; while (*srv == ' ' || *srv == '\t')
srv++;
} }
else if (strncasecmp(bufr+i, "LOCATION:", 9) == 0) else if (strncasecmp(bufr+i, "LOCATION:", 9) == 0)
{ {
loc = bufr+i+9; loc = bufr+i+9;
while(*loc == ' ' || *loc == '\t') loc++; while (*loc == ' ' || *loc == '\t')
while(loc[loc_len]!='\r' && loc[loc_len]!='\n') loc_len++; loc++;
while (loc[loc_len]!='\r' && loc[loc_len]!='\n')
loc_len++;
} }
else if (strncasecmp(bufr+i, "NTS:", 4) == 0) else if (strncasecmp(bufr+i, "NTS:", 4) == 0)
{ {
nts = bufr+i+4; nts = bufr+i+4;
while(*nts == ' ' || *nts == '\t') nts++; while (*nts == ' ' || *nts == '\t')
nts++;
} }
else if (strncasecmp(bufr+i, "NT:", 3) == 0) else if (strncasecmp(bufr+i, "NT:", 3) == 0)
{ {
nt = bufr+i+3; nt = bufr+i+3;
while(*nt == ' ' || *nt == '\t') nt++; while(*nt == ' ' || *nt == '\t')
nt++;
} }
} }
if (!loc || !srv || !nt || !nts || (strncmp(nts, "ssdp:alive", 10) != 0) || if (!loc || !srv || !nt || !nts || (strncmp(nts, "ssdp:alive", 10) != 0) ||
(strncmp(nt, "urn:schemas-upnp-org:device:MediaRenderer", 41) != 0)) (strncmp(nt, "urn:schemas-upnp-org:device:MediaRenderer", 41) != 0))
{
return; return;
}
loc[loc_len] = '\0'; loc[loc_len] = '\0';
if ((strncmp(srv, "Allegro-Software-RomPlug", 24) == 0) || /* Roku */ if ((strncmp(srv, "Allegro-Software-RomPlug", 24) == 0) || /* Roku */
(strstr(loc, "SamsungMRDesc.xml") != NULL) || /* Samsung TV */ (strstr(loc, "SamsungMRDesc.xml") != NULL) || /* Samsung TV */
@ -604,34 +591,40 @@ ProcessSSDPRequest(int s, unsigned short port)
if (bufr[i] == '*') if (bufr[i] == '*')
break; break;
} }
if( !strcasestrc(bufr+i, "HTTP/1.1", '\r') ) if (strcasestrc(bufr+i, "HTTP/1.1", '\r') == NULL)
return; return;
while (i < n) while (i < n)
{ {
while((i < n - 2) && (bufr[i] != '\r' || bufr[i+1] != '\n')) while ((i < n) && (bufr[i] != '\r' || bufr[i+1] != '\n'))
i++; i++;
i += 2; i += 2;
if (strncasecmp(bufr+i, "ST:", 3) == 0) if (strncasecmp(bufr+i, "ST:", 3) == 0)
{ {
st = bufr+i+3; st = bufr+i+3;
st_len = 0; st_len = 0;
while(*st == ' ' || *st == '\t') st++; while (*st == ' ' || *st == '\t')
while(st[st_len]!='\r' && st[st_len]!='\n') st_len++; st++;
while (st[st_len]!='\r' && st[st_len]!='\n')
st_len++;
} }
else if (strncasecmp(bufr+i, "MX:", 3) == 0) else if (strncasecmp(bufr+i, "MX:", 3) == 0)
{ {
mx = bufr+i+3; mx = bufr+i+3;
mx_len = 0; mx_len = 0;
while(*mx == ' ' || *mx == '\t') mx++; while (*mx == ' ' || *mx == '\t')
while(mx[mx_len]!='\r' && mx[mx_len]!='\n') mx_len++; mx++;
while (mx[mx_len]!='\r' && mx[mx_len]!='\n')
mx_len++;
mx_val = strtol(mx, &mx_end, 10); mx_val = strtol(mx, &mx_end, 10);
} }
else if (strncasecmp(bufr+i, "MAN:", 4) == 0) else if (strncasecmp(bufr+i, "MAN:", 4) == 0)
{ {
man = bufr+i+4; man = bufr+i+4;
man_len = 0; man_len = 0;
while(*man == ' ' || *man == '\t') man++; while (*man == ' ' || *man == '\t')
while(man[man_len]!='\r' && man[man_len]!='\n') man_len++; man++;
while (man[man_len]!='\r' && man[man_len]!='\n')
man_len++;
} }
} }
/*DPRINTF(E_INFO, L_SSDP, "SSDP M-SEARCH packet received from %s:%d\n", /*DPRINTF(E_INFO, L_SSDP, "SSDP M-SEARCH packet received from %s:%d\n",
@ -647,7 +640,8 @@ ProcessSSDPRequest(int s, unsigned short port)
DPRINTF(E_INFO, L_SSDP, "WARNING: Ignoring invalid SSDP M-SEARCH from %s [bad %s header '%.*s']\n", DPRINTF(E_INFO, L_SSDP, "WARNING: Ignoring invalid SSDP M-SEARCH from %s [bad %s header '%.*s']\n",
inet_ntoa(sendername.sin_addr), "MAN", man_len, man); inet_ntoa(sendername.sin_addr), "MAN", man_len, man);
} }
else if( !mx || mx == mx_end || mx_val < 0 ) { else if (!mx || mx == mx_end || mx_val < 0)
{
DPRINTF(E_INFO, L_SSDP, "WARNING: Ignoring invalid SSDP M-SEARCH from %s [bad %s header '%.*s']\n", DPRINTF(E_INFO, L_SSDP, "WARNING: Ignoring invalid SSDP M-SEARCH from %s [bad %s header '%.*s']\n",
inet_ntoa(sendername.sin_addr), "MX", mx_len, mx); inet_ntoa(sendername.sin_addr), "MX", mx_len, mx);
} }
@ -658,14 +652,14 @@ ProcessSSDPRequest(int s, unsigned short port)
/* find in which sub network the client is */ /* find in which sub network the client is */
for (i = 0; i < n_lan_addr; i++) for (i = 0; i < n_lan_addr; i++)
{ {
if( (sendername.sin_addr.s_addr & lan_addr[i].mask.s_addr) if((sendername.sin_addr.s_addr & lan_addr[i].mask.s_addr) ==
== (lan_addr[i].addr.s_addr & lan_addr[i].mask.s_addr)) (lan_addr[i].addr.s_addr & lan_addr[i].mask.s_addr))
{ {
lan_addr_index = i; lan_addr_index = i;
break; break;
} }
} }
if( i == n_lan_addr ) if (n_lan_addr == i)
{ {
DPRINTF(E_DEBUG, L_SSDP, "Ignoring SSDP M-SEARCH on other interface [%s]\n", DPRINTF(E_DEBUG, L_SSDP, "Ignoring SSDP M-SEARCH on other interface [%s]\n",
inet_ntoa(sendername.sin_addr)); inet_ntoa(sendername.sin_addr));
@ -679,7 +673,7 @@ ProcessSSDPRequest(int s, unsigned short port)
for (i = 0; known_service_types[i]; i++) for (i = 0; known_service_types[i]; i++)
{ {
l = strlen(known_service_types[i]); l = strlen(known_service_types[i]);
if( l <= st_len && (memcmp(st, known_service_types[i], l) == 0)) if ((l <= st_len) && (memcmp(st, known_service_types[i], l) == 0))
{ {
if (st_len != l) if (st_len != l)
{ {
@ -700,21 +694,19 @@ ProcessSSDPRequest(int s, unsigned short port)
break; break;
} }
_usleep(random()>>20); _usleep(random()>>20);
SendSSDPAnnounce2(s, sendername, SendSSDPAnnounce2(s, sendername, i,
i,
lan_addr[lan_addr_index].str, port); lan_addr[lan_addr_index].str, port);
break; break;
} }
} }
/* Responds to request with ST: ssdp:all */ /* Responds to request with ST: ssdp:all */
/* strlen("ssdp:all") == 8 */ /* strlen("ssdp:all") == 8 */
if(st_len==8 && (0 == memcmp(st, "ssdp:all", 8))) if ((st_len == 8) && (memcmp(st, "ssdp:all", 8) == 0))
{ {
for (i=0; known_service_types[i]; i++) for (i=0; known_service_types[i]; i++)
{ {
l = (int)strlen(known_service_types[i]); l = strlen(known_service_types[i]);
SendSSDPAnnounce2(s, sendername, SendSSDPAnnounce2(s, sendername, i,
i,
lan_addr[lan_addr_index].str, port); lan_addr[lan_addr_index].str, port);
} }
} }
@ -783,7 +775,8 @@ SendSSDPGoodbye(int * sockets, int n_sockets)
* register services offered by MiniUPnPd to a running instance of * register services offered by MiniUPnPd to a running instance of
* MiniSSDPd */ * MiniSSDPd */
int int
SubmitServicesToMiniSSDPD(const char * host, unsigned short port) { SubmitServicesToMiniSSDPD(const char *host, unsigned short port)
{
struct sockaddr_un addr; struct sockaddr_un addr;
int s; int s;
unsigned char buffer[2048]; unsigned char buffer[2048];
@ -792,21 +785,24 @@ SubmitServicesToMiniSSDPD(const char * host, unsigned short port) {
int i, l; int i, l;
s = socket(AF_UNIX, SOCK_STREAM, 0); s = socket(AF_UNIX, SOCK_STREAM, 0);
if(s < 0) { if (s < 0)
{
DPRINTF(E_ERROR, L_SSDP, "socket(unix): %s", strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "socket(unix): %s", strerror(errno));
return -1; return -1;
} }
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, minissdpdsocketpath, sizeof(addr.sun_path)); strncpy(addr.sun_path, minissdpdsocketpath, sizeof(addr.sun_path));
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) { if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
{
DPRINTF(E_ERROR, L_SSDP, "connect(\"%s\"): %s", DPRINTF(E_ERROR, L_SSDP, "connect(\"%s\"): %s",
minissdpdsocketpath, strerror(errno)); minissdpdsocketpath, strerror(errno));
return -1; return -1;
} }
for(i = 0; known_service_types[i]; i++) { for (i = 0; known_service_types[i]; i++)
{
buffer[0] = 4; buffer[0] = 4;
p = buffer + 1; p = buffer + 1;
l = (int)strlen(known_service_types[i]); l = strlen(known_service_types[i]);
if (i > 0) if (i > 0)
l++; l++;
CODELENGTH(l, p); CODELENGTH(l, p);
@ -819,7 +815,7 @@ SubmitServicesToMiniSSDPD(const char * host, unsigned short port) {
CODELENGTH(l, p); CODELENGTH(l, p);
memcpy(p, strbuf, l); memcpy(p, strbuf, l);
p += l; p += l;
l = (int)strlen(MINIDLNA_SERVER_STRING); l = strlen(MINIDLNA_SERVER_STRING);
CODELENGTH(l, p); CODELENGTH(l, p);
memcpy(p, MINIDLNA_SERVER_STRING, l); memcpy(p, MINIDLNA_SERVER_STRING, l);
p += l; p += l;
@ -828,7 +824,8 @@ SubmitServicesToMiniSSDPD(const char * host, unsigned short port) {
CODELENGTH(l, p); CODELENGTH(l, p);
memcpy(p, strbuf, l); memcpy(p, strbuf, l);
p += l; p += l;
if(write(s, buffer, p - buffer) < 0) { if(write(s, buffer, p - buffer) < 0)
{
DPRINTF(E_ERROR, L_SSDP, "write(): %s", strerror(errno)); DPRINTF(E_ERROR, L_SSDP, "write(): %s", strerror(errno));
return -1; return -1;
} }