loadlwipv6 inlined
[vde.git] / vde-2 / src / vde_tunctl.c
blob2c7d1379c0c3c31cc7177cf3584c5d1cca36fe48
1 /* Copyright 2002 Jeff Dike
2 * Licensed under the GPL
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <pwd.h>
12 #include <net/if.h>
13 #include <sys/ioctl.h>
14 #include <linux/if_tun.h>
16 #include <config.h>
17 #include <vde.h>
18 #include <vdecommon.h>
20 static void Usage(char *name)
22 fprintf(stderr, "Create: %s [-b] [-u owner] [-t device-name] "
23 "[-f tun-clone-device]\n", name);
24 fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
25 name);
26 fprintf(stderr, "The default tun clone device is /dev/net/tun - some systems"
27 " use\n/dev/misc/net/tun instead\n\n");
28 fprintf(stderr, "-b will result in brief output (just the device name)\n");
29 exit(1);
32 int main(int argc, char **argv)
34 struct ifreq ifr;
35 struct passwd *pw;
36 long owner = geteuid();
37 int tap_fd, opt, delete = 0, brief = 0;
38 char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end;
40 while((opt = getopt(argc, argv, "bd:f:t:u:")) > 0){
41 switch(opt) {
42 case 'b':
43 brief = 1;
44 break;
45 case 'd':
46 delete = 1;
47 tun = optarg;
48 break;
49 case 'f':
50 file = optarg;
51 break;
52 case 'u':
53 pw = getpwnam(optarg);
54 if(pw != NULL){
55 owner = pw->pw_uid;
56 break;
58 owner = strtol(optarg, &end, 0);
59 if(*end != '\0'){
60 fprintf(stderr, "'%s' is neither a username nor a numeric uid.\n",
61 optarg);
62 Usage(name);
64 break;
65 case 't':
66 tun = optarg;
67 break;
68 case 'h':
69 default:
70 Usage(name);
74 argv += optind;
75 argc -= optind;
77 if(argc > 0)
78 Usage(name);
80 if((tap_fd = open(file, O_RDWR)) < 0){
81 fprintf(stderr, "Failed to open '%s' : ", file);
82 perror("");
83 exit(1);
86 memset(&ifr, 0, sizeof(ifr));
88 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
89 strncpy(ifr.ifr_name, tun, sizeof(ifr.ifr_name) - 1);
90 if(ioctl(tap_fd, TUNSETIFF, (void *) &ifr) < 0){
91 perror("TUNSETIFF");
92 exit(1);
95 if(delete){
96 if(ioctl(tap_fd, TUNSETPERSIST, 0) < 0){
97 perror("TUNSETPERSIST");
98 exit(1);
100 printf("Set '%s' nonpersistent\n", ifr.ifr_name);
102 else {
103 if(ioctl(tap_fd, TUNSETPERSIST, 1) < 0){
104 perror("TUNSETPERSIST");
105 exit(1);
107 if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
108 perror("TUNSETPERSIST");
109 exit(1);
111 if(brief)
112 printf("%s\n", ifr.ifr_name);
113 else printf("Set '%s' persistent and owned by uid %ld\n", ifr.ifr_name,
114 owner);
116 return(0);