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