implementanción a través de colas a otra clase (DevQue).
# Para valgrind o debug
CFLAGS += -ggdb -DDEBUG
+# Para más verbose
+CFLAGS += -DDEBUG2
+
# Opciones para el compilador C++.
CXXFLAGS = $(CFLAGS) -fno-inline
tests: depend $(tests)
-test_send: test_send.o dev.o
+test_send: test_send.o devque.o
-test_recv: test_recv.o dev.o
+test_recv: test_recv.o devque.o
test_ipaddr: test_ipaddr.o ipaddr.o ipheader.o
-test_ipin: test_ipin.o ipin.o ipaddr.o ipheader.o dev.o
+test_ipin: test_ipin.o ipin.o ipaddr.o ipheader.o devque.o
-test_ipout: test_ipout.o ipout.o ipaddr.o ipheader.o dev.o routetable.o
+test_ipout: test_ipout.o ipout.o ipaddr.o ipheader.o devque.o routetable.o
-ip: ip.o ipout.o ipin.o ipaddr.o ipheader.o dev.o routetable.o
+ip: ip.o ipout.o ipin.o ipaddr.o ipheader.o devque.o routetable.o
depend:
@makedepend $(fuentes) > /dev/null 2>&1
# DO NOT DELETE
-dev.o: dev.h /usr/include/unistd.h /usr/include/features.h
-dev.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-dev.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
-dev.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
-dev.o: /usr/include/bits/confname.h /usr/include/getopt.h
-dev.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
-dev.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/endian.h
-dev.o: /usr/include/bits/endian.h /usr/include/sys/select.h
-dev.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
-dev.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
-dev.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
-dev.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
-dev.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h /usr/include/bits/msq.h
+devque.o: devque.h dev.h /usr/include/unistd.h /usr/include/features.h
+devque.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
+devque.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
+devque.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
+devque.o: /usr/include/bits/confname.h /usr/include/getopt.h
+devque.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
+devque.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/endian.h
+devque.o: /usr/include/bits/endian.h /usr/include/sys/select.h
+devque.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
+devque.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
+devque.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
+devque.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
+devque.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h
+devque.o: /usr/include/bits/msq.h
ip.o: ipout.h ipaddr.h ipheader.h /usr/include/stdint.h
ip.o: /usr/include/features.h /usr/include/sys/cdefs.h
ip.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h
-ip.o: /usr/include/bits/wordsize.h routetable.h dev.h ipin.h
+ip.o: /usr/include/bits/wordsize.h routetable.h dev.h ipin.h devque.h
ip.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h
ip.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h
ip.o: /usr/include/bits/confname.h /usr/include/getopt.h /usr/include/fcntl.h
test_ipin.o: ipin.h ipaddr.h ipheader.h /usr/include/stdint.h
test_ipin.o: /usr/include/features.h /usr/include/sys/cdefs.h
test_ipin.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h
-test_ipin.o: /usr/include/bits/wordsize.h dev.h /usr/include/unistd.h
-test_ipin.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
-test_ipin.o: /usr/include/bits/typesizes.h /usr/include/bits/confname.h
-test_ipin.o: /usr/include/getopt.h /usr/include/fcntl.h
-test_ipin.o: /usr/include/bits/fcntl.h /usr/include/sys/types.h
-test_ipin.o: /usr/include/time.h /usr/include/endian.h
-test_ipin.o: /usr/include/bits/endian.h /usr/include/sys/select.h
-test_ipin.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
-test_ipin.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
-test_ipin.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
-test_ipin.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
-test_ipin.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h
-test_ipin.o: /usr/include/bits/msq.h
+test_ipin.o: /usr/include/bits/wordsize.h dev.h devque.h
+test_ipin.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h
+test_ipin.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h
+test_ipin.o: /usr/include/bits/confname.h /usr/include/getopt.h
+test_ipin.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
+test_ipin.o: /usr/include/sys/types.h /usr/include/time.h
+test_ipin.o: /usr/include/endian.h /usr/include/bits/endian.h
+test_ipin.o: /usr/include/sys/select.h /usr/include/bits/select.h
+test_ipin.o: /usr/include/bits/sigset.h /usr/include/bits/time.h
+test_ipin.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h
+test_ipin.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h
+test_ipin.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h
+test_ipin.o: /usr/include/sys/msg.h /usr/include/bits/msq.h
test_ipout.o: ipout.h ipaddr.h ipheader.h /usr/include/stdint.h
test_ipout.o: /usr/include/features.h /usr/include/sys/cdefs.h
test_ipout.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h
-test_ipout.o: /usr/include/bits/wordsize.h routetable.h dev.h
+test_ipout.o: /usr/include/bits/wordsize.h routetable.h dev.h devque.h
test_ipout.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h
test_ipout.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h
test_ipout.o: /usr/include/bits/confname.h /usr/include/getopt.h
test_ipout.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h
test_ipout.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h
test_ipout.o: /usr/include/sys/msg.h /usr/include/bits/msq.h
-test_recv.o: dev.h /usr/include/unistd.h /usr/include/features.h
+test_recv.o: devque.h dev.h /usr/include/unistd.h /usr/include/features.h
test_recv.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
test_recv.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
test_recv.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
test_recv.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h
test_recv.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h
test_recv.o: /usr/include/sys/msg.h /usr/include/bits/msq.h
-test_send.o: dev.h /usr/include/unistd.h /usr/include/features.h
+test_send.o: devque.h dev.h /usr/include/unistd.h /usr/include/features.h
test_send.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
test_send.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
test_send.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
#include <stdexcept>
-#define DEV_DEFAULT_KEY 0x1abcdef1
#define DEV_MAX_MTU 1500
/// Dispositivo de red (capa de enlace)
/// MTU
size_t mtu;
- /// Identificador de la cola a usar
- int que_id;
-
/// Constructor
- Dev(mac_type mac, key_t key = DEV_DEFAULT_KEY, size_t mtu = DEV_MAX_MTU)
- throw (std::runtime_error, std::logic_error);
+ Dev(mac_type mac, size_t mtu = DEV_MAX_MTU)
+ throw (std::runtime_error, std::logic_error):
+ mac(mac), mtu(mtu)
+ {
+ if (mtu > DEV_MAX_MTU)
+ throw std::logic_error("MTU más grande que DEV_MAX_MTU");
+ }
/// Envía un frame
- void transmit(const std::string& data, const mac_type& mac)
- throw (std::runtime_error, std::logic_error);
+ virtual void transmit(const std::string& data, const mac_type& mac)
+ throw (std::runtime_error, std::logic_error) = 0;
/// Recibe un frame
- std::string receive()
- throw (std::runtime_error);
+ virtual std::string receive()
+ throw (std::runtime_error) = 0;
+
+ /// Destructor virtual por si las moscas
+ virtual ~Dev() {}
// Nada de andar copiando placas...
private:
-#include "dev.h"
+#include "devque.h"
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
char frame[1];
};
-Dev::Dev(mac_type mac, key_t key, size_t mtu)
+DevQue::DevQue(mac_type mac, key_t key, size_t mtu)
throw (std::runtime_error, std::logic_error):
- mac(mac), mtu(mtu)
+ Dev(mac, mtu)
{
- if (mtu > DEV_MAX_MTU)
- throw std::logic_error("MTU más grande que DEV_MAX_MTU");
que_id = msgget(key, 0666); // Debe estar previamente creada
if (que_id == -1)
throw std::runtime_error("No se pudo crear la cola");
}
-void Dev::transmit(const std::string& data, const mac_type& mac)
+void DevQue::transmit(const std::string& data, const mac_type& mac)
throw (std::runtime_error, std::logic_error)
{
if (data.size() > mtu)
f->size = data.size();
memcpy(f->frame, data.c_str(), data.size());
int res = msgsnd(que_id, f, mtu + sizeof(size_t), 0);
-#ifdef DEBUG
- //std::cout << "Dev::transmit(msgtype/mac = " << f->mac << ", size = "
- // << f->size << ")\n";
+#ifdef DEBUG2
+ std::cout << "DevQue::transmit(msgtype/mac = " << f->mac << ", size = "
+ << f->size << ")\n";
#endif
free(f);
if (res == -1)
throw std::runtime_error("Error al poner en la cola");
}
-std::string Dev::receive() throw (std::runtime_error)
+std::string DevQue::receive() throw (std::runtime_error)
{
Frame* f = (Frame*) malloc(sizeof(Frame) + mtu);
if (!f)
}
std::string s((char*) f->frame, f->size);
free(f);
-#ifdef DEBUG
- //std::cout << "Dev::receive(msgtype/mac = " << mac << ", size = "
- // << s.size() << ")\n";
+#ifdef DEBUG2
+ std::cout << "DevQue::receive(msgtype/mac = " << mac << ", size = "
+ << s.size() << ")\n";
#endif
return s;
}
--- /dev/null
+#ifndef _DEVQUE_H_
+#define _DEVQUE_H_
+
+#include "dev.h"
+
+#define DEVQUE_DEFAULT_KEY 0x1abcdef1
+
+/// Dispositivo de red (capa de enlace) implementado con una cola
+struct DevQue: Dev
+{
+
+ /// Identificador de la cola a usar
+ int que_id;
+
+ /// Constructor
+ DevQue(mac_type mac, key_t key = DEVQUE_DEFAULT_KEY,
+ size_t mtu = DEV_MAX_MTU)
+ throw (std::runtime_error, std::logic_error);
+
+ /// Envía un frame
+ void transmit(const std::string& data, const mac_type& mac)
+ throw (std::runtime_error, std::logic_error);
+
+ /// Recibe un frame
+ std::string receive()
+ throw (std::runtime_error);
+
+};
+
+#endif // _DEVQUE_H_
+
+// vim: set et sw=4 sts=4 :
#include "ipin.h"
#include "ipaddr.h"
#include "routetable.h"
-#include "dev.h"
+#include "devque.h"
#include <iostream>
#include <fstream>
#include <sstream>
bool router = false;
bool forward = false;
unsigned proto = 0;
- key_t queue_id = DEV_DEFAULT_KEY;
+ key_t queue_id = DEVQUE_DEFAULT_KEY;
std::string fname = "route.txt";
if (argc < 2)
{
proto = atoi(argv[6]);
// Creo colas
int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1);
- que_id = msgget(DEV_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1);
+ que_id = msgget(DEVQUE_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1);
// Abro archivo con rutas
std::ifstream ifs(fname.c_str()); assert(ifs);
// Creo medio físico y cola para forwarding
- Dev dev(addr, queue_id);
- Dev fwque(addr, DEV_DEFAULT_KEY-1);
+ DevQue dev(addr, queue_id);
+ DevQue fwque(addr, DEVQUE_DEFAULT_KEY-1);
// Creo procesos
pid_t pid_send = fork();
if (pid_send == -1)
#include "ipin.h"
#include "ipaddr.h"
-#include "dev.h"
+#include "devque.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
bool router = false;
bool forward = false;
unsigned proto = 0;
- key_t queue_id = DEV_DEFAULT_KEY;
+ key_t queue_id = DEVQUE_DEFAULT_KEY;
if (argc > 1)
addr = IPAddr(argv[1]);
if (argc > 2)
queue_id = atoi(argv[6]);
int que_id = msgget(queue_id, IPC_CREAT | 0666);
assert(que_id != -1);
- Dev dev(addr, mtu, queue_id);
+ DevQue dev(addr, mtu, queue_id);
que_id = msgget(queue_id+1, IPC_CREAT | 0666);
assert(que_id != -1);
- Dev fwque(addr, DEV_MAX_MTU, queue_id+1);
+ DevQue fwque(addr, DEV_MAX_MTU, queue_id+1);
IPIn ipin(addr, dev, fwque, router, forward, std::cerr);
struct msqid_ds minfo;
for (msgctl(dev.que_id, IPC_STAT, &minfo); minfo.msg_qnum;
#include "ipout.h"
#include "ipaddr.h"
#include "routetable.h"
-#include "dev.h"
+#include "devque.h"
#include <iostream>
#include <fstream>
#include <sstream>
// Uso: ./test_ipout ip dst mtu routes_file proto queue_id
-void add_routes(RouteTable& rt, std::istream& is, Dev& dev);
+void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev);
int main(int argc, char* argv[])
{
IPAddr dst("10.10.10.1");
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]);
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);
- Dev dev(addr, mtu, queue_id);
- Dev fwque(addr, DEV_MAX_MTU, queue_id+1);
+ DevQue dev(addr, mtu, queue_id);
+ DevQue fwque(addr, DEV_MAX_MTU, queue_id+1);
RouteTable table(dev);
- add_routes(table, ifs, dev);
+ add_routes(table, ifs, mtu, dev);
IPOut ipout(addr, table, fwque, std::cerr);
std::string msg;
while (std::getline(std::cin, msg))
return 0;
}
-void add_routes(RouteTable& rt, std::istream& is, Dev& dev)
+void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev)
{
std::string line;
while (std::getline(is, line))
iss >> net >> gw >> metric;
if (net == "0") net = "0.0.0.0";
if (gw == "0") gw = "0.0.0.0";
- rt.add(net.c_str(), gw.c_str(), metric, dev);
+ rt.add(net.c_str(), gw.c_str(), mtu, metric, dev);
}
}
-#include "dev.h"
+#include "devque.h"
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
int main()
{
- Dev dev(4321);
+ DevQue dev(4321);
struct msqid_ds minfo;
for (msgctl(dev.que_id, IPC_STAT, &minfo); minfo.msg_qnum;
msgctl(dev.que_id, IPC_STAT, &minfo))
-#include "dev.h"
+#include "devque.h"
#include <iostream>
#include <cassert>
#include <unistd.h>
int main()
{
- int que_id = msgget(DEV_DEFAULT_KEY, IPC_CREAT | 0666);
+ int que_id = msgget(DEVQUE_DEFAULT_KEY, IPC_CREAT | 0666);
assert(que_id != -1);
- Dev dev(1234);
+ DevQue dev(1234);
dev.transmit("hola mundo", 4321);
std::cout << "Enviado 'hola mundo' a 4321\n";
return 0;