| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitchannelsocket.h" |
| 9 |
|
| 10 |
#include "cockpitchannel.h" |
| 11 |
#include "cockpitflow.h" |
| 12 |
|
| 13 |
#include "websocket.h" |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#define COCKPIT_TYPE_CHANNEL_SOCKET (cockpit_channel_socket_get_type ()) |
| 18 |
#define COCKPIT_CHANNEL_SOCKET(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), COCKPIT_TYPE_CHANNEL_SOCKET, CockpitChannelSocket)) |
| 19 |
#define COCKPIT_IS_CHANNEL_SOCKET(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), COCKPIT_TYPE_CHANNEL_SOCKET)) |
| 20 |
|
| 21 |
typedef struct { |
| 22 |
CockpitChannel parent; |
| 23 |
gboolean closed; |
| 24 |
|
| 25 |
/* The WebSocket side of things */ |
| 26 |
WebSocketConnection *socket; |
| 27 |
WebSocketDataType data_type; |
| 28 |
gulong socket_open; |
| 29 |
gulong socket_message; |
| 30 |
gulong socket_close; |
| 31 |
} CockpitChannelSocket; |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
CockpitChannelClass parent; |
| 35 |
} CockpitChannelSocketClass; |
| 36 |
|
| 37 |
GType cockpit_channel_socket_get_type (void); |
| 38 |
|
| 39 |
G_DEFINE_TYPE (CockpitChannelSocket, cockpit_channel_socket, COCKPIT_TYPE_CHANNEL); |
| 40 |
|
| 41 |
static void |
| 42 |
cockpit_channel_socket_init (CockpitChannelSocket *self) |
| 43 |
{ |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
cockpit_channel_socket_recv (CockpitChannel *channel, |
| 49 |
GBytes *payload) |
| 50 |
{ |
| 51 |
CockpitChannelSocket *self = COCKPIT_CHANNEL_SOCKET (channel); |
| 52 |
|
| 53 |
if (web_socket_connection_get_ready_state (self->socket) == WEB_SOCKET_STATE_OPEN) |
| 54 |
web_socket_connection_send (self->socket, self->data_type, NULL, payload); |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
cockpit_channel_socket_finalize (GObject *object) |
| 59 |
{ |
| 60 |
CockpitChannelSocket *self = COCKPIT_CHANNEL_SOCKET (object); |
| 61 |
|
| 62 |
g_signal_handler_disconnect (self->socket, self->socket_open); |
| 63 |
g_signal_handler_disconnect (self->socket, self->socket_message); |
| 64 |
g_signal_handler_disconnect (self->socket, self->socket_close); |
| 65 |
g_object_unref (self->socket); |
| 66 |
|
| 67 |
G_OBJECT_CLASS (cockpit_channel_socket_parent_class)->finalize (object); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
cockpit_channel_socket_close (CockpitChannel *channel, |
| 72 |
const gchar *problem) |
| 73 |
{ |
| 74 |
CockpitChannelSocket *self = COCKPIT_CHANNEL_SOCKET (channel); |
| 75 |
gushort code; |
| 76 |
|
| 77 |
self->closed = TRUE; |
| 78 |
|
| 79 |
if (web_socket_connection_get_ready_state (self->socket) < WEB_SOCKET_STATE_CLOSING) |
| 80 |
{ |
| 81 |
if (problem) |
| 82 |
code = WEB_SOCKET_CLOSE_GOING_AWAY; |
| 83 |
else |
| 84 |
code = WEB_SOCKET_CLOSE_NORMAL; |
| 85 |
web_socket_connection_close (self->socket, code, problem); |
| 86 |
} |
| 87 |
|
| 88 |
COCKPIT_CHANNEL_CLASS(cockpit_channel_socket_parent_class)->close (channel, problem); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
on_socket_open (WebSocketConnection *connection, |
| 93 |
gpointer user_data) |
| 94 |
{ |
| 95 |
CockpitChannel *channel = COCKPIT_CHANNEL (user_data); |
| 96 |
JsonObject *open; |
| 97 |
|
| 98 |
/* |
| 99 |
* Actually open the channel. We wait until the WebSocket is open |
| 100 |
* before doing this, so we don't receive messages from the bridge |
| 101 |
* before the websocket is open. |
| 102 |
*/ |
| 103 |
|
| 104 |
open = cockpit_channel_get_options (channel); |
| 105 |
cockpit_channel_control (channel, "open", open); |
| 106 |
|
| 107 |
/* Tell the channel we're ready */ |
| 108 |
cockpit_channel_ready (channel, NULL); |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
on_socket_message (WebSocketConnection *connection, |
| 113 |
WebSocketDataType data_type, |
| 114 |
GBytes *payload, |
| 115 |
gpointer user_data) |
| 116 |
{ |
| 117 |
CockpitChannel *channel = COCKPIT_CHANNEL (user_data); |
| 118 |
cockpit_channel_send (channel, payload, data_type == WEB_SOCKET_DATA_TEXT); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
on_socket_close (WebSocketConnection *socket, |
| 123 |
gpointer user_data) |
| 124 |
{ |
| 125 |
CockpitChannelSocket *self = COCKPIT_CHANNEL_SOCKET (user_data); |
| 126 |
CockpitChannel *channel = COCKPIT_CHANNEL (user_data); |
| 127 |
const gchar *problem = NULL; |
| 128 |
gushort code; |
| 129 |
|
| 130 |
if (self->closed) |
| 131 |
return; |
| 132 |
|
| 133 |
code = web_socket_connection_get_close_code (socket); |
| 134 |
if (code == WEB_SOCKET_CLOSE_NORMAL) |
| 135 |
{ |
| 136 |
cockpit_channel_control (channel, "done", NULL); |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
problem = web_socket_connection_get_close_data (socket); |
| 141 |
if (problem == NULL) |
| 142 |
problem = "disconnected"; |
| 143 |
} |
| 144 |
|
| 145 |
cockpit_channel_close (channel, problem); |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
respond_with_error (CockpitWebRequest *request, |
| 150 |
guint status, |
| 151 |
const gchar *message) |
| 152 |
{ |
| 153 |
CockpitWebResponse *response; |
| 154 |
|
| 155 |
response = cockpit_web_request_respond (request); |
| 156 |
cockpit_web_response_error (response, status, NULL, "%s", message); |
| 157 |
g_object_unref (response); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
cockpit_channel_socket_class_init (CockpitChannelSocketClass *klass) |
| 162 |
{ |
| 163 |
CockpitChannelClass *channel_class = COCKPIT_CHANNEL_CLASS (klass); |
| 164 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 165 |
|
| 166 |
object_class->finalize = cockpit_channel_socket_finalize; |
| 167 |
|
| 168 |
channel_class->recv = cockpit_channel_socket_recv; |
| 169 |
channel_class->close = cockpit_channel_socket_close; |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
cockpit_channel_socket_open (CockpitWebService *service, |
| 174 |
JsonObject *open, |
| 175 |
CockpitWebRequest *request) |
| 176 |
{ |
| 177 |
CockpitChannelSocket *self = NULL; |
| 178 |
WebSocketDataType data_type; |
| 179 |
CockpitTransport *transport; |
| 180 |
g_autofree const gchar **protocols = NULL; |
| 181 |
g_autofree gchar *id = NULL; |
| 182 |
|
| 183 |
if (!cockpit_web_service_parse_external (open, NULL, NULL, NULL, &protocols) || |
| 184 |
!cockpit_web_service_parse_binary (open, &data_type)) |
| 185 |
{ |
| 186 |
respond_with_error (request, 400, "Bad channel request"); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
transport = cockpit_web_service_get_transport (service); |
| 191 |
if (!transport) |
| 192 |
{ |
| 193 |
respond_with_error (request, 502, "Failed to open channel transport"); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
json_object_set_boolean_member (open, "flow-control", TRUE); |
| 198 |
|
| 199 |
id = cockpit_web_service_unique_channel (service); |
| 200 |
self = g_object_new (COCKPIT_TYPE_CHANNEL_SOCKET, |
| 201 |
"transport", transport, |
| 202 |
"options", open, |
| 203 |
"id", id, |
| 204 |
NULL); |
| 205 |
|
| 206 |
self->data_type = data_type; |
| 207 |
|
| 208 |
self->socket = cockpit_web_service_create_socket (protocols, request); |
| 209 |
self->socket_open = g_signal_connect (self->socket, "open", G_CALLBACK (on_socket_open), self); |
| 210 |
self->socket_message = g_signal_connect (self->socket, "message", G_CALLBACK (on_socket_message), self); |
| 211 |
self->socket_close = g_signal_connect (self->socket, "close", G_CALLBACK (on_socket_close), self); |
| 212 |
|
| 213 |
/* Unref when the channel closes */ |
| 214 |
g_signal_connect_after (self, "closed", G_CALLBACK (g_object_unref), NULL); |
| 215 |
|
| 216 |
/* Tell the channel to throttle based on back pressure from socket */ |
| 217 |
cockpit_flow_throttle (COCKPIT_FLOW (self), COCKPIT_FLOW (self->socket)); |
| 218 |
|
| 219 |
/* Tell the socket peer's output to throttle based on back pressure */ |
| 220 |
cockpit_flow_throttle (COCKPIT_FLOW (self->socket), COCKPIT_FLOW (self)); |
| 221 |
} |