X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/0a2739fe97762f57a80599ef170df98e07422a3d..f17a708ff0e870baf2709bb0d63f32f166e082d3:/practicas/pipi/src/test_ipout.cpp diff --git a/practicas/pipi/src/test_ipout.cpp b/practicas/pipi/src/test_ipout.cpp index acdb77f..1f11fc1 100644 --- a/practicas/pipi/src/test_ipout.cpp +++ b/practicas/pipi/src/test_ipout.cpp @@ -2,8 +2,10 @@ #include "ipout.h" #include "ipaddr.h" #include "routetable.h" -#include "dev.h" +#include "devque.h" #include +#include +#include #include #include #include @@ -12,41 +14,63 @@ #include #include -// Uso: ./test_ipout ip dst msg mtu proto queue_id +// Uso: ./test_ipout ip dst mtu routes_file proto queue_id + +void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev); int main(int argc, char* argv[]) { IPAddr addr("10.10.10.2"); IPAddr dst("10.10.10.1"); - std::string msg = "hola mundo"; unsigned mtu = 25; unsigned proto = 0; - key_t queue_id = DEV_DEFAULT_KEY; + key_t queue_id = DEVQUE_DEFAULT_KEY; + std::string fname = "route.txt"; if (argc > 1) addr = IPAddr(argv[1]); if (argc > 2) dst = IPAddr(argv[2]); if (argc > 3) - msg = argv[3]; + mtu = atoi(argv[3]); if (argc > 4) - mtu = atoi(argv[4]); + fname = argv[4]; if (argc > 5) proto = atoi(argv[5]); if (argc > 6) queue_id = atoi(argv[6]); - int que_id = msgget(queue_id, IPC_CREAT | 0666); - assert(que_id != -1); - Dev dev(addr, mtu, queue_id); + int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1); + que_id = msgget(queue_id+1, IPC_CREAT | 0666); assert(que_id != -1); + std::ifstream ifs(fname.c_str()); assert(ifs); + DevQue dev(addr, mtu, queue_id); + DevQue fwque(addr, DEV_MAX_MTU, queue_id+1); RouteTable table(dev); - table.add("10.10.10.1", 0, 0, dev); - table.add("10.10.10.2", 0, 0, dev); - table.add("10.10.10.3", "10.10.10.10", 0, dev); - IPOut ipout(addr, table); - if (ipout.send(msg, proto, dst)) - std::cout << "Enviado '" << msg << "' a " << dst << "\n"; - else - std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n"; + add_routes(table, ifs, mtu, dev); + IPOut ipout(addr, table, fwque, std::cerr); + std::string msg; + while (std::getline(std::cin, msg)) + { + if (ipout.send(msg, proto, dst)) + std::cout << "Enviado '" << msg << "' a " << dst << "\n"; + else + std::cout << "NO SE PUDO ENVIAR '" << msg << "' a " << dst << "\n"; + } return 0; } +void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev) +{ + std::string line; + while (std::getline(is, line)) + { + std::istringstream iss(line); + std::string net; + std::string gw; + unsigned metric; + iss >> net >> gw >> metric; + if (net == "0") net = "0.0.0.0"; + if (gw == "0") gw = "0.0.0.0"; + rt.add(net, gw, mtu, metric, dev); + } +} + // vim: set et sw=4 sts=4 :