5 #include "routetable.h"
14 #include <sys/types.h>
20 // Uso: ./test_ipout ip [router forward routes_file queue_id proto]
22 void send_loop(IPOut& ipout, unsigned proto);
24 void add_routes(RouteTable& rt, std::istream& is, Dev& dev);
26 int main(int argc, char* argv[])
31 key_t queue_id = DEVQUE_DEFAULT_KEY;
32 std::string fname = "route.txt";
35 std::cerr << "Uso: ./test_ipout ip [router forward routes_file "
41 router = atoi(argv[2]);
43 forward = atoi(argv[3]);
47 queue_id = atoi(argv[5]);
49 proto = atoi(argv[6]);
51 int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1);
52 que_id = msgget(DEVQUE_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1);
53 // Abro archivo con rutas
54 std::ifstream ifs(fname.c_str()); assert(ifs);
55 // Creo medio físico y cola para forwarding
56 DevQue dev(addr, queue_id);
57 DevQue fwque(addr, DEVQUE_DEFAULT_KEY-1);
59 pid_t pid_send = fork();
62 perror("fork() send");
65 if (pid_send) // IPOut
67 RouteTable table(dev);
68 add_routes(table, ifs, dev);
69 IPOut ipout(addr, table, fwque, std::cerr);
70 pid_t pid_fw = fork();
73 perror("fork() forward");
76 if (pid_fw) // Padre (IPOut send)
79 send_loop(ipout, proto);
80 kill(pid_send, SIGTERM);
81 waitpid(pid_send, &ret, 0);
82 kill(pid_fw, SIGTERM);
83 waitpid(pid_fw, &ret, 0);
86 else // Hijo 1 (IPOut forward)
94 IPIn ipin(addr, dev, fwque, router, forward, std::cerr);
98 std::string s = ipin.recv(proto, src, dst);
99 std::cout << "Recibido '" << s << "' (len " << s.size() << ") de "
100 << src << " para " << dst << " (proto = " << proto << ")\n";
107 void send_loop(IPOut& ipout, unsigned proto)
111 while (std::getline(std::cin, dst))
113 if (!std::getline(std::cin, msg))
115 if (ipout.send(msg, proto, IPAddr(dst.c_str())))
116 std::cout << "Enviado '" << msg << "' a " << dst << "\n";
118 std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n";
122 void add_routes(RouteTable& rt, std::istream& is, Dev& dev)
125 while (std::getline(is, line))
127 std::istringstream iss(line);
132 iss >> net >> gw >> mtu >> metric;
133 if (net == "0") net = "0.0.0.0";
134 if (gw == "0") gw = "0.0.0.0";
135 rt.add(net.c_str(), gw.c_str(), metric, mtu, dev);
139 // vim: set et sw=4 sts=4 :