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 void add_routes(RouteTable& rt, std::istream& is, Dev& dev);
27 int main(int argc, char* argv[])
32 uint16_t port = DEVTCP_DEFAULT_PORT;
33 std::string fname = "route.txt";
36 std::cerr << "Uso: " << argv[0] << " ip [router forward routes_file "
42 router = atoi(argv[2]);
44 forward = atoi(argv[3]);
50 proto = atoi(argv[6]);
51 // Creo cola para comunicar el IPIn con IPOut
52 int 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 DevTCP dev(addr, port);
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 = " << unsigned(proto)
108 void send_loop(IPOut& ipout, unsigned proto)
112 while (std::getline(std::cin, dst))
114 if (!std::getline(std::cin, msg))
116 if (ipout.send(msg, proto, IPAddr(dst.c_str())))
117 std::cout << "Enviado '" << msg << "' a " << dst << "\n";
119 std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n";
123 void add_routes(RouteTable& rt, std::istream& is, Dev& dev)
126 while (std::getline(is, line))
128 std::istringstream iss(line);
133 iss >> net >> gw >> mtu >> metric;
134 if (net == "0") net = "0.0.0.0";
135 if (gw == "0") gw = "0.0.0.0";
136 rt.add(net, gw, metric, mtu, dev);
140 // vim: set et sw=4 sts=4 :