File: uml_net.c

package info (click to toggle)
uml-utilities 20070815.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 728 kB
  • sloc: ansic: 3,391; perl: 1,277; makefile: 239; exp: 129; sh: 122
file content (139 lines) | stat: -rw-r--r-- 3,075 bytes parent folder | download | duplicates (8)
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
/* Copyright 2001 Jeff Dike and others
 * Licensed under the GPL
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#define CURRENT_VERSION (4)

extern void ethertap_v0(int argc, char **argv);
extern void ethertap_v1_v2(int argc, char **argv);
extern void ethertap_v1_v2(int argc, char **argv);
extern void ethertap_v3(int argc, char **argv);
extern void ethertap_v4(int argc, char **argv);

void (*ethertap_handlers[])(int argc, char **argv) = {
  ethertap_v0, 
  ethertap_v1_v2,
  ethertap_v1_v2,
  ethertap_v3,
  ethertap_v4,
};

extern void slip_v0_v2(int argc, char **argv);
extern void slip_v3(int argc, char **argv);
extern void slip_v4(int argc, char **argv);

void (*slip_handlers[])(int argc, char **argv) = { 
  slip_v0_v2, 
  slip_v0_v2,
  slip_v0_v2, 
  slip_v3, 
  slip_v4, 
};

#ifdef TUNTAP

extern void tuntap_v2(int argc, char **argv);
extern void tuntap_v3(int argc, char **argv);
extern void tuntap_v4(int argc, char **argv);

void (*tuntap_handlers[])(int argc, char **argv) = {
  NULL,
  NULL,
  tuntap_v2,
  tuntap_v3,
  tuntap_v4,
};

#endif

extern void add_address_v4(int argc, char **argv);

void (*add_handlers[])(int argc, char **argv) = {
  NULL,
  NULL,
  NULL,
  NULL,
  add_address_v4,
};

extern void del_address_v4(int argc, char **argv);

void (*del_handlers[])(int argc, char **argv) = {
  NULL,
  NULL,
  NULL,
  NULL,
  del_address_v4,
};

static void handler(int sig)
{
}

int main(int argc, char **argv)
{
  char *version = argv[1];
  char *transport = argv[2];
  void (**handlers)(int, char **);
  struct sigaction sa;
  char *out;
  int n = 3;
  unsigned int v;

  if(sigaction(SIGCHLD, NULL, &sa) < 0){
    perror("sigaction");
    exit(1);
  }
  sa.sa_handler = handler;
  sa.sa_flags &= ~SA_NOCLDWAIT;
  sa.sa_flags |= SA_RESTART;
  if(sigaction(SIGCHLD, &sa, NULL) < 0){
    perror("sigaction");
    exit(1);
  }

  if(argc < 3){
    fprintf(stderr, 
	    "uml_net : bad argument list - if you're running uml_net by\n"
	    "hand, don't bother.  uml_net is run automatically by UML when\n"
	    "necessary.\n");
    exit(1);
  }
  setenv("PATH", "/bin:/usr/bin:/sbin:/usr/sbin", 1);
  v = strtoul(version, &out, 0);
  if(out != version){
    if(v > CURRENT_VERSION){
      fprintf(stderr, "Version mismatch - requested version %d, uml_net "
	      "supports up to version %d\n", v, CURRENT_VERSION);
      exit(1);
    }
  }
  else {
    v = 0;
    transport = version;
    n = 2;
  }
  if(!strcmp(transport, "ethertap")) handlers = ethertap_handlers;
  else if(!strcmp(transport, "slip")) handlers = slip_handlers;
#ifdef TUNTAP
  else if(!strcmp(transport, "tuntap")) handlers = tuntap_handlers;
#endif
  else if(!strcmp(transport, "add")) handlers = add_handlers;
  else if(!strcmp(transport, "del")) handlers = del_handlers;
  else {
    printf("Unknown transport : '%s'\n", transport);
    exit(1);
  }
  if(handlers[v] != NULL) (*handlers[v])(argc - n, &argv[n]);
  else {
    printf("No version #%d handler for '%s'\n", v, transport);
    exit(1);
  }
  return(0);
}