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);
47 switch (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;
109 * gras_trp_socket_new:
111 * Malloc a new socket, and initialize it with defaults
113 gras_error_t gras_trp_socket_new(int incoming,
114 gras_socket_t **dst) {
118 if (! (sock=malloc(sizeof(gras_socket_t))) )
124 sock->incoming = incoming ? 1:0;
125 sock->outgoing = incoming ? 0:1;
126 sock->accepting = incoming ? 1:0;
129 sock->peer_port = -1;
130 sock->peer_name = NULL;
139 * gras_socket_server:
141 * Opens a server socket and make it ready to be listened to.
142 * In real life, you'll get a TCP socket.
145 gras_socket_server(unsigned short port,
146 /* OUT */ gras_socket_t **dst) {
148 gras_error_t errcode;
149 gras_trp_plugin_t *trp;
154 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
155 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
158 /* defaults settings */
159 TRY(gras_trp_socket_new(1,&sock));
163 /* Call plugin socket creation function */
164 errcode = trp->socket_server(trp, port, sock);
165 DEBUG3("in=%c out=%c accept=%c",
166 sock->incoming?'y':'n',
167 sock->outgoing?'y':'n',
168 sock->accepting?'y':'n');
170 if (errcode != no_error) {
176 /* Register this socket */
177 errcode = gras_dynar_push(_gras_trp_sockets,dst);
178 if (errcode != no_error) {
188 * gras_socket_client:
190 * Opens a client socket to a remote host.
191 * In real life, you'll get a TCP socket.
194 gras_socket_client(const char *host,
196 /* OUT */ gras_socket_t **dst) {
198 gras_error_t errcode;
199 gras_trp_plugin_t *trp;
204 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
207 /* defaults settings */
208 TRY(gras_trp_socket_new(0,&sock));
210 sock->peer_port = port;
211 sock->peer_name = strdup(host?host:"localhost");
213 /* plugin-specific */
214 errcode= (* trp->socket_client)(trp,
215 host ? host : "localhost", port,
217 DEBUG3("in=%c out=%c accept=%c",
218 sock->incoming?'y':'n',
219 sock->outgoing?'y':'n',
220 sock->accepting?'y':'n');
222 if (errcode != no_error) {
227 /* register socket */
229 errcode = gras_dynar_push(_gras_trp_sockets,dst);
230 if (errcode != no_error) {
239 void gras_socket_close(gras_socket_t **sock) {
240 gras_socket_t *sock_iter;
243 /* FIXME: Issue an event when the socket is closed */
245 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
246 if (*sock == sock_iter) {
247 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
248 if ( (*sock)->plugin->socket_close)
249 (* (*sock)->plugin->socket_close)(*sock);
251 /* free the memory */
257 WARN0("Ignoring request to free an unknown socket");
262 * gras_trp_chunk_send:
264 * Send a bunch of bytes from on socket
267 gras_trp_chunk_send(gras_socket_t *sd,
270 gras_assert1(sd->outgoing,
271 "Socket not suited for data send (outgoing=%c)",
272 sd->outgoing?'y':'n');
273 gras_assert1(sd->plugin->chunk_send,
274 "No function chunk_send on transport plugin %s",
276 return (*sd->plugin->chunk_send)(sd,data,size);
279 * gras_trp_chunk_recv:
281 * Receive a bunch of bytes from a socket
284 gras_trp_chunk_recv(gras_socket_t *sd,
287 gras_assert0(sd->incoming,
288 "Socket not suited for data receive");
289 gras_assert1(sd->plugin->chunk_recv,
290 "No function chunk_recv on transport plugin %s",
292 return (sd->plugin->chunk_recv)(sd,data,size);
297 gras_trp_plugin_get_by_name(const char *name,
298 gras_trp_plugin_t **dst){
300 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
303 int gras_socket_my_port (gras_socket_t *sock) {
306 int gras_socket_peer_port(gras_socket_t *sock) {
307 return sock->peer_port;
309 char *gras_socket_peer_name(gras_socket_t *sock) {
310 return sock->peer_name;