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 "gras/Transport/transport_private.h"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,gras,"Conveying bytes over the network");
15 static gras_dict_t *_gras_trp_plugins; /* All registered plugins */
16 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
18 static void gras_trp_socket_free(void *s); /* free one socket */
21 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup);
24 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
27 gras_trp_plugin_t *plug = gras_new0(gras_trp_plugin_t, 1);
29 DEBUG1("Create plugin %s",name);
34 plug->name=(char*)strdup(name);
38 errcode = setup(plug);
41 /* SG plugin return mismatch when in RL mode (and vice versa) */
42 gras_free(plug->name);
47 TRY(gras_dict_set(_gras_trp_plugins,
48 name, plug, gras_trp_plugin_free));
62 /* make room for all plugins */
63 TRY(gras_dict_new(&_gras_trp_plugins));
66 TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
67 TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
68 TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
70 /* buf is composed, so it must come after the others */
71 TRY(gras_trp_plugin_new("buf", gras_trp_buf_setup));
78 gras_dict_free(&_gras_trp_plugins);
82 void gras_trp_plugin_free(void *p) {
83 gras_trp_plugin_t *plug = p;
88 } else if (plug->data) {
89 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
90 gras_free(plug->data);
93 gras_free(plug->name);
100 * gras_trp_socket_new:
102 * Malloc a new socket, and initialize it with defaults
104 gras_error_t gras_trp_socket_new(int incoming,
105 gras_socket_t **dst) {
109 if (! (sock=gras_new(gras_socket_t,1)) )
111 DEBUG1("Create a new socket (%p)", (void*)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_ext:
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_ext(unsigned short port,
141 unsigned long int bufSize,
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("buf",&trp));
155 /* defaults settings */
156 TRY(gras_trp_socket_new(1,&sock));
159 sock->bufSize = bufSize;
162 /* Call plugin socket creation function */
163 errcode = trp->socket_server(trp, sock);
164 DEBUG3("in=%c out=%c accept=%c",
165 sock->incoming?'y':'n',
166 sock->outgoing?'y':'n',
167 sock->accepting?'y':'n');
169 if (errcode != no_error) {
180 * gras_socket_client_ext:
182 * Opens a client socket to a remote host.
183 * In real life, you'll get a TCP socket.
186 gras_socket_client_ext(const char *host,
189 unsigned long int bufSize,
192 /* OUT */ gras_socket_t **dst) {
194 gras_error_t errcode;
195 gras_trp_plugin_t *trp;
200 TRY(gras_trp_plugin_get_by_name("buf",&trp));
202 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
203 /* defaults settings */
204 TRY(gras_trp_socket_new(0,&sock));
206 sock->peer_port = port;
207 sock->peer_name = (char*)strdup(host?host:"localhost");
208 sock->bufSize = bufSize;
211 /* plugin-specific */
212 errcode= (*trp->socket_client)(trp, sock);
213 DEBUG3("in=%c out=%c accept=%c",
214 sock->incoming?'y':'n',
215 sock->outgoing?'y':'n',
216 sock->accepting?'y':'n');
218 if (errcode != no_error) {
229 * gras_socket_server:
231 * Opens a server socket and make it ready to be listened to.
232 * In real life, you'll get a TCP socket.
235 gras_socket_server(unsigned short port,
236 /* OUT */ gras_socket_t **dst) {
237 return gras_socket_server_ext(port,32,0,dst);
241 * gras_socket_client:
243 * Opens a client socket to a remote host.
244 * In real life, you'll get a TCP socket.
247 gras_socket_client(const char *host,
249 /* OUT */ gras_socket_t **dst) {
250 return gras_socket_client_ext(host,port,32,0,dst);
254 void gras_socket_close(gras_socket_t *sock) {
255 gras_dynar_t *sockets = gras_socketset_get();
256 gras_socket_t *sock_iter;
259 /* FIXME: Issue an event when the socket is closed */
261 gras_dynar_foreach(sockets,cursor,sock_iter) {
262 if (sock == sock_iter) {
263 gras_dynar_cursor_rm(sockets,&cursor);
264 if ( sock->plugin->socket_close)
265 (* sock->plugin->socket_close)(sock);
267 /* free the memory */
269 gras_free(sock->peer_name);
274 WARN0("Ignoring request to free an unknown socket");
279 * gras_trp_chunk_send:
281 * Send a bunch of bytes from on socket
284 gras_trp_chunk_send(gras_socket_t *sd,
287 gras_assert1(sd->outgoing,
288 "Socket not suited for data send (outgoing=%c)",
289 sd->outgoing?'y':'n');
290 gras_assert1(sd->plugin->chunk_send,
291 "No function chunk_send on transport plugin %s",
293 return (*sd->plugin->chunk_send)(sd,data,size);
296 * gras_trp_chunk_recv:
298 * Receive a bunch of bytes from a socket
301 gras_trp_chunk_recv(gras_socket_t *sd,
304 gras_assert0(sd->incoming,
305 "Socket not suited for data receive");
306 gras_assert1(sd->plugin->chunk_recv,
307 "No function chunk_recv on transport plugin %s",
309 return (sd->plugin->chunk_recv)(sd,data,size);
315 * Make sure all pending communications are done
318 gras_trp_flush(gras_socket_t *sd) {
319 return (sd->plugin->flush)(sd);
323 gras_trp_plugin_get_by_name(const char *name,
324 gras_trp_plugin_t **dst){
326 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
329 int gras_socket_my_port (gras_socket_t *sock) {
332 int gras_socket_peer_port(gras_socket_t *sock) {
333 return sock->peer_port;
335 char *gras_socket_peer_name(gras_socket_t *sock) {
336 return sock->peer_name;
339 gras_error_t gras_socket_raw_send(gras_socket_t *peer,
340 unsigned int timeout,
341 unsigned long int expSize,
342 unsigned long int msgSize) {
344 gras_assert0(peer->raw,"Asked to send raw data on a regular socket\n");
345 return gras_socket_raw_exchange(peer,1,timeout,expSize,msgSize);
348 gras_error_t gras_socket_raw_recv(gras_socket_t *peer,
349 unsigned int timeout,
350 unsigned long int expSize,
351 unsigned long int msgSize){
353 gras_assert0(peer->raw,"Asked to recveive raw data on a regular socket\n");
354 return gras_socket_raw_exchange(peer,0,timeout,expSize,msgSize);