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