~ubuntu-branches/ubuntu/utopic/batmand/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/version_info.patch/posix/init.c

  • Committer: Package Import Robot
  • Author(s): Sven Eckelmann
  • Date: 2011-12-11 14:09:48 UTC
  • Revision ID: package-import@ubuntu.com-20111211140948-3m0ezb5014syw1r6
Tags: 0.3.2-11
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Mark all targets in debian/rules as phony
* Remove hardening-includes which are now integrated in dpkg-
  buildflags
* Use debian packaging manual URL as format identifier in
  debian/copyright
* Let dh_installinit handle the installation of /etc/default/batmand
* debian/patches:
  - Add non_parallel_build.patch, Don't start parallel build for
    environments disallowing it
  - Add build_rules.patch, Use make like build rules to support all
    flags provided through dpkg-buildflags
  - Add install_manpage.patch, Directly install manpage by build script
  - Add version_info.patch, Allow to add debian revision to the version
    string

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006-2009 BATMAN contributors:
 
3
 *
 
4
 * Thomas Lopatic, Marek Lindner
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of version 2 of the GNU General Public
 
8
 * License as published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
 * General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
 * 02110-1301, USA
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
 
 
24
#include <stdlib.h>
 
25
#include <unistd.h>
 
26
#include <stdio.h>
 
27
#include <syslog.h>
 
28
#include <errno.h>
 
29
#include <signal.h>
 
30
#include <paths.h>
 
31
#include <string.h>
 
32
#include <sys/ioctl.h>
 
33
#include <sys/socket.h>
 
34
#include <net/if.h>
 
35
#include <arpa/inet.h>
 
36
#include <fcntl.h>
 
37
#include <getopt.h>
 
38
 
 
39
 
 
40
#include "../os.h"
 
41
#include "../batman.h"
 
42
#include "../hna.h"
 
43
 
 
44
#define IOCSETDEV 1
 
45
 
 
46
int8_t stop;
 
47
 
 
48
 
 
49
 
 
50
static int my_daemon(void) {
 
51
 
 
52
        int fd;
 
53
 
 
54
        switch( fork() ) {
 
55
 
 
56
                case -1:
 
57
                        return -1;
 
58
 
 
59
                case 0:
 
60
                        break;
 
61
 
 
62
                default:
 
63
                        exit(EXIT_SUCCESS);
 
64
 
 
65
        }
 
66
 
 
67
        if ( setsid() == -1 )
 
68
                return(-1);
 
69
 
 
70
        /* Make certain we are not a session leader, or else we might reacquire a controlling terminal */
 
71
        if ( fork() )
 
72
                exit(EXIT_SUCCESS);
 
73
 
 
74
        chdir( "/" );
 
75
 
 
76
        if ( ( fd = open(_PATH_DEVNULL, O_RDWR, 0) ) != -1 ) {
 
77
 
 
78
                dup2(fd, STDIN_FILENO);
 
79
                dup2(fd, STDOUT_FILENO);
 
80
                dup2(fd, STDERR_FILENO);
 
81
 
 
82
                if ( fd > 2 )
 
83
                        close(fd);
 
84
 
 
85
        }
 
86
 
 
87
        return 0;
 
88
 
 
89
}
 
90
 
 
91
static void create_routing_pipe(void)
 
92
{
 
93
        int fd[2], pipe_opts;
 
94
 
 
95
        if (pipe(fd) < 0) {
 
96
                printf("Could not create a pipe to '%s': %s\n", policy_routing_script, strerror(errno));
 
97
                exit(EXIT_FAILURE);
 
98
        }
 
99
 
 
100
        if ((policy_routing_script_pid = fork()) < 0) {
 
101
 
 
102
                printf("Could not fork to execute '%s': %s\n", policy_routing_script, strerror(errno));
 
103
                exit(EXIT_FAILURE);
 
104
 
 
105
        /* parent */
 
106
        } else if (policy_routing_script_pid > 0) {
 
107
 
 
108
                close(fd[0]);
 
109
                policy_routing_pipe = fd[1];
 
110
 
 
111
                /* make pipe non blocking */
 
112
                pipe_opts = fcntl(policy_routing_pipe, F_GETFL, 0);
 
113
                fcntl(policy_routing_pipe, F_SETFL, pipe_opts | O_NONBLOCK);
 
114
 
 
115
        /* child */
 
116
        } else {
 
117
 
 
118
                close(fd[1]);
 
119
 
 
120
                if (fd[0] != STDIN_FILENO) {
 
121
 
 
122
                        if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) {
 
123
                                printf("Could not dup2 to redirect stdin to '%s'\n", policy_routing_script);
 
124
                                exit(EXIT_FAILURE);
 
125
                        }
 
126
 
 
127
                        close(fd[0]);
 
128
 
 
129
                }
 
130
 
 
131
                signal(SIGINT, SIG_IGN);
 
132
                signal(SIGTERM, SIG_IGN);
 
133
                signal(SIGPIPE, SIG_IGN);
 
134
 
 
135
                if (execl("/bin/sh", "/bin/sh", "-c", policy_routing_script, NULL) < 0)
 
136
                        printf("Could not execute '%s': %s\n", policy_routing_script, strerror(errno));
 
137
 
 
138
        }
 
139
}
 
