5 #include "routetable.h"
15 #include <sys/types.h>
21 // Uso: ./ip ip [router forward routes_file port proto]
23 void send_loop(IPOut& ipout, unsigned proto);
25 int main(int argc, char* argv[])
30 uint16_t port = DEVTCP_DEFAULT_PORT;
31 std::string fname = "route.txt";
34 std::cerr << "Uso: " << argv[0] << " ip [router forward routes_file "
40 router = atoi(argv[2]);
42 forward = atoi(argv[3]);
48 proto = atoi(argv[6]);
49 // Abro archivo con rutas
50 std::ifstream ifs(fname.c_str()); assert(ifs);
51 // Creo medio físico y cola para forwarding
52 DevTCP dev(addr, port);
53 DevQue fwque(addr, DEVQUE_DEFAULT_KEY-1);
55 pid_t pid_send = fork();
58 perror("fork() send");
61 if (pid_send) // IPOut
63 RouteTable table(dev);
64 add_routes(table, ifs, dev);
65 IPOut ipout(addr, table, fwque, std::cerr);
66 pid_t pid_fw = fork();
69 perror("fork() forward");
72 if (pid_fw) // Padre (IPOut send)
75 send_loop(ipout, proto);
76 kill(pid_send, SIGTERM);
77 waitpid(pid_send, &ret, 0);
78 kill(pid_fw, SIGTERM);
79 waitpid(pid_fw, &ret, 0);
82 else // Hijo 1 (IPOut forward)
90 IPIn ipin(addr, dev, fwque, router, forward, std::cerr);
94 std::string s = ipin.recv(proto, src, dst);
95 std::cout << "Recibido '" << s << "' (len " << s.size() << ") de "
96 << src << " para " << dst << " (proto = " << unsigned(proto)
104 void send_loop(IPOut& ipout, unsigned proto)
108 while (std::getline(std::cin, dst))
110 if (!std::getline(std::cin, msg))
112 if (ipout.send(msg, proto, IPAddr(dst.c_str())))
113 std::cout << "Enviado '" << msg << "' a " << dst << "\n";
115 std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n";
119 // vim: set et sw=4 sts=4 :