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