140
 
 
141
void apply_init_args( int argc, char *argv[] ) {
 
142
 
 
143
        struct in_addr tmp_ip_holder;
 
144
        struct batman_if *batman_if;
 
145
        struct hna_task *hna_task;
 
146
        struct debug_level_info *debug_level_info;
 
147
        uint8_t found_args = 1, batch_mode = 0, info_output = 0, was_hna = 0;
 
148
        int8_t res;
 
149
 
 
150
        int32_t optchar, option_index, recv_buff_len, bytes_written, download_speed = 0, upload_speed = 0;
 
151
        char str1[16], str2[16], *slash_ptr, *unix_buff, *buff_ptr, *cr_ptr;
 
152
        char routing_class_opt = 0, gateway_class_opt = 0, pref_gw_opt = 0;
 
153
        char hop_penalty_opt = 0, purge_timeout_opt = 0;
 
154
        uint32_t vis_server = 0;
 
155
        struct option long_options[] =
 
156
        {
 
157
                {"policy-routing-script",     required_argument,       0, 'n'},
 
158
                {"hop-penalty",     required_argument,       0, 'm'},
 
159
                {"purge-timeout",     required_argument,       0, 'q'},
 
160
                {"disable-aggregation",     no_argument,       0, 'x'},
 
161
                {"disable-client-nat",     no_argument,       0, 'z'},
 
162
                {0, 0, 0, 0}
 
163
        };
 
164
 
 
165
        memset( &tmp_ip_holder, 0, sizeof (struct in_addr) );
 
166
        stop = 0;
 
167
        prog_name = argv[0];
 
168
 
 
169
        if ( strstr( SOURCE_VERSION, "-" ) != NULL )
 
170
                printf( "WARNING: You are using the unstable batman branch. If you are interested in *using* batman get the latest stable release !\n" );
 
171
 
 
172
        while ( ( optchar = getopt_long( argc, argv, "a:A:bcd:hHio:g:p:r:s:vV", long_options, &option_index ) ) != -1 ) {
 
173
 
 
174
                switch ( optchar ) {
 
175
 
 
176
                        case 'a':
 
177
                                hna_local_task_add_str(optarg, ROUTE_ADD, 0);
 
178
 
 
179
                                /* increment found_args only by one if optarg and optchar are directly following each other
 
180
                                   increment found_args by two if there is some space between the arguments */
 
181
                                found_args += ((*((char*)(optarg - 1)) == optchar ) ? 1 : 2);
 
182
                                break;
 
183
 
 
184
                        case 'A':
 
185
                                hna_local_task_add_str(optarg, ROUTE_DEL, 0);
 
186
 
 
187
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
188
                                break;
 
189
 
 
190
                        case 'b':
 
191
                                batch_mode++;
 
192
                                found_args++;
 
193
                                break;
 
194
 
 
195
                        case 'c':
 
196
                                unix_client++;
 
197
                                found_args++;
 
198
                                break;
 
199
 
 
200
                        case 'd':
 
201
 
 
202
                                errno = 0;
 
203
 
 
204
                                debug_level = strtol( optarg, NULL, 10 );
 
205
 
 
206
                                if ( ( errno == ERANGE ) || ( errno != 0 && debug_level == 0 ) ) {
 
207
 
 
208
                                        perror("strtol");
 
209
                                        exit(EXIT_FAILURE);
 
210
 
 
211
                                }
 
212
 
 
213
                                if ( debug_level > debug_level_max ) {
 
214
 
 
215
                                        printf( "Invalid debug level: %i\nDebug level has to be between 0 and %i.\n", debug_level, debug_level_max );
 
216
                                        exit(EXIT_FAILURE);
 
217
 
 
218
                                }
 
219
 
 
220
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
221
                                break;
 
222
 
 
223
                        case 'g':
 
224
 
 
225
                                if ( ( slash_ptr = strchr( optarg, '/' ) ) != NULL )
 
226
                                        *slash_ptr = '\0';
 
227
 
 
228
                                errno = 0;
 
229
 
 
230
                                download_speed = strtol( optarg, NULL, 10 );
 
231
 
 
232
                                if ( ( errno == ERANGE ) || ( errno != 0 && download_speed == 0 ) ) {
 
233
 
 
234
                                        perror("strtol");
 
235
                                        exit(EXIT_FAILURE);
 
236
 
 
237
                                }
 
238
 
 
239
                                if ( ( strlen( optarg ) > 4 ) && ( ( strncmp( optarg + strlen( optarg ) - 4, "MBit", 4 ) == 0 ) || ( strncmp( optarg + strlen( optarg ) - 4, "mbit", 4 ) == 0 ) || ( strncmp( optarg + strlen( optarg ) - 4, "Mbit", 4 ) == 0 ) ) )
 
240
                                        download_speed *= 1024;
 
241
 
 
242
                                if ( slash_ptr != NULL ) {
 
243
 
 
244
                                        errno = 0;
 
245
 
 
246
                                        upload_speed = strtol( slash_ptr + 1, NULL, 10 );
 
247
 
 
248
                                        if ( ( errno == ERANGE ) || ( errno != 0 && upload_speed == 0 ) ) {
 
249
                                                perror("strtol");
 
250
                                                exit(EXIT_FAILURE);
 
251
                                        }
 
252
 
 
253
                                        if ( ( strlen( slash_ptr + 1 ) > 4 ) && ( ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "MBit", 4 ) == 0 ) || ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "mbit", 4 ) == 0 ) || ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "Mbit", 4 ) == 0 ) ) )
 
254
                                                upload_speed *= 1024;
 
255
 
 
256
                                        *slash_ptr = '/';
 
257
 
 
258
                                }
 
259
 
 
260
                                gateway_class_opt = 1;
 
261
 
 
262
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
263
                                break;
 
264
 
 
265
                        case 'H':
 
266
                                verbose_usage();
 
267
                                exit(EXIT_SUCCESS);
 
268
 
 
269
                        case 'i':
 
270
                                info_output++;
 
271
                                found_args++;
 
272
                                break;
 
273
 
 
274
                        case 'n':
 
275
                                policy_routing_script = optarg;
 
276
 
 
277
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
278
                                break;
 
279
 
 
280
                        case 'm':
 
281
 
 
282
                                errno = 0;
 
283
 
 
284
                                hop_penalty = strtol( optarg, NULL, 10 );
 
285
                                hop_penalty_opt = 1;
 
286
 
 
287
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
288
                                break;
 
289
 
 
290
                        case 'q':
 
291
 
 
292
                                errno = 0;
 
293
 
 
294
                                purge_timeout = strtol(optarg, NULL, 10);
 
295
                                purge_timeout_opt = 1;
 
296
 
 
297
                                found_args += ((*((char*)( optarg - 1)) == optchar ) ? 1 : 2);
 
298
                                break;
 
