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