Debian Codegraph @

libgphoto2_port/ptpip/ptpip.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* ptpip.c
3 *
4 * Copyright (c) 2006 Marcus Meissner <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #include <gphoto2/gphoto2-port-library.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #ifdef HAVE_FCNTL_H
28 # include <fcntl.h>
29 #endif
30 #include <errno.h>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
33 #endif
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #include <string.h>
38
39 #include <gphoto2/gphoto2-port.h>
40 #include <gphoto2/gphoto2-port-result.h>
41 #include <gphoto2/gphoto2-port-log.h>
42
43 #include "libgphoto2_port/i18n.h"
44
45
46 #define CHECK(result) {int r=(result); if (r<0) return (r);}
47
48 struct _GPPortPrivateLibrary {
49 int dummy; /* unused yet */
50 };
51
52 GPPortType
53 gp_port_library_type (void)
54 {
55 return GP_PORT_PTPIP;
56 }
57
58 /* thanks to Avahi this makes more trouble than its worth */
59 #undef HAVE_MDNS_BONJOUR
60 #ifdef HAVE_MDNS_BONJOUR
61
62 #include <netinet/in.h>
63 #include <dns_sd.h>
64 #include <netdb.h>
65 #include <arpa/inet.h>
66
67 struct mdnsinfo
68 {
69 GPPortInfoList* list;
70 const char* name;
71 };
72
73 static void
74 _ptpip_resolved (
75 DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
76 DNSServiceErrorType errorCode,
77 const char *fullname, const char *hosttarget, uint16_t port,
78 uint16_t txtLen, const char *txtRecord, void *context
79 ) {
80 struct hostent* hent;
81 struct in_addr inaddr;
82 GPPortInfo *info;
83 int i, cnt;
84 struct mdnsinfo *mdnsi = context;
85 char path[200];
86
87 if (errorCode != kDNSServiceErr_NoError) {
88 GP_LOG_E ("Error on 2nd level query.");
89 return;
90 }
91 GP_LOG_D ("fullname %s, hosttarget %s, port %d", fullname, hosttarget, htons(port));
92 cnt = TXTRecordGetCount (txtLen, txtRecord);
93 for (i=0;i<cnt;i++) {
94 char key[256];
95 uint8_t valuelen;
96 const void *value;
97
98 valuelen = 0;
99 if (kDNSServiceErr_NoError == TXTRecordGetItemAtIndex (txtLen, txtRecord, i, sizeof(key), key, &valuelen, &value))
100 GP_LOG_D ("%d: %s:%s", i, key, (char*)value);
101 }
102 hent = gethostbyname (hosttarget);
103 if (!hent) {
104 GP_LOG_E ("Could not resolve the returned host: %s", hosttarget);
105 return;
106 }
107 memcpy(&inaddr.s_addr,hent->h_addr_list[0],hent->h_length);
108 gp_port_info_new (&info);
109 gp_port_info_set_type (info, GP_PORT_PTPIP);
110 gp_port_info_set_name (info, mdnsi->name);
111 snprintf (path, sizeof(path), "ptpip:%s:%d", inet_ntoa(inaddr), htons(port));
112 gp_port_info_set_path (info, path);
113 gp_port_info_list_append (mdnsi->list, info);
114 }
115
116 static void
117 _ptpip_enumerate (
118 DNSServiceRef sdRef, DNSServiceFlags flags,
119 uint32_t interfaceIndex, DNSServiceErrorType errorCode,
120 const char *serviceName, const char *regtype, const char *replyDomain,
121 void *context
122 ) {
123 struct mdnsinfo mdnsi;
124 DNSServiceRef sd;
125
126 if (errorCode != kDNSServiceErr_NoError) {
127 GP_LOG_E ("Error on _ptp._tcp query.");
128 return;
129 }
130 GP_LOG_D ("found %s, %s, %s", serviceName, regtype, replyDomain);
131 mdnsi.list = context;
132 mdnsi.name = serviceName;
133 DNSServiceResolve (&sd,
134 0,
135 interfaceIndex,
136 serviceName,
137 regtype,
138 replyDomain,
139 _ptpip_resolved,
140 &mdnsi
141 );
142 DNSServiceProcessResult (sd);
143 DNSServiceRefDeallocate (sd);
144 }
145 #endif
146
147 int
148 gp_port_library_list (GPPortInfoList *list)
149 {
150 GPPortInfo info;
151 #ifdef HAVE_MDNS_BONJOUR
152 DNSServiceRef sd;
153 DNSServiceErrorType ret;
154 int fd;
155 fd_set infds;
156 struct timeval tv;
157 #endif
158
159 gp_port_info_new (&info);
160 gp_port_info_set_type (info, GP_PORT_PTPIP);
161 gp_port_info_set_name (info, _("PTP/IP Connection"));
162 gp_port_info_set_path (info, "ptpip:");
163 CHECK (gp_port_info_list_append (list, info));
164
165 /* Generic matcher so you can pass any IP address */
166 gp_port_info_new (&info);
167 gp_port_info_set_type (info, GP_PORT_PTPIP);
168 gp_port_info_set_name (info, "");
169 gp_port_info_set_path (info, "^ptpip:");
170 gp_port_info_list_append (list, info); /* do not check return */
171
172 gp_port_info_new (&info);
173 gp_port_info_set_type (info, GP_PORT_IP);
174 gp_port_info_set_name (info, _("IP Connection"));
175 gp_port_info_set_path (info, "ip:");
176 CHECK (gp_port_info_list_append (list, info));
177
178 /* Generic matcher so you can pass any IP address */
179 gp_port_info_new (&info);
180 gp_port_info_set_type (info, GP_PORT_IP);
181 gp_port_info_set_name (info, "");
182 gp_port_info_set_path (info, "^ip:");
183 gp_port_info_list_append (list, info); /* do not check return */
184 #ifdef HAVE_MDNS_BONJOUR
185 ret = DNSServiceBrowse (
186 &sd,
187 0, /* unused flags */
188 0, /* all ifaces */
189 "_ptp._tcp",
190 NULL,
191 _ptpip_enumerate,
192 list
193 );
194 /* We need to make it a non-blocking query */
195 fd = DNSServiceRefSockFD(sd);
196 if (fd != -1) {
197 FD_ZERO (&infds); FD_SET (fd, &infds);
198 tv.tv_sec = 0; tv.tv_usec = 1;
199 /* If we have input, we can try to process a result */
200 if (1 == select (fd+1, &infds, NULL, NULL, &tv))
201 DNSServiceProcessResult (sd);
202 }
203 DNSServiceRefDeallocate (sd);
204 #endif
205 return GP_OK;
206 }
207
208 static int gp_port_ptpip_init (GPPort *dev)
209 {
210 C_MEM (dev->pl = calloc (1, sizeof (GPPortPrivateLibrary)));
211
212 return GP_OK;
213 }
214
215 static int
216 gp_port_ptpip_exit (GPPort *port)
217 {
218 free (port->pl);
219 port->pl = NULL;
220
221 return GP_OK;
222 }
223
224 static int
225 gp_port_ptpip_open (GPPort *port)
226 {
227 return GP_OK;
228 }
229
230 static int
231 gp_port_ptpip_close (GPPort *port)
232 {
233 return GP_OK;
234 }
235
236 static int
237 gp_port_ptpip_write (GPPort *port, const char *bytes, int size)
238 {
239 return GP_OK;
240 }
241
242 static int
243 gp_port_ptpip_read(GPPort *port, char *bytes, int size)
244 {
245 return GP_OK;
246 }
247
248 static int
249 gp_port_ptpip_update (GPPort *port)
250 {
251 return GP_OK;
252 }
253
254
255 GPPortOperations *
256 gp_port_library_operations (void)
257 {
258 GPPortOperations *ops;
259
260 ops = calloc (1, sizeof (GPPortOperations));
261 if (!ops)
262 return NULL;
263
264 ops->init = gp_port_ptpip_init;
265 ops->exit = gp_port_ptpip_exit;
266 ops->open = gp_port_ptpip_open;
267 ops->close = gp_port_ptpip_close;
268 ops->read = gp_port_ptpip_read;
269 ops->write = gp_port_ptpip_write;
270 ops->update = gp_port_ptpip_update;
271 return ops;
272 }