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_t *plug = gras_new0(gras_trp_plugin_t, 1);
26 DEBUG1("Create plugin %s",name);
28 plug->name=gras_strdup(name);
30 errcode = setup(plug);
33 /* SG plugin return mismatch when in RL mode (and vice versa) */
34 gras_free(plug->name);
39 gras_dict_set(_gras_trp_plugins,
40 name, plug, gras_trp_plugin_free);
49 void gras_trp_init(void){
50 /* make room for all plugins */
51 _gras_trp_plugins=gras_dict_new();
54 gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
55 gras_trp_plugin_new("file",gras_trp_file_setup);
56 gras_trp_plugin_new("sg",gras_trp_sg_setup);
58 /* buf is composed, so it must come after the others */
59 gras_trp_plugin_new("buf", gras_trp_buf_setup);
65 gras_dict_free(&_gras_trp_plugins);
69 void gras_trp_plugin_free(void *p) {
70 gras_trp_plugin_t *plug = p;
75 } else if (plug->data) {
76 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
77 gras_free(plug->data);
80 gras_free(plug->name);
87 * gras_trp_socket_new:
89 * Malloc a new socket, and initialize it with defaults
91 void gras_trp_socket_new(int incoming,
92 gras_socket_t **dst) {
94 gras_socket_t *sock=gras_new(gras_socket_t,1);
96 DEBUG1("Create a new socket (%p)", (void*)sock);
102 sock->incoming = incoming ? 1:0;
103 sock->outgoing = incoming ? 0:1;
104 sock->accepting = incoming ? 1:0;
107 sock->peer_port = -1;
108 sock->peer_name = NULL;
113 gras_dynar_push(gras_socketset_get(),dst);
118 * gras_socket_server_ext:
120 * Opens a server socket and make it ready to be listened to.
121 * In real life, you'll get a TCP socket.
124 gras_socket_server_ext(unsigned short port,
126 unsigned long int bufSize,
129 /* OUT */ gras_socket_t **dst) {
131 gras_error_t errcode;
132 gras_trp_plugin_t *trp;
137 DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
138 TRY(gras_trp_plugin_get_by_name("buf",&trp));
140 /* defaults settings */
141 gras_trp_socket_new(1,&sock);
144 sock->bufSize = bufSize;
147 /* Call plugin socket creation function */
148 errcode = trp->socket_server(trp, sock);
149 DEBUG3("in=%c out=%c accept=%c",
150 sock->incoming?'y':'n',
151 sock->outgoing?'y':'n',
152 sock->accepting?'y':'n');
154 if (errcode != no_error) {
165 * gras_socket_client_ext:
167 * Opens a client socket to a remote host.
168 * In real life, you'll get a TCP socket.
171 gras_socket_client_ext(const char *host,
174 unsigned long int bufSize,
177 /* OUT */ gras_socket_t **dst) {
179 gras_error_t errcode;
180 gras_trp_plugin_t *trp;
185 TRY(gras_trp_plugin_get_by_name("buf",&trp));
187 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
188 /* defaults settings */
189 gras_trp_socket_new(0,&sock);
191 sock->peer_port = port;
192 sock->peer_name = (char*)strdup(host?host:"localhost");
193 sock->bufSize = bufSize;
196 /* plugin-specific */
197 errcode= (*trp->socket_client)(trp, sock);
198 DEBUG3("in=%c out=%c accept=%c",
199 sock->incoming?'y':'n',
200 sock->outgoing?'y':'n',
201 sock->accepting?'y':'n');
203 if (errcode != no_error) {
214 * gras_socket_server:
216 * Opens a server socket and make it ready to be listened to.
217 * In real life, you'll get a TCP socket.
220 gras_socket_server(unsigned short port,
221 /* OUT */ gras_socket_t **dst) {
222 return gras_socket_server_ext(port,32,0,dst);
226 * gras_socket_client:
228 * Opens a client socket to a remote host.
229 * In real life, you'll get a TCP socket.
232 gras_socket_client(const char *host,
234 /* OUT */ gras_socket_t **dst) {
235 return gras_socket_client_ext(host,port,32,0,dst);
239 void gras_socket_close(gras_socket_t *sock) {
240 gras_dynar_t *sockets = gras_socketset_get();
241 gras_socket_t *sock_iter;
244 /* FIXME: Issue an event when the socket is closed */
246 gras_dynar_foreach(sockets,cursor,sock_iter) {
247 if (sock == sock_iter) {
248 gras_dynar_cursor_rm(sockets,&cursor);
249 if ( sock->plugin->socket_close)
250 (* sock->plugin->socket_close)(sock);
252 /* free the memory */
254 gras_free(sock->peer_name);
259 WARN0("Ignoring request to free an unknown socket");
264 * gras_trp_chunk_send:
266 * Send a bunch of bytes from on socket
269 gras_trp_chunk_send(gras_socket_t *sd,
272 gras_assert1(sd->outgoing,
273 "Socket not suited for data send (outgoing=%c)",
274 sd->outgoing?'y':'n');
275 gras_assert1(sd->plugin->chunk_send,
276 "No function chunk_send on transport plugin %s",
278 return (*sd->plugin->chunk_send)(sd,data,size);
281 * gras_trp_chunk_recv:
283 * Receive a bunch of bytes from a socket
286 gras_trp_chunk_recv(gras_socket_t *sd,
289 gras_assert0(sd->incoming,
290 "Socket not suited for data receive");
291 gras_assert1(sd->plugin->chunk_recv,
292 "No function chunk_recv on transport plugin %s",
294 return (sd->plugin->chunk_recv)(sd,data,size);
300 * Make sure all pending communications are done
303 gras_trp_flush(gras_socket_t *sd) {
304 return (sd->plugin->flush)(sd);
308 gras_trp_plugin_get_by_name(const char *name,
309 gras_trp_plugin_t **dst){
311 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
314 int gras_socket_my_port (gras_socket_t *sock) {
317 int gras_socket_peer_port(gras_socket_t *sock) {
318 return sock->peer_port;
320 char *gras_socket_peer_name(gras_socket_t *sock) {
321 return sock->peer_name;
324 gras_error_t gras_socket_raw_send(gras_socket_t *peer,
325 unsigned int timeout,
326 unsigned long int exp_size,
327 unsigned long int msg_size) {
328 gras_error_t errcode;
329 char *chunk = gras_malloc(msg_size);
332 gras_assert0(peer->raw,"Asked to send raw data on a regular socket\n");
333 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
334 TRY(gras_trp_chunk_send(peer,chunk,msg_size));
338 return no_error;//gras_socket_raw_exchange(peer,1,timeout,expSize,msgSize);
341 gras_error_t gras_socket_raw_recv(gras_socket_t *peer,
342 unsigned int timeout,
343 unsigned long int exp_size,
344 unsigned long int msg_size){
346 gras_error_t errcode;
347 char *chunk = gras_malloc(msg_size);
350 gras_assert0(peer->raw,"Asked to recveive raw data on a regular socket\n");
351 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
352 TRY(gras_trp_chunk_send(peer,chunk,msg_size));
356 return no_error;//gras_socket_raw_exchange(peer,0,timeout,expSize,msgSize);