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