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"
16 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
18 static gras_dict_t *_gras_trp_plugins; /* All registered plugins */
19 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
22 gras_dynar_t *_gras_trp_sockets; /* all existing sockets */
23 static void gras_trp_socket_free(void *s); /* free one socket */
30 gras_trp_plugin_t *plug;
32 /* make room for all socket ownership descriptions */
33 TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
35 /* We do not ear for any socket for now */
38 /* make room for all plugins */
39 TRY(gras_dict_new(&_gras_trp_plugins));
42 TRY(gras_trp_tcp_init(&plug));
43 TRY(gras_dict_set(_gras_trp_plugins,
44 plug->name, plug, gras_trp_plugin_free));
47 TRY(gras_trp_file_init(&plug));
48 TRY(gras_dict_set(_gras_trp_plugins,
49 plug->name, plug, gras_trp_plugin_free));
56 gras_dict_free(&_gras_trp_plugins);
57 gras_dynar_free(_gras_trp_sockets);
61 void gras_trp_plugin_free(void *p) {
62 gras_trp_plugin_t *plug = p;
65 if (plug->free_specific && plug->specific)
66 plug->free_specific(plug->specific);
75 * gras_trp_socket_new:
77 * Malloc a new socket, and initialize it with defaults
79 gras_error_t gras_trp_socket_new(int incoming,
80 gras_socket_t **dst) {
84 if (! (sock=malloc(sizeof(gras_socket_t))) )
90 sock->incoming = incoming? 1:0;
91 sock->outgoing = incoming? 0:1;
92 sock->accepting = incoming? 1:0;
93 DEBUG3("in=%c out=%c accept=%c",
94 sock->incoming?'y':'n',
95 sock->outgoing?'y':'n',
96 sock->accepting?'y':'n');
100 sock->peer_name = NULL;
108 * gras_socket_server:
110 * Opens a server socket and make it ready to be listened to.
111 * In real life, you'll get a TCP socket.
114 gras_socket_server(unsigned short port,
115 /* OUT */ gras_socket_t **dst) {
117 gras_error_t errcode;
118 gras_trp_plugin_t *trp;
123 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
126 /* defaults settings */
127 TRY(gras_trp_socket_new(1,&sock));
131 /* Call plugin socket creation function */
132 errcode = trp->socket_server(trp, port, sock);
133 if (errcode != no_error) {
139 /* Register this socket */
140 errcode = gras_dynar_push(_gras_trp_sockets,dst);
141 if (errcode != no_error) {
151 * gras_socket_client:
153 * Opens a client socket to a remote host.
154 * In real life, you'll get a TCP socket.
157 gras_socket_client(const char *host,
159 /* OUT */ gras_socket_t **dst) {
161 gras_error_t errcode;
162 gras_trp_plugin_t *trp;
167 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
170 /* defaults settings */
171 TRY(gras_trp_socket_new(0,&sock));
173 sock->peer_port = port;
174 sock->peer_name = strdup(host?host:"localhost");
176 /* plugin-specific */
177 errcode= (* trp->socket_client)(trp,
178 host ? host : "localhost", port,
180 if (errcode != no_error) {
185 /* register socket */
187 errcode = gras_dynar_push(_gras_trp_sockets,dst);
188 if (errcode != no_error) {
197 void gras_socket_close(gras_socket_t **sock) {
198 gras_socket_t *sock_iter;
201 /* FIXME: Issue an event when the socket is closed */
203 gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
204 if (*sock == sock_iter) {
205 gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
206 if ( (*sock)->plugin->socket_close)
207 (* (*sock)->plugin->socket_close)(*sock);
209 /* free the memory */
215 WARN0("Ignoring request to free an unknown socket");
220 * gras_trp_chunk_send:
222 * Send a bunch of bytes from on socket
225 gras_trp_chunk_send(gras_socket_t *sd,
228 gras_assert1(sd->outgoing,
229 "Socket not suited for data send (outgoing=%c)",
230 sd->outgoing?'y':'n');
231 gras_assert1(sd->plugin->chunk_send,
232 "No function chunk_send on transport plugin %s",
234 return (*sd->plugin->chunk_send)(sd,data,size);
237 * gras_trp_chunk_recv:
239 * Receive a bunch of bytes from a socket
242 gras_trp_chunk_recv(gras_socket_t *sd,
245 gras_assert0(sd->incoming,
246 "Socket not suited for data receive");
247 gras_assert1(sd->plugin->chunk_recv,
248 "No function chunk_recv on transport plugin %s",
250 return (sd->plugin->chunk_recv)(sd,data,size);
255 gras_trp_plugin_get_by_name(const char *name,
256 gras_trp_plugin_t **dst){
258 return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
261 int gras_socket_my_port (gras_socket_t *sock) {
264 int gras_socket_peer_port(gras_socket_t *sock) {
265 return sock->peer_port;
267 char *gras_socket_peer_name(gras_socket_t *sock) {
268 return sock->peer_name;