handle default key sizes in openssl_crypter
[strongswan.git] / src / dumm / testing.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
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 MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <signal.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20
21 #include <library.h>
22 #include <dumm.h>
23
24 /**
25 * number of running guests
26 */
27 static int running = 0;
28
29 /**
30 * Guest invocation callback
31 */
32 static pid_t invoke(void *vte, guest_t *guest,
33 char *args[], int argc)
34 {
35 pid_t pid;
36
37 args[argc] = "con0=xterm";
38
39 pid = fork();
40 switch (pid)
41 {
42 case 0: /* child */
43 dup2(open("/dev/null", 0), 1);
44 dup2(open("/dev/null", 0), 2);
45 execvp(args[0], args);
46 exit(-1);
47 case -1:
48 fprintf(stderr, "starting guest '%s' failed\n", guest->get_name(guest));
49 return 0;
50 default:
51 printf("started guest '%s', pid: %d\n", guest->get_name(guest), pid);
52 running++;
53 return pid;
54 }
55 }
56
57 /**
58 * main routine, parses args and reads from console
59 */
60 int main(int argc, char *argv[])
61 {
62 dumm_t *dumm;
63 enumerator_t *enumerator;
64 guest_t *guest;
65 bridge_t *switch0, *switch1, *switch2;
66 iface_t *iface;
67 sigset_t set;
68 siginfo_t info;
69
70 library_init(NULL);
71
72 dumm = dumm_create(NULL);
73
74 switch0 = dumm->create_bridge(dumm, "switch0");
75 switch1 = dumm->create_bridge(dumm, "switch1");
76 switch2 = dumm->create_bridge(dumm, "switch2");
77
78 if (switch0 && switch1 && switch2)
79 {
80 enumerator = dumm->create_guest_enumerator(dumm);
81 while (enumerator->enumerate(enumerator, &guest))
82 {
83 if (!guest->start(guest, invoke, NULL, NULL))
84 {
85 continue;
86 }
87 if (streq(guest->get_name(guest), "alice"))
88 {
89 iface = guest->create_iface(guest, "eth0");
90 if (iface)
91 {
92 switch1->connect_iface(switch1, iface);
93 }
94 iface = guest->create_iface(guest, "eth1");
95 if (iface)
96 {
97 switch0->connect_iface(switch0, iface);
98 }
99 }
100 else if (streq(guest->get_name(guest), "moon") ||
101 streq(guest->get_name(guest), "sun"))
102 {
103 iface = guest->create_iface(guest, "eth0");
104 if (iface)
105 {
106 switch0->connect_iface(switch0, iface);
107 }
108 iface = guest->create_iface(guest, "eth1");
109 if (iface)
110 {
111 switch1->connect_iface(switch1, iface);
112 }
113 }
114 else if (streq(guest->get_name(guest), "bob"))
115 {
116 iface = guest->create_iface(guest, "eth0");
117 if (iface)
118 {
119 switch2->connect_iface(switch2, iface);
120 }
121 }
122 else if (streq(guest->get_name(guest), "venus"))
123 {
124 iface = guest->create_iface(guest, "eth0");
125 if (iface)
126 {
127 switch1->connect_iface(switch1, iface);
128 }
129 }
130 else if (streq(guest->get_name(guest), "carol") ||
131 streq(guest->get_name(guest), "winnetou") ||
132 streq(guest->get_name(guest), "dave"))
133 {
134 iface = guest->create_iface(guest, "eth0");
135 if (iface)
136 {
137 switch0->connect_iface(switch0, iface);
138 }
139 }
140 }
141 enumerator->destroy(enumerator);
142
143 sigemptyset(&set);
144 sigaddset(&set, SIGINT);
145 sigaddset(&set, SIGHUP);
146 sigaddset(&set, SIGTERM);
147 sigaddset(&set, SIGCHLD);
148 sigprocmask(SIG_SETMASK, &set, NULL);
149 while (running)
150 {
151 if (sigwaitinfo(&set, &info) == SIGCHLD)
152 {
153 enumerator = dumm->create_guest_enumerator(dumm);
154 while (enumerator->enumerate(enumerator, &guest))
155 {
156 if (guest->get_pid(guest) == info.si_pid)
157 {
158 running--;
159 guest->sigchild(guest);
160 break;
161 }
162 }
163 enumerator->destroy(enumerator);
164 }
165 }
166 }
167 dumm->destroy(dumm);
168
169 library_deinit();
170 return 0;
171 }