Date:2011-05-11 21:46:29 (12 years 10 months ago)
Author:Werner Almesberger
Commit:de72af351f661b538add81cbc9965278a2f8c40c
Message:tools/dirtpan/: if given an interface conf command, run it with tunX in $ITF

- dirtpan.c (open_tun): if a command is given, set the environment
variable ITF to the interface name and system(3) the command
- dirtpan.c (usage, main): accept an interface configuration command as
the 4th command-line argument
Files: tools/dirtpan/dirtpan.c (7 diffs)

Change Details

tools/dirtpan/dirtpan.c
1717#include <unistd.h>
1818#include <string.h>
1919#include <fcntl.h>
20#include <signal.h>
2021#include <assert.h>
2122#include <sys/types.h>
2223#include <sys/time.h>
24#include <sys/wait.h>
2325#include <sys/select.h>
2426#include <sys/ioctl.h>
2527#include <sys/socket.h>
...... 
474476}
475477
476478
477static int open_tun(void)
479static int open_tun(const char *cmd)
478480{
479481    struct ifreq ifr;
480    int fd;
482    int fd, res;
481483
482484    fd = open(TUN_DEV, O_RDWR);
483485    if (fd < 0) {
...... 
493495        exit(1);
494496    }
495497
496    fprintf(stderr, "%s\n", ifr.ifr_name);
498    if (!cmd) {
499        fprintf(stderr, "%s\n", ifr.ifr_name);
500        return fd;
501    }
502
503    if (setenv("ITF", ifr.ifr_name, 1) < 0) {
504        perror("setenv");
505        exit(1);
506    }
507
508    res = system(cmd);
509    if (res < 0) {
510        perror("system");
511        exit(1);
512    }
513    if (WIFEXITED(res)) {
514        if (!WEXITSTATUS(res))
515            return fd;
516        exit(WEXITSTATUS(res));
517    }
518    if (WIFSIGNALED(res)) {
519        raise(WTERMSIG(res));
520        exit(1);
521    }
497522
498    return fd;
523    fprintf(stderr, "cryptic exit status %d\n", res);
524    exit(1);
499525}
500526
501527
...... 
504530
505531static void usage(const char *name)
506532{
507    fprintf(stderr, "usage: %s [-d [-d]] pan_id src_addr dst_addr\n",
508        name);
533    fprintf(stderr,
534"usage: %s [-d [-d]] pan_id src_addr dst_addr [command]\n"
535    , name);
509536    exit(1);
510537}
511538
...... 
526553
527554int main(int argc, char **argv)
528555{
556    const char *cmd = NULL;
529557    uint16_t pan, src, dst;
530558    int c;
531559
...... 
539567        }
540568
541569    switch (argc-optind) {
570    case 4:
571        cmd = argv[optind+3];
572        /* fall through */
542573    case 3:
543574        pan = addr(*argv, argv[optind]);
544575        src = addr(*argv, argv[optind+1]);
...... 
549580    }
550581
551582    net = open_net(pan, src, dst);
552    tun = open_tun();
583    tun = open_tun(cmd);
553584    while (1)
554585        event();
555586

Archive Download the corresponding diff file



interactive