libamxp  1.4.0
Patterns C Implementation
amxp_connections.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <string.h>
60 
61 #include <amxc/amxc_macros.h>
62 #include <amxc/amxc.h>
63 #include <amxp/amxp_signal.h>
64 #include <amxp/amxp_connection.h>
65 
66 static amxc_llist_t listeners;
67 static amxc_llist_t connections;
68 
70  int fd) {
71  amxp_connection_t* con = NULL;
72 
73  amxc_llist_for_each(it, list) {
74  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
75  if(con->fd == fd) {
76  break;
77  }
78  con = NULL;
79  }
80 
81  return con;
82 }
83 
84 static int amxp_can_add_connection(int fd,
85  uint32_t type) {
86  int retval = -1;
87  amxp_connection_t* con = NULL;
88 
89  if(type == AMXP_CONNECTION_LISTEN) {
91  } else {
93  }
94 
95  retval = con == NULL ? 0 : -1;
96 
97  return retval;
98 }
99 
102  int retval = -1;
103  amxc_var_t var_fd;
104 
105  amxc_var_init(&var_fd);
106  amxc_var_set(fd_t, &var_fd, con->fd);
107 
108  con->reader = reader;
109  if(con->type == AMXP_CONNECTION_LISTEN) {
110  amxc_llist_append(&listeners, &con->it);
111  amxp_sigmngr_trigger_signal(NULL, "listen-added", &var_fd);
112  } else {
113  amxc_llist_append(&connections, &con->it);
114  amxp_sigmngr_trigger_signal(NULL, "connection-added", &var_fd);
115  }
116 
117  retval = 0;
118 
119  amxc_var_clean(&var_fd);
120  return retval;
121 }
122 
123 static void amxp_connection_free(amxc_llist_it_t* it) {
124  amxp_connection_t* con = amxc_llist_it_get_data(it, amxp_connection_t, it);
125  free(con->uri);
126  free(con);
127 }
128 
131  const char* uri,
132  uint32_t type,
133  void* priv) {
134  int retval = -1;
135  amxp_connection_t* con = NULL;
136 
137  when_null(reader, exit);
138  when_true(fd < 0, exit);
139 
140  retval = amxp_can_add_connection(fd, type);
141  when_failed(retval, exit);
142 
143  retval = -1;
144  con = (amxp_connection_t*) calloc(1, sizeof(amxp_connection_t));
145  when_null(con, exit);
146 
147  con->uri = uri == NULL ? NULL : strdup(uri);
148  con->fd = fd;
149  con->priv = priv;
150  con->type = type;
151 
152  retval = amxp_connection_add_impl(con, reader);
153 
154 exit:
155  if(retval != 0) {
156  free(con);
157  }
158  return retval;
159 }
160 
162  amxp_fd_cb_t can_write_cb) {
163  int retval = -1;
164  amxp_connection_t* con = NULL;
165  amxc_var_t var_fd;
166  const char* signal = "connection-wait-write";
167 
168  amxc_var_init(&var_fd);
169  when_true(fd < 0, exit);
170  when_null(can_write_cb, exit);
171 
173  when_null(con, exit);
174 
175  amxc_var_set(fd_t, &var_fd, fd);
176  amxp_sigmngr_trigger_signal(NULL, signal, &var_fd);
177  con->can_write = can_write_cb;
178 
179  retval = 0;
180 
181 exit:
182  amxc_var_clean(&var_fd);
183  return retval;
184 }
185 
187  int retval = -1;
188  amxp_connection_t* con = NULL;
189  amxc_var_t var_fd;
190  const char* signal = "connection-deleted";
191 
192  amxc_var_init(&var_fd);
193 
195  if(con == NULL) {
196  signal = "listen-deleted";
198  }
199  when_null(con, exit);
200 
201  amxc_var_set(fd_t, &var_fd, fd);
202  amxp_sigmngr_trigger_signal(NULL, signal, &var_fd);
203  amxc_llist_it_clean(&con->it, amxp_connection_free);
204 
205  retval = 0;
206 
207 exit:
208  amxc_var_clean(&var_fd);
209  return retval;
210 }
211 
213  amxp_connection_t* con = NULL;
214 
216  if(con != NULL) {
217  goto exit;
218  }
220 
221 exit:
222  return con;
223 }
224 
226  amxp_connection_t* con = NULL;
227 
228  amxc_llist_for_each(it, &connections) {
229  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
230  if(con->type == type) {
231  break;
232  }
233  con = NULL;
234  }
235 
236  return con;
237 }
238 
240  uint32_t type) {
241  amxc_llist_it_t* it = NULL;
242  when_null(con, exit);
243  when_true(con->it.llist != &connections, exit);
244 
245  it = amxc_llist_it_get_next(&con->it);
246  con = NULL;
247  while(it) {
248  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
249  if(con->type == type) {
250  break;
251  }
252  con = NULL;
253  it = amxc_llist_it_get_next(it);
254  }
255 
256 exit:
257  return con;
258 }
259 
261  void* el_data) {
262  int retval = -1;
263  amxp_connection_t* con = NULL;
264 
266  if(con == NULL) {
268  }
269 
270  when_null(con, exit);
271  con->el_data = el_data;
272 
273  retval = 0;
274 
275 exit:
276  return retval;
277 }
278 
279 amxc_llist_t* amxp_connection_get_connections(void) {
280  return &connections;
281 }
282 
283 amxc_llist_t* amxp_connection_get_listeners(void) {
284  return &listeners;
285 }
286 
287 DESTRUCTOR static void amxp_connection_cleanup(void) {
288  amxc_llist_clean(&connections, amxp_connection_free);
289  amxc_llist_clean(&listeners, amxp_connection_free);
290 }
void(* amxp_fd_cb_t)(int fd, void *priv)
#define AMXP_CONNECTION_LISTEN
static int amxp_can_add_connection(int fd, uint32_t type)
static DESTRUCTOR void amxp_connection_cleanup(void)
static void amxp_connection_free(amxc_llist_it_t *it)
static int amxp_connection_add_impl(amxp_connection_t *con, amxp_fd_cb_t reader)
static amxc_llist_t connections
static amxc_llist_t listeners
static amxp_connection_t * amxp_connection_get_internal(amxc_llist_t *list, int fd)
Ambiorix signal manager and signal API header file.
int amxp_connection_add(int fd, amxp_fd_cb_t reader, const char *uri, uint32_t type, void *priv)
Adds a file descriptor (fd) to the list of fds that must be watched.
amxc_llist_t * amxp_connection_get_listeners(void)
Get of the current listen sockets of the application.
amxp_connection_t * amxp_connection_get_next(amxp_connection_t *con, uint32_t type)
Gets the next connection data for a file descriptor.
int amxp_connection_set_el_data(int fd, void *el_data)
Sets event-loop data.
int amxp_connection_wait_write(int fd, amxp_fd_cb_t can_write_cb)
Adds a watcher to check if a fd is ready for write.
amxp_connection_t * amxp_connection_get_first(uint32_t type)
Gets the first connection of the given type.
amxp_connection_t * amxp_connection_get(int fd)
Gets the connection data for a file descriptor.
amxc_llist_t * amxp_connection_get_connections(void)
Get a list of the current connections of the application.
int amxp_connection_remove(int fd)
Removes the fd from the connection list.
void amxp_sigmngr_trigger_signal(amxp_signal_mngr_t *const sig_mngr, const char *name, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:492
amxc_llist_it_t it
amxp_fd_cb_t can_write
amxp_fd_cb_t reader
static void reader(UNUSED int fd, UNUSED void *priv)
static char * el_data