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) */
55 TRY(gras_dict_set(_gras_trp_plugins,
56 name, plug, gras_trp_plugin_free));
70 /* make room for all socket ownership descriptions */
71 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
73 /* We do not ear for any socket for now */
76 /* make room for all plugins */
77 TRY(gras_dict_new(&_gras_trp_plugins));
80 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
81 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
82 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
89 gras_dict_free(&_gras_trp_plugins);
90 gras_dynar_free(_gras_trp_sockets);
94 void gras_trp_plugin_free(void *p) {
95 gras_trp_plugin_t *plug = p;
100 } else if (plug->data) {
101 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
112 * gras_trp_socket_new:
114 * Malloc a new socket, and initialize it with defaults
116 gras_error_t gras_trp_socket_new(int incoming,
117 gras_socket_t **dst) {
121 if (! (sock=malloc(sizeof(gras_socket_t))) )
127 sock->incoming = incoming ? 1:0;
128 sock->outgoing = incoming ? 0:1;
129 sock->accepting = incoming ? 1:0;
132 sock->peer_port = -1;
133 sock->peer_name = NULL;
142 * gras_socket_server:
144 * Opens a server socket and make it ready to be listened to.
145 * In real life, you'll get a TCP socket.
148 gras_socket_server(unsigned short port,
149 /* OUT */ gras_socket_t **dst) {
151 gras_error_t errcode;
152 gras_trp_plugin_t *trp;
157 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
158 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
161 /* defaults settings */
162 TRY(gras_trp_socket_new(1,&sock));
166 /* Call plugin socket creation function */
167 errcode = trp->socket_server(trp, port, sock);
168 DEBUG3("in=%c out=%c accept=%c",
169 sock->incoming?'y':'n',
170 sock->outgoing?'y':'n',
171 sock->accepting?'y':'n');
173 if (errcode != no_error) {
179 /* Register this socket */
180 errcode = gras_dynar_push(_gras_trp_sockets,dst);
181 if (errcode != no_error) {
191 * gras_socket_client:
193 * Opens a client socket to a remote host.
194 * In real life, you'll get a TCP socket.
197 gras_socket_client(const char *host,
199 /* OUT */ gras_socket_t **dst) {
201 gras_error_t errcode;
202 gras_trp_plugin_t *trp;
207 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
210 /* defaults settings */
211 TRY(gras_trp_socket_new(0,&sock));
213 sock->peer_port = port;
214 sock->peer_name = strdup(host?host:"localhost");
216 /* plugin-specific */
217 errcode= (* trp->socket_client)(trp,
218 host ? host : "localhost", port,
220 DEBUG3("in=%c out=%c accept=%c",
221 sock->incoming?'y':'n',
222 sock->outgoing?'y':'n',
223 sock->accepting?'y':'n');
225 if (errcode != no_error) {
230 /* register socket */
232 errcode = gras_dynar_push(_gras_trp_sockets,dst);
233 if (errcode != no_error) {
242 void gras_socket_close(gras_socket_t *sock) {
243 gras_socket_t *sock_iter;
246 /* FIXME: Issue an event when the socket is closed */
248 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
249 if (sock == sock_iter) {
250 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
251 if ( sock->plugin->socket_close)
252 (* sock->plugin->socket_close)(sock);
254 /* 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;