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 */
25 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup);
28 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
31 gras_trp_plugin_t *plug = malloc(sizeof(gras_trp_plugin_t));
33 DEBUG1("Create plugin %s",name);
38 memset(plug,0,sizeof(gras_trp_plugin_t));
40 plug->name=strdup(name);
44 errcode = setup(plug);
47 /* SG plugin return mismatch when in RL mode (and vice versa) */
53 TRY(gras_dict_set(_gras_trp_plugins,
54 name, plug, gras_trp_plugin_free));
68 /* make room for all socket ownership descriptions */
69 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
71 /* make room for all plugins */
72 TRY(gras_dict_new(&_gras_trp_plugins));
75 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
76 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
77 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
84 gras_dict_free(&_gras_trp_plugins);
85 gras_dynar_free(_gras_trp_sockets);
89 void gras_trp_plugin_free(void *p) {
90 gras_trp_plugin_t *plug = p;
95 } else if (plug->data) {
96 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
107 * gras_trp_socket_new:
109 * Malloc a new socket, and initialize it with defaults
111 gras_error_t gras_trp_socket_new(int incoming,
112 gras_socket_t **dst) {
116 if (! (sock=malloc(sizeof(gras_socket_t))) )
122 sock->incoming = incoming ? 1:0;
123 sock->outgoing = incoming ? 0:1;
124 sock->accepting = incoming ? 1:0;
127 sock->peer_port = -1;
128 sock->peer_name = NULL;
137 * gras_socket_server:
139 * Opens a server socket and make it ready to be listened to.
140 * In real life, you'll get a TCP socket.
143 gras_socket_server(unsigned short port,
144 /* OUT */ gras_socket_t **dst) {
146 gras_error_t errcode;
147 gras_trp_plugin_t *trp;
152 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
153 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
156 /* defaults settings */
157 TRY(gras_trp_socket_new(1,&sock));
161 /* Call plugin socket creation function */
162 errcode = trp->socket_server(trp, port, sock);
163 DEBUG3("in=%c out=%c accept=%c",
164 sock->incoming?'y':'n',
165 sock->outgoing?'y':'n',
166 sock->accepting?'y':'n');
168 if (errcode != no_error) {
174 /* Register this socket */
175 errcode = gras_dynar_push(_gras_trp_sockets,dst);
176 if (errcode != no_error) {
186 * gras_socket_client:
188 * Opens a client socket to a remote host.
189 * In real life, you'll get a TCP socket.
192 gras_socket_client(const char *host,
194 /* OUT */ gras_socket_t **dst) {
196 gras_error_t errcode;
197 gras_trp_plugin_t *trp;
202 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
205 /* defaults settings */
206 TRY(gras_trp_socket_new(0,&sock));
208 sock->peer_port = port;
209 sock->peer_name = strdup(host?host:"localhost");
211 /* plugin-specific */
212 errcode= (* trp->socket_client)(trp,
213 host ? host : "localhost", port,
215 DEBUG3("in=%c out=%c accept=%c",
216 sock->incoming?'y':'n',
217 sock->outgoing?'y':'n',
218 sock->accepting?'y':'n');
220 if (errcode != no_error) {
225 /* register socket */
227 errcode = gras_dynar_push(_gras_trp_sockets,dst);
228 if (errcode != no_error) {
237 void gras_socket_close(gras_socket_t *sock) {
238 gras_socket_t *sock_iter;
241 /* FIXME: Issue an event when the socket is closed */
243 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
244 if (sock == sock_iter) {
245 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
246 if ( sock->plugin->socket_close)
247 (* sock->plugin->socket_close)(sock);
249 /* free the memory */
254 WARN0("Ignoring request to free an unknown socket");
259 * gras_trp_chunk_send:
261 * Send a bunch of bytes from on socket
264 gras_trp_chunk_send(gras_socket_t *sd,
267 gras_assert1(sd->outgoing,
268 "Socket not suited for data send (outgoing=%c)",
269 sd->outgoing?'y':'n');
270 gras_assert1(sd->plugin->chunk_send,
271 "No function chunk_send on transport plugin %s",
273 return (*sd->plugin->chunk_send)(sd,data,size);
276 * gras_trp_chunk_recv:
278 * Receive a bunch of bytes from a socket
281 gras_trp_chunk_recv(gras_socket_t *sd,
284 gras_assert0(sd->incoming,
285 "Socket not suited for data receive");
286 gras_assert1(sd->plugin->chunk_recv,
287 "No function chunk_recv on transport plugin %s",
289 return (sd->plugin->chunk_recv)(sd,data,size);
294 gras_trp_plugin_get_by_name(const char *name,
295 gras_trp_plugin_t **dst){
297 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
300 int gras_socket_my_port (gras_socket_t *sock) {
303 int gras_socket_peer_port(gras_socket_t *sock) {
304 return sock->peer_port;
306 char *gras_socket_peer_name(gras_socket_t *sock) {
307 return sock->peer_name;