| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "socop.h" | ||
| 2 | |||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | 3 | int socop_client_init(socop_client_t* session, | |
| 7 | socop_read_byte_t reader_func, void* reader_param, | ||
| 8 | socop_write_byte_t writer_func, void* writer_param) | ||
| 9 | { | ||
| 10 | 3 | session->driver.read_byte = reader_func; | |
| 11 | 3 | session->driver.read_param = reader_param; | |
| 12 | 3 | session->driver.write_byte = writer_func; | |
| 13 | 3 | session->driver.write_param = writer_param; | |
| 14 | 3 | return SOCOP_ERROR_NOTIMPL; | |
| 15 | } | ||
| 16 | |||
| 17 | 305 | static void serialize_word(unsigned char target[2], socop_word_t src) | |
| 18 | { | ||
| 19 | 305 | target[0] = (unsigned char)(src >> 8); | |
| 20 | 305 | target[1] = (unsigned char)(src & 0xff); | |
| 21 | 305 | } | |
| 22 | |||
| 23 | 305 | static void deserialize_word(socop_word_t* target, unsigned char const* src) | |
| 24 | { | ||
| 25 | 305 | *target = ((((socop_word_t)src[0]) << 8) | ((socop_word_t)(src[1] & 0xff))); | |
| 26 | 305 | } | |
| 27 | |||
| 28 | |||
| 29 | 50 | int socop_driver_send_msg(socop_driver_t* driver, socop_msg_base_t const* msg, void const* data) | |
| 30 | { | ||
| 31 | unsigned char msg_buffer[SOCOP_MSG_BASE_LENGTH]; | ||
| 32 | unsigned ndx; | ||
| 33 | 50 | unsigned datalen = msg->msg_length - SOCOP_MSG_BASE_LENGTH; | |
| 34 | 50 | unsigned char const* ptr_data = (unsigned char const*)data; | |
| 35 | unsigned more; /* true until no more bytes will follow */ | ||
| 36 | int result; | ||
| 37 | |||
| 38 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
50 | if((datalen > 0) && (data == NULL)) |
| 39 | { | ||
| 40 | ✗ | return SOCOP_ERROR_INVALIDARG; | |
| 41 | } | ||
| 42 | |||
| 43 | /* serialize header */ | ||
| 44 | 50 | serialize_word(&msg_buffer[0], msg->msg_type); | |
| 45 | 50 | serialize_word(&msg_buffer[2], msg->msg_length); | |
| 46 | 50 | serialize_word(&msg_buffer[4], msg->resource_id); | |
| 47 | 50 | serialize_word(&msg_buffer[6], msg->resource_param); | |
| 48 | 50 | serialize_word(&msg_buffer[8], msg->state); | |
| 49 | 50 | serialize_word(&msg_buffer[10], msg->user_param); | |
| 50 | |||
| 51 | /* send header */ | ||
| 52 | 50 | more = 1; | |
| 53 |
2/2✓ Branch 0 taken 600 times.
✓ Branch 1 taken 50 times.
|
650 | for (ndx = 0; ndx != SOCOP_MSG_BASE_LENGTH; ++ndx) |
| 54 | { | ||
| 55 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 550 times.
|
600 | if (ndx == SOCOP_MSG_BASE_LENGTH - 1) |
| 56 | { | ||
| 57 | 50 | more = (datalen == 0) ? 0 : 1; | |
| 58 | } | ||
| 59 | 600 | result = driver->write_byte(msg_buffer[ndx], more, driver->write_param); | |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
|
600 | if (result != SOCOP_OK) |
| 61 | { | ||
| 62 | ✗ | return SOCOP_ERROR_IO; | |
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | /* send content if available */ | ||
| 67 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 50 times.
|
100 | for (ndx = 0; ndx != datalen; ++ndx) |
| 68 | { | ||
| 69 | 50 | more = (ndx == datalen - 1) ? 0 : 1; | |
| 70 | 50 | result = driver->write_byte(ptr_data[ndx], more, driver->write_param); | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (result != SOCOP_OK) |
| 72 | { | ||
| 73 | ✗ | return SOCOP_ERROR_IO; | |
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 50 | return SOCOP_OK; | |
| 78 | } | ||
| 79 | |||
| 80 | 57 | static int receive_buffer(socop_driver_t* driver, unsigned char* target, unsigned length) | |
| 81 | { | ||
| 82 | unsigned ndx; | ||
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 650 times.
✓ Branch 1 taken 57 times.
|
707 | for (ndx = 0; ndx != length; ++ndx) |
| 85 | { | ||
| 86 | 650 | unsigned more = (ndx == length - 1) ? 0 : 1; | |
| 87 | 650 | int result = driver->read_byte(&target[ndx], more, driver->read_param); | |
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
|
650 | if (result == SOCOP_ERROR_TIMEOUT) |
| 89 | { | ||
| 90 | ✗ | continue; | |
| 91 | } | ||
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
|
650 | if (result != SOCOP_OK) |
| 93 | { | ||
| 94 | ✗ | return result; | |
| 95 | } | ||
| 96 | } | ||
| 97 | 57 | return SOCOP_OK; | |
| 98 | } | ||
| 99 | |||
| 100 | 50 | int socop_driver_receive_msg(socop_driver_t* driver, socop_msg_buffer_t* buffer) | |
| 101 | { | ||
| 102 | unsigned char msg_buffer[SOCOP_MSG_BASE_LENGTH]; | ||
| 103 | unsigned data_len; | ||
| 104 | int result; | ||
| 105 | |||
| 106 | 50 | result = receive_buffer(driver, msg_buffer, SOCOP_MSG_BASE_LENGTH); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (result != SOCOP_OK) |
| 108 | { | ||
| 109 | ✗ | return result; | |
| 110 | } | ||
| 111 | |||
| 112 | 50 | deserialize_word(&buffer->base.msg_type, &msg_buffer[0]); | |
| 113 | 50 | deserialize_word(&buffer->base.msg_length, &msg_buffer[2]); | |
| 114 | 50 | deserialize_word(&buffer->base.resource_id, &msg_buffer[4]); | |
| 115 | 50 | deserialize_word(&buffer->base.resource_param, &msg_buffer[6]); | |
| 116 | 50 | deserialize_word(&buffer->base.state, &msg_buffer[8]); | |
| 117 | 50 | deserialize_word(&buffer->base.user_param, &msg_buffer[10]); | |
| 118 | |||
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (buffer->base.msg_length < SOCOP_MSG_BASE_LENGTH) |
| 120 | { | ||
| 121 | ✗ | return SOCOP_ERROR_INVALIDDATA; | |
| 122 | } | ||
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if ((buffer->base.msg_type & SOCOP_MSG_MASK) != SOCOP_MSG_MASK) |
| 124 | { | ||
| 125 | ✗ | return SOCOP_ERROR_INVALIDDATA; | |
| 126 | } | ||
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7 times.
|
50 | if (buffer->base.msg_length == SOCOP_MSG_BASE_LENGTH) |
| 129 | { | ||
| 130 | 43 | return SOCOP_OK; | |
| 131 | } | ||
| 132 | 7 | data_len = buffer->base.msg_length - SOCOP_MSG_BASE_LENGTH; | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (data_len > sizeof(buffer->content)) |
| 134 | { | ||
| 135 | ✗ | return SOCOP_ERROR_OUTOFBOUNDS; | |
| 136 | } | ||
| 137 | 7 | return receive_buffer(driver, (unsigned char*)&buffer->content[0], data_len); | |
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | 50 | static void init_msg_base(socop_msg_base_t* msg, socop_word_t type, socop_word_t sock_id, socop_word_t sock_param, | |
| 143 | socop_word_t state, socop_word_t user, socop_word_t content_length) | ||
| 144 | { | ||
| 145 | 50 | msg->msg_type = type; | |
| 146 | 50 | msg->msg_length = SOCOP_MSG_BASE_LENGTH + content_length; | |
| 147 | 50 | msg->resource_id = sock_id; | |
| 148 | 50 | msg->resource_param = sock_param; | |
| 149 | 50 | msg->state = state; | |
| 150 | 50 | msg->user_param = user; | |
| 151 | 50 | } | |
| 152 | |||
| 153 | 25 | static int receive_message_type(socop_driver_t* session, socop_msg_buffer_t* ptr_msg, socop_word_t desired_type, socop_word_t desired_resource_id) | |
| 154 | { | ||
| 155 | 25 | int result = socop_driver_receive_msg(session, ptr_msg); | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (result != SOCOP_OK) |
| 157 | { | ||
| 158 | ✗ | return result; | |
| 159 | } | ||
| 160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (ptr_msg->base.msg_type != desired_type) |
| 161 | { | ||
| 162 | ✗ | return SOCOP_ERROR_WRONGMESSAGE; | |
| 163 | } | ||
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (ptr_msg->base.state != SOCOP_OK) |
| 165 | { | ||
| 166 | ✗ | return ptr_msg->base.state; | |
| 167 | } | ||
| 168 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
25 | if (desired_resource_id && (ptr_msg->base.resource_id != desired_resource_id)) |
| 169 | { | ||
| 170 | ✗ | return SOCOP_ERROR_RESOURCEERROR; | |
| 171 | } | ||
| 172 | 25 | return SOCOP_OK; | |
| 173 | } | ||
| 174 | |||
| 175 | 5 | static socop_word_t serialize_addr(unsigned char target[6], socop_addr_t const* ptr_addr) | |
| 176 | { | ||
| 177 | 5 | target[0] = ptr_addr->ip[0]; | |
| 178 | 5 | target[1] = ptr_addr->ip[1]; | |
| 179 | 5 | target[2] = ptr_addr->ip[2]; | |
| 180 | 5 | target[3] = ptr_addr->ip[3]; | |
| 181 | 5 | serialize_word(&target[4], ptr_addr->port); | |
| 182 | 5 | return 6; | |
| 183 | } | ||
| 184 | |||
| 185 | 5 | static void deserialize_addr(socop_addr_t* target, unsigned char const src[6]) | |
| 186 | { | ||
| 187 | 5 | target->ip[0] = src[0]; | |
| 188 | 5 | target->ip[1] = src[1]; | |
| 189 | 5 | target->ip[2] = src[2]; | |
| 190 | 5 | target->ip[3] = src[3]; | |
| 191 | 5 | deserialize_word(&target->port, &src[4]); | |
| 192 | 5 | } | |
| 193 | |||
| 194 | 50 | static void invoke_callback(socop_client_t* session, int message_type, int result_code) | |
| 195 | { | ||
| 196 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
|
50 | if (session && session->callback) |
| 197 | { | ||
| 198 | 50 | session->callback(session, message_type, result_code); | |
| 199 | } | ||
| 200 | 50 | } | |
| 201 | |||
| 202 | 3 | int socop_client_reset(socop_client_t* session) | |
| 203 | { | ||
| 204 | int result; | ||
| 205 | socop_msg_buffer_t msg; | ||
| 206 | |||
| 207 | 3 | init_msg_base(&msg.base, SOCOP_MSG_RESET_REQUEST, 0, 0, 0, 0, 0); | |
| 208 | 3 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 209 | 3 | invoke_callback(session, SOCOP_MSG_RESET_REQUEST, result); | |
| 210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 211 | { | ||
| 212 | ✗ | return result; | |
| 213 | } | ||
| 214 | 3 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_RESET_RESPONSE, 0); | |
| 215 | 3 | invoke_callback(session, SOCOP_MSG_RESET_RESPONSE, result); | |
| 216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 217 | { | ||
| 218 | ✗ | return result; | |
| 219 | } | ||
| 220 | 3 | return SOCOP_OK; | |
| 221 | } | ||
| 222 | |||
| 223 | 3 | int socop_client_netstatus(socop_client_t* session, unsigned* ptr_status) | |
| 224 | { | ||
| 225 | int result; | ||
| 226 | socop_msg_buffer_t msg; | ||
| 227 | |||
| 228 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_STATUS_REQUEST, 0, 0, 0, 0, 0); | |
| 229 | 3 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 230 | 3 | invoke_callback(session, SOCOP_MSG_NET_STATUS_REQUEST, result); | |
| 231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 232 | { | ||
| 233 | ✗ | return result; | |
| 234 | } | ||
| 235 | 3 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_NET_STATUS_RESPONSE, 0); | |
| 236 | 3 | invoke_callback(session, SOCOP_MSG_NET_STATUS_RESPONSE, result); | |
| 237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 238 | { | ||
| 239 | ✗ | return result; | |
| 240 | } | ||
| 241 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (ptr_status) |
| 242 | { | ||
| 243 | 3 | *ptr_status = msg.base.resource_param; | |
| 244 | } | ||
| 245 | 3 | return SOCOP_OK; | |
| 246 | } | ||
| 247 | |||
| 248 | 3 | int socop_client_netstart(socop_client_t* session, char const* parameters, unsigned parameters_length) | |
| 249 | { | ||
| 250 | int result; | ||
| 251 | socop_msg_buffer_t msg; | ||
| 252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (parameters_length > SOCOP_MAX_CONTENT_LENGTH) |
| 253 | { | ||
| 254 | ✗ | parameters_length = SOCOP_MAX_CONTENT_LENGTH; | |
| 255 | } | ||
| 256 | |||
| 257 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_START_REQUEST, 0, 0, 0, 0, (socop_word_t)parameters_length); | |
| 258 | 3 | result = socop_driver_send_msg(&session->driver, &msg.base, parameters); | |
| 259 | 3 | invoke_callback(session, SOCOP_MSG_NET_START_REQUEST, result); | |
| 260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 261 | { | ||
| 262 | ✗ | return result; | |
| 263 | } | ||
| 264 | 3 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_NET_START_RESPONSE, 0); | |
| 265 | 3 | invoke_callback(session, SOCOP_MSG_NET_START_RESPONSE, result); | |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 267 | { | ||
| 268 | ✗ | return result; | |
| 269 | } | ||
| 270 | 3 | return SOCOP_OK; | |
| 271 | } | ||
| 272 | |||
| 273 | 3 | int socop_client_netstop(socop_client_t* session) | |
| 274 | { | ||
| 275 | int result; | ||
| 276 | socop_msg_buffer_t msg; | ||
| 277 | |||
| 278 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_STOP_REQUEST, 0, 0, 0, 0, 0); | |
| 279 | 3 | result = socop_driver_send_msg(&session->driver, &msg.base, NULL); | |
| 280 | 3 | invoke_callback(session, SOCOP_MSG_NET_STOP_REQUEST, result); | |
| 281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 282 | { | ||
| 283 | ✗ | return result; | |
| 284 | } | ||
| 285 | 3 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_NET_STOP_RESPONSE, 0); | |
| 286 | 3 | invoke_callback(session, SOCOP_MSG_NET_STOP_RESPONSE, result); | |
| 287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 288 | { | ||
| 289 | ✗ | return result; | |
| 290 | } | ||
| 291 | 3 | return SOCOP_OK; | |
| 292 | } | ||
| 293 | |||
| 294 | 1 | int socop_client_connect(socop_client_t* session, socop_addr_t const* addr, socop_socket_t* ptr_sock) | |
| 295 | { | ||
| 296 | int result; | ||
| 297 | socop_msg_buffer_t msg; | ||
| 298 | 1 | const socop_word_t content_len = serialize_addr((unsigned char*)&msg.content[0], addr); | |
| 299 | |||
| 300 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_CONNECT_REQUEST, 0, 0, 0, 0, content_len); | |
| 301 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 302 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_CONNECT_REQUEST, result); | |
| 303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 304 | { | ||
| 305 | ✗ | return result; | |
| 306 | } | ||
| 307 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_CONNECT_RESPONSE, 0); | |
| 308 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_CONNECT_RESPONSE, result); | |
| 309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 310 | { | ||
| 311 | ✗ | return result; | |
| 312 | } | ||
| 313 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (msg.base.resource_id) |
| 314 | { | ||
| 315 | 1 | *ptr_sock = msg.base.resource_id; | |
| 316 | 1 | return SOCOP_OK; | |
| 317 | } | ||
| 318 | else | ||
| 319 | { | ||
| 320 | ✗ | return SOCOP_ERROR_RESOURCEERROR; | |
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | 1 | int socop_client_bind(socop_client_t* session, socop_addr_t const* addr, socop_socket_t* ptr_sock) | |
| 325 | { | ||
| 326 | int result; | ||
| 327 | socop_msg_buffer_t msg; | ||
| 328 | 1 | const socop_word_t content_len = serialize_addr((unsigned char*)&msg.content[0], addr); | |
| 329 | |||
| 330 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_BIND_REQUEST, 0, 0, 0, 0, content_len); | |
| 331 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 332 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_BIND_REQUEST, result); | |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 334 | { | ||
| 335 | ✗ | return result; | |
| 336 | } | ||
| 337 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_BIND_RESPONSE, 0); | |
| 338 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_BIND_RESPONSE, result); | |
| 339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 340 | { | ||
| 341 | ✗ | return result; | |
| 342 | } | ||
| 343 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (msg.base.resource_id) |
| 344 | { | ||
| 345 | 1 | *ptr_sock = msg.base.resource_id; | |
| 346 | 1 | return SOCOP_OK; | |
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | ✗ | return SOCOP_ERROR_RESOURCEERROR; | |
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | 1 | int socop_client_listen(socop_client_t* session, socop_addr_t const* addr, socop_socket_t* ptr_sock) | |
| 355 | { | ||
| 356 | int result; | ||
| 357 | socop_msg_buffer_t msg; | ||
| 358 | 1 | const socop_word_t content_len = serialize_addr((unsigned char*)&msg.content[0], addr); | |
| 359 | |||
| 360 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_LISTEN_REQUEST, 0, 0, 0, 0, content_len); | |
| 361 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 362 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_LISTEN_REQUEST, result); | |
| 363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 364 | { | ||
| 365 | ✗ | return result; | |
| 366 | } | ||
| 367 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_LISTEN_RESPONSE, 0); | |
| 368 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_LISTEN_RESPONSE, result); | |
| 369 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 370 | { | ||
| 371 | ✗ | return result; | |
| 372 | } | ||
| 373 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (msg.base.resource_id) |
| 374 | { | ||
| 375 | 1 | *ptr_sock = msg.base.resource_id; | |
| 376 | 1 | return SOCOP_OK; | |
| 377 | } | ||
| 378 | else | ||
| 379 | { | ||
| 380 | ✗ | return SOCOP_ERROR_RESOURCEERROR; | |
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | 1 | int socop_client_accept(socop_client_t* session, socop_socket_t sock, socop_socket_t* ptr_sock) | |
| 385 | { | ||
| 386 | int result; | ||
| 387 | socop_msg_buffer_t msg; | ||
| 388 | |||
| 389 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_ACCEPT_REQUEST, sock, 0, 0, 0, 0); | |
| 390 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, NULL); | |
| 391 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_ACCEPT_REQUEST, result); | |
| 392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 393 | { | ||
| 394 | ✗ | return result; | |
| 395 | } | ||
| 396 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_ACCEPT_RESPONSE, 0); | |
| 397 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_ACCEPT_RESPONSE, result); | |
| 398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 399 | { | ||
| 400 | ✗ | return result; | |
| 401 | } | ||
| 402 | /* original server socket is returned as resource_param */ | ||
| 403 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (msg.base.resource_param == sock) |
| 404 | { | ||
| 405 | 1 | *ptr_sock = msg.base.resource_id; | |
| 406 | 1 | return SOCOP_OK; | |
| 407 | } | ||
| 408 | else | ||
| 409 | { | ||
| 410 | ✗ | return SOCOP_ERROR_RESOURCEERROR; | |
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | 3 | int socop_client_close(socop_client_t* session, socop_socket_t sock) | |
| 416 | { | ||
| 417 | int result; | ||
| 418 | socop_msg_buffer_t msg; | ||
| 419 | |||
| 420 | 3 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_CLOSE_REQUEST, sock, 0, 0, 0, 0); | |
| 421 | 3 | result = socop_driver_send_msg(&session->driver, &msg.base, NULL); | |
| 422 | 3 | invoke_callback(session, SOCOP_MSG_SOCK_CLOSE_REQUEST, result); | |
| 423 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result != SOCOP_OK) |
| 424 | { | ||
| 425 | ✗ | return result; | |
| 426 | } | ||
| 427 | 3 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_CLOSE_RESPONSE, 0); | |
| 428 | 3 | invoke_callback(session, SOCOP_MSG_SOCK_CLOSE_RESPONSE, result); | |
| 429 | 3 | return result; | |
| 430 | } | ||
| 431 | |||
| 432 | 1 | int socop_client_receive(socop_client_t* session, socop_socket_t sock, | |
| 433 | void* buffer, unsigned bufferlen, unsigned* bufferused) | ||
| 434 | { | ||
| 435 | int result; | ||
| 436 | socop_msg_buffer_t msg; | ||
| 437 | unsigned received_content; | ||
| 438 | |||
| 439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bufferlen > SOCOP_MAX_CONTENT_LENGTH) |
| 440 | { | ||
| 441 | ✗ | bufferlen = SOCOP_MAX_CONTENT_LENGTH; | |
| 442 | } | ||
| 443 | |||
| 444 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_RECV_REQUEST, sock, (socop_word_t)bufferlen, 0, 0, 0); | |
| 445 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, NULL); | |
| 446 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_RECV_REQUEST, result); | |
| 447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 448 | { | ||
| 449 | ✗ | return result; | |
| 450 | } | ||
| 451 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_RECV_RESPONSE, sock); | |
| 452 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_RECV_RESPONSE, result); | |
| 453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 454 | { | ||
| 455 | ✗ | return result; | |
| 456 | } | ||
| 457 | 1 | received_content = msg.base.msg_length - SOCOP_MSG_BASE_LENGTH; | |
| 458 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (received_content) |
| 459 | { | ||
| 460 | 1 | memcpy(buffer, msg.content, received_content); | |
| 461 | } | ||
| 462 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (bufferused) |
| 463 | { | ||
| 464 | 1 | *bufferused = received_content; | |
| 465 | } | ||
| 466 | 1 | return SOCOP_OK; | |
| 467 | } | ||
| 468 | |||
| 469 | 1 | int socop_client_recv_from(socop_client_t* session, socop_socket_t sock, | |
| 470 | void* buffer, unsigned bufferlen, unsigned* bufferused, | ||
| 471 | socop_addr_t* ptr_addr) | ||
| 472 | { | ||
| 473 | int result; | ||
| 474 | socop_msg_buffer_t msg; | ||
| 475 | unsigned address_length; | ||
| 476 | unsigned received_content; | ||
| 477 | |||
| 478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bufferlen > (SOCOP_MAX_CONTENT_LENGTH - 8)) |
| 479 | { | ||
| 480 | ✗ | bufferlen = SOCOP_MAX_CONTENT_LENGTH - 8; | |
| 481 | } | ||
| 482 | |||
| 483 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_RECVFROM_REQUEST, sock, (socop_word_t)bufferlen, 0, 0, 0); | |
| 484 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, NULL); | |
| 485 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_RECVFROM_REQUEST, result); | |
| 486 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 487 | { | ||
| 488 | ✗ | return result; | |
| 489 | } | ||
| 490 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_RECVFROM_RESPONSE, sock); | |
| 491 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_RECVFROM_RESPONSE, result); | |
| 492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 493 | { | ||
| 494 | ✗ | return result; | |
| 495 | } | ||
| 496 | |||
| 497 | 1 | address_length = msg.base.resource_param; | |
| 498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (address_length > (unsigned)(msg.base.msg_length - SOCOP_MSG_BASE_LENGTH)) |
| 499 | { | ||
| 500 | ✗ | return SOCOP_ERROR_CORRUPTION; | |
| 501 | } | ||
| 502 | |||
| 503 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptr_addr) |
| 504 | { | ||
| 505 | 1 | deserialize_addr(ptr_addr, (unsigned char const*)&msg.content[0]); | |
| 506 | } | ||
| 507 | |||
| 508 | 1 | received_content = msg.base.msg_length - address_length - SOCOP_MSG_BASE_LENGTH; | |
| 509 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (received_content) |
| 510 | { | ||
| 511 | 1 | memcpy(buffer, &msg.content[address_length], received_content); | |
| 512 | } | ||
| 513 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (bufferused) |
| 514 | { | ||
| 515 | 1 | *bufferused = received_content; | |
| 516 | } | ||
| 517 | 1 | return SOCOP_OK; | |
| 518 | } | ||
| 519 | |||
| 520 | 1 | int socop_client_send(socop_client_t* session, socop_socket_t sock, | |
| 521 | void const* buffer, unsigned bufferlen, unsigned* bufferused) | ||
| 522 | { | ||
| 523 | int result; | ||
| 524 | socop_msg_buffer_t msg; | ||
| 525 | |||
| 526 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bufferlen > (SOCOP_MAX_CONTENT_LENGTH)) |
| 527 | { | ||
| 528 | ✗ | bufferlen = SOCOP_MAX_CONTENT_LENGTH; | |
| 529 | } | ||
| 530 | |||
| 531 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SEND_REQUEST, sock, 0, 0, 0, (socop_word_t)bufferlen); | |
| 532 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, buffer); | |
| 533 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SEND_REQUEST, result); | |
| 534 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 535 | { | ||
| 536 | ✗ | return result; | |
| 537 | } | ||
| 538 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_SEND_RESPONSE, sock); | |
| 539 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SEND_RESPONSE, result); | |
| 540 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 541 | { | ||
| 542 | ✗ | return result; | |
| 543 | } | ||
| 544 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (bufferused) |
| 545 | { | ||
| 546 | 1 | *bufferused = msg.base.resource_param; | |
| 547 | } | ||
| 548 | 1 | return SOCOP_OK; | |
| 549 | } | ||
| 550 | |||
| 551 | 1 | int socop_client_sendto(socop_client_t* session, socop_socket_t sock, | |
| 552 | void const* buffer, unsigned bufferlen, unsigned* bufferused, | ||
| 553 | socop_addr_t const* ptr_addr) | ||
| 554 | { | ||
| 555 | int result; | ||
| 556 | socop_msg_buffer_t msg; | ||
| 557 | 1 | const socop_word_t addr_length = serialize_addr((unsigned char*)&msg.content[0], ptr_addr); | |
| 558 | |||
| 559 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bufferlen > (unsigned)(SOCOP_MAX_CONTENT_LENGTH - addr_length)) |
| 560 | { | ||
| 561 | ✗ | bufferlen = SOCOP_MAX_CONTENT_LENGTH - addr_length; | |
| 562 | } | ||
| 563 | 1 | memcpy(&msg.content[addr_length], buffer, bufferlen); | |
| 564 | |||
| 565 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SENDTO_REQUEST, sock, addr_length, 0, 0, (socop_word_t)(bufferlen + addr_length)); | |
| 566 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 567 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SENDTO_REQUEST, result); | |
| 568 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 569 | { | ||
| 570 | ✗ | return result; | |
| 571 | } | ||
| 572 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_SENDTO_RESPONSE, sock); | |
| 573 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SENDTO_RESPONSE, result); | |
| 574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 575 | { | ||
| 576 | ✗ | return result; | |
| 577 | } | ||
| 578 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (bufferused) |
| 579 | { | ||
| 580 | 1 | *bufferused = msg.base.resource_param; | |
| 581 | } | ||
| 582 | 1 | return SOCOP_OK; | |
| 583 | } | ||
| 584 | |||
| 585 | 1 | int socop_client_shutdown(socop_client_t* session, socop_socket_t sock, unsigned target) | |
| 586 | { | ||
| 587 | int result; | ||
| 588 | socop_msg_buffer_t msg; | ||
| 589 | |||
| 590 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SHUTDOWN_REQUEST, sock, (socop_word_t)target, 0, 0, 0); | |
| 591 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 592 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SHUTDOWN_REQUEST, result); | |
| 593 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 594 | { | ||
| 595 | ✗ | return result; | |
| 596 | } | ||
| 597 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_SHUTDOWN_RESPONSE, sock); | |
| 598 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SHUTDOWN_RESPONSE, result); | |
| 599 | 1 | return result; | |
| 600 | } | ||
| 601 | |||
| 602 | 1 | int socop_client_select(socop_client_t* session, socop_socket_t sock, unsigned* flags, unsigned timeout_ms) | |
| 603 | { | ||
| 604 | int result; | ||
| 605 | socop_msg_buffer_t msg; | ||
| 606 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | const socop_word_t sel_flags = flags ? ((socop_word_t)*flags) : (SOCOP_MSG_FLAG_RECV | SOCOP_MSG_FLAG_SEND | SOCOP_MSG_FLAG_ERROR); |
| 607 | socop_word_t timeout; | ||
| 608 | |||
| 609 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (timeout_ms > (unsigned int)0xffff) |
| 610 | { | ||
| 611 | ✗ | timeout = 0xffff; | |
| 612 | } | ||
| 613 | else | ||
| 614 | { | ||
| 615 | 1 | timeout = (socop_word_t)timeout_ms; | |
| 616 | } | ||
| 617 | |||
| 618 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SELECT_REQUEST, sock, sel_flags, timeout, 0, 0); | |
| 619 | 1 | result = socop_driver_send_msg(&session->driver, &msg.base, msg.content); | |
| 620 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SELECT_REQUEST, result); | |
| 621 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 622 | { | ||
| 623 | ✗ | return result; | |
| 624 | } | ||
| 625 | 1 | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_SELECT_RESPONSE, sock); | |
| 626 | 1 | invoke_callback(session, SOCOP_MSG_SOCK_SELECT_RESPONSE, result); | |
| 627 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result != SOCOP_OK) |
| 628 | { | ||
| 629 | ✗ | return result; | |
| 630 | } | ||
| 631 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (flags) |
| 632 | { | ||
| 633 | 1 | *flags = msg.base.resource_param; | |
| 634 | } | ||
| 635 | 1 | return SOCOP_OK; | |
| 636 | } | ||
| 637 | |||
| 638 | ✗ | int socop_client_peer(socop_client_t* session, socop_socket_t sock, socop_addr_t* ptr_addr) | |
| 639 | { | ||
| 640 | int result; | ||
| 641 | socop_msg_buffer_t msg; | ||
| 642 | |||
| 643 | ✗ | init_msg_base(&msg.base, SOCOP_MSG_SOCK_PEER_REQUEST, sock, 0, 0, 0, 0); | |
| 644 | ✗ | result = socop_driver_send_msg(&session->driver, &msg.base, 0); | |
| 645 | ✗ | invoke_callback(session, SOCOP_MSG_SOCK_PEER_REQUEST, result); | |
| 646 | ✗ | if (result != SOCOP_OK) | |
| 647 | { | ||
| 648 | ✗ | return result; | |
| 649 | } | ||
| 650 | ✗ | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_PEER_RESPONSE, sock); | |
| 651 | ✗ | invoke_callback(session, SOCOP_MSG_SOCK_PEER_RESPONSE, result); | |
| 652 | ✗ | if (result != SOCOP_OK) | |
| 653 | { | ||
| 654 | ✗ | return result; | |
| 655 | } | ||
| 656 | ✗ | deserialize_addr(ptr_addr, (unsigned char const*)&msg.content[0]); | |
| 657 | ✗ | return SOCOP_OK; | |
| 658 | } | ||
| 659 | |||
| 660 | ✗ | int socop_client_ioctl(socop_client_t* session, socop_socket_t sock, unsigned option, void* data, unsigned datalen) | |
| 661 | { | ||
| 662 | int result; | ||
| 663 | socop_msg_buffer_t msg; | ||
| 664 | unsigned content_length; | ||
| 665 | |||
| 666 | ✗ | if (datalen > SOCOP_MAX_CONTENT_LENGTH) | |
| 667 | { | ||
| 668 | ✗ | datalen = SOCOP_MAX_CONTENT_LENGTH; | |
| 669 | } | ||
| 670 | ✗ | if ((datalen > 0) && !data) | |
| 671 | { | ||
| 672 | ✗ | return SOCOP_ERROR_INVALIDARG; | |
| 673 | } | ||
| 674 | |||
| 675 | ✗ | init_msg_base(&msg.base, SOCOP_MSG_SOCK_IOCTL_REQUEST, sock, (socop_word_t)option, 0, 0, (socop_word_t)datalen); | |
| 676 | ✗ | result = socop_driver_send_msg(&session->driver, &msg.base, data); | |
| 677 | ✗ | invoke_callback(session, SOCOP_MSG_SOCK_IOCTL_REQUEST, result); | |
| 678 | ✗ | if (result != SOCOP_OK) | |
| 679 | { | ||
| 680 | ✗ | return result; | |
| 681 | } | ||
| 682 | ✗ | result = receive_message_type(&session->driver, &msg, SOCOP_MSG_SOCK_IOCTL_RESPONSE, sock); | |
| 683 | ✗ | invoke_callback(session, SOCOP_MSG_SOCK_IOCTL_RESPONSE, result); | |
| 684 | ✗ | if (result != SOCOP_OK) | |
| 685 | { | ||
| 686 | ✗ | return result; | |
| 687 | } | ||
| 688 | ✗ | content_length = msg.base.msg_length - SOCOP_MSG_BASE_LENGTH; | |
| 689 | ✗ | if (content_length > datalen) | |
| 690 | { | ||
| 691 | ✗ | content_length = datalen; | |
| 692 | } | ||
| 693 | ✗ | if (content_length > 0) | |
| 694 | { | ||
| 695 | ✗ | memcpy(data, msg.content, content_length); | |
| 696 | } | ||
| 697 | ✗ | return SOCOP_OK; | |
| 698 | } | ||
| 699 | |||
| 700 | |||
| 701 | |||
| 702 | |||
| 703 | 3 | int socop_server_init(socop_server_t* server, void* server_param, | |
| 704 | socop_read_byte_t reader_func, void* reader_param, socop_write_byte_t writer_func, void* writer_param) | ||
| 705 | { | ||
| 706 | 3 | memset(server, 0, sizeof(socop_server_t)); | |
| 707 | 3 | server->server_param = server_param; | |
| 708 | 3 | server->driver.read_byte = reader_func; | |
| 709 | 3 | server->driver.read_param = reader_param; | |
| 710 | 3 | server->driver.write_byte = writer_func; | |
| 711 | 3 | server->driver.write_param = writer_param; | |
| 712 | 3 | return SOCOP_OK; | |
| 713 | } | ||
| 714 | |||
| 715 | |||
| 716 | |||
| 717 | 25 | int socop_server_chat_once(socop_server_t* server) | |
| 718 | { | ||
| 719 | int result; | ||
| 720 | 25 | socop_msg_buffer_t msg = { 0 }; | |
| 721 | 25 | unsigned content_length = 0; | |
| 722 | 25 | socop_addr_t addr = { 0 }; | |
| 723 | 25 | socop_socket_t sock = 0; | |
| 724 | 25 | unsigned flags = 0; | |
| 725 | |||
| 726 | 25 | result = socop_driver_receive_msg(&server->driver, &msg); | |
| 727 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (SOCOP_OK != result) |
| 728 | { | ||
| 729 | ✗ | return result; | |
| 730 | } | ||
| 731 | |||
| 732 | 25 | content_length = msg.base.msg_length - SOCOP_MSG_BASE_LENGTH; | |
| 733 | |||
| 734 |
15/17✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
25 | switch (msg.base.msg_type) |
| 735 | { | ||
| 736 | 3 | case SOCOP_MSG_RESET_REQUEST: | |
| 737 | { | ||
| 738 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!server->on_reset) |
| 739 | { | ||
| 740 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 741 | } | ||
| 742 | else | ||
| 743 | { | ||
| 744 | 3 | result = server->on_reset(server, msg.content, content_length); | |
| 745 | } | ||
| 746 | 3 | init_msg_base(&msg.base, SOCOP_MSG_RESET_RESPONSE, 0, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 747 | 3 | result = SOCOP_OK; | |
| 748 | 3 | break; | |
| 749 | } | ||
| 750 | |||
| 751 | 3 | case SOCOP_MSG_NET_STATUS_REQUEST: | |
| 752 | { | ||
| 753 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!server->on_netstatus) |
| 754 | { | ||
| 755 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 756 | } | ||
| 757 | else | ||
| 758 | { | ||
| 759 | 3 | result = server->on_netstatus(server, NULL, 0); | |
| 760 | } | ||
| 761 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_STATUS_RESPONSE, 0, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 762 | 3 | result = SOCOP_OK; | |
| 763 | 3 | break; | |
| 764 | } | ||
| 765 | |||
| 766 | 3 | case SOCOP_MSG_NET_START_REQUEST: | |
| 767 | { | ||
| 768 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!server->on_netstart) |
| 769 | { | ||
| 770 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 771 | } | ||
| 772 | else | ||
| 773 | { | ||
| 774 | 3 | result = server->on_netstart(server, msg.content, content_length); | |
| 775 | } | ||
| 776 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_START_RESPONSE, 0, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 777 | 3 | result = SOCOP_OK; | |
| 778 | 3 | break; | |
| 779 | } | ||
| 780 | |||
| 781 | 3 | case SOCOP_MSG_NET_STOP_REQUEST: | |
| 782 | { | ||
| 783 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!server->on_netstop) |
| 784 | { | ||
| 785 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 786 | } | ||
| 787 | else | ||
| 788 | { | ||
| 789 | 3 | result = server->on_netstop(server, NULL, 0); | |
| 790 | } | ||
| 791 | 3 | init_msg_base(&msg.base, SOCOP_MSG_NET_STOP_RESPONSE, 0, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 792 | 3 | result = SOCOP_OK; | |
| 793 | 3 | break; | |
| 794 | } | ||
| 795 | |||
| 796 | 1 | case SOCOP_MSG_SOCK_CONNECT_REQUEST: | |
| 797 | { | ||
| 798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_connect) |
| 799 | { | ||
| 800 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 801 | } | ||
| 802 | else | ||
| 803 | { | ||
| 804 | 1 | deserialize_addr(&addr, (unsigned char const*)&msg.content[0]); | |
| 805 | 1 | result = server->on_connect(server, &addr, &sock); | |
| 806 | } | ||
| 807 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_CONNECT_RESPONSE, sock, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 808 | 1 | result = SOCOP_OK; | |
| 809 | 1 | break; | |
| 810 | } | ||
| 811 | |||
| 812 | 1 | case SOCOP_MSG_SOCK_BIND_REQUEST: | |
| 813 | { | ||
| 814 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_bind) |
| 815 | { | ||
| 816 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 817 | } | ||
| 818 | else | ||
| 819 | { | ||
| 820 | 1 | deserialize_addr(&addr, (unsigned char const*)&msg.content[0]); | |
| 821 | 1 | result = server->on_bind(server, &addr, &sock); | |
| 822 | } | ||
| 823 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_BIND_RESPONSE, sock, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 824 | 1 | result = SOCOP_OK; | |
| 825 | 1 | break; | |
| 826 | } | ||
| 827 | |||
| 828 | 1 | case SOCOP_MSG_SOCK_LISTEN_REQUEST: | |
| 829 | { | ||
| 830 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_bind) |
| 831 | { | ||
| 832 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 833 | } | ||
| 834 | else | ||
| 835 | { | ||
| 836 | 1 | deserialize_addr(&addr, (unsigned char const*)&msg.content[0]); | |
| 837 | 1 | result = server->on_listen(server, &addr, &sock); | |
| 838 | } | ||
| 839 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_LISTEN_RESPONSE, sock, 0, (socop_word_t)result, msg.base.user_param, 0); | |
| 840 | 1 | result = SOCOP_OK; | |
| 841 | 1 | break; | |
| 842 | } | ||
| 843 | |||
| 844 | 1 | case SOCOP_MSG_SOCK_ACCEPT_REQUEST: | |
| 845 | { | ||
| 846 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_accept) |
| 847 | { | ||
| 848 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 849 | } | ||
| 850 | else | ||
| 851 | { | ||
| 852 | 1 | result = server->on_accept(server, msg.base.resource_id, &sock); | |
| 853 | } | ||
| 854 | 1 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_ACCEPT_RESPONSE, sock, msg.base.resource_id, (socop_word_t)result, msg.base.user_param, 0); | |
| 855 | 1 | result = SOCOP_OK; | |
| 856 | 1 | break; | |
| 857 | } | ||
| 858 | |||
| 859 | 3 | case SOCOP_MSG_SOCK_CLOSE_REQUEST: | |
| 860 | { | ||
| 861 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!server->on_close) |
| 862 | { | ||
| 863 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 864 | } | ||
| 865 | else | ||
| 866 | { | ||
| 867 | 3 | result = server->on_close(server, msg.base.resource_id, 0); | |
| 868 | } | ||
| 869 | 6 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_CLOSE_RESPONSE, msg.base.resource_id, 0, | |
| 870 | 3 | (socop_word_t)result, msg.base.user_param, 0); | |
| 871 | 3 | result = SOCOP_OK; | |
| 872 | 3 | break; | |
| 873 | } | ||
| 874 | |||
| 875 | 1 | case SOCOP_MSG_SOCK_RECV_REQUEST: | |
| 876 | { | ||
| 877 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_recv) |
| 878 | { | ||
| 879 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 880 | } | ||
| 881 | else | ||
| 882 | { | ||
| 883 | 1 | content_length = msg.base.resource_param; | |
| 884 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (content_length > SOCOP_MAX_CONTENT_LENGTH) |
| 885 | { | ||
| 886 | ✗ | content_length = SOCOP_MAX_CONTENT_LENGTH; | |
| 887 | } | ||
| 888 | 1 | result = server->on_recv(server, msg.base.resource_id, msg.content, content_length, &content_length); | |
| 889 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (SOCOP_OK != result) |
| 890 | { | ||
| 891 | ✗ | content_length = 0; | |
| 892 | } | ||
| 893 | } | ||
| 894 | 3 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_RECV_RESPONSE, msg.base.resource_id, 0, | |
| 895 | 1 | (socop_word_t)result, msg.base.user_param, (socop_word_t)content_length); | |
| 896 | 1 | result = SOCOP_OK; | |
| 897 | 1 | break; | |
| 898 | } | ||
| 899 | |||
| 900 | 1 | case SOCOP_MSG_SOCK_RECVFROM_REQUEST: | |
| 901 | { | ||
| 902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_recvfrom) |
| 903 | { | ||
| 904 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 905 | } | ||
| 906 | else | ||
| 907 | { | ||
| 908 | 1 | content_length = msg.base.resource_param; | |
| 909 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (content_length > SOCOP_MAX_CONTENT_LENGTH) |
| 910 | { | ||
| 911 | ✗ | content_length = SOCOP_MAX_CONTENT_LENGTH; | |
| 912 | } | ||
| 913 | 1 | result = server->on_recvfrom(server, msg.base.resource_id, &msg.content[6], content_length, &content_length, &addr); | |
| 914 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (SOCOP_OK != result) |
| 915 | { | ||
| 916 | ✗ | content_length = 0; | |
| 917 | ✗ | msg.base.resource_param = 0; | |
| 918 | } | ||
| 919 | else | ||
| 920 | { | ||
| 921 | 1 | msg.base.resource_param = serialize_addr((unsigned char*)&msg.content[0], &addr); | |
| 922 | 1 | content_length += msg.base.resource_param; | |
| 923 | } | ||
| 924 | } | ||
| 925 | 3 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_RECVFROM_RESPONSE, msg.base.resource_id, msg.base.resource_param, | |
| 926 | 1 | (socop_word_t)result, msg.base.user_param, (socop_word_t)content_length); | |
| 927 | 1 | result = SOCOP_OK; | |
| 928 | 1 | break; | |
| 929 | } | ||
| 930 | 1 | case SOCOP_MSG_SOCK_SEND_REQUEST: | |
| 931 | { | ||
| 932 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_send) |
| 933 | { | ||
| 934 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 935 | } | ||
| 936 | else | ||
| 937 | { | ||
| 938 | 1 | result = server->on_send(server, msg.base.resource_id, msg.content, content_length, &content_length); | |
| 939 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (SOCOP_OK != result) |
| 940 | { | ||
| 941 | ✗ | content_length = 0; | |
| 942 | } | ||
| 943 | } | ||
| 944 | 2 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SEND_RESPONSE, msg.base.resource_id, (socop_word_t)content_length, | |
| 945 | 1 | (socop_word_t)result, msg.base.user_param, 0); | |
| 946 | 1 | result = SOCOP_OK; | |
| 947 | 1 | break; | |
| 948 | } | ||
| 949 | |||
| 950 | 1 | case SOCOP_MSG_SOCK_SENDTO_REQUEST: | |
| 951 | { | ||
| 952 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_sendto) |
| 953 | { | ||
| 954 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 955 | } | ||
| 956 | else | ||
| 957 | { | ||
| 958 | 1 | deserialize_addr(&addr, (unsigned char const*)&msg.content[0]); | |
| 959 | 1 | result = server->on_sendto(server, msg.base.resource_id, &msg.content[6], content_length - 6, &content_length, &addr); | |
| 960 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (SOCOP_OK != result) |
| 961 | { | ||
| 962 | ✗ | content_length = 0; | |
| 963 | } | ||
| 964 | } | ||
| 965 | 2 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SENDTO_RESPONSE, msg.base.resource_id, (socop_word_t)content_length, | |
| 966 | 1 | (socop_word_t)result, msg.base.user_param, 0); | |
| 967 | 1 | result = SOCOP_OK; | |
| 968 | 1 | break; | |
| 969 | } | ||
| 970 | |||
| 971 | 1 | case SOCOP_MSG_SOCK_SHUTDOWN_REQUEST: | |
| 972 | { | ||
| 973 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_shutdown) |
| 974 | { | ||
| 975 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 976 | } | ||
| 977 | else | ||
| 978 | { | ||
| 979 | 1 | result = server->on_shutdown(server, msg.base.resource_id, msg.base.resource_param); | |
| 980 | } | ||
| 981 | 2 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SHUTDOWN_RESPONSE, msg.base.resource_id, 0, | |
| 982 | 1 | (socop_word_t)result, msg.base.user_param, 0); | |
| 983 | 1 | result = SOCOP_OK; | |
| 984 | 1 | break; | |
| 985 | } | ||
| 986 | |||
| 987 | 1 | case SOCOP_MSG_SOCK_SELECT_REQUEST: | |
| 988 | { | ||
| 989 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!server->on_select) |
| 990 | { | ||
| 991 | ✗ | result = SOCOP_ERROR_NOTIMPL; | |
| 992 | } | ||
| 993 | else | ||
| 994 | { | ||
| 995 | 1 | flags = msg.base.resource_param; | |
| 996 | 1 | result = server->on_select(server, msg.base.resource_id, &flags); | |
| 997 | } | ||
| 998 | 2 | init_msg_base(&msg.base, SOCOP_MSG_SOCK_SELECT_RESPONSE, msg.base.resource_id, (socop_word_t)flags, | |
| 999 | 1 | (socop_word_t)result, msg.base.user_param, 0); | |
| 1000 | 1 | result = SOCOP_OK; | |
| 1001 | 1 | break; | |
| 1002 | } | ||
| 1003 | |||
| 1004 | ✗ | case SOCOP_MSG_SOCK_PEER_REQUEST: | |
| 1005 | case SOCOP_MSG_SOCK_IOCTL_REQUEST: | ||
| 1006 | { | ||
| 1007 | ✗ | result = SOCOP_ERROR_WRONGMESSAGE; | |
| 1008 | ✗ | break; | |
| 1009 | } | ||
| 1010 | |||
| 1011 | ✗ | default: | |
| 1012 | { | ||
| 1013 | ✗ | result = SOCOP_ERROR_WRONGMESSAGE; | |
| 1014 | ✗ | break; | |
| 1015 | } | ||
| 1016 | } | ||
| 1017 | |||
| 1018 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (SOCOP_OK != result) |
| 1019 | { | ||
| 1020 | ✗ | init_msg_base(&msg.base, SOCOP_MSG_SOCK_UNKNOWN, msg.base.resource_id, msg.base.resource_param, | |
| 1021 | ✗ | (socop_word_t)result, msg.base.user_param, 0); | |
| 1022 | } | ||
| 1023 | |||
| 1024 | 25 | return socop_driver_send_msg(&server->driver, &msg.base, msg.content); | |
| 1025 | } | ||
| 1026 | |||
| 1027 |