Moved data structures to new collections subfolder
[strongswan.git] / src / libcharon / network / socket.h
1 /*
2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup socket socket
21 * @{ @ingroup network
22 */
23
24 #ifndef SOCKET_H_
25 #define SOCKET_H_
26
27 typedef struct socket_t socket_t;
28
29 #include <library.h>
30 #include <networking/packet.h>
31 #include <collections/enumerator.h>
32 #include <plugins/plugin.h>
33
34 /**
35 * Constructor prototype for sockets.
36 */
37 typedef socket_t *(*socket_constructor_t)();
38
39 /**
40 * Socket interface definition.
41 */
42 struct socket_t {
43
44 /**
45 * Receive a packet.
46 *
47 * Reads a packet from the socket and sets source/dest
48 * appropriately.
49 *
50 * @param packet pinter gets address from allocated packet_t
51 * @return
52 * - SUCCESS when packet successfully received
53 * - FAILED when unable to receive
54 */
55 status_t (*receive) (socket_t *this, packet_t **packet);
56
57 /**
58 * Send a packet.
59 *
60 * Sends a packet to the net using source and destination addresses of
61 * the packet.
62 *
63 * @param packet packet_t to send
64 * @return
65 * - SUCCESS when packet successfully sent
66 * - FAILED when unable to send
67 */
68 status_t (*send) (socket_t *this, packet_t *packet);
69
70 /**
71 * Get the port this socket is listening on.
72 *
73 * @param nat_t TRUE to get the port used to float in case of NAT-T
74 * @return the port
75 */
76 u_int16_t (*get_port) (socket_t *this, bool nat_t);
77
78 /**
79 * Destroy a socket implementation.
80 */
81 void (*destroy) (socket_t *this);
82 };
83
84 /**
85 * Helper function to (un-)register socket interfaces from plugin features.
86 *
87 * This function is a plugin_feature_callback_t and can be used with the
88 * PLUGIN_CALLBACK macro to register an socket interface constructor.
89 *
90 * @param plugin plugin registering the socket interface
91 * @param feature associated plugin feature
92 * @param reg TRUE to register, FALSE to unregister
93 * @param data data passed to callback, a socket_constructor_t
94 */
95 bool socket_register(plugin_t *plugin, plugin_feature_t *feature,
96 bool reg, void *data);
97
98 #endif /** SOCKET_H_ @}*/