1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Authors: Ryan Lortie <desrt@desrt.ca>
16 * SECTION: gsocketcontrolmessage
17 * @title: GSocketControlMessage
18 * @short_description: A GSocket control message
19 * @see_also: #GSocket.
21 * A #GSocketControlMessage is a special-purpose utility message that
22 * can be sent to or received from a #GSocket. These types of
23 * messages are often called "ancillary data".
25 * The message can represent some sort of special instruction to or
26 * information from the socket or can represent a special kind of
27 * transfer to the peer (for example, sending a file description over
30 * These messages are sent with g_socket_send_message() and received
31 * with g_socket_receive_message().
33 * To extend the set of control message that can be sent, subclass this
34 * class and override the get_size, get_level, get_type and serialize
37 * To extend the set of control messages that can be received, subclass
38 * this class and implement the deserialize method. Also, make sure your
39 * class is registered with the GType typesystem before calling
40 * g_socket_receive_message() to read such a message.
46 #include "gsocketcontrolmessage.h"
50 #include "gunixfdmessage.h"
51 #include <sys/socket.h>
53 # include <winsock2.h>
59 G_DEFINE_ABSTRACT_TYPE (GSocketControlMessage,
60 g_socket_control_message,
64 * g_socket_control_message_get_size:
65 * @message: a #GSocketControlMessage
67 * Returns the space required for the control message, not including
68 * headers or alignment.
70 * Returns: The number of bytes required.
75 g_socket_control_message_get_size (GSocketControlMessage *message)
77 g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
79 return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_size (message);
83 * g_socket_control_message_get_level:
84 * @message: a #GSocketControlMessage
86 * Returns the "level" (i.e. the originating protocol) of the control message.
87 * This is often SOL_SOCKET.
89 * Returns: an integer describing the level
94 g_socket_control_message_get_level (GSocketControlMessage *message)
96 g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
98 return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_level (message);
102 * g_socket_control_message_get_msg_type:
103 * @message: a #GSocketControlMessage
105 * Returns the protocol specific type of the control message.
106 * For instance, for UNIX fd passing this would be SCM_RIGHTS.
108 * Returns: an integer describing the type of control message
113 g_socket_control_message_get_msg_type (GSocketControlMessage *message)
115 g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
117 return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_type (message);
121 * g_socket_control_message_serialize:
122 * @message: a #GSocketControlMessage
123 * @data: A buffer to write data to
125 * Converts the data in the message to bytes placed in the
128 * @data is guaranteed to have enough space to fit the size
129 * returned by g_socket_control_message_get_size() on this
135 g_socket_control_message_serialize (GSocketControlMessage *message,
138 g_return_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message));
140 G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->serialize (message, data);
145 g_socket_control_message_init (GSocketControlMessage *message)
150 g_socket_control_message_class_init (GSocketControlMessageClass *class)
155 * g_socket_control_message_deserialize:
156 * @level: a socket level
157 * @type: a socket control message type for the given @level
158 * @size: the size of the data in bytes
159 * @data: pointer to the message data
161 * Tries to deserialize a socket control message of a given
162 * @level and @type. This will ask all known (to GType) subclasses
163 * of #GSocketControlMessage if they can understand this kind
164 * of message and if so deserialize it into a #GSocketControlMessage.
166 * If there is no implementation for this kind of control message, %NULL
169 * Returns: the deserialized message or %NULL
173 GSocketControlMessage *
174 g_socket_control_message_deserialize (int level,
179 GSocketControlMessageClass *klass;
180 GSocketControlMessage *message;
181 GType *message_types;
182 guint n_message_types;
185 volatile GType a_type;
188 /* Ensure we know about the built in types */
190 a_type = g_unix_fd_message_get_type ();
193 message_types = g_type_children (G_TYPE_SOCKET_CONTROL_MESSAGE, &n_message_types);
196 for (i = 0; i < n_message_types; i++)
198 klass = (GSocketControlMessageClass *)g_type_class_ref (type);
200 if (klass && klass->deserialize)
202 message = klass->deserialize (level, type, size, data);
203 g_type_class_unref ((GTypeClass *) klass);
210 g_free (message_types);
213 g_warning ("unknown control message type %d:%d", level, type);
218 #define __G_SOCKET_CONTROL_MESSAGE_C__
219 #include "gioaliasdef.c"