csp_socket_t * csp_socket(uint32_t opts) { /* Validate socket options */ #ifndef CSP_USE_RDP if (opts & CSP_SO_RDPREQ) { csp_log_error("Attempt to create socket that requires RDP, but CSP was compiled without RDP support\r\n"); return NULL; } #endif #ifndef CSP_USE_XTEA if (opts & CSP_SO_XTEAREQ) { csp_log_error("Attempt to create socket that requires XTEA, but CSP was compiled without XTEA support\r\n"); return NULL; } #endif #ifndef CSP_USE_HMAC if (opts & CSP_SO_HMACREQ) { csp_log_error("Attempt to create socket that requires HMAC, but CSP was compiled without HMAC support\r\n"); return NULL; } #endif #ifndef CSP_USE_CRC32 if (opts & CSP_SO_CRC32REQ) { csp_log_error("Attempt to create socket that requires CRC32, but CSP was compiled without CRC32 support\r\n"); return NULL; } #endif /* Drop packet if reserved flags are set */ if (opts & ~(CSP_SO_RDPREQ | CSP_SO_XTEAREQ | CSP_SO_HMACREQ | CSP_SO_CRC32REQ | CSP_SO_CONN_LESS)) { csp_log_error("Invalid socket option\r\n"); return NULL; } /* Use CSP buffers instead? */ csp_socket_t * sock = csp_conn_allocate(CONN_SERVER); if (sock == NULL) return NULL; /* If connectionless, init the queue to a pre-defined size * if not, the user must init the queue using csp_listen */ if (opts & CSP_SO_CONN_LESS) { sock->socket = csp_queue_create(CSP_CONN_QUEUE_LENGTH, sizeof(csp_packet_t *)); if (sock->socket == NULL) return NULL; } else { sock->socket = NULL; } sock->opts = opts; return sock; }
csp_conn_t * csp_conn_new(csp_id_t idin, csp_id_t idout) { /* Allocate connection structure */ csp_conn_t * conn = csp_conn_allocate(CONN_CLIENT); if (conn) { /* No lock is needed here, because nobody else * * has a reference to this connection yet. */ conn->idin.ext = idin.ext; conn->idout.ext = idout.ext; conn->timestamp = csp_get_ms(); /* Ensure connection queue is empty */ csp_conn_flush_rx_queue(conn); } return conn; }