Mercurial > pt1.oyama
annotate src/ushare.c @ 172:89e24a1c8a64
Fix problem: If boot argument channel had selected by DLNA. Stream will interrupted.
author | Naoya OYAMA <naoya.oyama@gmail.com> |
---|---|
date | Mon, 29 Oct 2012 22:25:59 +0900 |
parents | 6c710e5a28b8 |
children | 2ae60285f383 |
rev | line source |
---|---|
125 | 1 /* |
2 * ushare.c : GeeXboX uShare UPnP Media Server. | |
3 * Originally developped for the GeeXboX project. | |
4 * Parts of the code are originated from GMediaServer from Oskar Liljeblad. | |
5 * Copyright (C) 2005-2007 Benjamin Zores <ben@geexbox.org> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Library General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License along | |
18 * with this program; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 */ | |
21 | |
22 #include <stdio.h> | |
23 #include <signal.h> | |
24 #include <string.h> | |
25 #include <stdlib.h> | |
26 #include <stdarg.h> | |
27 #include <unistd.h> | |
28 #include <errno.h> | |
29 #include <getopt.h> | |
30 | |
31 #if (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__)) | |
32 #include <sys/socket.h> | |
33 #include <sys/sysctl.h> | |
34 #include <net/if_dl.h> | |
35 #endif | |
36 | |
37 #if (defined(__APPLE__)) | |
38 #include <net/route.h> | |
39 #endif | |
40 | |
41 #include <net/if.h> | |
42 #include <sys/ioctl.h> | |
43 #include <sys/types.h> | |
44 #include <sys/stat.h> | |
45 #include <stdbool.h> | |
46 #include <fcntl.h> | |
47 | |
48 #ifdef HAVE_IFADDRS_H | |
49 #include <ifaddrs.h> | |
50 #endif | |
51 | |
52 #if (defined(__unix__) || defined(unix)) && !defined(USG) | |
53 #include <sys/param.h> | |
54 #endif | |
55 | |
136
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
133
diff
changeset
|
56 #include <upnp.h> |
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
133
diff
changeset
|
57 #include <upnptools.h> |
125 | 58 |
59 #if (defined(HAVE_SETLOCALE) && defined(CONFIG_NLS)) | |
60 # include <locale.h> | |
61 #endif | |
62 | |
63 #include "config.h" | |
64 #include "ushare.h" | |
65 #include "services.h" | |
66 #include "http.h" | |
67 #include "metadata.h" | |
68 #include "util_iconv.h" | |
69 #include "content.h" | |
70 #include "cfgparser.h" | |
71 #include "gettext.h" | |
72 #include "trace.h" | |
73 #include "buffer.h" | |
74 #include "ctrl_telnet.h" | |
75 #include "recpt1.h" | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
76 #include "pt1_common.h" |
125 | 77 |
78 struct ushare_t *ut = NULL; | |
79 | |
80 static struct ushare_t * ushare_new (void) | |
81 __attribute__ ((malloc)); | |
82 | |
83 static struct ushare_t * | |
84 ushare_new (void) | |
85 { | |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
86 extern thread_data *gp_tdata; |
152
30e91361506a
EXPERIMENTAL: Enable change phisical channel by DLNA.(ISDB-T only)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
146
diff
changeset
|
87 extern struct channel_info_list *channel_list; |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
88 thread_data *tdata = gp_tdata; |
125 | 89 struct ushare_t *ut = (struct ushare_t *) malloc (sizeof (struct ushare_t)); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
90 char model_name[32]; |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
91 char host_name[HOST_NAME_MAX+1]; |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
92 model_name[0] = '\0'; |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
93 gethostname(host_name, sizeof(host_name)); |
125 | 94 if (!ut) |
95 return NULL; | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
96 snprintf(model_name, sizeof(model_name), "%s: %s", host_name, tdata->device_name); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
97 model_name[sizeof(model_name)-1] = '\0'; |
125 | 98 |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
99 // ut->name = strdup (DEFAULT_USHARE_NAME); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
100 ut->name = strdup (model_name); |
125 | 101 ut->interface = strdup (DEFAULT_USHARE_IFACE); |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
102 // ut->model_name = strdup (DEFAULT_USHARE_NAME); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
103 ut->model_name = strdup (model_name); |
125 | 104 ut->contentlist = NULL; |
105 ut->rb = rbinit (rb_compare, NULL); | |
106 ut->root_entry = NULL; | |
107 ut->nr_entries = 0; | |
108 ut->starting_id = STARTING_ENTRY_ID_DEFAULT; | |
109 ut->init = 0; | |
110 ut->dev = 0; | |
111 ut->udn = NULL; | |
112 ut->ip = NULL; | |
113 ut->port = 0; /* Randomly attributed by libupnp */ | |
114 ut->telnet_port = CTRL_TELNET_PORT; | |
115 ut->presentation = NULL; | |
116 ut->use_presentation = true; | |
117 ut->use_telnet = true; | |
118 #ifdef HAVE_DLNA | |
119 ut->dlna_enabled = false; | |
120 ut->dlna = NULL; | |
121 ut->dlna_flags = DLNA_ORG_FLAG_STREAMING_TRANSFER_MODE | | |
122 DLNA_ORG_FLAG_BACKGROUND_TRANSFERT_MODE | | |
123 DLNA_ORG_FLAG_CONNECTION_STALL | | |
124 DLNA_ORG_FLAG_DLNA_V15; | |
125 #endif /* HAVE_DLNA */ | |
126 ut->xbox360 = false; | |
127 ut->verbose = false; | |
128 ut->daemon = false; | |
129 ut->override_iconv_err = false; | |
130 ut->cfg_file = NULL; | |
166 | 131 ut->channel_list = channel_list; |
125 | 132 |
133 pthread_mutex_init (&ut->termination_mutex, NULL); | |
134 pthread_cond_init (&ut->termination_cond, NULL); | |
135 | |
136 return ut; | |
137 } | |
138 | |
139 static void | |
140 ushare_free (struct ushare_t *ut) | |
141 { | |
142 if (!ut) | |
143 return; | |
144 | |
145 if (ut->name) | |
146 free (ut->name); | |
147 if (ut->interface) | |
148 free (ut->interface); | |
149 if (ut->model_name) | |
150 free (ut->model_name); | |
151 if (ut->contentlist) | |
152 content_free (ut->contentlist); | |
153 if (ut->rb) | |
154 rbdestroy (ut->rb); | |
155 if (ut->root_entry) | |
156 upnp_entry_free (ut, ut->root_entry); | |
157 if (ut->udn) | |
158 free (ut->udn); | |
159 if (ut->ip) | |
160 free (ut->ip); | |
161 if (ut->presentation) | |
162 buffer_free (ut->presentation); | |
163 #ifdef HAVE_DLNA | |
164 if (ut->dlna_enabled) | |
165 { | |
166 if (ut->dlna) | |
167 dlna_uninit (ut->dlna); | |
168 ut->dlna = NULL; | |
169 } | |
170 #endif /* HAVE_DLNA */ | |
171 if (ut->cfg_file) | |
172 free (ut->cfg_file); | |
173 | |
174 pthread_cond_destroy (&ut->termination_cond); | |
175 pthread_mutex_destroy (&ut->termination_mutex); | |
176 | |
177 free (ut); | |
178 } | |
179 | |
180 static void | |
181 ushare_signal_exit (void) | |
182 { | |
183 pthread_mutex_lock (&ut->termination_mutex); | |
184 pthread_cond_signal (&ut->termination_cond); | |
185 pthread_mutex_unlock (&ut->termination_mutex); | |
186 } | |
187 | |
188 static void | |
189 handle_action_request (struct Upnp_Action_Request *request) | |
190 { | |
191 struct service_t *service; | |
192 struct service_action_t *action; | |
193 char val[256]; | |
194 uint32_t ip; | |
195 | |
196 if (!request || !ut) | |
197 return; | |
198 | |
199 if (request->ErrCode != UPNP_E_SUCCESS) | |
200 return; | |
201 | |
202 if (strcmp (request->DevUDN + 5, ut->udn)) | |
203 return; | |
204 | |
205 if (ut->verbose) | |
206 { | |
207 DOMString str = ixmlPrintDocument (request->ActionRequest); | |
208 log_verbose ("***************************************************\n"); | |
209 log_verbose ("** New Action Request **\n"); | |
210 log_verbose ("***************************************************\n"); | |
211 log_verbose ("ServiceID: %s\n", request->ServiceID); | |
212 log_verbose ("ActionName: %s\n", request->ActionName); | |
213 log_verbose ("Action Request:\n%s\n", str); | |
214 ixmlFreeDOMString (str); | |
215 } | |
216 | |
217 if (find_service_action (request, &service, &action)) | |
218 { | |
219 struct action_event_t event; | |
220 | |
221 event.request = request; | |
222 event.status = true; | |
223 event.service = service; | |
224 | |
225 if (action->function (&event) && event.status) | |
226 request->ErrCode = UPNP_E_SUCCESS; | |
227 | |
228 if (ut->verbose) | |
229 { | |
230 DOMString str = ixmlPrintDocument (request->ActionResult); | |
231 log_verbose ("Action Result:\n%s", str); | |
232 log_verbose ("***************************************************\n"); | |
233 log_verbose ("\n"); | |
234 ixmlFreeDOMString (str); | |
235 } | |
236 | |
237 return; | |
238 } | |
239 | |
240 if (service) /* Invalid Action name */ | |
241 strcpy (request->ErrStr, "Unknown Service Action"); | |
242 else /* Invalid Service name */ | |
243 strcpy (request->ErrStr, "Unknown Service ID"); | |
244 | |
245 request->ActionResult = NULL; | |
246 request->ErrCode = UPNP_SOAP_E_INVALID_ACTION; | |
247 } | |
248 | |
249 static int | |
250 device_callback_event_handler (Upnp_EventType type, void *event, | |
251 void *cookie __attribute__((unused))) | |
252 { | |
253 switch (type) | |
254 { | |
255 case UPNP_CONTROL_ACTION_REQUEST: | |
256 handle_action_request ((struct Upnp_Action_Request *) event); | |
257 break; | |
258 case UPNP_CONTROL_ACTION_COMPLETE: | |
259 case UPNP_EVENT_SUBSCRIPTION_REQUEST: | |
260 case UPNP_CONTROL_GET_VAR_REQUEST: | |
261 break; | |
262 default: | |
263 break; | |
264 } | |
265 | |
266 return 0; | |
267 } | |
268 | |
269 static int | |
270 finish_upnp (struct ushare_t *ut) | |
271 { | |
272 if (!ut) | |
273 return -1; | |
274 | |
275 log_info (_("Stopping UPnP Service ...\n")); | |
276 UpnpUnRegisterRootDevice (ut->dev); | |
277 UpnpFinish (); | |
278 | |
279 return UPNP_E_SUCCESS; | |
280 } | |
281 | |
282 static int | |
283 init_upnp (struct ushare_t *ut) | |
284 { | |
285 char *description = NULL; | |
286 int res; | |
287 size_t len; | |
288 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
289 char *friendly_name = ut->name; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
290 char manufacturer[] = "GeeXboX Team"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
291 char manufacturer_url[] = "http://ushare.geexbox.org/"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
292 char model_description[] = "uShare : DLNA Media Server"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
293 char *model_name = ut->model_name; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
294 char model_number[] = "001"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
295 char model_url[] = "http://ushare.geexbox.org/"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
296 char serial_number[] = "USHARE-01"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
297 char *uuid = ut->udn; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
298 char presentation_url[] = "/web/ushare.html"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
299 char cms_scpd_url[] = "/web/cms.xml"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
300 char cms_control_url[] = "/web/cms_control"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
301 char cms_event_url[] = "/web/cms_event"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
302 char cds_scpd_url[] = "/web/cds.xml"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
303 char cds_control_url[] = "/web/cds_control"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
304 char cds_event_url[] = "/web/cds_event"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
305 |
125 | 306 if (!ut || !ut->name || !ut->udn || !ut->ip) |
307 return -1; | |
308 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
309 len = strlen (UPNP_DMS_DESCRIPTION) + strlen (friendly_name) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
310 + strlen (manufacturer) + strlen (manufacturer_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
311 + strlen (model_description) + strlen (model_name) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
312 + strlen (model_number) + strlen (model_url) + strlen (serial_number) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
313 + strlen (uuid) + strlen (presentation_url) + strlen (cms_scpd_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
314 + strlen (cms_control_url) + strlen (cms_event_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
315 + strlen (cds_scpd_url) + strlen (cds_control_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
316 + strlen (cds_event_url) + 1; |
125 | 317 description = (char *) malloc (len * sizeof (char)); |
318 memset (description, 0, len); | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
319 sprintf (description, UPNP_DMS_DESCRIPTION, friendly_name, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
320 manufacturer, manufacturer_url, model_description, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
321 model_name, model_number, model_url, serial_number, uuid, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
322 presentation_url, cms_scpd_url, cms_control_url, cms_event_url, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
323 cds_scpd_url, cds_control_url, cds_event_url); |
125 | 324 |
325 log_info (_("Initializing UPnP subsystem ...\n")); | |
326 res = UpnpInit (ut->ip, ut->port); | |
327 if (res != UPNP_E_SUCCESS) | |
328 { | |
329 log_error (_("Cannot initialize UPnP subsystem\n")); | |
330 return -1; | |
331 } | |
332 | |
333 if (UpnpSetMaxContentLength (UPNP_MAX_CONTENT_LENGTH) != UPNP_E_SUCCESS) | |
334 log_info (_("Could not set Max content UPnP\n")); | |
335 | |
336 if (ut->xbox360) | |
337 log_info (_("Starting in XboX 360 compliant profile ...\n")); | |
338 | |
339 #ifdef HAVE_DLNA | |
340 if (ut->dlna_enabled) | |
341 { | |
342 log_info (_("Starting in DLNA compliant profile ...\n")); | |
343 ut->dlna = dlna_init (); | |
344 dlna_set_verbosity (ut->dlna, ut->verbose ? 1 : 0); | |
345 dlna_set_extension_check (ut->dlna, 1); | |
346 dlna_register_all_media_profiles (ut->dlna); | |
347 } | |
348 #endif /* HAVE_DLNA */ | |
349 | |
350 ut->port = UpnpGetServerPort(); | |
351 log_info (_("UPnP MediaServer listening on %s:%d\n"), | |
352 UpnpGetServerIpAddress (), ut->port); | |
353 | |
354 UpnpEnableWebserver (TRUE); | |
355 | |
356 res = UpnpSetVirtualDirCallbacks (&virtual_dir_callbacks); | |
357 if (res != UPNP_E_SUCCESS) | |
358 { | |
359 log_error (_("Cannot set virtual directory callbacks\n")); | |
360 free (description); | |
361 return -1; | |
362 } | |
363 | |
364 res = UpnpAddVirtualDir (VIRTUAL_DIR); | |
365 if (res != UPNP_E_SUCCESS) | |
366 { | |
367 log_error (_("Cannot add virtual directory for web server\n")); | |
368 free (description); | |
369 return -1; | |
370 } | |
371 | |
372 res = UpnpRegisterRootDevice2 (UPNPREG_BUF_DESC, description, 0, 1, | |
373 device_callback_event_handler, | |
374 NULL, &(ut->dev)); | |
375 if (res != UPNP_E_SUCCESS) | |
376 { | |
377 log_error (_("Cannot register UPnP device\n")); | |
378 free (description); | |
379 return -1; | |
380 } | |
381 | |
382 res = UpnpUnRegisterRootDevice (ut->dev); | |
383 if (res != UPNP_E_SUCCESS) | |
384 { | |
385 log_error (_("Cannot unregister UPnP device\n")); | |
386 free (description); | |
387 return -1; | |
388 } | |
389 | |
390 res = UpnpRegisterRootDevice2 (UPNPREG_BUF_DESC, description, 0, 1, | |
391 device_callback_event_handler, | |
392 NULL, &(ut->dev)); | |
393 if (res != UPNP_E_SUCCESS) | |
394 { | |
395 log_error (_("Cannot register UPnP device\n")); | |
396 free (description); | |
397 return -1; | |
398 } | |
399 | |
400 log_info (_("Sending UPnP advertisement for device ...\n")); | |
401 UpnpSendAdvertisement (ut->dev, 1800); | |
402 | |
403 log_info (_("Listening for control point connections ...\n")); | |
404 | |
405 if (description) | |
406 free (description); | |
407 | |
408 return 0; | |
409 } | |
410 | |
411 static bool | |
412 has_iface (char *interface) | |
413 { | |
414 #ifdef HAVE_IFADDRS_H | |
415 struct ifaddrs *itflist, *itf; | |
416 | |
417 if (!interface) | |
418 return false; | |
419 | |
420 if (getifaddrs (&itflist) < 0) | |
421 { | |
422 perror ("getifaddrs"); | |
423 return false; | |
424 } | |
425 | |
426 itf = itflist; | |
427 while (itf) | |
428 { | |
429 if ((itf->ifa_flags & IFF_UP) | |
430 && !strncmp (itf->ifa_name, interface, IFNAMSIZ)) | |
431 { | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
432 //log_error (_("Interface %s is down.\n"), interface); |
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
433 //log_error (_("Recheck uShare's configuration and try again !\n")); |
125 | 434 freeifaddrs (itflist); |
435 return true; | |
436 } | |
437 itf = itf->ifa_next; | |
438 } | |
439 | |
440 freeifaddrs (itf); | |
441 #else | |
442 int sock, i, n; | |
443 struct ifconf ifc; | |
444 struct ifreq ifr; | |
445 char buff[8192]; | |
446 | |
447 if (!interface) | |
448 return false; | |
449 | |
450 /* determine UDN according to MAC address */ | |
451 sock = socket (AF_INET, SOCK_STREAM, 0); | |
452 if (sock < 0) | |
453 { | |
454 perror ("socket"); | |
455 return false; | |
456 } | |
457 | |
458 /* get list of available interfaces */ | |
459 ifc.ifc_len = sizeof (buff); | |
460 ifc.ifc_buf = buff; | |
461 | |
462 if (ioctl (sock, SIOCGIFCONF, &ifc) < 0) | |
463 { | |
464 perror ("ioctl"); | |
465 close (sock); | |
466 return false; | |
467 } | |
468 | |
469 n = ifc.ifc_len / sizeof (struct ifreq); | |
470 for (i = n - 1 ; i >= 0 ; i--) | |
471 { | |
472 ifr = ifc.ifc_req[i]; | |
473 | |
474 if (strncmp (ifr.ifr_name, interface, IFNAMSIZ)) | |
475 continue; | |
476 | |
477 if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) | |
478 { | |
479 perror ("ioctl"); | |
480 close (sock); | |
481 return false; | |
482 } | |
483 | |
484 if (!(ifr.ifr_flags & IFF_UP)) | |
485 { | |
486 /* interface is down */ | |
487 log_error (_("Interface %s is down.\n"), interface); | |
488 log_error (_("Recheck uShare's configuration and try again !\n")); | |
489 close (sock); | |
490 return false; | |
491 } | |
492 | |
493 /* found right interface */ | |
494 close (sock); | |
495 return true; | |
496 } | |
497 close (sock); | |
498 #endif | |
499 | |
500 log_error (_("Can't find interface %s.\n"),interface); | |
501 log_error (_("Recheck uShare's configuration and try again !\n")); | |
502 | |
503 return false; | |
504 } | |
505 | |
506 static char * | |
507 create_udn (char *interface) | |
508 { | |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
509 extern thread_data *gp_tdata; |
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
510 thread_data *tdata = gp_tdata; |
125 | 511 int sock = -1; |
512 char *buf; | |
513 unsigned char *ptr; | |
514 | |
515 #if (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__)) | |
516 int mib[6]; | |
517 size_t len; | |
518 struct if_msghdr *ifm; | |
519 struct sockaddr_dl *sdl; | |
520 #else /* Linux */ | |
521 struct ifreq ifr; | |
522 #endif | |
523 | |
524 if (!interface) | |
525 return NULL; | |
526 | |
527 #if (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__)) | |
528 mib[0] = CTL_NET; | |
529 mib[1] = AF_ROUTE; | |
530 mib[2] = 0; | |
531 mib[3] = AF_LINK; | |
532 mib[4] = NET_RT_IFLIST; | |
533 | |
534 mib[5] = if_nametoindex (interface); | |
535 if (mib[5] == 0) | |
536 { | |
537 perror ("if_nametoindex"); | |
538 return NULL; | |
539 } | |
540 | |
541 if (sysctl (mib, 6, NULL, &len, NULL, 0) < 0) | |
542 { | |
543 perror ("sysctl"); | |
544 return NULL; | |
545 } | |
546 | |
547 buf = malloc (len); | |
548 if (sysctl (mib, 6, buf, &len, NULL, 0) < 0) | |
549 { | |
550 perror ("sysctl"); | |
551 return NULL; | |
552 } | |
553 | |
554 ifm = (struct if_msghdr *) buf; | |
555 sdl = (struct sockaddr_dl*) (ifm + 1); | |
556 ptr = (unsigned char *) LLADDR (sdl); | |
557 #else /* Linux */ | |
558 /* determine UDN according to MAC address */ | |
559 sock = socket (AF_INET, SOCK_STREAM, 0); | |
560 if (sock < 0) | |
561 { | |
562 perror ("socket"); | |
563 return NULL; | |
564 } | |
565 | |
566 strcpy (ifr.ifr_name, interface); | |
567 strcpy (ifr.ifr_hwaddr.sa_data, ""); | |
568 | |
569 if (ioctl (sock, SIOCGIFHWADDR, &ifr) < 0) | |
570 { | |
571 perror ("ioctl"); | |
572 return NULL; | |
573 } | |
574 | |
575 buf = (char *) malloc (64 * sizeof (char)); | |
576 memset (buf, 0, 64); | |
577 ptr = (unsigned char *) ifr.ifr_hwaddr.sa_data; | |
578 #endif /* (defined(BSD) || defined(__FreeBSD__)) */ | |
579 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
580 snprintf (buf, 64, "%s-%04x-%02x%02x%02x%02x%02x%02x", DEFAULT_UUID, get_device_id_by_name(tdata->device_name), |
125 | 581 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377), |
582 (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)); | |
583 | |
584 if (sock) | |
585 close (sock); | |
586 | |
587 return buf; | |
588 } | |
589 | |
590 static char * | |
591 get_iface_address (char *interface) | |
592 { | |
593 int sock; | |
594 uint32_t ip; | |
595 struct ifreq ifr; | |
596 char *val; | |
597 | |
598 if (!interface) | |
599 return NULL; | |
600 | |
601 /* determine UDN according to MAC address */ | |
602 sock = socket (AF_INET, SOCK_STREAM, 0); | |
603 if (sock < 0) | |
604 { | |
605 perror ("socket"); | |
606 return NULL; | |
607 } | |
608 | |
609 strcpy (ifr.ifr_name, interface); | |
610 ifr.ifr_addr.sa_family = AF_INET; | |
611 | |
612 if (ioctl (sock, SIOCGIFADDR, &ifr) < 0) | |
613 { | |
614 perror ("ioctl"); | |
615 close (sock); | |
616 return NULL; | |
617 } | |
618 | |
619 val = (char *) malloc (16 * sizeof (char)); | |
620 ip = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr; | |
621 ip = ntohl (ip); | |
622 sprintf (val, "%d.%d.%d.%d", | |
623 (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); | |
624 | |
625 close (sock); | |
626 | |
627 return val; | |
628 } | |
629 | |
630 static int | |
631 restart_upnp (struct ushare_t *ut) | |
632 { | |
633 finish_upnp (ut); | |
634 | |
635 if (ut->udn) | |
636 free (ut->udn); | |
637 ut->udn = create_udn (ut->interface); | |
638 if (!ut->udn) | |
639 return -1; | |
640 | |
641 if (ut->ip) | |
642 free (ut->ip); | |
643 ut->ip = get_iface_address (ut->interface); | |
644 if (!ut->ip) | |
645 return -1; | |
646 | |
647 return (init_upnp (ut)); | |
648 } | |
649 | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
650 //static void |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
651 void |
125 | 652 UPnPBreak (int s __attribute__ ((unused))) |
653 { | |
654 ushare_signal_exit (); | |
655 } | |
656 | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
657 #if 0 |
125 | 658 static void |
659 reload_config (int s __attribute__ ((unused))) | |
660 { | |
661 struct ushare_t *ut2; | |
662 bool reload = false; | |
663 | |
664 log_info (_("Reloading configuration...\n")); | |
665 | |
666 ut2 = ushare_new (); | |
667 if (!ut || !ut2) | |
668 return; | |
669 | |
670 if (parse_config_file (ut2) < 0) | |
671 return; | |
672 | |
673 if (ut->name && strcmp (ut->name, ut2->name)) | |
674 { | |
675 free (ut->name); | |
676 ut->name = ut2->name; | |
677 ut2->name = NULL; | |
678 reload = true; | |
679 } | |
680 | |
681 if (ut->interface && strcmp (ut->interface, ut2->interface)) | |
682 { | |
683 if (!has_iface (ut2->interface)) | |
684 { | |
685 ushare_free (ut2); | |
686 raise (SIGINT); | |
687 } | |
688 else | |
689 { | |
690 free (ut->interface); | |
691 ut->interface = ut2->interface; | |
692 ut2->interface = NULL; | |
693 reload = true; | |
694 } | |
695 } | |
696 | |
697 if (ut->port != ut2->port) | |
698 { | |
699 ut->port = ut2->port; | |
700 reload = true; | |
701 } | |
702 | |
703 if (reload) | |
704 { | |
705 if (restart_upnp (ut) < 0) | |
706 { | |
707 ushare_free (ut2); | |
708 raise (SIGINT); | |
709 } | |
710 } | |
711 | |
712 if (ut->contentlist) | |
713 content_free (ut->contentlist); | |
714 ut->contentlist = ut2->contentlist; | |
715 ut2->contentlist = NULL; | |
716 ushare_free (ut2); | |
717 | |
718 if (ut->contentlist) | |
719 { | |
720 free_metadata_list (ut); | |
721 build_metadata_list (ut); | |
722 } | |
723 else | |
724 { | |
725 log_error (_("Error: no content directory to be shared.\n")); | |
726 raise (SIGINT); | |
727 } | |
728 } | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
729 #endif |
125 | 730 |
731 inline void | |
732 display_headers (void) | |
733 { | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
734 printf (_("%s (version %s), Recoding DTV and a lightweight UPnP A/V and DLNA Media Server.\n"), |
125 | 735 PACKAGE_NAME, VERSION); |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
736 printf (_("Naoya OYAMA (C) 2010-2012.\n")); |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
737 printf (_("See http://hg.honeyplanet.jp/pt1.oyama/ for updates.\n")); |
125 | 738 } |
739 | |
740 inline static void | |
741 setup_i18n(void) | |
742 { | |
743 #ifdef CONFIG_NLS | |
744 #ifdef HAVE_SETLOCALE | |
745 setlocale (LC_ALL, ""); | |
746 #endif | |
747 #if (!defined(BSD) && !defined(__FreeBSD__)) | |
748 bindtextdomain (PACKAGE, LOCALEDIR); | |
749 #endif | |
750 textdomain (PACKAGE); | |
751 #endif | |
752 } | |
753 | |
754 #define SHUTDOWN_MSG _("Server is shutting down: other clients will be notified soon, Bye bye ...\n") | |
755 | |
756 static void | |
757 ushare_kill (ctrl_telnet_client *client, | |
758 int argc __attribute__((unused)), | |
759 char **argv __attribute__((unused))) | |
760 { | |
761 if (ut->use_telnet) | |
762 { | |
763 ctrl_telnet_client_send (client, SHUTDOWN_MSG); | |
764 client->exiting = true; | |
765 } | |
766 ushare_signal_exit (); | |
767 } | |
768 | |
769 //main (int argc, char **argv) | |
770 void * | |
771 dlna_startup (void *p) | |
772 { | |
773 ut = ushare_new (); | |
774 log_verbose ("dlna_startup() start\n"); | |
775 | |
776 if (!ut) | |
777 return NULL; | |
778 | |
779 setup_i18n (); | |
780 setup_iconv (); | |
781 | |
782 #if 0 | |
783 /* Parse args before cfg file, as we may override the default file */ | |
784 if (parse_command_line (ut, argc, argv) < 0) | |
785 { | |
786 ushare_free (ut); | |
787 return NULL; | |
788 } | |
789 #endif | |
790 | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
791 #if 0 |
125 | 792 if (parse_config_file (ut) < 0) |
793 { | |
794 /* fprintf here, because syslog not yet ready */ | |
795 fprintf (stderr, _("Warning: can't parse file \"%s\".\n"), | |
796 ut->cfg_file ? ut->cfg_file : SYSCONFDIR "/" USHARE_CONFIG_FILE); | |
797 } | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
798 #endif |
125 | 799 ut->verbose = true; |
800 ut->port = 0; | |
801 ut->use_presentation = false; | |
802 ut->use_telnet = false; | |
803 ut->dlna_enabled = true; | |
804 ut->override_iconv_err = false; | |
805 ut->xbox360 = true; | |
806 ut->daemon = false; | |
807 ut->contentlist = "/tmp"; | |
808 | |
809 if (ut->xbox360) | |
810 { | |
811 char *name; | |
812 | |
813 name = malloc (strlen (XBOX_MODEL_NAME) + strlen (ut->model_name) + 4); | |
814 sprintf (name, "%s (%s)", XBOX_MODEL_NAME, ut->model_name); | |
815 free (ut->model_name); | |
816 ut->model_name = strdup (name); | |
817 free (name); | |
818 | |
819 ut->starting_id = STARTING_ENTRY_ID_XBOX360; | |
820 } | |
821 | |
822 if (ut->daemon) | |
823 { | |
824 /* starting syslog feature as soon as possible */ | |
825 start_log (); | |
826 } | |
827 | |
828 if (!ut->contentlist) | |
829 { | |
830 log_error (_("Error: no content directory to be shared.\n")); | |
831 ushare_free (ut); | |
832 return NULL; | |
833 } | |
834 | |
835 if (!has_iface (ut->interface)) | |
836 { | |
837 ushare_free (ut); | |
838 return NULL; | |
839 } | |
840 | |
841 ut->udn = create_udn (ut->interface); | |
842 if (!ut->udn) | |
843 { | |
844 ushare_free (ut); | |
845 return NULL; | |
846 } | |
847 | |
848 ut->ip = get_iface_address (ut->interface); | |
849 if (!ut->ip) | |
850 { | |
851 ushare_free (ut); | |
852 return NULL; | |
853 } | |
854 | |
855 if (ut->daemon) | |
856 { | |
857 int err; | |
858 err = daemon (0, 0); | |
859 if (err == -1) | |
860 { | |
861 log_error (_("Error: failed to daemonize program : %s\n"), | |
862 strerror (err)); | |
863 ushare_free (ut); | |
864 return NULL; | |
865 } | |
866 } | |
867 else | |
868 { | |
869 display_headers (); | |
870 } | |
871 | |
872 #if 0 | |
873 if (ut->use_telnet) | |
874 { | |
875 if (ctrl_telnet_start (ut->telnet_port) < 0) | |
876 { | |
877 ushare_free (ut); | |
878 return NULL; | |
879 } | |
880 | |
881 ctrl_telnet_register ("kill", ushare_kill, | |
882 _("Terminates the uShare server")); | |
883 } | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
884 #endif |
125 | 885 log_verbose ("init_upnp() start\n"); |
886 | |
887 if (init_upnp (ut) < 0) | |
888 { | |
889 finish_upnp (ut); | |
890 ushare_free (ut); | |
891 return NULL; | |
892 } | |
893 | |
152
30e91361506a
EXPERIMENTAL: Enable change phisical channel by DLNA.(ISDB-T only)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
146
diff
changeset
|
894 build_metadata_list(ut); |
30e91361506a
EXPERIMENTAL: Enable change phisical channel by DLNA.(ISDB-T only)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
146
diff
changeset
|
895 |
125 | 896 /* Let main sleep until it's time to die... */ |
897 pthread_mutex_lock (&ut->termination_mutex); | |
898 pthread_cond_wait (&ut->termination_cond, &ut->termination_mutex); | |
899 pthread_mutex_unlock (&ut->termination_mutex); | |
900 | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
901 #if 0 |
125 | 902 if (ut->use_telnet) |
903 ctrl_telnet_stop (); | |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
904 #endif |
125 | 905 finish_upnp (ut); |
906 free_metadata_list (ut); | |
907 ushare_free (ut); | |
908 finish_iconv (); | |
909 | |
910 /* it should never be executed */ | |
911 return NULL; | |
912 } | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
913 |