3 /* transport - low level communication */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 Martin Quinson. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <time.h> /* time() */
13 #include "Transport/transport_private.h"
15 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
17 static gras_dict_t *_gras_trp_plugins; /* All registered plugins */
18 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
21 gras_dynar_t *_gras_trp_sockets; /* all existing sockets */
22 static void gras_trp_socket_free(void *s); /* free one socket */
27 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup);
30 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
33 gras_trp_plugin_t *plug = malloc(sizeof(gras_trp_plugin_t));
35 DEBUG1("Create plugin %s",name);
40 memset(plug,0,sizeof(gras_trp_plugin_t));
42 plug->name=strdup(name);
46 errcode = setup(plug);
49 /* SG plugin return mismatch when in RL mode (and vice versa) */
54 TRY(gras_dict_set(_gras_trp_plugins,
55 name, plug, gras_trp_plugin_free));
69 /* make room for all socket ownership descriptions */
70 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
72 /* We do not ear for any socket for now */
75 /* make room for all plugins */
76 TRY(gras_dict_new(&_gras_trp_plugins));
79 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
80 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
81 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
88 gras_dict_free(&_gras_trp_plugins);
89 gras_dynar_free(_gras_trp_sockets);
93 void gras_trp_plugin_free(void *p) {
94 gras_trp_plugin_t *plug = p;
99 } else if (plug->data) {
100 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
111 * gras_trp_socket_new:
113 * Malloc a new socket, and initialize it with defaults
115 gras_error_t gras_trp_socket_new(int incoming,
116 gras_socket_t **dst) {
120 if (! (sock=malloc(sizeof(gras_socket_t))) )
126 sock->incoming = incoming ? 1:0;
127 sock->outgoing = incoming ? 0:1;
128 sock->accepting = incoming ? 1:0;
131 sock->peer_port = -1;
132 sock->peer_name = NULL;
141 * gras_socket_server:
143 * Opens a server socket and make it ready to be listened to.
144 * In real life, you'll get a TCP socket.
147 gras_socket_server(unsigned short port,
148 /* OUT */ gras_socket_t **dst) {
150 gras_error_t errcode;
151 gras_trp_plugin_t *trp;
156 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
157 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
160 /* defaults settings */
161 TRY(gras_trp_socket_new(1,&sock));
165 /* Call plugin socket creation function */
166 errcode = trp->socket_server(trp, port, sock);
167 DEBUG3("in=%c out=%c accept=%c",
168 sock->incoming?'y':'n',
169 sock->outgoing?'y':'n',
170 sock->accepting?'y':'n');
172 if (errcode != no_error) {
178 /* Register this socket */
179 errcode = gras_dynar_push(_gras_trp_sockets,dst);
180 if (errcode != no_error) {
190 * gras_socket_client:
192 * Opens a client socket to a remote host.
193 * In real life, you'll get a TCP socket.
196 gras_socket_client(const char *host,
198 /* OUT */ gras_socket_t **dst) {
200 gras_error_t errcode;
201 gras_trp_plugin_t *trp;
206 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
209 /* defaults settings */
210 TRY(gras_trp_socket_new(0,&sock));
212 sock->peer_port = port;
213 sock->peer_name = strdup(host?host:"localhost");
215 /* plugin-specific */
216 errcode= (* trp->socket_client)(trp,
217 host ? host : "localhost", port,
219 DEBUG3("in=%c out=%c accept=%c",
220 sock->incoming?'y':'n',
221 sock->outgoing?'y':'n',
222 sock->accepting?'y':'n');
224 if (errcode != no_error) {
229 /* register socket */
231 errcode = gras_dynar_push(_gras_trp_sockets,dst);
232 if (errcode != no_error) {
241 void gras_socket_close(gras_socket_t **sock) {
242 gras_socket_t *sock_iter;
245 /* FIXME: Issue an event when the socket is closed */
247 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
248 if (*sock == sock_iter) {
249 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
250 if ( (*sock)->plugin->socket_close)
251 (* (*sock)->plugin->socket_close)(*sock);
253 /* free the memory */
259 WARN0("Ignoring request to free an unknown socket");
264 * gras_trp_chunk_send:
266 * Send a bunch of bytes from on socket
269 gras_trp_chunk_send(gras_socket_t *sd,
272 gras_assert1(sd->outgoing,
273 "Socket not suited for data send (outgoing=%c)",
274 sd->outgoing?'y':'n');
275 gras_assert1(sd->plugin->chunk_send,
276 "No function chunk_send on transport plugin %s",
278 return (*sd->plugin->chunk_send)(sd,data,size);
281 * gras_trp_chunk_recv:
283 * Receive a bunch of bytes from a socket
286 gras_trp_chunk_recv(gras_socket_t *sd,
289 gras_assert0(sd->incoming,
290 "Socket not suited for data receive");
291 gras_assert1(sd->plugin->chunk_recv,
292 "No function chunk_recv on transport plugin %s",
294 return (sd->plugin->chunk_recv)(sd,data,size);
299 gras_trp_plugin_get_by_name(const char *name,
300 gras_trp_plugin_t **dst){
302 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
305 int gras_socket_my_port (gras_socket_t *sock) {
308 int gras_socket_peer_port(gras_socket_t *sock) {
309 return sock->peer_port;
311 char *gras_socket_peer_name(gras_socket_t *sock) {
312 return sock->peer_name;