299
 
 
300
                        case 'o':
 
301
 
 
302
                                errno = 0;
 
303
 
 
304
                                originator_interval = strtol( optarg, NULL, 10 );
 
305
 
 
306
                                if ( originator_interval < 1 ) {
 
307
 
 
308
                                        printf( "Invalid originator interval specified: %i.\nThe Interval has to be greater than 0.\n", originator_interval );
 
309
                                        exit(EXIT_FAILURE);
 
310
 
 
311
                                }
 
312
 
 
313
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
314
                                break;
 
315
 
 
316
                        case 'p':
 
317
 
 
318
                                errno = 0;
 
319
 
 
320
                                if ( inet_pton( AF_INET, optarg, &tmp_ip_holder ) < 1 ) {
 
321
 
 
322
                                        printf( "Invalid preferred gateway IP specified: %s\n", optarg );
 
323
                                        exit(EXIT_FAILURE);
 
324
 
 
325
                                }
 
326
 
 
327
                                pref_gateway = tmp_ip_holder.s_addr;
 
328
 
 
329
                                pref_gw_opt = 1;
 
330
 
 
331
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
332
                                break;
 
333
 
 
334
                        case 'r':
 
335
 
 
336
                                errno = 0;
 
337
 
 
338
                                routing_class = strtol(optarg, NULL, 10);
 
339
                                routing_class_opt = 1;
 
340
 
 
341
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
342
                                break;
 
343
 
 
344
                        case 's':
 
345
 
 
346
                                errno = 0;
 
347
                                if ( inet_pton( AF_INET, optarg, &tmp_ip_holder ) < 1 ) {
 
348
 
 
349
                                        printf( "Invalid preferred visualation server IP specified: %s\n", optarg );
 
350
                                        exit(EXIT_FAILURE);
 
351
 
 
352
                                }
 
353
 
 
354
                                vis_server = tmp_ip_holder.s_addr;
 
355
 
 
356
 
 
357
                                found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
 
358
                                break;
 
359
 
 
360
                        case 'v':
 
361
 
 
362
                                printf("B.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
 
363
                                exit(EXIT_SUCCESS);
 
364
 
 
365
                        case 'V':
 
366
 
 
367
                                print_animation();
 
368
 
 
369
                                printf("\x1B[0;0HB.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
 
370
                                printf("\x1B[9;0H \t May the bat guide your path ...\n\n\n");
 
371
 
 
372
                                exit(EXIT_SUCCESS);
 
373
 
 
374
                        case 'x':
 
375
                                aggregation_enabled = 0;
 
376
                                found_args++;
 
377
                                break;
 
378
 
 
379
                        case 'z':
 
380
                                disable_client_nat = 1;
 
381
                                found_args++;
 
382
                                break;
 
383
 
 
384
                        case 'h':
 
385
                        default:
 
386
                                usage();
 
387
                                exit(EXIT_SUCCESS);
 
388
 
 
389
                }
 
390
 
 
391
        }
 
392
 
 
393
        if (!unix_client && info_output) {
 
394
 
 
395
                internal_output(1);
 
396
                exit(EXIT_SUCCESS);
 
397
 
 
398
        }
 
399
 
 
400
        if ( ( download_speed > 0 ) && ( upload_speed == 0 ) )
 
401
                upload_speed = download_speed / 5;
 
402
 
 
403
        if (download_speed > 0) {
 
404
 
 
405
                gateway_class = get_gw_class(download_speed, upload_speed);
 
406
                get_gw_speeds(gateway_class, &download_speed, &upload_speed);
 
407
 
 
408
        }
 
409
 
 
410
        if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) {
 
411
                fprintf( stderr, "Error - routing class can't be set while gateway class is in use !\n" );
 
412
                usage();
 
413
                exit(EXIT_FAILURE);
 
414
        }
 
415
 
 
416
        if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) {
 
417
                fprintf( stderr, "Error - preferred gateway can't be set while gateway class is in use !\n" );
 
418
                usage();
 
419
                exit(EXIT_FAILURE);
 
420
        }
 
421
 
 
422
        /* use routing class 1 if none specified */
 
423
        if ( ( routing_class == 0 ) && ( pref_gateway != 0 ) )
 
424
                routing_class = DEFAULT_ROUTING_CLASS;
 
425
 
 
426
        if ( ( ( routing_class != 0 ) || ( gateway_class != 0 ) ) && ( !probe_tun(1) ) )
 
427
                exit(EXIT_FAILURE);
 
