1 /*****************************************************************************
2 * sdp.c : SDP creation helpers
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_charset.h>
29 #include <vlc_network.h>
30 #include <vlc_memstream.h>
32 #include "sdp_helper.h"
34 #define MAXSDPADDRESS 47
37 char *AddressToSDP (const struct sockaddr
*addr
, size_t addrlen
, char *buf
)
39 if (addrlen
< offsetof (struct sockaddr
, sa_family
)
40 + sizeof (addr
->sa_family
))
43 strcpy (buf
, "IN IP* ");
45 if (vlc_getnameinfo (addr
, addrlen
, buf
+ 7, MAXSDPADDRESS
- 7, NULL
,
49 switch (addr
->sa_family
)
53 if (net_SockAddrIsMulticast (addr
, addrlen
))
54 strcat (buf
, "/255"); // obsolete in RFC4566, dummy value
62 char *ptr
= strchr (buf
, '%');
64 *ptr
= '\0'; // remove scope ID
78 static bool IsSDPString (const char *str
)
80 if (strchr (str
, '\r') != NULL
)
82 if (strchr (str
, '\n') != NULL
)
89 int vlc_sdp_Start(struct vlc_memstream
*restrict stream
,
90 vlc_object_t
*obj
, const char *cfgpref
,
91 const struct sockaddr
*src
, size_t srclen
,
92 const struct sockaddr
*addr
, size_t addrlen
)
94 char connection
[MAXSDPADDRESS
];
97 size_t cfglen
= strlen(cfgpref
);
101 char varname
[cfglen
+ sizeof ("description")];
102 char *subvar
= varname
+ cfglen
;
104 strcpy(varname
, cfgpref
);
106 vlc_memstream_open(stream
);
107 vlc_memstream_puts(stream
, "v=0\r\n");
109 if (AddressToSDP(addr
, addrlen
, connection
) == NULL
)
112 const uint_fast64_t now
= NTPtime64();
115 gethostname(hostname
, sizeof (hostname
));
117 vlc_memstream_printf(stream
, "o=- %"PRIu64
" %"PRIu64
" IN IP%c %s\r\n",
118 now
, now
, connection
[5], hostname
);
121 strcpy(subvar
, "name");
122 str
= var_GetNonEmptyString(obj
, varname
);
125 if (!IsSDPString(str
))
128 vlc_memstream_printf(stream
, "s=%s\r\n", str
);
132 vlc_memstream_printf(stream
, "s=%s\r\n", "Unnamed");
134 strcpy(subvar
, "description");
135 str
= var_GetNonEmptyString(obj
, varname
);
138 if (!IsSDPString(str
))
141 vlc_memstream_printf(stream
, "i=%s\r\n", str
);
145 // no URL, email, no phone (useless)
147 vlc_memstream_printf(stream
, "c=%s\r\n", connection
);
148 // bandwidth not specified
149 vlc_memstream_puts(stream
, "t=0 0\r\n"); // one dummy time span
151 // no time zone adjustment (silly idea anyway)
152 // no encryption key (deprecated)
154 vlc_memstream_printf(stream
, "a=tool:%s\r\n", PACKAGE_STRING
);
155 vlc_memstream_puts(stream
, "a=recvonly\r\n");
156 vlc_memstream_puts(stream
, "a=type:broadcast\r\n");
157 vlc_memstream_puts(stream
, "a=charset:UTF-8\r\n");
161 char machine
[MAXSDPADDRESS
];
163 if (AddressToSDP(src
, srclen
, machine
) != NULL
)
164 vlc_memstream_printf(stream
,
165 "a=source-filter: incl IN IP%c * %s\r\n",
166 machine
[5], machine
+ 7);
169 strcpy(subvar
, "cat");
170 str
= var_GetNonEmptyString(obj
, varname
);
173 if (IsSDPString(str
))
176 vlc_memstream_printf(stream
, "a=cat:%s\r\n", str
);
177 vlc_memstream_printf(stream
, "a=x-plgroup:%s\r\n", str
);
183 if (vlc_memstream_close(stream
) == 0)