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() */
14 #include "Transport/transport_private.h"
17 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
19 static gras_dict_t *_gras_trp_plugins; /* All registered plugins */
20 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
23 gras_dynar_t *_gras_trp_sockets; /* all existing sockets */
24 static void gras_trp_socket_free(void *s); /* free one socket */
31 gras_trp_plugin_t *plug;
33 /* make room for all socket ownership descriptions */
34 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
36 /* We do not ear for any socket for now */
39 /* make room for all plugins */
40 TRY(gras_dict_new(&_gras_trp_plugins));
43 TRY(gras_trp_tcp_init(&plug));
44 TRY(gras_dict_insert(_gras_trp_plugins,
45 plug->name, plug, gras_trp_plugin_free));
48 TRY(gras_trp_file_init(&plug));
49 TRY(gras_dict_insert(_gras_trp_plugins,
50 plug->name, plug, gras_trp_plugin_free));
57 gras_dict_free(&_gras_trp_plugins);
58 gras_dynar_free(_gras_trp_sockets);
62 void gras_trp_plugin_free(void *p) {
63 gras_trp_plugin_t *plug = p;
66 if (plug->free_specific && plug->specific)
67 plug->free_specific(plug->specific);
76 * gras_trp_socket_new:
78 * Malloc a new socket, and initialize it with defaults
80 gras_error_t gras_trp_socket_new(int incoming,
81 gras_socket_t **dst) {
85 if (! (sock=malloc(sizeof(gras_socket_t))) )
91 sock->incoming = incoming? 1:0;
92 sock->outgoing = incoming? 0:1;
93 sock->accepting = incoming? 1:0;
94 DEBUG3("in=%c out=%c accept=%c",
95 sock->incoming?'y':'n',
96 sock->outgoing?'y':'n',
97 sock->accepting?'y':'n');
100 sock->peer_port = -1;
101 sock->peer_name = NULL;
109 * gras_socket_server:
111 * Opens a server socket and make it ready to be listened to.
112 * In real life, you'll get a TCP socket.
115 gras_socket_server(unsigned short port,
116 /* OUT */ gras_socket_t **dst) {
118 gras_error_t errcode;
119 gras_trp_plugin_t *trp;
124 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
127 /* defaults settings */
128 TRY(gras_trp_socket_new(1,&sock));
132 /* Call plugin socket creation function */
133 errcode = trp->socket_server(trp, port, sock);
134 if (errcode != no_error) {
140 /* Register this socket */
141 errcode = gras_dynar_push(_gras_trp_sockets,dst);
142 if (errcode != no_error) {
152 * gras_socket_client:
154 * Opens a client socket to a remote host.
155 * In real life, you'll get a TCP socket.
158 gras_socket_client(const char *host,
160 /* OUT */ gras_socket_t **dst) {
162 gras_error_t errcode;
163 gras_trp_plugin_t *trp;
168 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
171 /* defaults settings */
172 TRY(gras_trp_socket_new(0,&sock));
174 sock->peer_port = port;
175 sock->peer_name = strdup(host?host:"localhost");
177 /* plugin-specific */
178 errcode= (* trp->socket_client)(trp,
179 host ? host : "localhost", port,
181 if (errcode != no_error) {
186 /* register socket */
188 errcode = gras_dynar_push(_gras_trp_sockets,dst);
189 if (errcode != no_error) {
198 void gras_socket_close(gras_socket_t **sock) {
199 gras_socket_t *sock_iter;
202 /* FIXME: Issue an event when the socket is closed */
204 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
205 if (*sock == sock_iter) {
206 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
207 if ( (*sock)->plugin->socket_close)
208 (* (*sock)->plugin->socket_close)(*sock);
210 /* free the memory */
216 WARNING0("Ignoring request to free an unknown socket");
221 * gras_trp_chunk_send:
223 * Send a bunch of bytes from on socket
226 gras_trp_chunk_send(gras_socket_t *sd,
229 gras_assert1(sd->outgoing,
230 "Socket not suited for data send (outgoing=%c)",
231 sd->outgoing?'y':'n');
232 gras_assert1(sd->plugin->chunk_send,
233 "No function chunk_send on transport plugin %s",
235 return (*sd->plugin->chunk_send)(sd,data,size);
238 * gras_trp_chunk_recv:
240 * Receive a bunch of bytes from a socket
243 gras_trp_chunk_recv(gras_socket_t *sd,
246 gras_assert0(sd->incoming,
247 "Socket not suited for data receive");
248 gras_assert1(sd->plugin->chunk_recv,
249 "No function chunk_recv on transport plugin %s",
251 return (sd->plugin->chunk_recv)(sd,data,size);
256 gras_trp_plugin_get_by_name(const char *name,
257 gras_trp_plugin_t **dst){
259 return gras_dict_retrieve(_gras_trp_plugins,name,(void**)dst);