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 // Creo cola para comunicar el IPIn con IPOut
50 int que_id = msgget(DEVQUE_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1);
51 // Abro archivo con rutas
52 std::ifstream ifs(fname.c_str()); assert(ifs);
53 // Creo medio físico y cola para forwarding
54 DevTCP dev(addr, port);
55 DevQue fwque(addr, DEVQUE_DEFAULT_KEY-1);
57 pid_t pid_send = fork();
60 perror("fork() send");
63 if (pid_send) // IPOut
65 RouteTable table(dev);
66 add_routes(table, ifs, dev);
67 IPOut ipout(addr, table, fwque, std::cerr);
68 pid_t pid_fw = fork();
71 perror("fork() forward");
74 if (pid_fw) // Padre (IPOut send)
77 send_loop(ipout, proto);
78 kill(pid_send, SIGTERM);
79 waitpid(pid_send, &ret, 0);
80 kill(pid_fw, SIGTERM);
81 waitpid(pid_fw, &ret, 0);
84 else // Hijo 1 (IPOut forward)
92 IPIn ipin(addr, dev, fwque, router, forward, std::cerr);
96 std::string s = ipin.recv(proto, src, dst);
97 std::cout << "Recibido '" << s << "' (len " << s.size() << ") de "
98 << src << " para " << dst << " (proto = " << unsigned(proto)
106 void send_loop(IPOut& ipout, unsigned proto)
110 while (std::getline(std::cin, dst))
112 if (!std::getline(std::cin, msg))
114 if (ipout.send(msg, proto, IPAddr(dst.c_str())))
115 std::cout << "Enviado '" << msg << "' a " << dst << "\n";
117 std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n";
121 // vim: set et sw=4 sts=4 :