]> git.sesse.net Git - vlc/blob - src/stream_output/sdp.c
Fix link-scoped IPv6 addresses
[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 #include "stream_output.h"
31
32 #define MAXSDPADDRESS 47
33
34 static
35 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
36 {
37     if (addrlen < offsetof (struct sockaddr, sa_family)
38                  + sizeof (addr->sa_family))
39         return NULL;
40
41     strcpy (buf, "IN IP* ");
42
43     if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
44                          NI_NUMERICHOST))
45         return NULL;
46
47     switch (addr->sa_family)
48     {
49         case AF_INET:
50         {
51             if (net_SockAddrIsMulticast (addr, addrlen))
52                 strcat (buf, "/255"); // obsolete in RFC4566, dummy value
53             buf[5] = '4';
54             break;
55         }
56
57 #ifdef AF_INET6
58         case AF_INET6:
59         {
60             char *ptr = strchr (buf, '%');
61             if (ptr != NULL)
62                 *ptr = '\0'; // remove scope ID
63             buf[5] = '6';
64             break;
65         }
66 #endif
67
68         default:
69             return NULL;
70     }
71
72     return buf;
73 }
74
75
76 static vlc_bool_t IsSDPString (const char *str)
77 {
78     if (strchr (str, '\r') != NULL)
79         return VLC_FALSE;
80     if (strchr (str, '\n') != NULL)
81         return VLC_FALSE;
82     if (!IsUTF8 (str))
83         return VLC_FALSE;
84     return VLC_TRUE;
85 }
86
87
88 char *StartSDP (const char *name, const char *description, const char *url,
89                 const char *email, const char *phone, vlc_bool_t ssm,
90                 const struct sockaddr *orig, socklen_t origlen,
91                 const struct sockaddr *addr, socklen_t addrlen)
92 {
93     uint64_t t = NTPtime64 ();
94     char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS],
95          sfilter[MAXSDPADDRESS + sizeof ("\r\na=source-filter: incl * ")];
96     const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
97
98     if (name == NULL)
99         name = "Unnamed";
100     if (description == NULL)
101         description = "N/A";
102     if (url == NULL)
103         preurl = url = "";
104     if (email == NULL)
105         premail = email = "";
106     if (phone == NULL)
107         prephone = phone = "";
108
109     if (!IsSDPString (name) || !IsSDPString (description)
110      || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
111      || (AddressToSDP ((struct sockaddr *)orig, origlen, machine) == NULL)
112      || (AddressToSDP ((struct sockaddr *)addr, addrlen, conn) == NULL))
113         return NULL;
114
115     if (ssm)
116         sprintf (sfilter, "\r\na=source-filter: incl IN IP%c * %s",
117                  machine[5], machine + 7);
118     else
119         *sfilter = '\0';
120
121     if (asprintf (&sdp, "v=0"
122                     "\r\no=- "I64Fu" "I64Fu" %s"
123                     "\r\ns=%s"
124                     "\r\ni=%s"
125                     "%s%s" // optional URL
126                     "%s%s" // optional email
127                     "%s%s" // optional phone number
128                     "\r\nc=%s"
129                         // bandwidth not specified
130                     "\r\nt=0 0" // one dummy time span
131                         // no repeating
132                         // no time zone adjustment (silly idea anyway)
133                         // no encryption key (deprecated)
134                     "\r\na=tool:"PACKAGE_STRING
135                     "\r\na=recvonly"
136                     "\r\na=type:broadcast"
137                     "\r\na=charset:UTF-8"
138                     "%s" // optional source filter
139                     "\r\n",
140                /* o= */ t, t, machine,
141                /* s= */ name,
142                /* i= */ description,
143                /* u= */ preurl, url,
144                /* e= */ premail, email,
145                /* p= */ prephone, phone,
146                /* c= */ conn,
147     /* source-filter */ sfilter) == -1)
148         return NULL;
149     return sdp;
150 }
151