Mercurial > pt1.oyama
annotate src/ushare.c @ 170:4f3640bf350d
Change display name of DLNA.
- old_display name: (recpt1:devN)
- new_display name: (hostname: /dev/ptXdevY)
- change UUID format.
author | Naoya OYAMA <naoya.oyama@gmail.com> |
---|---|
date | Wed, 17 Oct 2012 23:47:16 +0900 |
parents | 726fe10d9e4a |
children | 6c710e5a28b8 |
rev | line source |
---|---|
125 | 1 /* |
2 * ushare.c : GeeXboX uShare UPnP Media Server. | |
3 * Originally developped for the GeeXboX project. | |
4 * Parts of the code are originated from GMediaServer from Oskar Liljeblad. | |
5 * Copyright (C) 2005-2007 Benjamin Zores <ben@geexbox.org> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Library General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License along | |
18 * with this program; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 */ | |
21 | |
22 #include <stdio.h> | |
23 #include <signal.h> | |
24 #include <string.h> | |
25 #include <stdlib.h> | |
26 #include <stdarg.h> | |
27 #include <unistd.h> | |
28 #include <errno.h> | |
29 #include <getopt.h> | |
30 | |
31 #if (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__)) | |
32 #include <sys/socket.h> | |
33 #include <sys/sysctl.h> | |
34 #include <net/if_dl.h> | |
35 #endif | |
36 | |
37 #if (defined(__APPLE__)) | |
38 #include <net/route.h> | |
39 #endif | |
40 | |
41 #include <net/if.h> | |
42 #include <sys/ioctl.h> | |
43 #include <sys/types.h> | |
44 #include <sys/stat.h> | |
45 #include <stdbool.h> | |
46 #include <fcntl.h> | |
47 | |
48 #ifdef HAVE_IFADDRS_H | |
49 #include <ifaddrs.h> | |
50 #endif | |
51 | |
52 #if (defined(__unix__) || defined(unix)) && !defined(USG) | |
53 #include <sys/param.h> | |
54 #endif | |
55 | |
136
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
133
diff
changeset
|
56 #include <upnp.h> |
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
133
diff
changeset
|
57 #include <upnptools.h> |
125 | 58 |
59 #if (defined(HAVE_SETLOCALE) && defined(CONFIG_NLS)) | |
60 # include <locale.h> | |
61 #endif | |
62 | |
63 #include "config.h" | |
64 #include "ushare.h" | |
65 #include "services.h" | |
66 #include "http.h" | |
67 #include "metadata.h" | |
68 #include "util_iconv.h" | |
69 #include "content.h" | |
70 #include "cfgparser.h" | |
71 #include "gettext.h" | |
72 #include "trace.h" | |
73 #include "buffer.h" | |
74 #include "ctrl_telnet.h" | |
75 #include "recpt1.h" | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
76 #include "pt1_common.h" |
125 | 77 |
78 struct ushare_t *ut = NULL; | |
79 | |
80 static struct ushare_t * ushare_new (void) | |
81 __attribute__ ((malloc)); | |
82 | |
83 static struct ushare_t * | |
84 ushare_new (void) | |
85 { | |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
86 extern thread_data *gp_tdata; |
152
30e91361506a
EXPERIMENTAL: Enable change phisical channel by DLNA.(ISDB-T only)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
146
diff
changeset
|
87 extern struct channel_info_list *channel_list; |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
88 thread_data *tdata = gp_tdata; |
125 | 89 struct ushare_t *ut = (struct ushare_t *) malloc (sizeof (struct ushare_t)); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
90 char model_name[32]; |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
91 char host_name[HOST_NAME_MAX+1]; |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
92 model_name[0] = '\0'; |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
93 gethostname(host_name, sizeof(host_name)); |
125 | 94 if (!ut) |
95 return NULL; | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
96 snprintf(model_name, sizeof(model_name), "%s: %s", host_name, tdata->device_name); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
97 model_name[sizeof(model_name)-1] = '\0'; |
125 | 98 |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
99 // ut->name = strdup (DEFAULT_USHARE_NAME); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
100 ut->name = strdup (model_name); |
125 | 101 ut->interface = strdup (DEFAULT_USHARE_IFACE); |
131
20442921bff5
change display name. modify PES buf size.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
102 // ut->model_name = strdup (DEFAULT_USHARE_NAME); |
133
0db6ccf0fe31
modify DLNA display name & UUID
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
131
diff
changeset
|
103 ut->model_name = strdup (model_name); |
125 | 104 ut->contentlist = NULL; |
105 ut->rb = rbinit (rb_compare, NULL); | |
106 ut->root_entry = NULL; | |
107 ut->nr_entries = 0; | |
108 ut->starting_id = STARTING_ENTRY_ID_DEFAULT; | |
109 ut->init = 0; | |
110 ut->dev = 0; | |
111 ut->udn = NULL; | |
112 ut->ip = NULL; | |
113 ut->port = 0; /* Randomly attributed by libupnp */ | |
114 ut->telnet_port = CTRL_TELNET_PORT; | |
115 ut->presentation = NULL; | |
116 ut->use_presentation = true; | |
117 ut->use_telnet = true; | |
118 #ifdef HAVE_DLNA | |
119 ut->dlna_enabled = false; | |
120 ut->dlna = NULL; | |
121 ut->dlna_flags = DLNA_ORG_FLAG_STREAMING_TRANSFER_MODE | | |
122 DLNA_ORG_FLAG_BACKGROUND_TRANSFERT_MODE | | |
123 DLNA_ORG_FLAG_CONNECTION_STALL | | |
124 DLNA_ORG_FLAG_DLNA_V15; | |
125 #endif /* HAVE_DLNA */ | |
126 ut->xbox360 = false; | |
127 ut->verbose = false; | |
128 ut->daemon = false; | |
129 ut->override_iconv_err = false; | |
130 ut->cfg_file = NULL; | |
166 | 131 ut->channel_list = channel_list; |
125 | 132 |
133 pthread_mutex_init (&ut->termination_mutex, NULL); | |
134 pthread_cond_init (&ut->termination_cond, NULL); | |
135 | |
136 return ut; | |
137 } | |
138 | |
139 static void | |
140 ushare_free (struct ushare_t *ut) | |
141 { | |
142 if (!ut) | |
143 return; | |
144 | |
145 if (ut->name) | |
146 free (ut->name); | |
147 if (ut->interface) | |
148 free (ut->interface); | |
149 if (ut->model_name) | |
150 free (ut->model_name); | |
151 if (ut->contentlist) | |
152 content_free (ut->contentlist); | |
153 if (ut->rb) | |
154 rbdestroy (ut->rb); | |
155 if (ut->root_entry) | |
156 upnp_entry_free (ut, ut->root_entry); | |
157 if (ut->udn) | |
158 free (ut->udn); | |
159 if (ut->ip) | |
160 free (ut->ip); | |
161 if (ut->presentation) | |
162 buffer_free (ut->presentation); | |
163 #ifdef HAVE_DLNA | |
164 if (ut->dlna_enabled) | |
165 { | |
166 if (ut->dlna) | |
167 dlna_uninit (ut->dlna); | |
168 ut->dlna = NULL; | |
169 } | |
170 #endif /* HAVE_DLNA */ | |
171 if (ut->cfg_file) | |
172 free (ut->cfg_file); | |
173 | |
174 pthread_cond_destroy (&ut->termination_cond); | |
175 pthread_mutex_destroy (&ut->termination_mutex); | |
176 | |
177 free (ut); | |
178 } | |
179 | |
180 static void | |
181 ushare_signal_exit (void) | |
182 { | |
183 pthread_mutex_lock (&ut->termination_mutex); | |
184 pthread_cond_signal (&ut->termination_cond); | |
185 pthread_mutex_unlock (&ut->termination_mutex); | |
186 } | |
187 | |
188 static void | |
189 handle_action_request (struct Upnp_Action_Request *request) | |
190 { | |
191 struct service_t *service; | |
192 struct service_action_t *action; | |
193 char val[256]; | |
194 uint32_t ip; | |
195 | |
196 if (!request || !ut) | |
197 return; | |
198 | |
199 if (request->ErrCode != UPNP_E_SUCCESS) | |
200 return; | |
201 | |
202 if (strcmp (request->DevUDN + 5, ut->udn)) | |
203 return; | |
204 | |
205 ip = request->CtrlPtIPAddr.s_addr; | |
206 ip = ntohl (ip); | |
207 sprintf (val, "%d.%d.%d.%d", | |
208 (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); | |
209 | |
210 if (ut->verbose) | |
211 { | |
212 DOMString str = ixmlPrintDocument (request->ActionRequest); | |
213 log_verbose ("***************************************************\n"); | |
214 log_verbose ("** New Action Request **\n"); | |
215 log_verbose ("***************************************************\n"); | |
216 log_verbose ("ServiceID: %s\n", request->ServiceID); | |
217 log_verbose ("ActionName: %s\n", request->ActionName); | |
218 log_verbose ("CtrlPtIP: %s\n", val); | |
219 log_verbose ("Action Request:\n%s\n", str); | |
220 ixmlFreeDOMString (str); | |
221 } | |
222 | |
223 if (find_service_action (request, &service, &action)) | |
224 { | |
225 struct action_event_t event; | |
226 | |
227 event.request = request; | |
228 event.status = true; | |
229 event.service = service; | |
230 | |
231 if (action->function (&event) && event.status) | |
232 request->ErrCode = UPNP_E_SUCCESS; | |
233 | |
234 if (ut->verbose) | |
235 { | |
236 DOMString str = ixmlPrintDocument (request->ActionResult); | |
237 log_verbose ("Action Result:\n%s", str); | |
238 log_verbose ("***************************************************\n"); | |
239 log_verbose ("\n"); | |
240 ixmlFreeDOMString (str); | |
241 } | |
242 | |
243 return; | |
244 } | |
245 | |
246 if (service) /* Invalid Action name */ | |
247 strcpy (request->ErrStr, "Unknown Service Action"); | |
248 else /* Invalid Service name */ | |
249 strcpy (request->ErrStr, "Unknown Service ID"); | |
250 | |
251 request->ActionResult = NULL; | |
252 request->ErrCode = UPNP_SOAP_E_INVALID_ACTION; | |
253 } | |
254 | |
255 static int | |
256 device_callback_event_handler (Upnp_EventType type, void *event, | |
257 void *cookie __attribute__((unused))) | |
258 { | |
259 switch (type) | |
260 { | |
261 case UPNP_CONTROL_ACTION_REQUEST: | |
262 handle_action_request ((struct Upnp_Action_Request *) event); | |
263 break; | |
264 case UPNP_CONTROL_ACTION_COMPLETE: | |
265 case UPNP_EVENT_SUBSCRIPTION_REQUEST: | |
266 case UPNP_CONTROL_GET_VAR_REQUEST: | |
267 break; | |
268 default: | |
269 break; | |
270 } | |
271 | |
272 return 0; | |
273 } | |
274 | |
275 static int | |
276 finish_upnp (struct ushare_t *ut) | |
277 { | |
278 if (!ut) | |
279 return -1; | |
280 | |
281 log_info (_("Stopping UPnP Service ...\n")); | |
282 UpnpUnRegisterRootDevice (ut->dev); | |
283 UpnpFinish (); | |
284 | |
285 return UPNP_E_SUCCESS; | |
286 } | |
287 | |
288 static int | |
289 init_upnp (struct ushare_t *ut) | |
290 { | |
291 char *description = NULL; | |
292 int res; | |
293 size_t len; | |
294 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
295 char *friendly_name = ut->name; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
296 char manufacturer[] = "GeeXboX Team"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
297 char manufacturer_url[] = "http://ushare.geexbox.org/"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
298 char model_description[] = "uShare : DLNA Media Server"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
299 char *model_name = ut->model_name; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
300 char model_number[] = "001"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
301 char model_url[] = "http://ushare.geexbox.org/"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
302 char serial_number[] = "USHARE-01"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
303 char *uuid = ut->udn; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
304 char presentation_url[] = "/web/ushare.html"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
305 char cms_scpd_url[] = "/web/cms.xml"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
306 char cms_control_url[] = "/web/cms_control"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
307 char cms_event_url[] = "/web/cms_event"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
308 char cds_scpd_url[] = "/web/cds.xml"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
309 char cds_control_url[] = "/web/cds_control"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
310 char cds_event_url[] = "/web/cds_event"; |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
311 |
125 | 312 if (!ut || !ut->name || !ut->udn || !ut->ip) |
313 return -1; | |
314 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
315 len = strlen (UPNP_DMS_DESCRIPTION) + strlen (friendly_name) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
316 + strlen (manufacturer) + strlen (manufacturer_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
317 + strlen (model_description) + strlen (model_name) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
318 + strlen (model_number) + strlen (model_url) + strlen (serial_number) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
319 + strlen (uuid) + strlen (presentation_url) + strlen (cms_scpd_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
320 + strlen (cms_control_url) + strlen (cms_event_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
321 + strlen (cds_scpd_url) + strlen (cds_control_url) |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
322 + strlen (cds_event_url) + 1; |
125 | 323 description = (char *) malloc (len * sizeof (char)); |
324 memset (description, 0, len); | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
325 sprintf (description, UPNP_DMS_DESCRIPTION, friendly_name, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
326 manufacturer, manufacturer_url, model_description, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
327 model_name, model_number, model_url, serial_number, uuid, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
328 presentation_url, cms_scpd_url, cms_control_url, cms_event_url, |
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
329 cds_scpd_url, cds_control_url, cds_event_url); |
125 | 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 | |
170
4f3640bf350d
Change display name of DLNA.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
166
diff
changeset
|
586 snprintf (buf, 64, "%s-%04x-%02x%02x%02x%02x%02x%02x", DEFAULT_UUID, get_device_id_by_name(tdata->device_name), |
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 |