428
 
 
429
        if (!unix_client) {
 
430
 
 
431
                if (argc <= found_args) {
 
432
                        fprintf(stderr, "Error - no interface specified\n");
 
433
                        usage();
 
434
                        exit(EXIT_FAILURE);
 
435
                }
 
436
 
 
437
                if ((disable_client_nat) && (routing_class == 0))
 
438
                        fprintf(stderr, "Warning - the activated option '--disable-client-nat' has no effect without setting a routing class.\n");
 
439
 
 
440
                nat_tool_avail = probe_nat_tool();
 
441
 
 
442
                if (policy_routing_script != NULL)
 
443
                        create_routing_pipe();
 
444
 
 
445
                signal( SIGINT, handler );
 
446
                signal( SIGTERM, handler );
 
447
                signal( SIGPIPE, SIG_IGN );
 
448
                signal( SIGSEGV, segmentation_fault );
 
449
 
 
450
                debug_clients.fd_list = debugMalloc( sizeof(struct list_head_first *) * debug_level_max, 203 );
 
451
                debug_clients.mutex = debugMalloc( sizeof(pthread_mutex_t *) * debug_level_max, 209 );
 
452
                debug_clients.clients_num = debugMalloc( sizeof(int16_t) * debug_level_max, 209 );
 
453
 
 
454
                for (res = 0; res < debug_level_max; res++) {
 
455
 
 
456
                        debug_clients.fd_list[res] = debugMalloc(sizeof(struct list_head_first), 204);
 
457
                        ((struct list_head_first *)debug_clients.fd_list[res])->next = debug_clients.fd_list[res];
 
458
                        ((struct list_head_first *)debug_clients.fd_list[res])->prev = debug_clients.fd_list[res];
 
459
 
 
460
                        debug_clients.mutex[res] = debugMalloc(sizeof(pthread_mutex_t), 209);
 
461
                        pthread_mutex_init((pthread_mutex_t *)debug_clients.mutex[res], NULL);
 
462
 
 
463
                        debug_clients.clients_num[res] = 0;
 
464
                }
 
465
 
 
466
                if ( flush_routes_rules(0) < 0 )
 
467
                        exit(EXIT_FAILURE);
 
468
 
 
469
                if ( flush_routes_rules(1) < 0 )
 
470
                        exit(EXIT_FAILURE);
 
471
 
 
472
                while (argc > found_args) {
 
473
 
 
474
                        if (argv[found_args][0] == '-') {
 
475
                                fprintf(stderr, "%s: invalid option -- %s\n", argv[0], argv[found_args]);
 
476
                                usage();
 
477
                                exit(EXIT_FAILURE);
 
478
                        }
 
479
 
 
480
                        batman_if = debugMalloc(sizeof(struct batman_if), 206);
 
481
                        memset(batman_if, 0, sizeof(struct batman_if));
 
482
                        INIT_LIST_HEAD( &batman_if->list );
 
483
 
 
484
                        batman_if->dev = argv[found_args];
 
485
                        batman_if->if_num = found_ifs;
 
486
 
 
487
                        batman_if->if_rp_filter_old = -1;
 
488
                        batman_if->if_send_redirects_old = -1;
 
489
 
 
490
                        list_add_tail(&batman_if->list, &if_list);
 
491
 
 
492
                        init_interface(batman_if);
 
493
 
 
494
                        if (batman_if->if_num > 0)
 
495
                                hna_local_task_add_ip(batman_if->addr.sin_addr.s_addr, 32, ROUTE_ADD);
 
496
 
 
497
                        if (batman_if->if_active) {
 
498
 
 
499
                                addr_to_string(batman_if->addr.sin_addr.s_addr, str1, sizeof (str1));
 
500
                                addr_to_string(batman_if->broad.sin_addr.s_addr, str2, sizeof (str2));
 
501
 
 
502
                                printf("Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2);
 
503
 
 
504
                        } else {
 
505
 
 
506
                                printf("Not using interface %s (retrying later): interface not active\n", batman_if->dev);
 
507
 
 
508
                        }
 
509
 
 
510
                        found_ifs++;
 
511
                        found_args++;
 
512
 
 
513
                }
 
514
 
 
515
 
 
516
                unlink( UNIX_PATH );
 
517
                unix_if.unix_sock = socket( AF_LOCAL, SOCK_STREAM, 0 );
 
518
 
 
519
                memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
 
520
                unix_if.addr.sun_family = AF_LOCAL;
 
521
                strcpy( unix_if.addr.sun_path, UNIX_PATH );
 
522
 
 
523
                if ( bind ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof (struct sockaddr_un) ) < 0 ) {
 
524
 
 
525
                        printf( "Error - can't bind unix socket '%s': %s\n", UNIX_PATH, strerror(errno) );
 
526
                        restore_defaults();
 
527
                        exit(EXIT_FAILURE);
 
528
 
 
529
                }
 
530
 
 
531
                if ( listen( unix_if.unix_sock, 10 ) < 0 ) {
 
532
 
 
533
                        printf( "Error - can't listen unix socket '%s': %s\n", UNIX_PATH, strerror(errno) );
 
534
                        restore_defaults();
 
535
                        exit(EXIT_FAILURE);
 
536
 
 
537
                }
 
538
 
 
539
                /* daemonize */
 
540
                if (debug_level == 0) {
 
541
 
 
542
                        if (my_daemon() < 0) {
 
543
 
 
544
                                printf("Error - can't fork to background: %s\n", strerror(errno));
 
545
                                restore_defaults();
 
546
                                exit(EXIT_FAILURE);
 
547
 
 
548
                        }
 
549
 
 
550
                        openlog("batmand", LOG_PID, LOG_DAEMON);
 
551
 
 
552
                } else {
 
553
 
 
554
                        printf("B.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
 
555
 
 
556
                        debug_clients.clients_num[ debug_level - 1 ]++;
 
557
                        debug_level_info = debugMalloc( sizeof(struct debug_level_info), 205 );
 
558
                        INIT_LIST_HEAD( &debug_level_info->list );
 
559
                        debug_level_info->fd = 2;
 
560
                        list_add( &debug_level_info->list, (struct list_head_first *)debug_clients.fd_list[debug_level - 1] );
 
561
 
 
562
                }
 
563
 
 
564
                log_facility_active = 1;
 
565
 
 
566
                pthread_create( &unix_if.listen_thread_id, NULL, &unix_listen, NULL );
 
567
 
 
568
                /* add rule for hna networks */
 
569
                add_del_rule(0, 0, BATMAN_RT_TABLE_NETWORKS, BATMAN_RT_PRIO_UNREACH - 1, 0, RULE_TYPE_DST, RULE_ADD);
 
570
 
 
571
                /* add unreachable routing table entry */
 
572
                add_del_route(0, 0, 0, 0, 0, "unknown", BATMAN_RT_TABLE_UNREACH, ROUTE_TYPE_UNREACHABLE, ROUTE_ADD);
 
573
 
 
574
                if (routing_class > 0) {
 
575
                        if (add_del_interface_rules(RULE_ADD) < 0) {
 
576
                                restore_defaults();
 
577
                                exit(EXIT_FAILURE);
 
578
                        }
 
579
                }
 
580
 
 
581
                memset(&vis_if, 0, sizeof(vis_if));
 
582
 
 
583
                if (vis_server) {
 
584
                        vis_if.addr.sin_family = AF_INET;
 
585
                        vis_if.addr.sin_port = htons(PORT + 2);
 
586
                        vis_if.addr.sin_addr.s_addr = vis_server;
 
587
                        vis_if.sock = socket( PF_INET, SOCK_DGRAM, 0 );
 
588
                }
 
589
 
 
590
                if (gateway_class != 0)
 
591
                        init_interface_gw();
 
592
 
 
593
                if ( debug_level > 0 ) {
 
594
 
 
595
                        printf( "debug level: %i\n", debug_level );
 
596
 
 
597
                        if ( originator_interval != 1000 )
 
598
                                printf( "originator interval: %i\n", originator_interval );
 
599
 
 
600
                        if ( gateway_class > 0 )
 
601
                                printf( "gateway class: %i -> propagating: %i%s/%i%s\n", gateway_class, ( download_speed > 2048 ? download_speed / 1024 : download_speed ), ( download_speed > 2048 ? "MBit" : "KBit" ), ( upload_speed > 2048 ? upload_speed / 1024 : upload_speed ), ( upload_speed > 2048 ? "MBit" : "KBit" ) );
 
602
 
 
603
                        if ( routing_class > 0 )
 
604
                                printf( "routing class: %i\n", routing_class );
 
605
 
 
606
                        if ( pref_gateway > 0 ) {
 
607
                                addr_to_string( pref_gateway, str1, sizeof(str1) );
 
608
                                printf( "preferred gateway: %s\n", str1 );
 
609
                        }
 
610
 
 
611
                        if ( vis_server > 0 ) {
 
612
                                addr_to_string( vis_server, str1, sizeof(str1) );
 
613
                                printf( "visualisation server: %s\n", str1 );
 
614
                        }
 
615
 
 
616
                }
 
617
 
 
618
        /* connect to running batmand via unix socket */
 
619
        } else {
 
620
 
 
621
more_hna:
 
622
                unix_if.unix_sock = socket( AF_LOCAL, SOCK_STREAM, 0 );
 
623
 
 
624
                memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
 
625
                unix_if.addr.sun_family = AF_LOCAL;
 
626
                strcpy( unix_if.addr.sun_path, UNIX_PATH );
 
627
 
 
628
                if ( connect ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof(struct sockaddr_un) ) < 0 ) {
 
629
 
 
630
                        printf( "Error - can't connect to unix socket '%s': %s ! Is batmand running on this host ?\n", UNIX_PATH, strerror(errno) );
 
631
                        close( unix_if.unix_sock );
 
632
                        exit(EXIT_FAILURE);
 
633
 
 
634
                }
 
635
 
 
636
                unix_buff = debugMalloc( 1501, 5001 );
 
637
 
 
638
                if ( debug_level > 0 ) {
 
639
 
 
640
                        if ( debug_level <= debug_level_max ) {
 
641
 
 
642
                                snprintf( unix_buff, 10, "d:%c", debug_level );
 
643
 
 
644
                                if ( ( debug_level > 2 ) && ( batch_mode ) )
 
645
                                        printf( "WARNING: Your chosen debug level (%i) does not support batch mode !\n", debug_level );
 
646
 
 
647
                        }
 
648
 
 
649
                } else if ( routing_class_opt ) {
 
650
 
 
651
                        batch_mode = 1;
 
652
                        snprintf( unix_buff, 10, "r:%c", routing_class );
 
653
 
 
654
                } else if ( pref_gw_opt ) {
 
655
 
 
656
                        batch_mode = 1;
 
657
                        addr_to_string( pref_gateway, str1, sizeof(str1) );
 
658
                        snprintf( unix_buff, 20, "p:%s", str1 );
 
659
 
 
660
                } else if ( gateway_class_opt ) {
 
661
 
 
662
                        batch_mode = 1;
 
663
                        snprintf( unix_buff, 10, "g:%c", gateway_class );
 
664
 
 
665
                } else if (hop_penalty_opt) {
 
666
 
 
667
                        batch_mode = 1;
 
668
                        snprintf(unix_buff, 10, "m:%c", hop_penalty);
 
669
 
 
670
                } else if (purge_timeout_opt) {
 
671
 
 
672
                        batch_mode = 1;
 
673
                        snprintf(unix_buff, 20, "q:%u", purge_timeout);
 
674
 
 
675
                } else if (info_output) {
 
676
 
 
677
                        batch_mode = 1;
 
678
                        snprintf( unix_buff, 10, "i" );
 
679
 
 
680
                } else if (!list_empty(&hna_chg_list)) {
 
681
 
 
682
                        batch_mode = was_hna = 1;
 
683
                        hna_task = (struct hna_task *)hna_chg_list.next;
 
684
                        addr_to_string(hna_task->addr, str1, sizeof(str1));
 
685
                        snprintf(unix_buff, 30, "%c:%s/%i",
 
686
                                 (hna_task->route_action == ROUTE_ADD ? 'a' : 'A'),
 
687
                                 str1, hna_task->netmask);
 
688
 
 
689
                        list_del((struct list_head *)&hna_chg_list, &hna_task->list, &hna_chg_list);
 
690
                        debugFree(hna_task, 1298);
 
691
 
 
692
                } else {
 
693
 
 
694
                        batch_mode = 1;
 
695
                        snprintf(unix_buff, 10, "y");
 
696
 
 
697
                }
 
698
 
 
699
                if ( write( unix_if.unix_sock, unix_buff, 30 ) < 0 ) {
 
700
 
 
701
                        printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
 
702
                        close( unix_if.unix_sock );
 
703
                        debugFree( unix_buff, 5101 );
 
704
                        exit(EXIT_FAILURE);
 
705
 
 
706
                }
 
707
 
 
708
                while ( ( recv_buff_len = read( unix_if.unix_sock, unix_buff, 1500 ) ) > 0 ) {
 
709
 
 
710
                        unix_buff[recv_buff_len] = '\0';
 
711
 
 
712
                        buff_ptr = unix_buff;
 
713
                        bytes_written = 0;
 
714
 
 
715
                        while ( ( cr_ptr = strchr( buff_ptr, '\n' ) ) != NULL ) {
 
716
 
 
717
                                *cr_ptr = '\0';
 
718
 
 
719
                                if ( strncmp( buff_ptr, "EOD", 3 ) == 0 ) {
 
720
 
 
721
                                        if (batch_mode)
 
722
                                                goto close_con;
 
723
 
 
724
                                } else if ( strncmp( buff_ptr, "BOD", 3 ) == 0 ) {
 
725
 
 
726
                                        if ( !batch_mode )
 
727
                                                system( "clear" );
 
728
 
 
729
                                } else {
 
730
 
 
731
                                        printf( "%s\n", buff_ptr );
 
732
 
 
733
                                }
 
734
 
 
735
                                bytes_written += strlen( buff_ptr ) + 1;
 
736
                                buff_ptr = cr_ptr + 1;
 
737
 
 
738
                        }
 
739
 
 
740
                        if ( bytes_written != recv_buff_len )
 
741
                                printf( "%s", buff_ptr );
 
742
 
 
743
                }
 
744
 
 
745
close_con:
 
746
                close( unix_if.unix_sock );
 
747
                debugFree( unix_buff, 5102 );
 
748
 
 
749
                if ( recv_buff_len < 0 ) {
 
750
 
 
751
                        printf( "Error - can't read from unix socket: %s\n", strerror(errno) );
 
752
                        exit(EXIT_FAILURE);
 
753
 
 
754
                } else {
 
755
 
 
756
                        if (!batch_mode)
 
757
                                printf( "Connection terminated by remote host\n" );
 
758
 
 
759
                }
 
760
 
 
761
                if ((was_hna) && (!list_empty(&hna_chg_list)))
 
762
                        goto more_hna;
 
763
 
 
764
                exit(EXIT_SUCCESS);
 
765
 
 
766
        }
 
