1 /*****************************************************************************
2 * sdp.c : SDP creation helpers
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
27 #include <vlc_network.h>
28 #include <vlc_charset.h>
30 #include "stream_output.h"
32 #define MAXSDPADDRESS 47
35 char *AddressToSDP (const struct sockaddr *addr, socklen_t addrlen, char *buf)
37 if (addrlen < offsetof (struct sockaddr, sa_family)
38 + sizeof (addr->sa_family))
41 const char *ttl = NULL;
42 strcpy (buf, "IN IP* ");
44 switch (addr->sa_family)
48 if (net_SockAddrIsMulticast (addr, addrlen))
49 ttl = "/255"; // obsolete in RFC4566, dummy value
64 if (vlc_getnameinfo (addr, addrlen, buf + 7, MAXSDPADDRESS - 7, NULL,
75 static vlc_bool_t IsSDPString (const char *str)
77 if (strchr (str, '\r') != NULL)
79 if (strchr (str, '\n') != NULL)
87 char *StartSDP (const char *name, const char *description, const char *url,
88 const char *email, const char *phone,
89 const struct sockaddr *orig, socklen_t origlen,
90 const struct sockaddr *addr, socklen_t addrlen)
92 uint64_t t = NTPtime64 ();
93 char *sdp, machine[MAXSDPADDRESS], conn[MAXSDPADDRESS];
94 const char *preurl = "\r\nu=", *premail = "\r\ne=", *prephone = "\r\np=";
98 if (description == NULL)
103 premail = email = "";
105 prephone = phone = "";
107 if (!IsSDPString (name) || !IsSDPString (description)
108 || !IsSDPString (url) || !IsSDPString (email) || !IsSDPString (phone)
109 || (AddressToSDP ((struct sockaddr *)orig, origlen, machine) == NULL)
110 || (AddressToSDP ((struct sockaddr *)addr, addrlen, conn) == NULL))
113 if (asprintf (&sdp, "v=0"
114 "\r\no=- "I64Fu" "I64Fu" %s"
117 "%s%s" // optional URL
118 "%s%s" // optional email
119 "%s%s" // optional phone number
121 // bandwidth not specified
122 "\r\nt=0 0" // one dummy time span
124 // no time zone adjustment (silly idea anyway)
125 // no encryption key (deprecated)
126 "\r\na=tool:"PACKAGE_STRING
128 "\r\na=type:broadcast"
129 "\r\na=charset:UTF-8"
131 /* o= */ t, t, machine,
133 /* i= */ description,
134 /* u= */ preurl, url,
135 /* e= */ premail, email,
136 /* p= */ prephone, phone,
137 /* c= */ conn) == -1)