]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-config.h
Add some new WEP code and some experimental non-working
[SourceForge/eyefi-config.git] / eyefi-config.h
1 #ifndef _EYEFI_CONFIG_H
2 #define _EYEFI_CONFIG_H
3
4 #include <sys/types.h>
5
6 #if defined(LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN)
7 #define __LITTLE_ENDIAN LITTLE_ENDIAN
8 #endif
9
10 #if defined(BIG_ENDIAN) && !defined(__BIG_ENDIAN)
11 #define __BIG_ENDIAN BIG_ENDIAN
12 #endif
13
14 #if !defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
15 #include <endian.h>
16 #include <byteswap.h>
17 #endif
18
19 extern int eyefi_debug_level;
20
21 #ifdef __CHDK__
22
23 #define CONFIG_EYEFI_STANDALONE 1
24 #define printf(...) do{}while(0)
25 #define putchar(...) do{}while(0)
26 #define puts(...) do{}while(0)
27 #define exit(i)                  return
28 #define perror(i)        do{}while(0)
29 #define system(i)        do{}while(0)
30 #define fd_dont_cache(fd) (0)
31 #define assert(x)        do{}while(0)
32 #define output_flush()   do{}while(0)
33
34 #else
35 #define CONFIG_EYEFI_WITH_OS 1
36 #include <assert.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <getopt.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <stdarg.h>
47
48 #define PATHNAME_MAX 4096
49
50 #define output_flush()  fflush(NULL)
51
52 #define debug_printf(level, args...) do {      \
53         if ((level) <= eyefi_debug_level)      \
54                 fprintf(stderr, ## args);      \
55         } while(0)
56
57 #endif
58
59 /*
60  * These are defined in both eyefi-unix.c and eyefi-chdk.c
61  */
62 extern void open_error(char *file, int err);
63 extern int eyefi_printf(const char *fmt, ...);
64
65 /*
66  * These have to be created by the unix variants
67  */
68 extern int fd_dont_cache(int);
69
70 /*
71  * Do some kernel-style types to make
72  * definitions shorter.
73  */
74 typedef unsigned long u32;
75 typedef unsigned char u8;
76
77 #define os_memset memset
78 #define os_memcpy memcpy
79 #define os_strlen strlen
80 #define os_strcpy strcpy
81
82 #define SHA1_MAC_LEN 20
83 #define MD5_MAC_LEN 16
84 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
85 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
86 void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
87                      const u8 *addr[], const size_t *len, u8 *mac);
88 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
89               u8 *mac);
90 void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
91                  int iterations, u8 *buf, size_t buflen);
92
93 static inline u32 swap_bytes(u32 src)
94 {
95         u32 dest = 0;
96         dest |= (src & 0xff000000) >> 24;
97         dest |= (src & 0x00ff0000) >>  8;
98         dest |= (src & 0x0000ff00) <<  8;
99         dest |= (src & 0x000000ff) << 24;
100         return dest;
101 }
102
103 #ifdef __LITTLE_ENDIAN
104 #define le_to_host32(n) (n)
105 #define be_to_host32(n) swap_bytes(n)
106 #define host_to_be32(n) swap_bytes(n)
107 #else  // __BIG_ENDIAN
108 #define le_to_host32(n) swap_bytes(n)
109 #define be_to_host32(n) (n)
110 #define host_to_be32(n) (n)
111 #endif
112
113 /*
114  * Just a few functions so that I can't easily forget about
115  * endinness.
116  */
117 struct __be32 {
118         u32 val;
119 } __attribute__((packed));
120 typedef struct __be32 be32;
121
122 /*
123  * These two obviously need to get fixed for
124  * big endian machines.
125  */
126 static inline u32 be32_to_u32(be32 src)
127 {
128         return swap_bytes(src.val);
129 }
130 static inline be32 u32_to_be32(u32 src)
131 {
132         be32 ret;
133         ret.val = swap_bytes(src);
134         return ret;
135 }
136
137 /*
138  * Eye-Fi Card data structures
139  */
140
141 struct card_seq_num {
142         u32 seq;
143 } __attribute__((packed));
144
145 #define EYEFI_BUF_SIZE 16384
146
147 /*
148  * Most of the eyefi strings are pascal-style with
149  * a length byte preceeding content.  (Did pascal
150  * have just a byte for length or more??)
151  */
152 struct pascal_string {
153         u8 length;
154         u8 value[32];
155 } __attribute__((packed));
156
157 /*
158  * The 'o' command has several sub-commands:
159  */
160 enum card_info_subcommand {
161         MAC_ADDRESS   = 1,
162         FIRMWARE_INFO = 2,
163         CARD_KEY      = 3,
164         API_URL       = 4,
165         UNKNOWN1      = 5, // Chris says these are 
166         UNKNOWN2      = 6, // checksums
167         LOG_LEN       = 7,
168 };
169
170 struct card_info_req {
171         u8 o;
172         u8 subcommand;
173 } __attribute__((packed));
174
175 struct card_info_rsp_key {
176         struct pascal_string key;
177 };
178
179 struct card_firmware_info {
180         struct pascal_string info;
181 };
182
183 #define MAC_BYTES 6
184 struct mac_address {
185         u8 length;
186         u8 mac[MAC_BYTES];
187 } __attribute__((packed));
188
189 struct card_info_api_url {
190         struct pascal_string key;
191 };
192
193 struct card_info_log_len {
194         u8 len;
195         be32 val;
196 } __attribute__((packed));
197
198 struct byte_response {
199         u8 response;
200 };
201
202 enum net_type {
203         NET_UNSECURED,
204         NET_WEP,
205         NET_WPA,
206         NET_WPA2
207 };
208
209 enum net_password_type {
210         NET_PASSWORD_ASCII,
211         NET_PASSWORD_RAW, /* raw hex bytes */
212 };
213
214 #define ESSID_LEN 32
215 struct scanned_net {
216         char essid[ESSID_LEN];
217         signed char strength;
218         u8 type;
219 } __attribute__((packed));
220
221 struct scanned_net_list {
222         u8 nr;
223         struct scanned_net nets[100];
224 } __attribute__((packed));
225
226 struct configured_net {
227         char essid[ESSID_LEN];
228 } __attribute__((packed));
229
230 struct configured_net_list {
231         u8 nr;
232         struct configured_net nets[100];
233 } __attribute__((packed));
234
235 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
236
237 #define WPA_KEY_BYTES 32
238 struct wpa_key {
239         u8 key[WPA_KEY_BYTES];
240 } __attribute((packed));
241
242 #define WEP_KEY_BYTES 13
243 struct wep_key {
244         u8 key[WEP_KEY_BYTES];
245 } __attribute((packed));
246
247 struct network_key {
248         u8 len;
249         union {
250                 struct wpa_key wpa;
251                 struct wep_key wep;
252         };
253 } __attribute((packed));
254
255 #define KEY_LEN 32
256 struct net_request {
257         char req;
258         u8 essid_len;
259         char essid[ESSID_LEN];
260         struct network_key key;
261 } __attribute((packed));
262
263 struct noarg_request {
264         u8 req;
265 };
266
267 /*
268  * Log structures
269  */
270 struct fetch_log_cmd {
271         char m;
272         be32 offset;
273 } __attribute__((packed));
274
275 /*
276  * When you ask for the log at offset 0x0, you
277  * get back 8 bytes of offsets into the rest of
278  * the data
279  */
280 struct first_log_response {
281         be32 log_end;
282         be32 log_start;
283         u8 data[EYEFI_BUF_SIZE-8];
284 } __attribute__((packed));
285
286 struct rest_log_response {
287         u8 data[EYEFI_BUF_SIZE];
288 } __attribute__((packed));
289
290 /*
291  * Functions that are exported from eyefi-config.c
292  */
293 u32 fetch_log_length(void);
294 int card_info_cmd(enum card_info_subcommand cmd);
295 void *eyefi_response(void);
296 struct card_info_rsp_key *fetch_card_key(void);
297 struct scanned_net_list *scan_nets(void);
298 const char *net_type_name(u8 type);
299 struct configured_net_list *fetch_configured_nets(void);
300 int issue_noarg_command(u8 cmd);
301 char *net_test_state_name(u8 state);
302 int network_action(char cmd, char *essid, char *wpa_ascii);
303 char *locate_eyefi_mount(void);
304 int get_log_into(u8 *resbuf);
305 void reboot_card(void);
306 void init_card(void);
307 void add_network(char *essid, char *ascii_password);
308 void remove_network(char *essid);
309 struct card_firmware_info *fetch_card_firmware_info(void);
310 /*
311  * Only used by the unix variants
312  */
313 enum eyefi_file {
314         REQC,
315         REQM,
316         RSPC,
317         RSPM
318 };
319 char *eyefi_file_on(enum eyefi_file file, char *mnt);
320 int atoh(char c);
321 #endif // _EYEFI_CONFIG_H