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