]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Add SDP generation helper with a bunch of would-be conformance fixes
[vlc] / src / stream_output / sdp.c
1 /*****************************************************************************
2  * sdp.c : SDP creation helpers
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Authors: Rémi Denis-Courmont
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 # include <vlc/vlc.h>
25
26 # include <string.h>
27 # include <vlc_network.h>
28 # include <vlc_charset.h>
29
30 # define MAXSDPADDRESS 47
31
32 static
33 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
34 {
35     const char *ttl = NULL;
36     strcpy (buf, "IN IP* ");
37
38     switch (addr->sa_family)
39     {
40         case AF_INET:
41         {
42             if (net_SockAddrIsMulticast (addr, addrlen))
43                 ttl = "/255"; // obsolete in RFC4566, dummy value
44             buf[5] = '4';
45             break;
46         }
47
48 #ifdef AF_INET6
49         case AF_INET6:
50             buf[5] = '6';
51             break;
52 #endif
53
54         default:
55             return NULL;
56     }
57
58     if (vlc_getnameinfo (addr, addrlen, buf + 4, MAXSDPADDRESS - 4, NULL,
59                          NI_NUMERICHOST))
60         return NULL;
61
62     if (ttl != NULL)
63         strcat (buf, ttl);
64
65     return buf;
66 }
67
68
69 char *StartSDP (const char *name,
70                 const struct sockaddr *orig, socklen_t origlen,
71                 const struct sockaddr *addr, socklen_t addrlen)
72 {
73     uint64_t t = NTPtime64 ();
74     char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS];
75
76     if (strchr (name, '\r') || strchr (name, '\n') || !IsUTF8 (name)
77      || (AddressToSDP ((struct sockaddr *)&orig, origlen, machine) == NULL)
78      || (AddressToSDP ((struct sockaddr *)&addr, addrlen, conn) == NULL))
79         return NULL;
80
81     if (asprintf (&sdp, "v=0\r\n"
82                         "o=- "I64Fu" "I64Fu" %s\r\n"
83                         "s=%s\r\n"
84                         "i=N/A\r\n" // must be there even if useless
85                         // no URL, email and phone here */
86                         "c=%s\r\n"
87                         // bandwidth not specified
88                         "t= 0 0" // one dummy time span
89                         // no repeating
90                         // no time zone adjustment (silly idea anyway)
91                         // no encryption key (deprecated)
92                         "a=tool:"PACKAGE_STRING"\r\n"
93                         "a=recvonly\r\n"
94                         "a=type:broadcast\r\n"
95                         "a=charset:UTF-8\r\n",
96                /* o= */ t, t, machine,
97                /* s= */ name,
98                /* c= */ conn) == -1)
99         return NULL;
100     return sdp;
101 }
102