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 (" << ip << "): " << msg << "\n\tBuffer: " << buf
26 void IPOut::drop(const std::string& msg, const IPHeader& iph)
28 log << "IPOut::drop (" << ip << "): " << msg << "\n\tIPHeader: " << iph
32 /// Envía un paquete IP
33 bool IPOut::send(const std::string& data, uint8_t proto, IPAddr dst, IPAddr src,
34 bool df, uint8_t ttl, uint16_t id)
35 throw (std::runtime_error)
37 // Mando todo lo que tengo para forwardear
40 std::string buf = forward_que.receive();
43 std::cout << "IPOut::send (" << ip << "): A forwardear\n";
45 send(iph, buf.substr(iph.header_len()));
47 // Mando el paquete en sí
53 IPHeader iph(4, IPHeader::header_len() + data.size(), id, df, 0, 0,
54 ttl, proto, src, dst);
55 return send(iph, data);
58 /// Envía un paquete IP
59 bool IPOut::send(IPHeader iph, std::string data) throw (std::runtime_error)
62 RouteTable::Route* r = rtable.get(iph.dst);
65 drop("No existe una ruta para el destino", iph);
68 // No quieren fragmentar
69 if (iph.df && (IPHeader::header_len() + data.size() > r->iface->mtu))
71 drop("Tamaño de paquete más grande que MTU y DF=1", iph);
74 // Fragmenta (de ser necesario)
75 int max_payload = r->iface->mtu - IPHeader::header_len();
76 int cant_frag = data.size() / max_payload;
77 if (data.size() % max_payload)
79 for (int i = 0; i < cant_frag; ++i)
82 if (i != (cant_frag - 1))
84 iph2.offset += i * max_payload;
85 iph2.total_len -= i * max_payload;
87 std::string buf((char*) &iph2, sizeof(IPHeader));
88 buf += data.substr(i * max_payload, max_payload);
90 std::cout << "IPOut::send (" << ip << "): Fragmento " << i
91 << " => IPHeader: " << iph2 << "\n";
92 std::string tmp = data.substr(i * max_payload, max_payload);
93 std::cout << "\tdata (" << tmp.size() << ") = " << tmp << "\n";
95 r->iface->transmit(buf, r->gateway ? r->gateway : IPAddr(iph.dst));
100 /// Obtiene un identificador para el paquete
101 uint16_t IPOut::get_id() const
106 /// Se fija si hay paquetes a forwardear (y devuelve cuantos hay)
107 unsigned IPOut::to_forward()
109 struct msqid_ds minfo;
110 msgctl(forward_que.que_id, IPC_STAT, &minfo);
111 return minfo.msg_qnum;
114 // vim: set et sw=4 sts=4 :