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);
66 drop("No existe una ruta para el destino -> ICMP", iph);
69 // No quieren fragmentar
70 if (iph.df && (IPHeader::header_len() + data.size() > r->iface->mtu))
73 drop("Tamaño de paquete más grande que MTU y DF=1", iph);
76 // Fragmenta (de ser necesario)
77 int max_payload = r->iface->mtu - IPHeader::header_len();
78 int cant_frag = data.size() / max_payload;
79 if (data.size() % max_payload)
81 for (int i = 0; i < cant_frag; ++i)
84 if (i != (cant_frag - 1))
86 iph2.offset += i * max_payload;
87 iph2.total_len -= i * max_payload;
89 std::string buf((char*) &iph2, sizeof(IPHeader));
90 buf += data.substr(i * max_payload, max_payload);
92 std::cout << "IPOut::send (" << ip << "): Fragmento " << i
93 << " => IPHeader: " << iph2 << "\n";
94 std::string tmp = data.substr(i * max_payload, max_payload);
95 std::cout << "\tdata (" << tmp.size() << ") = " << tmp << "\n";
97 r->iface->transmit(buf, r->gateway ? r->gateway : IPAddr(iph.dst));
102 /// Obtiene un identificador para el paquete
103 uint16_t IPOut::get_id() const
108 /// Se fija si hay paquetes a forwardear (y devuelve cuantos hay)
109 unsigned IPOut::to_forward()
111 struct msqid_ds minfo;
112 msgctl(forward_que.que_id, IPC_STAT, &minfo);
113 return minfo.msg_qnum;
116 // vim: set et sw=4 sts=4 :