767
 
 
768
}
 
769
 
 
770
void interface_listen_sockets(void)
 
771
{
 
772
        struct list_head *list_pos;
 
773
        struct batman_if *batman_if;
 
774
 
 
775
        FD_ZERO(&receive_wait_set);
 
776
        receive_max_sock = 0;
 
777
 
 
778
        list_for_each(list_pos, &if_list) {
 
779
                batman_if = list_entry(list_pos, struct batman_if, list);
 
780
 
 
781
                if (batman_if->if_active) {
 
782
                        if (batman_if->udp_recv_sock > receive_max_sock)
 
783
                                receive_max_sock = batman_if->udp_recv_sock;
 
784
 
 
785
                        FD_SET(batman_if->udp_recv_sock, &receive_wait_set);
 
786
                }
 
787
        }
 
788
}
 
789
 
 
790
static int is_interface_up(char *dev)
 
791
{
 
792
        struct ifreq int_req;
 
793
        int sock;
 
794
 
 
795
        if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
 
796
                return 0;
 
797
 
 
798
        memset(&int_req, 0, sizeof (struct ifreq));
 
799
        strncpy(int_req.ifr_name, dev, IFNAMSIZ - 1);
 
800
 
 
801
        if (ioctl(sock, SIOCGIFFLAGS, &int_req) < 0)
 
802
                goto failure;
 
803
 
 
804
        if (!(int_req.ifr_flags & IFF_UP))
 
805
                goto failure;
 
806
 
 
807
        if (ioctl(sock, SIOCGIFADDR, &int_req) < 0)
 
808
                goto failure;
 
809
 
 
810
        close(sock);
 
811
        return 1;
 
812
 
 
813
failure:
 
814
        close(sock);
 
815
        return 0;
 
816
}
 
