15 IPOut::IPOut(const IPAddr& ip, RouteTable& rtable, Dev& forward_que, std::ostream& log):
16 ip(ip), rtable(rtable), forward_que(forward_que), log(log)
20 void IPOut::drop(const std::string& msg, const std::string& buf)
22 log << "IPOut::drop: " << msg << "\n\tBuffer: " << buf << "\n";
25 void IPOut::drop(const std::string& msg, const IPHeader& iph)
27 log << "IPOut::drop: " << msg << "\n\tIPHeader: " << iph << "\n";
30 /// Envía un paquete IP
31 bool IPOut::send(const std::string& data, uint8_t proto, IPAddr dst, IPAddr src,
32 bool df, uint8_t ttl, uint16_t id)
33 throw (std::runtime_error)
35 // Mando todo lo que tengo para forwardear
38 std::string buf = forward_que.receive();
41 std::cout << "IPOut::send: A forwardear\n";
43 send(iph, buf.substr(iph.header_len()));
45 // Mando el paquete en sí
51 IPHeader iph(4, IPHeader::header_len() + data.size(), id, df, 0, 0,
52 ttl, proto, src, dst);
53 return send(iph, data);
56 /// Envía un paquete IP
57 bool IPOut::send(IPHeader iph, std::string data) throw (std::runtime_error)
60 RouteTable::Route* r = rtable.get(iph.dst);
63 drop("No existe una ruta para el destino", iph);
66 // No quieren fragmentar
67 if (iph.df && (IPHeader::header_len() + data.size() > r->iface->mtu))
69 drop("Tamaño de paquete más grande que MTU y DF=1", iph);
72 // Fragmenta (de ser necesario)
73 int max_payload = r->iface->mtu - IPHeader::header_len();
74 int cant_frag = data.size() / max_payload;
75 if (data.size() % max_payload)
77 for (int i = 0; i < cant_frag; ++i)
80 if (i != (cant_frag - 1))
82 iph2.offset += i * max_payload;
83 iph2.total_len -= i * max_payload;
85 std::string buf((char*) &iph2, sizeof(IPHeader));
86 buf += data.substr(i * max_payload, max_payload);
88 std::cout << "IPOut::send: Fragmento 0 => IPHeader: " << iph2 << "\n";
89 std::string tmp = data.substr(i * max_payload, max_payload);
90 std::cout << "\tdata (" << tmp.size() << ") = " << tmp << "\n";
92 r->iface->transmit(buf, r->gateway ? r->gateway : IPAddr(iph.dst));
97 /// Obtiene un identificador para el paquete
98 uint16_t IPOut::get_id() const
103 /// Se fija si hay paquetes a forwardear (y devuelve cuantos hay)
104 unsigned IPOut::to_forward()
106 struct msqid_ds minfo;
107 msgctl(forward_que.que_id, IPC_STAT, &minfo);
108 return minfo.msg_qnum;
111 // vim: set et sw=4 sts=4 :