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 */
20 static void gras_trp_socket_free(void *s); /* free one socket */
23 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup);
26 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
29 gras_trp_plugin_t *plug = malloc(sizeof(gras_trp_plugin_t));
31 DEBUG1("Create plugin %s",name);
36 memset(plug,0,sizeof(gras_trp_plugin_t));
38 plug->name=strdup(name);
42 errcode = setup(plug);
45 /* SG plugin return mismatch when in RL mode (and vice versa) */
51 TRY(gras_dict_set(_gras_trp_plugins,
52 name, plug, gras_trp_plugin_free));
66 /* make room for all plugins */
67 TRY(gras_dict_new(&_gras_trp_plugins));
70 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
71 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
72 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
79 gras_dict_free(&_gras_trp_plugins);
83 void gras_trp_plugin_free(void *p) {
84 gras_trp_plugin_t *plug = p;
89 } else if (plug->data) {
90 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
101 * gras_trp_socket_new:
103 * Malloc a new socket, and initialize it with defaults
105 gras_error_t gras_trp_socket_new(int incoming,
106 gras_socket_t **dst) {
110 if (! (sock=malloc(sizeof(gras_socket_t))) )
112 DEBUG1("Create a new socket (%p)", sock);
117 sock->incoming = incoming ? 1:0;
118 sock->outgoing = incoming ? 0:1;
119 sock->accepting = incoming ? 1:0;
122 sock->peer_port = -1;
123 sock->peer_name = NULL;
128 return gras_dynar_push(gras_socketset_get(),dst);
133 * gras_socket_server:
135 * Opens a server socket and make it ready to be listened to.
136 * In real life, you'll get a TCP socket.
139 gras_socket_server(unsigned short port,
140 /* OUT */ gras_socket_t **dst) {
142 gras_error_t errcode;
143 gras_trp_plugin_t *trp;
148 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
149 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
152 /* defaults settings */
153 TRY(gras_trp_socket_new(1,&sock));
157 /* Call plugin socket creation function */
158 errcode = trp->socket_server(trp, port, sock);
159 DEBUG3("in=%c out=%c accept=%c",
160 sock->incoming?'y':'n',
161 sock->outgoing?'y':'n',
162 sock->accepting?'y':'n');
164 if (errcode != no_error) {
175 * gras_socket_client:
177 * Opens a client socket to a remote host.
178 * In real life, you'll get a TCP socket.
181 gras_socket_client(const char *host,
183 /* OUT */ gras_socket_t **dst) {
185 gras_error_t errcode;
186 gras_trp_plugin_t *trp;
191 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
194 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
195 /* defaults settings */
196 TRY(gras_trp_socket_new(0,&sock));
198 sock->peer_port = port;
199 sock->peer_name = strdup(host?host:"localhost");
201 /* plugin-specific */
202 errcode= (* trp->socket_client)(trp,
203 host ? host : "localhost", port,
205 DEBUG3("in=%c out=%c accept=%c",
206 sock->incoming?'y':'n',
207 sock->outgoing?'y':'n',
208 sock->accepting?'y':'n');
210 if (errcode != no_error) {
220 void gras_socket_close(gras_socket_t *sock) {
221 gras_dynar_t *sockets = gras_socketset_get();
222 gras_socket_t *sock_iter;
225 /* FIXME: Issue an event when the socket is closed */
227 gras_dynar_foreach(sockets,cursor,sock_iter) {
228 if (sock == sock_iter) {
229 gras_dynar_cursor_rm(sockets,&cursor);
230 if ( sock->plugin->socket_close)
231 (* sock->plugin->socket_close)(sock);
233 /* free the memory */
235 free(sock->peer_name);
240 WARN0("Ignoring request to free an unknown socket");
245 * gras_trp_chunk_send:
247 * Send a bunch of bytes from on socket
250 gras_trp_chunk_send(gras_socket_t *sd,
253 gras_assert1(sd->outgoing,
254 "Socket not suited for data send (outgoing=%c)",
255 sd->outgoing?'y':'n');
256 gras_assert1(sd->plugin->chunk_send,
257 "No function chunk_send on transport plugin %s",
259 return (*sd->plugin->chunk_send)(sd,data,size);
262 * gras_trp_chunk_recv:
264 * Receive a bunch of bytes from a socket
267 gras_trp_chunk_recv(gras_socket_t *sd,
270 gras_assert0(sd->incoming,
271 "Socket not suited for data receive");
272 gras_assert1(sd->plugin->chunk_recv,
273 "No function chunk_recv on transport plugin %s",
275 return (sd->plugin->chunk_recv)(sd,data,size);
280 gras_trp_plugin_get_by_name(const char *name,
281 gras_trp_plugin_t **dst){
283 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
286 int gras_socket_my_port (gras_socket_t *sock) {
289 int gras_socket_peer_port(gras_socket_t *sock) {
290 return sock->peer_port;
292 char *gras_socket_peer_name(gras_socket_t *sock) {
293 return sock->peer_name;