817
 
 
818
void deactivate_interface(struct batman_if *batman_if)
 
819
{
 
820
        if (batman_if->udp_recv_sock != 0)
 
821
                close(batman_if->udp_recv_sock);
 
822
 
 
823
        if (batman_if->udp_send_sock != 0)
 
824
                close(batman_if->udp_send_sock);
 
825
 
 
826
        batman_if->udp_recv_sock = 0;
 
827
        batman_if->udp_send_sock = 0;
 
828
 
 
829
        if ((batman_if->netaddr > 0) && (batman_if->netmask > 0)) {
 
830
 
 
831
                add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_HOSTS, BATMAN_RT_PRIO_DEFAULT + batman_if->if_num, 0, RULE_TYPE_DST, RULE_DEL);
 
832
                add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_UNREACH, BATMAN_RT_PRIO_UNREACH + batman_if->if_num, 0, RULE_TYPE_DST, RULE_DEL);
 
833
 
 
834
        }
 
835
 
 
836
        batman_if->if_active = 0;
 
837
        active_ifs--;
 
838
 
 
839
        if (batman_if->if_rp_filter_old > -1)
 
840
                set_rp_filter(batman_if->if_rp_filter_old, batman_if->dev);
 
841
 
 
842
        if (batman_if->if_send_redirects_old > -1)
 
843
                set_send_redirects(batman_if->if_send_redirects_old, batman_if->dev);
 
844
 
 
845
        batman_if->if_rp_filter_old = -1;
 
846
        batman_if->if_send_redirects_old = -1;
 
847
 
 
848
        interface_listen_sockets();
 
849
        debug_output(3, "Interface deactivated: %s\n", batman_if->dev);
 
850
}
 
851
 
 
852
static void activate_interface(struct batman_if *batman_if)
 
