/*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
* Copyright (C) 2005-2010 Martin Willi
* Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005 Jan Hutter
#define SOCKET_H_
typedef struct socket_t socket_t;
+typedef enum socket_family_t socket_family_t;
#include <library.h>
#include <networking/packet.h>
typedef socket_t *(*socket_constructor_t)();
/**
+ * Address families supported by socket implementations.
+ */
+enum socket_family_t {
+ /**
+ * No address families supported
+ */
+ SOCKET_FAMILY_NONE = 0,
+
+ /**
+ * IPv4
+ */
+ SOCKET_FAMILY_IPV4 = (1 << 0),
+
+ /**
+ * IPv6
+ */
+ SOCKET_FAMILY_IPV6 = (1 << 1),
+
+ /**
+ * Both address families supported
+ */
+ SOCKET_FAMILY_BOTH = (1 << 2) - 1,
+};
+
+/**
* Socket interface definition.
*/
struct socket_t {
* - SUCCESS when packet successfully received
* - FAILED when unable to receive
*/
- status_t (*receive) (socket_t *this, packet_t **packet);
+ status_t (*receive)(socket_t *this, packet_t **packet);
/**
* Send a packet.
* - SUCCESS when packet successfully sent
* - FAILED when unable to send
*/
- status_t (*send) (socket_t *this, packet_t *packet);
+ status_t (*send)(socket_t *this, packet_t *packet);
/**
* Get the port this socket is listening on.
* @param nat_t TRUE to get the port used to float in case of NAT-T
* @return the port
*/
- u_int16_t (*get_port) (socket_t *this, bool nat_t);
+ u_int16_t (*get_port)(socket_t *this, bool nat_t);
+
+ /**
+ * Get the address families this socket is listening on.
+ *
+ * @return supported families
+ */
+ socket_family_t (*supported_families)(socket_t *this);
/**
* Destroy a socket implementation.
*/
- void (*destroy) (socket_t *this);
+ void (*destroy)(socket_t *this);
};
/**
return port;
}
+METHOD(socket_manager_t, supported_families, socket_family_t,
+ private_socket_manager_t *this)
+{
+ socket_family_t families = SOCKET_FAMILY_NONE;
+ this->lock->read_lock(this->lock);
+ if (this->socket)
+ {
+ families = this->socket->supported_families(this->socket);
+ }
+ this->lock->unlock(this->lock);
+ return families;
+}
+
static void create_socket(private_socket_manager_t *this)
{
socket_constructor_t create;
.send = _sender,
.receive = _receiver,
.get_port = _get_port,
+ .supported_families = _supported_families,
.add_socket = _add_socket,
.remove_socket = _remove_socket,
.destroy = _destroy,
/*
- * Copyright (C) 2010-2012 Tobias Brunner
+ * Copyright (C) 2010-2013 Tobias Brunner
* Hochschule fuer Technik Rapperswil
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 revosec AG
* - SUCCESS when packet successfully received
* - FAILED when unable to receive
*/
- status_t (*receive) (socket_manager_t *this, packet_t **packet);
+ status_t (*receive)(socket_manager_t *this, packet_t **packet);
/**
* Send a packet using the registered socket.
* - SUCCESS when packet successfully sent
* - FAILED when unable to send
*/
- status_t (*send) (socket_manager_t *this, packet_t *packet);
+ status_t (*send)(socket_manager_t *this, packet_t *packet);
/**
* Get the port the registered socket is listening on.
* @param nat_t TRUE to get the port used to float in case of NAT-T
* @return the port, or 0, if no socket is registered
*/
- u_int16_t (*get_port) (socket_manager_t *this, bool nat_t);
+ u_int16_t (*get_port)(socket_manager_t *this, bool nat_t);
+
+ /**
+ * Get the address families the registered socket is listening on.
+ *
+ * @return address families
+ */
+ socket_family_t (*supported_families)(socket_manager_t *this);
/**
* Register a socket constructor.
/*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
* Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005-2010 Martin Willi
* Copyright (C) 2005 Jan Hutter
return nat_t ? this->natt : this->port;
}
+METHOD(socket_t, supported_families, socket_family_t,
+ private_socket_default_socket_t *this)
+{
+ socket_family_t families = SOCKET_FAMILY_NONE;
+
+ if (this->ipv4 != -1 || this->ipv4_natt != -1)
+ {
+ families |= SOCKET_FAMILY_IPV4;
+ }
+ if (this->ipv6 != -1 || this->ipv6_natt != -1)
+ {
+ families |= SOCKET_FAMILY_IPV6;
+ }
+ return families;
+}
+
/**
* open a socket to send and receive packets
*/
.send = _sender,
.receive = _receiver,
.get_port = _get_port,
+ .supported_families = _supported_families,
.destroy = _destroy,
},
},
/*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
* Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005-2010 Martin Willi
* Copyright (C) 2005 Jan Hutter
return 0;
}
+METHOD(socket_t, supported_families, socket_family_t,
+ private_socket_dynamic_socket_t *this)
+{
+ /* we could return only the families of the opened sockets, but it could
+ * be that both families are supported even if no socket is yet open */
+ return SOCKET_FAMILY_BOTH;
+}
+
METHOD(socket_t, destroy, void,
private_socket_dynamic_socket_t *this)
{
.send = _sender,
.receive = _receiver,
.get_port = _get_port,
+ .supported_families = _supported_families,
.destroy = _destroy,
},
},