Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Hide some functions to end-users (gras_trp_block_send/recv; trp module init/exit...
[simgrid.git] / src / gras / Transport / transport_tcp.c
1 /* $Id$ */
2
3 /* tcp trp (transport) - send/receive a bunch of bytes from a tcp socket    */
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 <unistd.h>       /* close() pipe() read() write() */
12 #include <signal.h>       /* close() pipe() read() write() */
13 #include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
14 #include <netinet/tcp.h>  /* TCP_NODELAY */
15 #include <arpa/inet.h>    /* inet_ntoa() */
16 #include <netdb.h>        /* getprotobyname() */
17 #include <sys/time.h>     /* struct timeval */
18 #include <errno.h>        /* errno */
19 #include <sys/wait.h>     /* waitpid() */
20 #include <sys/socket.h>   /* getpeername() socket() */
21 #include <stdlib.h>
22
23
24 #include "gras_private.h"
25 #include "transport_private.h"
26
27 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport);
28
29 typedef struct {
30   int buffsize;
31 } gras_trp_tcp_sock_specific_t;
32
33 /***
34  *** Prototypes 
35  ***/
36 gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
37                                         const char *host,
38                                         unsigned short port,
39                                         unsigned int bufSize, 
40                                         /* OUT */ gras_socket_t **dst);
41 gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
42                                         unsigned short port,
43                                         unsigned int bufSize, 
44                                         /* OUT */ gras_socket_t **dst);
45 gras_error_t gras_trp_tcp_socket_accept(gras_socket_t  *sock,
46                                         gras_socket_t **dst);
47
48 void         gras_trp_tcp_socket_close(gras_socket_t *sd);
49   
50 gras_error_t gras_trp_tcp_bloc_send(gras_socket_t *sd,
51                                     char *data,
52                                     size_t size);
53
54 gras_error_t gras_trp_tcp_bloc_recv(gras_socket_t *sd,
55                                     char *data,
56                                     size_t size);
57
58 void         gras_trp_tcp_free_specific(void *s);
59
60
61 static int TcpProtoNumber(void);
62 /***
63  *** Specific plugin part
64  ***/
65
66 typedef struct {
67   fd_set incoming_socks;
68 } gras_trp_tcp_specific_t;
69
70 /***
71  *** Specific socket part
72  ***/
73
74
75 /***
76  *** Code
77  ***/
78 gras_error_t
79 gras_trp_tcp_init(gras_trp_plugin_t **dst) {
80
81   gras_trp_plugin_t *res=malloc(sizeof(gras_trp_plugin_t));
82   gras_trp_tcp_specific_t *tcp = malloc(sizeof(gras_trp_tcp_specific_t));
83   if (!res || !tcp)
84     RAISE_MALLOC;
85
86   FD_ZERO(&(tcp->incoming_socks));
87
88   res->socket_client = gras_trp_tcp_socket_client;
89   res->socket_server = gras_trp_tcp_socket_server;
90   res->socket_accept = gras_trp_tcp_socket_accept;
91   res->socket_close  = gras_trp_tcp_socket_close;
92
93   res->bloc_send     = gras_trp_tcp_bloc_send;
94   res->bloc_recv     = gras_trp_tcp_bloc_recv;
95
96   res->specific      = (void*)tcp;
97   res->free_specific = gras_trp_tcp_free_specific;
98
99   *dst = res;
100   return no_error;
101 }
102
103 void gras_trp_tcp_free_specific(void *s) {
104   gras_trp_tcp_specific_t *specific = s;
105   free(specific);
106 }
107
108 gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
109                                         const char *host,
110                                         unsigned short port,
111                                         unsigned int bufSize, 
112                                         /* OUT */ gras_socket_t **dst){
113   /*
114   int addrCount;
115   IPAddress addresses[10];
116   int i;
117   int sd;
118   
119   if (!(*sock=malloc(sizeof(gras_socket_t)))) {
120     fprintf(stderr,"Malloc error\n");
121     return malloc_error;
122   }
123   (*sock)->peer_addr=NULL;
124   
125   if (!(addrCount = IPAddressValues(host, addresses, 10))) {
126     fprintf(stderr,"grasOpenClientSocket: address retrieval of '%s' failed\n",host);
127     return system_error;
128   }
129   
130   for(i = 0; i < addrCount && i<10 ; i++) {
131     if(CallAddr(addresses[i], port, &sd, -1)) {
132       (*sock)->sock = sd;
133       (*sock)->port = port;
134       return no_error;
135     }
136   }
137   free(*sock);
138   fprintf(stderr,"grasOpenClientSocket: something wicked happenned while connecting to %s:%d",
139           host,port);
140   return system_error;
141   */
142   RAISE_UNIMPLEMENTED;
143 }
144
145 /**
146  * gras_trp_tcp_socket_server:
147  *
148  * Open a socket used to receive messages. bufSize is in ko.
149  */
150 gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
151                                         unsigned short port,
152                                         unsigned int bufSize, 
153                                         /* OUT */ gras_socket_t **dst){
154   int size = bufSize * 1024;
155   int on = 1;
156   int sd = -1;
157   struct sockaddr_in server;
158
159   gras_socket_t *res;
160   gras_trp_tcp_specific_t *data=(gras_trp_tcp_specific_t*)self -> specific;
161  
162   res=malloc(sizeof(gras_socket_t));
163   if (!res)
164     RAISE_MALLOC;
165
166   server.sin_port = htons((u_short)port);
167   server.sin_addr.s_addr = INADDR_ANY;
168   server.sin_family = AF_INET;
169   if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
170     free(res);
171     RAISE0(system_error,"socket allocation failed");
172   }
173
174   (void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
175   (void)setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size));
176   (void)setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size));
177   if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
178     free(res);
179     close(sd);
180     RAISE1(system_error,"Cannot bind to port %d",port);
181   }
182
183   if (listen(sd, 5) != -1) {
184     free(res);
185     close(sd);
186     RAISE1(system_error,"Cannot listen to port %d",port);
187   }
188
189   FD_SET(sd, &(data->incoming_socks));
190
191   *dst=res;
192   res->plugin = self;
193   res->incoming = 1;
194   res->sd = sd;
195   res->port=port;
196   res->peer_port=-1;
197   res->peer_name=NULL;
198
199   DEBUG2("Openned a server socket on port %d (sock %d)",port,sd);
200   
201   return no_error;
202 }
203
204 gras_error_t
205 gras_trp_tcp_socket_accept(gras_socket_t  *sock,
206                            gras_socket_t **dst) {
207   gras_socket_t *res;
208   
209   struct sockaddr_in peer_in;
210   socklen_t peer_in_len = sizeof(peer_in);
211
212   int sd;
213   int tmp_errno;
214                                 
215   res=malloc(sizeof(gras_socket_t));
216   if (!res)
217     RAISE_MALLOC;
218
219   sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
220   tmp_errno = errno;
221
222   if(sd == -1) {
223     gras_socket_close(sock);
224     RAISE1(system_error,
225            "Accept failed (%s). Droping server socket.", strerror(tmp_errno));
226   } else {
227     int i = 1;
228     socklen_t s = sizeof(int);
229   
230     if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s) 
231         || setsockopt(sd, TcpProtoNumber(), TCP_NODELAY, (char *)&i, s)) {
232       WARNING0("setsockopt failed, cannot condition the accepted socket");
233     }
234  
235     i = ((gras_trp_tcp_sock_specific_t*)sock->specific)->buffsize;
236     if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&i, s)
237         || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&i, s)) {
238       WARNING0("setsockopt failed, cannot set buffsize");       
239     }
240  
241     res->plugin = sock->plugin;
242     res->incoming = 1;
243     res->sd = sd;
244     res->port= -1;
245     res->peer_port= peer_in.sin_port;
246
247     if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
248       res->peer_name = strdup("unknown");
249     } else {
250       struct in_addr addrAsInAddr;
251       char *tmp;
252  
253       addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
254       
255       tmp = inet_ntoa(addrAsInAddr);
256       if (tmp != NULL) {
257         res->peer_name = strdup(inet_ntoa(addrAsInAddr));
258       } else {
259         res->peer_name = strdup("unknown");
260       }
261     }
262
263     VERB3("accepted socket %d to %s:%d\n", sd, res->peer_name,res->peer_port);
264     
265     *dst = res;
266
267     return no_error;
268   }
269 }
270
271 void gras_trp_tcp_socket_close(gras_socket_t *sock){
272   gras_trp_tcp_specific_t *tcp;
273   
274   if (!sock) return; /* close only once */
275   tcp=sock->plugin->specific;
276
277   DEBUG1("close tcp connection %d\n", sock->sd);
278
279   /* FIXME: no pipe in GRAS so far  
280   if(!FD_ISSET(sd, &connectedPipes)) {
281     if(shutdown(sd, 2) < 0) {
282       GetNWSLock(&lock);
283       tmp_errno = errno;
284       ReleaseNWSLock(&lock);
285       
286       / * The other side may have beaten us to the reset. * /
287       if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
288         WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
289       }
290     }
291   } */
292
293   /* close the socket */
294   if(close(sock->sd) < 0) {
295     WARNING3("error while closing tcp socket %d: %d (%s)\n", sock->sd, errno, strerror(errno));
296   }
297
298   /* forget about it */
299   FD_CLR(sock->sd, &(tcp->incoming_socks));
300
301 }
302
303 gras_error_t gras_trp_tcp_bloc_send(gras_socket_t *sock,
304                                     char *data,
305                                     size_t size){
306
307   gras_assert0(sock && !sock->incoming, "Ascked to send stuff on an incomming socket");
308   gras_assert0(size >= 0, "Cannot send a negative amount of data");
309
310   while (size) {
311     int status = 0;
312     
313     status = write(sock->sd, data, (size_t)size);
314     DEBUG3("write(%d, %p, %ld);\n", sock->sd, data, size);
315     
316     if (status == -1) {
317       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
318              sock->sd, data, (int)size,
319              strerror(errno));
320     }
321     
322     if (status) {
323       size  -= status;
324       data  += status;
325     } else {
326       RAISE0(system_error,"file descriptor closed");
327     }
328   }
329
330   return no_error;
331 }
332
333 gras_error_t gras_trp_tcp_bloc_recv(gras_socket_t *sock,
334                                     char *data,
335                                     size_t size){
336
337   gras_assert0(sock && !sock->incoming, "Ascked to receive stuff on an outcomming socket");
338   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
339   
340   while (size) {
341     int status = 0;
342     
343     status = read(sock->sd, data, (size_t)size);
344     DEBUG3("read(%d, %p, %ld);\n", sock->sd, data, size);
345     
346     if (status == -1) {
347       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
348              sock->sd, data, (int)size,
349              strerror(errno));
350     }
351     
352     if (status) {
353       size  -= status;
354       data  += status;
355     } else {
356       RAISE0(system_error,"file descriptor closed");
357     }
358   }
359   
360   return no_error;
361 }
362
363
364 /*
365  * Returns the tcp protocol number from the network protocol data base.
366  *
367  * getprotobyname() is not thread safe. We need to lock it.
368  */
369 static int TcpProtoNumber(void) {
370   struct protoent *fetchedEntry;
371   static int returnValue = 0;
372   
373   if(returnValue == 0) {
374     fetchedEntry = getprotobyname("tcp");
375     gras_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
376     returnValue = fetchedEntry->p_proto;
377   }
378   
379   return returnValue;
380 }