853
{
 
854
        struct ifreq int_req;
 
855
        int on = 1, sock_opts;
 
856
 
 
857
        if ( ( batman_if->udp_recv_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {
 
858
 
 
859
                debug_output(3, "Error - can't create receive socket: %s\n", strerror(errno) );
 
860
                goto error;
 
861
 
 
862
        }
 
863
 
 
864
        memset( &int_req, 0, sizeof (struct ifreq) );
 
865
        strncpy( int_req.ifr_name, batman_if->dev, IFNAMSIZ - 1 );
 
866
 
 
867
        if ( ioctl( batman_if->udp_recv_sock, SIOCGIFADDR, &int_req ) < 0 ) {
 
868
 
 
869
                debug_output(3, "Error - can't get IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
 
870
                goto error;
 
871
 
 
872
        }
 
873
 
 
874
        batman_if->addr.sin_family = AF_INET;
 
875
        batman_if->addr.sin_port = htons(PORT);
 
876
        batman_if->addr.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr;
 
877
 
 
878
        if (batman_if->addr.sin_addr.s_addr == 0) {
 
879
 
 
880
                debug_output(3, "Error - invalid ip address detected (0.0.0.0): %s\n", batman_if->dev);
 
881
                goto error;
 
882
 
 
883
        }
 
884
 
 
885
        if ( ioctl( batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req ) < 0 ) {
 
886
 
 
887
                debug_output(3, "Error - can't get broadcast IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
 
888
                goto error;
 
889
 
 
890
        }
 
891
 
 
892
        batman_if->broad.sin_family = AF_INET;
 
893
        batman_if->broad.sin_port = htons(PORT);
 
894
        batman_if->broad.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_broadaddr)->sin_addr.s_addr;
 
895
 
 
896
        if ( batman_if->broad.sin_addr.s_addr == 0 ) {
 
897
 
 
898
                debug_output(3, "Error - invalid broadcast address detected (0.0.0.0): %s\n", batman_if->dev );
 
899
                goto error;
 
900
 
 
901
        }
 
902
 
 
903
 
 
904
#ifdef __linux__
 
905
        /* The SIOCGIFINDEX ioctl is Linux specific, but I am not yet sure if the
 
906
        * equivalent exists on *BSD. There is a function called if_nametoindex()
 
907
        * on both Linux and BSD.
 
908
        * Maybe it does the same as this code and we can simply call it instead?
 
909
        * --stsp
 
910
        */
 
911
        if ( ioctl( batman_if->udp_recv_sock, SIOCGIFINDEX, &int_req ) < 0 ) {
 
912
 
 
913
                debug_output(3, "Error - can't get index of interface %s: %s\n", batman_if->dev, strerror(errno) );
 
914
                goto error;
 
915
 
 
916
        }
 
917
 
 
918
        batman_if->if_index = int_req.ifr_ifindex;
 
919
#else
 
920
        batman_if->if_index = 0;
 
921
#endif
 
922
        batman_if->wifi_if = is_wifi_interface(batman_if->dev, batman_if->udp_recv_sock);
 
923
 
 
924
        if (ioctl(batman_if->udp_recv_sock, SIOCGIFNETMASK, &int_req) < 0) {
 
925
 
 
926
                debug_output(3, "Error - can't get netmask address of interface %s: %s\n", batman_if->dev, strerror(errno));
 
927
                goto error;
 
928
 
 
929
        }
 
930
 
 
931
        batman_if->netaddr = (((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr & batman_if->addr.sin_addr.s_addr);
 
932
        batman_if->netmask = bit_count(((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr);
 
933
 
 
934
        add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_HOSTS, BATMAN_RT_PRIO_DEFAULT + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);
 
935
        add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_UNREACH, BATMAN_RT_PRIO_UNREACH + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);
 
936
 
 
937
        if ( ( batman_if->udp_send_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {
 
938
 
 
939
                debug_output(3, "Error - can't create send socket: %s\n", strerror(errno) );
 
940
                goto error;
 
941
 
 
942
        }
 
943
 
 
944
        if ( setsockopt( batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on) ) < 0 ) {
 
945
 
 
946
                debug_output(3, "Error - can't enable broadcasts: %s\n", strerror(errno) );
 
947
                goto error;
 
948
 
 
949
        }
 
950
 
 
951
        if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) {
 
952
 
 
953
                debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
 
954
                goto error;
 
955
 
 
956
        }
 
957
 
 
958
        if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {
 
959
 
 
960
                debug_output(3, "Error - can't bind send socket: %s\n", strerror(errno) );
 
961
                goto error;
 
962
 
 
963
        }
 
964
 
 
965
        /* make udp socket non blocking */
 
966
        sock_opts = fcntl(batman_if->udp_send_sock, F_GETFL, 0);
 
967
        fcntl(batman_if->udp_send_sock, F_SETFL, sock_opts | O_NONBLOCK);
 
968
 
 
969
        if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) {
 
970
 
 
971
                debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
 
972
                goto error;
 
973
 
 
974
        }
 
975
 
 
976
        if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof(struct sockaddr_in) ) < 0 ) {
 
977
 
 
978
                debug_output(3, "Error - can't bind receive socket: %s\n", strerror(errno));
 
979
                goto error;
 
980
 
 
981
        }
 
982
 
 
983
        batman_if->out.orig = batman_if->addr.sin_addr.s_addr;
 
984
        batman_if->out.prev_sender = batman_if->addr.sin_addr.s_addr;
 
985
 
 
986
        batman_if->if_active = 1;
 
987
        active_ifs++;
 
988
 
 
989
        interface_listen_sockets();
 
990
        debug_output(3, "Interface activated: %s\n", batman_if->dev);
 
991
 
 
992
        batman_if->if_rp_filter_old = get_rp_filter(batman_if->dev);
 
993
        set_rp_filter(0, batman_if->dev);
 
994
 
 
995
        batman_if->if_send_redirects_old = get_send_redirects(batman_if->dev);
 
996
        set_send_redirects(0, batman_if->dev);
 
997
 
 
998
        return;
 
999
 
 
1000
error:
 
1001
        deactivate_interface(batman_if);
 
1002
}
 
1003
 
 
1004
void init_interface(struct batman_if *batman_if)
 
1005
{
 
1006
        if (strlen( batman_if->dev ) > IFNAMSIZ - 1) {
 
1007
                printf("Error - interface name too long: %s\n", batman_if->dev);
 
1008
                restore_defaults();
 
1009
                exit(EXIT_FAILURE);
 
1010
        }
 
1011
 
 
1012
        if (is_interface_up(batman_if->dev))
 
1013
                activate_interface(batman_if);
 
1014
}
 
1015
 
 
1016
void check_inactive_interfaces(void)
 
1017
{
 
1018
        struct list_head *list_pos;
 
1019
        struct batman_if *batman_if;
 
1020
 
 
1021
        /* all available interfaces are active */
 
1022
        if (found_ifs == active_ifs)
 
1023
                return;
 
1024
 
 
1025
        list_for_each(list_pos, &if_list) {
 
1026
                batman_if = list_entry(list_pos, struct batman_if, list);
 
1027
 
 
1028
                if ((!batman_if->if_active) && (is_interface_up(batman_if->dev)))
 
1029
                        activate_interface(batman_if);
 
1030
        }
 
1031
}
 
1032
 
 
1033
void check_active_interfaces(void)
 
1034
{
 
1035
        struct list_head *list_pos;
 
1036
        struct batman_if *batman_if;
 
1037
 
 
1038
        /* all available interfaces are deactive */
 
1039
        if (active_ifs == 0)
 
1040
                return;
 
1041
 
 
1042
        list_for_each(list_pos, &if_list) {
 
1043
                batman_if = list_entry(list_pos, struct batman_if, list);
 
1044
 
 
1045
                if ((batman_if->if_active) && (!is_interface_up(batman_if->dev)))
 
1046
                        deactivate_interface(batman_if);
 
1047
        }
 
1048
}
 
1049
 
 
1050
 
 
1051
 
 
1052
 
 
1053
void init_interface_gw (void) {
 
1054
 
 
1055
        int32_t sock_opts, err, skfd;
 
1056
        struct ifreq ifr;
 
1057
        struct sockaddr_in sin;
 
1058
        struct batgat_ioc_args ioc;
 
1059
        struct batman_if *batman_if = (struct batman_if *)if_list.next;
 
1060
 
 
1061
 
 
1062
        if ((batman_if->udp_tunnel_sock = use_gateway_module()) < 0) {
 
1063
 
 
1064
                batman_if->addr.sin_port = htons(GW_PORT);
 
1065
 
 
1066
                batman_if->udp_tunnel_sock = socket(PF_INET, SOCK_DGRAM, 0);
 
1067
 
 
1068
                if (batman_if->udp_tunnel_sock < 0) {
 
1069
 
 
1070
                        debug_output( 0, "Error - can't create tunnel socket: %s\n", strerror(errno) );
 
1071
                        restore_defaults();
 
1072
                        exit(EXIT_FAILURE);
 
1073
 
 
1074
                }
 
1075
 
 
1076
                if ( bind( batman_if->udp_tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {
 
1077
 
 
1078
                        debug_output( 0, "Error - can't bind tunnel socket: %s\n", strerror(errno) );
 
1079
                        restore_defaults();
 
1080
                        exit(EXIT_FAILURE);
 
1081
 
 
1082
                }
 
1083
 
 
1084
                /* make udp socket non blocking */
 
1085
                sock_opts = fcntl( batman_if->udp_tunnel_sock, F_GETFL, 0 );
 
1086
                fcntl( batman_if->udp_tunnel_sock, F_SETFL, sock_opts | O_NONBLOCK );
 
1087
 
 
1088
                batman_if->addr.sin_port = htons(PORT);
 
1089
 
 
1090
                pthread_create( &batman_if->listen_thread_id, NULL, &gw_listen, batman_if );
 
1091
 
 
1092
        } else {
 
1093
 
 
1094
                ioc.universal = strlen(batman_if->dev);
 
1095
                ioc.exists = 0;
 
1096
                strncpy(ioc.dev_name, batman_if->dev, IFNAMSIZ - 1);
 
1097
 
 
1098
                if (ioctl(batman_if->udp_tunnel_sock, IOCSETDEV, &ioc) < 0) {
 
1099
                        debug_output(0, "Error - can't add device %s: %s\n", batman_if->dev,strerror(errno));
 
1100
                        batman_if->dev = NULL;
 
1101
                        restore_defaults();
 
1102
                        exit(EXIT_FAILURE);
 
1103
                }
 
1104
 
 
1105
                /* set ip address of gate device */
 
1106
                if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
 
1107
                        debug_output( 0, "Error - can't create socket to gate\n" );
 
1108
                        restore_defaults();
 
1109
                        exit( EXIT_FAILURE );
 
1110
                }
 
1111
 
 
1112
                memset(&ifr, 0, sizeof(ifr));
 
1113
                memset(&sin, 0, sizeof(struct sockaddr));
 
1114
 
 
1115
                strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
 
1116
 
 
1117
                sin.sin_family = AF_INET;
 
1118
                sin.sin_addr.s_addr = ioc.universal;
 
1119
                memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
 
1120
 
 
1121
                if ((err = ioctl(skfd, SIOCSIFADDR, &ifr)) < 0) {
 
1122
                        debug_output(0, "Error - can't set IFADDR %s: %s\n", ioc.dev_name, strerror(err));
 
1123
                        close(skfd);
 
1124
                        restore_defaults();
 
1125
                        exit( EXIT_FAILURE );
 
1126
                }
 
1127
 
 
1128
                memset(&ifr, 0, sizeof(ifr));
 
1129
                strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
 
1130
                if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
 
1131
                        debug_output(0, "Error - can't get IFFLAGS for %s: %s\n", ioc.dev_name, strerror(errno));
 
1132
                        close(skfd);
 
1133
                        restore_defaults();
 
1134
                        exit( EXIT_FAILURE );
 
1135
                }
 
1136
 
 
1137
                strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
 
1138
                ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
 
1139
 
 
1140
                if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
 
1141
                        debug_output(0, "Error - can't set IFFLAGS for %s: %s\n", ioc.dev_name, strerror(errno));
 
1142
                        close(skfd);
 
1143
                        restore_defaults();
 
1144
                        exit( EXIT_FAILURE );
 
1145
                }
 
1146
 
 
1147
                close(skfd);
 
1148
 
 
1149
                if (!ioc.exists)
 
1150
                        add_del_route(ioc.universal, 16, 0, 0, ioc.ifindex, ioc.dev_name, 254, ROUTE_TYPE_UNICAST, ROUTE_ADD);
 
1151
 
 
1152
 
 
1153
        }
 
1154
 
 
1155
}
 
1156
 
 
1157