Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Reput hook for raw sockets, needed for BW experiments
[simgrid.git] / src / gras / Transport / transport.c
1 /* $Id$ */
2
3 /* transport - low level communication                                      */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
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. */
10
11 #include <time.h>       /* time() */
12
13 #include "Transport/transport_private.h"
14
15
16 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
17
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 */
20
21
22 gras_dynar_t *_gras_trp_sockets; /* all existing sockets */
23 static void gras_trp_socket_free(void *s); /* free one socket */
24
25 static fd_set FDread;
26
27 gras_error_t 
28 gras_trp_init(void){
29   gras_error_t errcode;
30   gras_trp_plugin_t *plug;
31   
32   /* make room for all socket ownership descriptions */
33   TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
34
35   /* We do not ear for any socket for now */
36   FD_ZERO(&FDread);
37   
38   /* make room for all plugins */
39   TRY(gras_dict_new(&_gras_trp_plugins));
40
41   /* TCP */
42   TRY(gras_trp_tcp_init(&plug));
43   TRY(gras_dict_set(_gras_trp_plugins, 
44                     plug->name, plug, gras_trp_plugin_free));
45
46   /* FILE */
47   TRY(gras_trp_file_init(&plug));
48   TRY(gras_dict_set(_gras_trp_plugins, 
49                     plug->name, plug, gras_trp_plugin_free));
50
51   return no_error;
52 }
53
54 void
55 gras_trp_exit(void){
56   gras_dict_free(&_gras_trp_plugins);
57   gras_dynar_free(_gras_trp_sockets);
58 }
59
60
61 void gras_trp_plugin_free(void *p) {
62   gras_trp_plugin_t *plug = p;
63
64   if (plug) {
65     if (plug->free_specific && plug->specific)
66       plug->free_specific(plug->specific);
67
68     free(plug->name);
69     free(plug);
70   }
71 }
72
73
74 /**
75  * gras_trp_socket_new:
76  *
77  * Malloc a new socket, and initialize it with defaults
78  */
79 gras_error_t gras_trp_socket_new(int incoming,
80                                  gras_socket_t **dst) {
81
82   gras_socket_t *sock;
83
84   if (! (sock=malloc(sizeof(gras_socket_t))) )
85     RAISE_MALLOC;
86
87   sock->plugin = NULL;
88   sock->sd     = -1;
89
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');
97
98   sock->port      = -1;
99   sock->peer_port = -1;
100   sock->peer_name = NULL;
101
102   *dst = sock;
103   return no_error;
104 }
105
106
107 /**
108  * gras_socket_server:
109  *
110  * Opens a server socket and make it ready to be listened to.
111  * In real life, you'll get a TCP socket.
112  */
113 gras_error_t
114 gras_socket_server(unsigned short port,
115                    /* OUT */ gras_socket_t **dst) {
116  
117   gras_error_t errcode;
118   gras_trp_plugin_t *trp;
119   gras_socket_t *sock;
120
121   *dst = NULL;
122
123   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
124                                   &trp));
125
126   /* defaults settings */
127   TRY(gras_trp_socket_new(1,&sock));
128   sock->plugin= trp;
129   sock->port=port;
130
131   /* Call plugin socket creation function */
132   errcode = trp->socket_server(trp, port, 0/* not raw */, sock);
133   if (errcode != no_error) {
134     free(sock);
135     return errcode;
136   }
137
138   *dst = sock;
139   /* Register this socket */
140   errcode = gras_dynar_push(_gras_trp_sockets,dst);
141   if (errcode != no_error) {
142     free(sock);
143     *dst = NULL;
144     return errcode;
145   }
146
147   return no_error;
148 }
149
150 /**
151  * gras_socket_client:
152  *
153  * Opens a client socket to a remote host.
154  * In real life, you'll get a TCP socket.
155  */
156 gras_error_t
157 gras_socket_client(const char *host,
158                    unsigned short port,
159                    /* OUT */ gras_socket_t **dst) {
160  
161   gras_error_t errcode;
162   gras_trp_plugin_t *trp;
163   gras_socket_t *sock;
164
165   *dst = NULL;
166
167   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
168                                   &trp));
169
170   /* defaults settings */
171   TRY(gras_trp_socket_new(0,&sock));
172   sock->plugin= trp;
173   sock->peer_port = port;
174   sock->peer_name = strdup(host?host:"localhost");
175
176   /* plugin-specific */
177   errcode= (* trp->socket_client)(trp, 
178                                   host ? host : "localhost", port,
179                                   0 /* not raw */,
180                                   sock);
181   if (errcode != no_error) {
182     free(sock);
183     return errcode;
184   }
185
186   /* register socket */
187   *dst = sock;
188   errcode = gras_dynar_push(_gras_trp_sockets,dst);
189   if (errcode != no_error) {
190     free(sock);
191     *dst = NULL;
192     return errcode;
193   }
194
195   return no_error;
196 }
197
198 void gras_socket_close(gras_socket_t **sock) {
199   gras_socket_t *sock_iter;
200   int cursor;
201
202   /* FIXME: Issue an event when the socket is closed */
203   if (sock && *sock) {
204     gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
205       if (*sock == sock_iter) {
206         gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
207         if ( (*sock)->plugin->socket_close) 
208           (* (*sock)->plugin->socket_close)(*sock);
209
210         /* free the memory */
211         free(*sock);
212         *sock=NULL;
213         return;
214       }
215     }
216     WARN0("Ignoring request to free an unknown socket");
217   }
218 }
219
220 /**
221  * gras_trp_chunk_send:
222  *
223  * Send a bunch of bytes from on socket
224  */
225 gras_error_t
226 gras_trp_chunk_send(gras_socket_t *sd,
227                     char *data,
228                     size_t size) {
229   gras_assert1(sd->outgoing,
230                "Socket not suited for data send (outgoing=%c)",
231                sd->outgoing?'y':'n');
232   gras_assert1(sd->plugin->chunk_send,
233                "No function chunk_send on transport plugin %s",
234                sd->plugin->name);
235   return (*sd->plugin->chunk_send)(sd,data,size);
236 }
237 /**
238  * gras_trp_chunk_recv:
239  *
240  * Receive a bunch of bytes from a socket
241  */
242 gras_error_t 
243 gras_trp_chunk_recv(gras_socket_t *sd,
244                     char *data,
245                     size_t size) {
246   gras_assert0(sd->incoming,
247                "Socket not suited for data receive");
248   gras_assert1(sd->plugin->chunk_recv,
249                "No function chunk_recv on transport plugin %s",
250                sd->plugin->name);
251   return (sd->plugin->chunk_recv)(sd,data,size);
252 }
253
254
255 gras_error_t
256 gras_trp_plugin_get_by_name(const char *name,
257                             gras_trp_plugin_t **dst){
258
259   return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
260 }
261
262 int   gras_socket_my_port  (gras_socket_t *sock) {
263   return sock->port;
264 }
265 int   gras_socket_peer_port(gras_socket_t *sock) {
266   return sock->peer_port;
267 }
268 char *gras_socket_peer_name(gras_socket_t *sock) {
269   return sock->peer_name;
270 }