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));
37 memset(plug,0,sizeof(gras_trp_plugin_t));
39 plug->name=strdup(name);
44 TRY(gras_dict_set(_gras_trp_plugins,
45 plug->name, plug, gras_trp_plugin_free));
54 /* make room for all socket ownership descriptions */
55 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
57 /* We do not ear for any socket for now */
60 /* make room for all plugins */
61 TRY(gras_dict_new(&_gras_trp_plugins));
64 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
65 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
66 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
73 gras_dict_free(&_gras_trp_plugins);
74 gras_dynar_free(_gras_trp_sockets);
78 void gras_trp_plugin_free(void *p) {
79 gras_trp_plugin_t *plug = p;
94 * gras_trp_socket_new:
96 * Malloc a new socket, and initialize it with defaults
98 gras_error_t gras_trp_socket_new(int incoming,
99 gras_socket_t **dst) {
103 if (! (sock=malloc(sizeof(gras_socket_t))) )
109 sock->incoming = incoming? 1:0;
110 sock->outgoing = incoming? 0:1;
111 sock->accepting = incoming? 1:0;
112 DEBUG3("in=%c out=%c accept=%c",
113 sock->incoming?'y':'n',
114 sock->outgoing?'y':'n',
115 sock->accepting?'y':'n');
118 sock->peer_port = -1;
119 sock->peer_name = NULL;
128 * gras_socket_server:
130 * Opens a server socket and make it ready to be listened to.
131 * In real life, you'll get a TCP socket.
134 gras_socket_server(unsigned short port,
135 /* OUT */ gras_socket_t **dst) {
137 gras_error_t errcode;
138 gras_trp_plugin_t *trp;
143 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
146 /* defaults settings */
147 TRY(gras_trp_socket_new(1,&sock));
151 /* Call plugin socket creation function */
152 errcode = trp->socket_server(trp, port, sock);
153 if (errcode != no_error) {
159 /* Register this socket */
160 errcode = gras_dynar_push(_gras_trp_sockets,dst);
161 if (errcode != no_error) {
171 * gras_socket_client:
173 * Opens a client socket to a remote host.
174 * In real life, you'll get a TCP socket.
177 gras_socket_client(const char *host,
179 /* OUT */ gras_socket_t **dst) {
181 gras_error_t errcode;
182 gras_trp_plugin_t *trp;
187 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
190 /* defaults settings */
191 TRY(gras_trp_socket_new(0,&sock));
193 sock->peer_port = port;
194 sock->peer_name = strdup(host?host:"localhost");
196 /* plugin-specific */
197 errcode= (* trp->socket_client)(trp,
198 host ? host : "localhost", port,
200 if (errcode != no_error) {
205 /* register socket */
207 errcode = gras_dynar_push(_gras_trp_sockets,dst);
208 if (errcode != no_error) {
217 void gras_socket_close(gras_socket_t **sock) {
218 gras_socket_t *sock_iter;
221 /* FIXME: Issue an event when the socket is closed */
223 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
224 if (*sock == sock_iter) {
225 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
226 if ( (*sock)->plugin->socket_close)
227 (* (*sock)->plugin->socket_close)(*sock);
229 /* free the memory */
235 WARN0("Ignoring request to free an unknown socket");
240 * gras_trp_chunk_send:
242 * Send a bunch of bytes from on socket
245 gras_trp_chunk_send(gras_socket_t *sd,
248 gras_assert1(sd->outgoing,
249 "Socket not suited for data send (outgoing=%c)",
250 sd->outgoing?'y':'n');
251 gras_assert1(sd->plugin->chunk_send,
252 "No function chunk_send on transport plugin %s",
254 return (*sd->plugin->chunk_send)(sd,data,size);
257 * gras_trp_chunk_recv:
259 * Receive a bunch of bytes from a socket
262 gras_trp_chunk_recv(gras_socket_t *sd,
265 gras_assert0(sd->incoming,
266 "Socket not suited for data receive");
267 gras_assert1(sd->plugin->chunk_recv,
268 "No function chunk_recv on transport plugin %s",
270 return (sd->plugin->chunk_recv)(sd,data,size);
275 gras_trp_plugin_get_by_name(const char *name,
276 gras_trp_plugin_t **dst){
278 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
281 int gras_socket_my_port (gras_socket_t *sock) {
284 int gras_socket_peer_port(gras_socket_t *sock) {
285 return sock->peer_port;
287 char *gras_socket_peer_name(gras_socket_t *sock) {
288 return sock->peer_name;