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");
14 GRAS_LOG_NEW_SUBCATEGORY(raw_trp,transport,"Conveying bytes over the network without formating");
16 static gras_dict_t _gras_trp_plugins; /* All registered plugins */
17 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
19 static void gras_trp_socket_free(void *s); /* free one socket */
22 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
25 gras_trp_plugin_t *plug = gras_new0(gras_trp_plugin_t, 1);
27 DEBUG1("Create plugin %s",name);
29 plug->name=gras_strdup(name);
31 errcode = setup(plug);
34 /* SG plugin return mismatch when in RL mode (and vice versa) */
35 gras_free(plug->name);
40 gras_dict_set(_gras_trp_plugins,
41 name, plug, gras_trp_plugin_free);
50 void gras_trp_init(void){
51 /* make room for all plugins */
52 _gras_trp_plugins=gras_dict_new();
55 gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
56 gras_trp_plugin_new("file",gras_trp_file_setup);
57 gras_trp_plugin_new("sg",gras_trp_sg_setup);
59 /* buf is composed, so it must come after the others */
60 gras_trp_plugin_new("buf", gras_trp_buf_setup);
66 gras_dict_free(&_gras_trp_plugins);
70 void gras_trp_plugin_free(void *p) {
71 gras_trp_plugin_t *plug = p;
76 } else if (plug->data) {
77 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
78 gras_free(plug->data);
81 gras_free(plug->name);
88 * gras_trp_socket_new:
90 * Malloc a new socket, and initialize it with defaults
92 void gras_trp_socket_new(int incoming,
95 gras_socket_t sock=gras_new0(s_gras_socket_t,1);
97 DEBUG1("Create a new socket (%p)", (void*)sock);
103 sock->incoming = incoming ? 1:0;
104 sock->outgoing = incoming ? 0:1;
105 sock->accepting = incoming ? 1:0;
108 sock->peer_port = -1;
109 sock->peer_name = NULL;
114 gras_dynar_push(gras_socketset_get(),dst);
119 * gras_socket_server_ext:
121 * Opens a server socket and make it ready to be listened to.
122 * In real life, you'll get a TCP socket.
125 gras_socket_server_ext(unsigned short port,
127 unsigned long int bufSize,
130 /* OUT */ gras_socket_t *dst) {
132 gras_error_t errcode;
133 gras_trp_plugin_t *trp;
138 DEBUG2("Create a server socket from plugin %s on port %d",
139 gras_if_RL() ? "tcp" : "sg",
141 TRY(gras_trp_plugin_get_by_name("buf",&trp));
143 /* defaults settings */
144 gras_trp_socket_new(1,&sock);
147 sock->bufSize = bufSize;
150 /* Call plugin socket creation function */
151 errcode = trp->socket_server(trp, sock);
152 DEBUG3("in=%c out=%c accept=%c",
153 sock->incoming?'y':'n',
154 sock->outgoing?'y':'n',
155 sock->accepting?'y':'n');
157 if (errcode != no_error) {
168 * gras_socket_client_ext:
170 * Opens a client socket to a remote host.
171 * In real life, you'll get a TCP socket.
174 gras_socket_client_ext(const char *host,
177 unsigned long int bufSize,
180 /* OUT */ gras_socket_t *dst) {
182 gras_error_t errcode;
183 gras_trp_plugin_t *trp;
188 TRY(gras_trp_plugin_get_by_name("buf",&trp));
190 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
191 /* defaults settings */
192 gras_trp_socket_new(0,&sock);
194 sock->peer_port = port;
195 sock->peer_name = (char*)strdup(host?host:"localhost");
196 sock->bufSize = bufSize;
199 /* plugin-specific */
200 errcode= (*trp->socket_client)(trp, sock);
201 DEBUG3("in=%c out=%c accept=%c",
202 sock->incoming?'y':'n',
203 sock->outgoing?'y':'n',
204 sock->accepting?'y':'n');
206 if (errcode != no_error) {
217 * gras_socket_server:
219 * Opens a server socket and make it ready to be listened to.
220 * In real life, you'll get a TCP socket.
223 gras_socket_server(unsigned short port,
224 /* OUT */ gras_socket_t *dst) {
225 return gras_socket_server_ext(port,32,0,dst);
229 * gras_socket_client:
231 * Opens a client socket to a remote host.
232 * In real life, you'll get a TCP socket.
235 gras_socket_client(const char *host,
237 /* OUT */ gras_socket_t *dst) {
238 return gras_socket_client_ext(host,port,32,0,dst);
242 void gras_socket_close(gras_socket_t sock) {
243 gras_dynar_t sockets = gras_socketset_get();
244 gras_socket_t sock_iter;
247 /* FIXME: Issue an event when the socket is closed */
249 gras_dynar_foreach(sockets,cursor,sock_iter) {
250 if (sock == sock_iter) {
251 gras_dynar_cursor_rm(sockets,&cursor);
252 if ( sock->plugin->socket_close)
253 (* sock->plugin->socket_close)(sock);
255 /* free the memory */
257 gras_free(sock->peer_name);
262 WARN0("Ignoring request to free an unknown socket");
267 * gras_trp_chunk_send:
269 * Send a bunch of bytes from on socket
272 gras_trp_chunk_send(gras_socket_t sd,
275 gras_assert1(sd->outgoing,
276 "Socket not suited for data send (outgoing=%c)",
277 sd->outgoing?'y':'n');
278 gras_assert1(sd->plugin->chunk_send,
279 "No function chunk_send on transport plugin %s",
281 return (*sd->plugin->chunk_send)(sd,data,size);
284 * gras_trp_chunk_recv:
286 * Receive a bunch of bytes from a socket
289 gras_trp_chunk_recv(gras_socket_t sd,
292 gras_assert0(sd->incoming,
293 "Socket not suited for data receive");
294 gras_assert1(sd->plugin->chunk_recv,
295 "No function chunk_recv on transport plugin %s",
297 return (sd->plugin->chunk_recv)(sd,data,size);
303 * Make sure all pending communications are done
306 gras_trp_flush(gras_socket_t sd) {
307 return (sd->plugin->flush)(sd);
311 gras_trp_plugin_get_by_name(const char *name,
312 gras_trp_plugin_t **dst){
314 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
317 int gras_socket_my_port (gras_socket_t sock) {
320 int gras_socket_peer_port(gras_socket_t sock) {
321 return sock->peer_port;
323 char *gras_socket_peer_name(gras_socket_t sock) {
324 return sock->peer_name;
327 gras_error_t gras_socket_raw_send(gras_socket_t peer,
328 unsigned int timeout,
329 unsigned long int exp_size,
330 unsigned long int msg_size) {
331 gras_error_t errcode;
332 char *chunk = gras_malloc(msg_size);
335 gras_assert0(peer->raw,"Asked to send raw data on a regular socket");
336 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
337 CDEBUG5(raw_trp,"Sent %d of %lu (msg_size=%ld) to %s:%d",
338 exp_sofar,exp_size,msg_size,
339 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
340 TRY(gras_trp_chunk_send(peer,chunk,msg_size));
342 CDEBUG5(raw_trp,"Sent %d of %lu (msg_size=%ld) to %s:%d",
343 exp_sofar,exp_size,msg_size,
344 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
347 return no_error;//gras_socket_raw_exchange(peer,1,timeout,expSize,msgSize);
350 gras_error_t gras_socket_raw_recv(gras_socket_t peer,
351 unsigned int timeout,
352 unsigned long int exp_size,
353 unsigned long int msg_size){
355 gras_error_t errcode;
356 char *chunk = gras_malloc(msg_size);
359 gras_assert0(peer->raw,"Asked to recveive raw data on a regular socket\n");
360 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
361 CDEBUG5(raw_trp,"Recvd %d of %lu (msg_size=%ld) from %s:%d",
362 exp_sofar,exp_size,msg_size,
363 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
364 TRY(gras_trp_chunk_recv(peer,chunk,msg_size));
366 CDEBUG5(raw_trp,"Recvd %d of %lu (msg_size=%ld) from %s:%d",
367 exp_sofar,exp_size,msg_size,
368 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
371 return no_error;//gras_socket_raw_exchange(peer,0,timeout,expSize,msgSize);