| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975 |
- /**
- * @file socket.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Synopsis:
- * sys.socket sys.connect(string addr)
- *
- * Variables:
- * string is_error - "true" if there was an error with the connection,
- * "false" if not
- *
- * Description:
- * Attempts to establish a connection to a server. The address should be
- * in one of the following forms:
- * - {"tcp", {"ipv4", ipv4_address, port_number}},
- * - {"tcp", {"ipv6", ipv6_address, port_number}},
- * - {"unix", socket_path}.
- * When the connection attempt is finished, the sys.connect() statement goes
- * up, and the 'is_error' variable should be used to check for connection
- * failure. If there was no error, the read(), write() and close() methods
- * can be used to work with the connection.
- * If an error occurs after the connection has been established, the
- * sys.connect() statement will automatically trigger backtracking, and the
- * 'is_error' variable will be changed to "true". This means that all
- * errors with the connection can be handled at the place of sys.connect(),
- * and no special care is normally needed to handle error in read() and
- * write().
- * WARNING: when you're not trying to either send or receive data, the
- * connection may be unable to detect any events with the connection.
- * You should never be neither sending nor receiving for an indefinite time.
- *
- * Synopsis:
- * sys.socket::read()
- *
- * Variables:
- * string (empty) - some data received from the socket, or empty on EOF
- * string not_eof - "true" if EOF was not encountered, "false" if it was
- *
- * Description:
- * Receives data from the connection. If EOF was encountered (remote host
- * has closed the connection), this returns no data. Otherwise it returns
- * at least one byte.
- * WARNING: after you receive EOF from a sys.listen() type socket, is is
- * your responsibility to call close() eventually, else the cline process
- * may remain alive indefinitely.
- * WARNING: this may return an arbitrarily small chunk of data. There is
- * no significance to the size of the chunks. Correct code will behave
- * the same no matter how the incoming data stream is split up.
- * WARNING: if a read() is terminated while it is still in progress, i.e.
- * has not gone up yet, then the connection is automatically closed, as
- * if close() was called.
- *
- * Synopsis:
- * sys.socket::write(string data)
- *
- * Description:
- * Sends data to the connection.
- * WARNING: this may block if the operating system's internal send buffer
- * is full. Be careful not to enter a deadlock where both ends of the
- * connection are trying to send data to the other, but neither is trying
- * to receive any data.
- * WARNING: if a write() is terminated while it is still in progress, i.e.
- * has not gone up yet, then the connection is automatically closed, as
- * if close() was called.
- *
- * Synopsis:
- * sys.socket::close()
- *
- * Description:
- * Closes the connection. After this, any further read(), write() or close()
- * will trigger an error with the interpreter. For client sockets created
- * via sys.listen(), this will immediately trigger termination of the client
- * process.
- *
- * Synopsis:
- * sys.listen(string address, string client_template, list args)
- *
- * Variables:
- * string is_error - "true" if listening failed to inittialize, "false" if
- * not
- *
- * Special objects and variables in client_template:
- * sys.socket _socket - the socket object for the client
- * string _socket.client_addr - the address of the client. The form is
- * like the second part of the sys.connect() address format, e.g.
- * {"ipv4", "1.2.3.4", "4000"}.
- *
- * Description:
- * Starts listening on the specified address. The 'is_error' variable
- * reflects the success of listening initiation. If listening succeeds,
- * then for every client that connects, a process is automatically created
- * from the template specified by 'client_template', and the 'args' list
- * is used as template arguments. Inside such processes, a special object
- * '_socket' is available, which represents the connection, and supports
- * the same methods as sys.connect(), i.e. read(), write() and close().
- * When an error occurs with the connection, the socket is automatically
- * closed, triggering process termination.
- */
- #include <stdlib.h>
- #include <limits.h>
- #include <stdarg.h>
- #include <misc/offset.h>
- #include <misc/debug.h>
- #include <structure/LinkedList0.h>
- #include <system/BConnection.h>
- #include <system/BConnectionGeneric.h>
- #include <ncd/NCDModule.h>
- #include <ncd/extra/value_utils.h>
- #include <ncd/extra/address_utils.h>
- #include <generated/blog_channel_ncd_socket.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- #define CONNECTION_TYPE_CONNECT 1
- #define CONNECTION_TYPE_LISTEN 2
- #define CONNECTION_STATE_CONNECTING 1
- #define CONNECTION_STATE_ESTABLISHED 2
- #define CONNECTION_STATE_ERROR 3
- #define CONNECTION_STATE_ABORTED 4
- #define READ_BUF_SIZE 1024
- struct connection {
- union {
- struct {
- NCDModuleInst *i;
- BConnector connector;
- } connect;
- struct {
- struct listen_instance *listen_inst;
- LinkedList0Node clients_list_node;
- BAddr addr;
- NCDModuleProcess process;
- } listen;
- };
-
- unsigned int type:2;
- unsigned int state:3;
- unsigned int recv_closed:1;
- BConnection connection;
- struct read_instance *read_inst;
- struct write_instance *write_inst;
- };
- struct read_instance {
- NCDModuleInst *i;
- struct connection *con_inst;
- size_t read_size;
- char buf[READ_BUF_SIZE];
- };
- struct write_instance {
- NCDModuleInst *i;
- struct connection *con_inst;
- const char *data;
- size_t length;
- };
- struct listen_instance {
- NCDModuleInst *i;
- unsigned int have_error:1;
- unsigned int dying:1;
- NCDValRef client_template;
- NCDValRef client_template_args;
- BListener listener;
- LinkedList0 clients_list;
- };
- enum {STRING_IS_ERROR, STRING_NOT_EOF, STRING_SOCKET, STRING_SYS_SOCKET, STRING_CLIENT_ADDR};
- static struct NCD_string_request strings[] = {
- {"is_error"}, {"not_eof"}, {"_socket"}, {"sys.socket"}, {"client_addr"}, {NULL}
- };
- static void connection_log (struct connection *o, int level, const char *fmt, ...);
- static void connection_free_connection (struct connection *o);
- static void connection_error (struct connection *o);
- static void connection_abort (struct connection *o);
- static void connection_connector_handler (void *user, int is_error);
- static void connection_connection_handler (void *user, int event);
- static void connection_send_handler_done (void *user, int data_len);
- static void connection_recv_handler_done (void *user, int data_len);
- static void connection_process_handler (struct NCDModuleProcess_s *process, int event);
- static int connection_process_func_getspecialobj (struct NCDModuleProcess_s *process, NCD_string_id_t name, NCDObject *out_object);
- static int connection_process_socket_obj_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value);
- static void listen_listener_handler (void *user);
- static void connection_log (struct connection *o, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
-
- switch (o->type) {
- case CONNECTION_TYPE_CONNECT: {
- NCDModuleInst_Backend_LogVarArg(o->connect.i, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- } break;
-
- case CONNECTION_TYPE_LISTEN: {
- if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
- char addr_str[BADDR_MAX_PRINT_LEN];
- BAddr_Print(&o->listen.addr, addr_str);
- BLog_Append("client %s: ", addr_str);
- NCDModuleInst_Backend_LogVarArg(o->listen.listen_inst->i, BLOG_CURRENT_CHANNEL, level, fmt, vl);
- }
- } break;
-
- default: ASSERT(0);
- }
-
- va_end(vl);
- }
- static void connection_free_connection (struct connection *o)
- {
- // disconnect read instance
- if (o->read_inst) {
- ASSERT(o->read_inst->con_inst == o)
- o->read_inst->con_inst = NULL;
- }
-
- // disconnect write instance
- if (o->write_inst) {
- ASSERT(o->write_inst->con_inst == o)
- o->write_inst->con_inst = NULL;
- }
-
- // free connection interfaces
- BConnection_RecvAsync_Free(&o->connection);
- BConnection_SendAsync_Free(&o->connection);
-
- // free connection
- BConnection_Free(&o->connection);
- }
- static void connection_error (struct connection *o)
- {
- ASSERT(o->state == CONNECTION_STATE_CONNECTING ||
- o->state == CONNECTION_STATE_ESTABLISHED)
-
- // for listen clients, we don't report errors directly,
- // we just terminate the client process
- if (o->type == CONNECTION_TYPE_LISTEN) {
- ASSERT(o->state != CONNECTION_STATE_CONNECTING)
- connection_abort(o);
- return;
- }
-
- // free connector
- if (o->state == CONNECTION_STATE_CONNECTING) {
- BConnector_Free(&o->connect.connector);
- }
-
- // free connection resources
- if (o->state == CONNECTION_STATE_ESTABLISHED) {
- connection_free_connection(o);
- }
-
- // trigger reporting of failure
- if (o->state == CONNECTION_STATE_ESTABLISHED) {
- NCDModuleInst_Backend_Down(o->connect.i);
- }
- NCDModuleInst_Backend_Up(o->connect.i);
-
- // set state
- o->state = CONNECTION_STATE_ERROR;
- }
- static void connection_abort (struct connection *o)
- {
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
-
- // free connection resources
- connection_free_connection(o);
-
- // if this is a listen connection, terminate client process
- if (o->type == CONNECTION_TYPE_LISTEN) {
- NCDModuleProcess_Terminate(&o->listen.process);
- }
-
- // set state
- o->state = CONNECTION_STATE_ABORTED;
- }
- static void connection_connector_handler (void *user, int is_error)
- {
- struct connection *o = user;
- ASSERT(o->type == CONNECTION_TYPE_CONNECT)
- ASSERT(o->state == CONNECTION_STATE_CONNECTING)
-
- // check error
- if (is_error) {
- connection_log(o, BLOG_ERROR, "connection failed");
- goto fail;
- }
-
- // init connection
- if (!BConnection_Init(&o->connection, BConnection_source_connector(&o->connect.connector), o->connect.i->params->iparams->reactor, o, connection_connection_handler)) {
- connection_log(o, BLOG_ERROR, "BConnection_Init failed");
- goto fail;
- }
-
- // init connection interfaces
- BConnection_SendAsync_Init(&o->connection);
- BConnection_RecvAsync_Init(&o->connection);
-
- // setup send/recv done callbacks
- StreamPassInterface_Sender_Init(BConnection_SendAsync_GetIf(&o->connection), connection_send_handler_done, o);
- StreamRecvInterface_Receiver_Init(BConnection_RecvAsync_GetIf(&o->connection), connection_recv_handler_done, o);
-
- // set not reading, not writing, recv not closed
- o->read_inst = NULL;
- o->write_inst = NULL;
- o->recv_closed = 0;
-
- // free connector
- BConnector_Free(&o->connect.connector);
-
- // set state
- o->state = CONNECTION_STATE_ESTABLISHED;
-
- // go up
- NCDModuleInst_Backend_Up(o->connect.i);
- return;
-
- fail:
- connection_error(o);
- }
- static void connection_connection_handler (void *user, int event)
- {
- struct connection *o = user;
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
- ASSERT(event == BCONNECTION_EVENT_RECVCLOSED || event == BCONNECTION_EVENT_ERROR)
- ASSERT(event != BCONNECTION_EVENT_RECVCLOSED || !o->recv_closed)
-
- if (event == BCONNECTION_EVENT_RECVCLOSED) {
- // if we have read operation, make it finish with eof
- if (o->read_inst) {
- ASSERT(o->read_inst->con_inst == o)
- o->read_inst->con_inst = NULL;
- o->read_inst->read_size = 0;
- NCDModuleInst_Backend_Up(o->read_inst->i);
- o->read_inst = NULL;
- }
-
- // set recv closed
- o->recv_closed = 1;
- return;
- }
-
- connection_log(o, BLOG_ERROR, "connection error");
-
- // handle error
- connection_error(o);
- }
- static void connection_send_handler_done (void *user, int data_len)
- {
- struct connection *o = user;
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
- ASSERT(o->write_inst)
- ASSERT(o->write_inst->con_inst == o)
- ASSERT(o->write_inst->length > 0)
- ASSERT(data_len > 0)
- ASSERT(data_len <= o->write_inst->length)
-
- struct write_instance *wr = o->write_inst;
-
- // update send state
- wr->data += data_len;
- wr->length -= data_len;
-
- // if there's more to send, send again
- if (wr->length > 0) {
- size_t to_send = (wr->length > INT_MAX ? INT_MAX : wr->length);
- StreamPassInterface_Sender_Send(BConnection_SendAsync_GetIf(&o->connection), (uint8_t *)wr->data, to_send);
- return;
- }
-
- // finish write operation
- wr->con_inst = NULL;
- NCDModuleInst_Backend_Up(wr->i);
- o->write_inst = NULL;
- }
- static void connection_recv_handler_done (void *user, int data_len)
- {
- struct connection *o = user;
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
- ASSERT(o->read_inst)
- ASSERT(o->read_inst->con_inst == o)
- ASSERT(!o->recv_closed)
- ASSERT(data_len > 0)
- ASSERT(data_len <= READ_BUF_SIZE)
-
- struct read_instance *re = o->read_inst;
-
- // finish read operation
- re->con_inst = NULL;
- re->read_size = data_len;
- NCDModuleInst_Backend_Up(re->i);
- o->read_inst = NULL;
- }
- static void connection_process_handler (struct NCDModuleProcess_s *process, int event)
- {
- struct connection *o = UPPER_OBJECT(process, struct connection, listen.process);
- ASSERT(o->type == CONNECTION_TYPE_LISTEN)
-
- switch (event) {
- case NCDMODULEPROCESS_EVENT_UP: {
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
- } break;
-
- case NCDMODULEPROCESS_EVENT_DOWN: {
- ASSERT(o->state == CONNECTION_STATE_ESTABLISHED)
- NCDModuleProcess_Continue(&o->listen.process);
- } break;
-
- case NCDMODULEPROCESS_EVENT_TERMINATED: {
- ASSERT(o->state == CONNECTION_STATE_ABORTED)
-
- struct listen_instance *li = o->listen.listen_inst;
- ASSERT(!li->have_error)
-
- // remove from clients list
- LinkedList0_Remove(&li->clients_list, &o->listen.clients_list_node);
-
- // free process
- NCDModuleProcess_Free(&o->listen.process);
-
- // free connection structure
- free(o);
-
- // if listener is dying and this was the last process, have it die
- if (li->dying && LinkedList0_IsEmpty(&li->clients_list)) {
- NCDModuleInst_Backend_Dead(li->i);
- }
- } break;
-
- default: ASSERT(0);
- }
- }
- static int connection_process_func_getspecialobj (struct NCDModuleProcess_s *process, NCD_string_id_t name, NCDObject *out_object)
- {
- struct connection *o = UPPER_OBJECT(process, struct connection, listen.process);
- ASSERT(o->type == CONNECTION_TYPE_LISTEN)
-
- if (name == strings[STRING_SOCKET].id) {
- *out_object = NCDObject_Build(strings[STRING_SYS_SOCKET].id, o, connection_process_socket_obj_func_getvar, NCDObject_no_getobj);
- return 1;
- }
-
- return 0;
- }
- static int connection_process_socket_obj_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
- {
- struct connection *o = NCDObject_DataPtr(obj);
- ASSERT(o->type == CONNECTION_TYPE_LISTEN)
-
- if (name == strings[STRING_CLIENT_ADDR].id) {
- *out_value = ncd_make_baddr(o->listen.addr, mem);
- if (NCDVal_IsInvalid(*out_value)) {
- connection_log(o, BLOG_ERROR, "ncd_make_baddr failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void listen_listener_handler (void *user)
- {
- struct listen_instance *o = user;
- ASSERT(!o->have_error)
- ASSERT(!o->dying)
-
- // allocate connection structure
- struct connection *con = malloc(sizeof(*con));
- if (!con) {
- ModuleLog(o->i, BLOG_ERROR, "malloc failed");
- goto fail0;
- }
-
- // set connection type and listen instance
- con->type = CONNECTION_TYPE_LISTEN;
- con->listen.listen_inst = o;
-
- // init connection
- if (!BConnection_Init(&con->connection, BConnection_source_listener(&o->listener, &con->listen.addr), o->i->params->iparams->reactor, con, connection_connection_handler)) {
- ModuleLog(o->i, BLOG_ERROR, "BConnection_Init failed");
- goto fail1;
- }
-
- // init connection interfaces
- BConnection_SendAsync_Init(&con->connection);
- BConnection_RecvAsync_Init(&con->connection);
-
- // setup send/recv done callbacks
- StreamPassInterface_Sender_Init(BConnection_SendAsync_GetIf(&con->connection), connection_send_handler_done, con);
- StreamRecvInterface_Receiver_Init(BConnection_RecvAsync_GetIf(&con->connection), connection_recv_handler_done, con);
-
- // init process
- if (!NCDModuleProcess_InitValue(&con->listen.process, o->i, o->client_template, o->client_template_args, connection_process_handler)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_InitValue failed");
- goto fail2;
- }
-
- // set special objects callback
- NCDModuleProcess_SetSpecialFuncs(&con->listen.process, connection_process_func_getspecialobj);
-
- // insert to clients list
- LinkedList0_Prepend(&o->clients_list, &con->listen.clients_list_node);
-
- // set not reading, not writing, recv not closed
- con->read_inst = NULL;
- con->write_inst = NULL;
- con->recv_closed = 0;
-
- // set state
- con->state = CONNECTION_STATE_ESTABLISHED;
- return;
-
- fail2:
- BConnection_RecvAsync_Free(&con->connection);
- BConnection_SendAsync_Free(&con->connection);
- BConnection_Free(&con->connection);
- fail1:
- free(con);
- fail0:
- return;
- }
- static void connect_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct connection *o = vo;
- o->type = CONNECTION_TYPE_CONNECT;
- o->connect.i = i;
-
- // pass connection pointer to methods so the same methods can work for
- // listen type connections
- NCDModuleInst_Backend_PassMemToMethods(i);
-
- // read arguments
- NCDValRef address_arg;
- if (!NCDVal_ListRead(params->args, 1, &address_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
-
- // read address
- struct BConnection_addr address;
- if (!ncd_read_bconnection_addr(address_arg, &address)) {
- ModuleLog(i, BLOG_ERROR, "wrong address");
- goto error;
- }
-
- // init connector
- if (!BConnector_InitGeneric(&o->connect.connector, address, i->params->iparams->reactor, o, connection_connector_handler)) {
- ModuleLog(i, BLOG_ERROR, "BConnector_InitGeneric failed");
- goto error;
- }
-
- // set state
- o->state = CONNECTION_STATE_CONNECTING;
- return;
-
- error:
- // go up in error state
- o->state = CONNECTION_STATE_ERROR;
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void connect_func_die (void *vo)
- {
- struct connection *o = vo;
- ASSERT(o->type == CONNECTION_TYPE_CONNECT)
-
- // free connector
- if (o->state == CONNECTION_STATE_CONNECTING) {
- BConnector_Free(&o->connect.connector);
- }
-
- // free connection resources
- if (o->state == CONNECTION_STATE_ESTABLISHED) {
- connection_free_connection(o);
- }
-
- NCDModuleInst_Backend_Dead(o->connect.i);
- }
- static int connect_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
- {
- struct connection *o = vo;
- ASSERT(o->type == CONNECTION_TYPE_CONNECT)
- ASSERT(o->state != CONNECTION_STATE_CONNECTING)
-
- if (name == strings[STRING_IS_ERROR].id) {
- int is_error = (o->state == CONNECTION_STATE_ERROR);
- *out = ncd_make_boolean(mem, is_error, o->connect.i->params->iparams->string_index);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->connect.i, BLOG_ERROR, "ncd_make_boolean failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct read_instance *o = vo;
- o->i = i;
-
- // read arguments
- if (!NCDVal_ListRead(params->args, 0)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
-
- // get connection
- struct connection *con_inst = params->method_user;
-
- // check connection state
- if (con_inst->state != CONNECTION_STATE_ESTABLISHED) {
- ModuleLog(i, BLOG_ERROR, "connection is not established");
- goto fail0;
- }
-
- // check if there's already a read in progress
- if (con_inst->read_inst) {
- ModuleLog(i, BLOG_ERROR, "read is already in progress");
- goto fail0;
- }
-
- // if eof was reached, go up immediately
- if (con_inst->recv_closed) {
- o->con_inst = NULL;
- o->read_size = 0;
- NCDModuleInst_Backend_Up(i);
- return;
- }
-
- // set connection
- o->con_inst = con_inst;
-
- // register read operation in connection
- con_inst->read_inst = o;
-
- // receive
- StreamRecvInterface_Receiver_Recv(BConnection_RecvAsync_GetIf(&con_inst->connection), (uint8_t *)o->buf, READ_BUF_SIZE);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void read_func_die (void *vo)
- {
- struct read_instance *o = vo;
-
- // if we're receiving, abort connection
- if (o->con_inst) {
- ASSERT(o->con_inst->state == CONNECTION_STATE_ESTABLISHED)
- ASSERT(o->con_inst->read_inst == o)
- connection_abort(o->con_inst);
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static int read_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
- {
- struct read_instance *o = vo;
- ASSERT(!o->con_inst)
-
- if (name == NCD_STRING_EMPTY) {
- *out = NCDVal_NewStringBin(mem, (const uint8_t *)o->buf, o->read_size);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewStringBin failed");
- }
- return 1;
- }
-
- if (name == strings[STRING_NOT_EOF].id) {
- *out = ncd_make_boolean(mem, (o->read_size != 0), o->i->params->iparams->string_index);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void write_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct write_instance *o = vo;
- o->i = i;
-
- // read arguments
- NCDValRef data_arg;
- if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsString(data_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- // get connection
- struct connection *con_inst = params->method_user;
-
- // check connection state
- if (con_inst->state != CONNECTION_STATE_ESTABLISHED) {
- ModuleLog(i, BLOG_ERROR, "connection is not established");
- goto fail0;
- }
-
- // check if there's already a write in progress
- if (con_inst->write_inst) {
- ModuleLog(i, BLOG_ERROR, "write is already in progress");
- goto fail0;
- }
-
- // set send state
- o->data = NCDVal_StringData(data_arg);
- o->length = NCDVal_StringLength(data_arg);
-
- // if there's nothing to send, go up immediately
- if (o->length == 0) {
- o->con_inst = NULL;
- NCDModuleInst_Backend_Up(i);
- return;
- }
-
- // set connection
- o->con_inst = con_inst;
-
- // register write operation in connection
- con_inst->write_inst = o;
-
- // send
- size_t to_send = (o->length > INT_MAX ? INT_MAX : o->length);
- StreamPassInterface_Sender_Send(BConnection_SendAsync_GetIf(&con_inst->connection), (uint8_t *)o->data, to_send);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void write_func_die (void *vo)
- {
- struct write_instance *o = vo;
-
- // if we're sending, abort connection
- if (o->con_inst) {
- ASSERT(o->con_inst->state == CONNECTION_STATE_ESTABLISHED)
- ASSERT(o->con_inst->write_inst == o)
- connection_abort(o->con_inst);
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void close_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- // read arguments
- if (!NCDVal_ListRead(params->args, 0)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
-
- // get connection
- struct connection *con_inst = params->method_user;
-
- // check connection state
- if (con_inst->state != CONNECTION_STATE_ESTABLISHED) {
- ModuleLog(i, BLOG_ERROR, "connection is not established");
- goto fail0;
- }
-
- // abort
- connection_abort(con_inst);
-
- // go up
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void listen_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- struct listen_instance *o = vo;
- o->i = i;
-
- // read arguments
- NCDValRef address_arg;
- NCDValRef client_template_arg;
- NCDValRef args_arg;
- if (!NCDVal_ListRead(params->args, 3, &address_arg, &client_template_arg, &args_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsString(client_template_arg) || !NCDVal_IsList(args_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- // remember client template and arguments
- o->client_template = client_template_arg;
- o->client_template_args = args_arg;
-
- // set no error, not dying
- o->have_error = 0;
- o->dying = 0;
-
- // read address
- struct BConnection_addr address;
- if (!ncd_read_bconnection_addr(address_arg, &address)) {
- ModuleLog(i, BLOG_ERROR, "wrong address");
- goto error;
- }
-
- // init listener
- if (!BListener_InitGeneric(&o->listener, address, i->params->iparams->reactor, o, listen_listener_handler)) {
- ModuleLog(i, BLOG_ERROR, "BListener_InitGeneric failed");
- goto error;
- }
-
- // init clients list
- LinkedList0_Init(&o->clients_list);
-
- // go up
- NCDModuleInst_Backend_Up(i);
- return;
-
- error:
- // go up with error
- o->have_error = 1;
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void listen_func_die (void *vo)
- {
- struct listen_instance *o = vo;
- ASSERT(!o->dying)
-
- // free listener
- if (!o->have_error) {
- BListener_Free(&o->listener);
- }
-
- // if we have no clients, die right away
- if (o->have_error || LinkedList0_IsEmpty(&o->clients_list)) {
- NCDModuleInst_Backend_Dead(o->i);
- return;
- }
-
- // set dying
- o->dying = 1;
-
- // abort all clients and wait for them
- for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->clients_list); ln; ln = LinkedList0Node_Next(ln)) {
- struct connection *con = UPPER_OBJECT(ln, struct connection, listen.clients_list_node);
- ASSERT(con->type == CONNECTION_TYPE_LISTEN)
- ASSERT(con->listen.listen_inst == o)
- ASSERT(con->state == CONNECTION_STATE_ESTABLISHED || con->state == CONNECTION_STATE_ABORTED)
-
- if (con->state != CONNECTION_STATE_ABORTED) {
- connection_abort(con);
- }
- }
- }
- static int listen_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
- {
- struct listen_instance *o = vo;
-
- if (name == strings[STRING_IS_ERROR].id) {
- *out = ncd_make_boolean(mem, o->have_error, o->i->params->iparams->string_index);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
- }
- return 1;
- }
-
- return 0;
- }
- static struct NCDModule modules[] = {
- {
- .type = "sys.connect",
- .base_type = "sys.socket",
- .func_new2 = connect_func_new,
- .func_die = connect_func_die,
- .func_getvar2 = connect_func_getvar,
- .alloc_size = sizeof(struct connection)
- }, {
- .type = "sys.socket::read",
- .func_new2 = read_func_new,
- .func_die = read_func_die,
- .func_getvar2 = read_func_getvar,
- .alloc_size = sizeof(struct read_instance)
- }, {
- .type = "sys.socket::write",
- .func_new2 = write_func_new,
- .func_die = write_func_die,
- .alloc_size = sizeof(struct write_instance)
- }, {
- .type = "sys.socket::close",
- .func_new2 = close_func_new
- }, {
- .type = "sys.listen",
- .func_new2 = listen_func_new,
- .func_die = listen_func_die,
- .func_getvar2 = listen_func_getvar,
- .alloc_size = sizeof(struct listen_instance)
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_socket = {
- .modules = modules,
- .strings = strings
- };
|