1 #ifndef POSIXX_LINUX_TIPC_HPP_
2 #define POSIXX_LINUX_TIPC_HPP_
4 #include "../socket/basic_socket.hpp" // posixx::socket
6 #include <linux/tipc.h> // all tipc stuff
7 #include <cstring> // memcpy
13 /// Linux specific functionality
16 /// TIPC socket domain.
21 /// Configuration port name.
22 CFG_SRV = TIPC_CFG_SRV,
23 /// Topology discovery port name.
24 TOP_SRV = TIPC_TOP_SRV,
25 /// Name of the last reserved port.
26 RESERVED_TYPES = TIPC_RESERVED_TYPES,
27 /// Maximum message size.
28 MAX_USER_MSG_SIZE = TIPC_MAX_USER_MSG_SIZE,
29 /// Wait forever (for subscriptions).
30 WAIT_FOREVER = TIPC_WAIT_FOREVER
44 * Constructor from a raw 32-bit address.
46 * @param addr Raw 32-bit address.
48 explicit addr(__u32 addr) throw ();
51 * Constructor from a zone, cluster and node information.
54 * @param cluster Cluster.
57 addr(unsigned int zone, unsigned int cluster, unsigned int node)
61 void zone(unsigned int zone) throw ();
64 unsigned int zone() const throw ();
67 void cluster(unsigned int cluster) throw ();
70 unsigned int cluster() const throw ();
73 void node(unsigned int node) throw ();
76 unsigned int node() const throw ();
78 /// Cast to a raw 32-bit address.
79 operator __u32 () const throw ();
86 * @see tipc_portid struct from linux/tipc.h.
88 struct portid: tipc_portid
94 * @param ref Unique ID fo the port in the address.
95 * @param node Node address.
97 portid(__u32 ref, addr node) throw ();
104 * @see tipc_name struct from linux/tipc.h.
106 struct name: tipc_name
111 * @param type Type of the named port.
112 * @param instance Instance of the named port.
114 name(__u32 type, __u32 instance) throw ();
121 * @see tipc_name_seq struct from linux/tipc.h.
123 struct nameseq: tipc_name_seq
128 * @param type Type of the sequence.
129 * @param lower Lower bound.
130 * @param upper Upper bound.
132 nameseq(__u32 type, __u32 lower, __u32 upper) throw ();
136 * @param type Type of the sequence.
137 * @param instance Lower and upper bound.
139 nameseq(__u32 type, __u32 instance) throw ();
143 * Multicast port sequence.
145 * It's a port sequence just like nameseq used to simplify the use of the
149 * @see Estructura tipc_name_seq de linux/tipc.h
151 struct multicast: tipc_name_seq
156 * @param type Type of the multicast sequence.
157 * @param lower Lower bound.
158 * @param upper Upper bound.
160 multicast(__u32 type, __u32 lower, __u32 upper) throw ();
164 * @param type Type of the multicast sequence.
165 * @param instance Lower and upper bound.
167 multicast(__u32 type, __u32 instance) throw ();
171 * Reasons for returned messages when recvmsg() is used.
173 * @see recvmsg(2) of TIPC documentation
177 /// Not a returned message.
179 /// Port name doesn't exist.
180 ERR_NO_NAME = TIPC_ERR_NO_NAME,
181 /// Port ID doesn't exist.
182 ERR_NO_PORT = TIPC_ERR_NO_PORT,
183 /// Node doesn't exist.
184 ERR_NO_NODE = TIPC_ERR_NO_NODE,
185 /// Reception queue is full.
186 ERR_OVERLOAD = TIPC_ERR_OVERLOAD,
187 /// Connection shutted down.
188 CONN_SHUTDOWN = TIPC_CONN_SHUTDOWN
192 * Subscription filter type.
194 * @see TIPC documentation: 1.4.3 Name Subscriptions
199 * Causes the topology service to generate a event::PUBLISHED event
200 * for each port name or port name sequence it finds that overlaps
201 * the specified port name sequence; a TIPC_WITHDRAWN event is issued
202 * each time a previously reported name becomes unavailable.
204 * Allows the topology service to inform the application if there
205 * are @b any ports of interest.
207 SUB_PORTS = TIPC_SUB_PORTS,
209 * Causes the topology service to generate a single publish event for
210 * the first port it finds with an overlapping name and a single
211 * withdraw event when the last such port becomes unavailable.
213 * Allows the topology service to inform the application about
214 * @b all ports of interest.
216 SUB_SERVICE = TIPC_SUB_SERVICE
220 * Subscription request message.
222 * @see TIPC documentation: 1.4.3 Name Subscriptions
224 struct subscription: tipc_subscr
229 * @param seq The port name sequence of interest to the application.
230 * @param timeout A subscription timeout value, in ms
232 * @param filter An event filter specifying which events are of
233 * interest to the application (see subscription_t).
234 * @param usr_handle An 8 byte user handle that is application-defined.
236 subscription(nameseq seq, __u32 timeout, __u32 filter,
237 const char* usr_handle = "") throw ();
243 * @see TIPC documentation: 1.4.3 Name Subscriptions
245 struct event: tipc_event
250 /// The port has been published.
251 PUBLISHED = TIPC_PUBLISHED,
252 /// The port has been withdrawn.
253 WITHDRAWN = TIPC_WITHDRAWN,
254 /// The event has timed out.
255 TIMEOUT = TIPC_SUBSCR_TIMEOUT
260 * TIPC socket address (name).
262 * @see bind(2), Socket::bind()
263 * @see connect(2), Socket::connect()
264 * @see getsockaddr(2), Socket::getsockaddr()
265 * @see setsockaddr(2), Socket::setsockaddr()
266 * @see accept(2), Socket::accept()
267 * @see sendto(2), Socket::send()
268 * @see recvfrom(2), Socket::recv()
270 struct sockaddr: sockaddr_tipc
273 /// Type of TIPC address
276 ID = TIPC_ADDR_ID, ///< Port ID
277 NAME = TIPC_ADDR_NAME, ///< Port name
278 NAMESEQ = TIPC_ADDR_NAMESEQ ///< Name sequence or multicast
284 * @see TIPC documentation: 2.1.2 bind
288 ZONE = TIPC_ZONE_SCOPE, ///< Zone scope.
289 CLUSTER = TIPC_CLUSTER_SCOPE, ///< Cluster scope.
290 NODE = TIPC_NODE_SCOPE ///< Node scope.
297 * Constructor using a port ID.
299 * @param port Port ID.
300 * @param scope Bind scope.
302 sockaddr(portid port, scope_t scope = ZONE) throw ();
305 * Constructor using a port name.
307 * @param name Port name.
308 * @param scope Bind scope.
309 * @param domain Domain lookup.
311 * @see Documentación de TIPC: 1.4.1 Address Resolution
313 sockaddr(name name, scope_t scope = ZONE,
314 tipc::addr domain = tipc::addr(0u))
318 * Constructor using a port name sequence.
320 * @param nameseq Port name sequence.
321 * @param scope Bind scope.
323 sockaddr(nameseq nameseq, scope_t scope = ZONE) throw ();
326 * Constructor using a multicast port name sequence.
328 * @param mcast Multicast port name sequence.
329 * @param scope Bind scope.
331 sockaddr(multicast mcast, scope_t scope = ZONE) throw ();
333 /// Type of TIPC address
334 type_t type() const throw ();
336 /// Length of this unix socket address
337 socklen_t length() const throw ();
339 /// Compare two TIPC socket addresses
340 bool operator == (const sockaddr& other) const throw ();
344 /// TIPC socket traits.
348 /// Socket address type.
349 typedef tipc::sockaddr sockaddr;
352 enum { PF = PF_TIPC };
357 typedef posixx::socket::basic_socket< traits > socket;
359 } } } // namespace posixx::linux::tipc
363 posixx::linux::tipc::addr::addr() throw (): address(0u)
368 posixx::linux::tipc::addr::addr(__u32 address) throw (): address(address)
373 posixx::linux::tipc::addr::addr(unsigned int zone, unsigned int cluster,
374 unsigned int node) throw ():
375 address(tipc_addr(zone, cluster, node))
380 void posixx::linux::tipc::addr::zone(unsigned int zone) throw ()
382 address = tipc_addr(zone, cluster(), node());
386 unsigned int posixx::linux::tipc::addr::zone() const throw ()
388 return tipc_zone(address);
392 void posixx::linux::tipc::addr::cluster(unsigned int cluster) throw ()
394 address = tipc_addr(zone(), cluster, node());
398 unsigned int posixx::linux::tipc::addr::cluster() const throw ()
400 return tipc_cluster(address);
404 void posixx::linux::tipc::addr::node(unsigned int node) throw ()
406 address = tipc_addr(zone(), cluster(), node);
410 unsigned int posixx::linux::tipc::addr::node() const throw ()
412 return tipc_node(address);
416 posixx::linux::tipc::addr::operator __u32 () const throw ()
422 posixx::linux::tipc::portid::portid(__u32 r, addr n) throw ()
429 posixx::linux::tipc::name::name(__u32 t, __u32 i) throw ()
436 posixx::linux::tipc::nameseq::nameseq(__u32 t, __u32 low, __u32 up) throw ()
444 posixx::linux::tipc::nameseq::nameseq(__u32 t, __u32 instance) throw ()
452 posixx::linux::tipc::multicast::multicast(__u32 t, __u32 low, __u32 up)
461 posixx::linux::tipc::multicast::multicast(__u32 t, __u32 instance) throw ()
469 posixx::linux::tipc::subscription::subscription(nameseq s, __u32 t, __u32 f,
470 const char* uh) throw ()
475 std::memcpy(usr_handle, uh, sizeof(usr_handle));
479 posixx::linux::tipc::sockaddr::sockaddr() throw ()
484 posixx::linux::tipc::sockaddr::sockaddr(portid port, scope_t s) throw ()
487 addrtype = TIPC_ADDR_ID;
493 posixx::linux::tipc::sockaddr::sockaddr(name name, scope_t s, tipc::addr d)
497 addrtype = TIPC_ADDR_NAME;
499 addr.name.name = name;
500 addr.name.domain = d;
504 posixx::linux::tipc::sockaddr::sockaddr(nameseq nameseq, scope_t s) throw ()
507 addrtype = TIPC_ADDR_NAMESEQ;
509 addr.nameseq = nameseq;
513 posixx::linux::tipc::sockaddr::sockaddr(multicast mcast, scope_t s) throw ()
516 addrtype = TIPC_ADDR_MCAST;
518 addr.nameseq = mcast;
522 posixx::linux::tipc::sockaddr::type_t posixx::linux::tipc::sockaddr::type()
525 return static_cast< type_t >(addrtype);
529 socklen_t posixx::linux::tipc::sockaddr::length() const throw ()
531 return sizeof(sockaddr_tipc);
535 bool posixx::linux::tipc::sockaddr::operator == (const sockaddr& other) const
538 return !memcmp(this, &other, sizeof(*this));
541 #endif // POSIXX_LINUX_TIPC_HPP_