Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make clear that the transport plugins are plugins; let the SG one compile
[simgrid.git] / src / gras / Transport / transport_plugin_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->specific      = (void*)tcp;
94   res->free_specific = gras_trp_tcp_free_specific;
95
96   *dst = res;
97   return no_error;
98 }
99
100 void gras_trp_tcp_free_specific(void *s) {
101   gras_trp_tcp_specific_t *specific = s;
102   free(specific);
103 }
104
105 gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
106                                         const char *host,
107                                         unsigned short port,
108                                         unsigned int bufSize, 
109                                         /* OUT */ gras_socket_t **dst){
110   
111   int addrCount;
112   IPAddress addresses[10];
113   int i;
114   int sd;
115   
116   if (!(*sock=malloc(sizeof(gras_socket_t))))
117     RAISE_MALLOC;
118   
119   (*sock)->peer_addr=NULL;
120   
121   if (!(addrCount = IPAddressValues(host, addresses, 10))) {
122     RAISE2(system_error,
123           "tcp address retrieval of '%s' failed: %s",
124            host,strerror(errno));
125   }
126   
127   for(i = 0; i < addrCount && i<10 ; i++) {
128     if(CallAddr(addresses[i], port, &sd, -1)) {
129       (*sock)->sock = sd;
130       (*sock)->port = port;
131       return no_error;
132     }
133   }
134   free(*sock);
135   RAISE2(system_error,"Something wicked happenned while connecting to %s:%d",
136           host,port);
137 }
138
139 /**
140  * gras_trp_tcp_socket_server:
141  *
142  * Open a socket used to receive messages. bufSize is in ko.
143  */
144 gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
145                                         unsigned short port,
146                                         unsigned int bufSize, 
147                                         /* OUT */ gras_socket_t **dst){
148   int size = bufSize * 1024;
149   int on = 1;
150   int sd = -1;
151   struct sockaddr_in server;
152
153   gras_socket_t *res;
154   gras_trp_tcp_specific_t *data=(gras_trp_tcp_specific_t*)self -> specific;
155  
156   res=malloc(sizeof(gras_socket_t));
157   if (!res)
158     RAISE_MALLOC;
159
160   server.sin_port = htons((u_short)port);
161   server.sin_addr.s_addr = INADDR_ANY;
162   server.sin_family = AF_INET;
163   if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
164     free(res);
165     RAISE1(system_error,"socket allocation failed: %s", strerror(errno));
166   }
167
168   (void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
169   (void)setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size));
170   (void)setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size));
171   if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
172     free(res);
173     close(sd);
174     RAISE2(system_error,"Cannot bind to port %d: %s",port, strerror(errno));
175   }
176
177   if (listen(sd, 5) != -1) {
178     free(res);
179     close(sd);
180     RAISE2(system_error,"Cannot listen to port %d: %s",port,strerror(errno));
181   }
182
183   FD_SET(sd, &(data->incoming_socks));
184
185   *dst=res;
186   res->plugin = self;
187   res->incoming = 1;
188   res->sd = sd;
189   res->port=port;
190   res->peer_port=-1;
191   res->peer_name=NULL;
192
193   DEBUG2("Openned a server socket on port %d (sock %d)",port,sd);
194   
195   return no_error;
196 }
197
198 gras_error_t
199 gras_trp_tcp_socket_accept(gras_socket_t  *sock,
200                            gras_socket_t **dst) {
201   gras_socket_t *res;
202   
203   struct sockaddr_in peer_in;
204   socklen_t peer_in_len = sizeof(peer_in);
205
206   int sd;
207   int tmp_errno;
208                                 
209   res=malloc(sizeof(gras_socket_t));
210   if (!res)
211     RAISE_MALLOC;
212
213   sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
214   tmp_errno = errno;
215
216   if(sd == -1) {
217     gras_socket_close(sock);
218     RAISE1(system_error,
219            "Accept failed (%s). Droping server socket.", strerror(tmp_errno));
220   } else {
221     int i = 1;
222     socklen_t s = sizeof(int);
223   
224     if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s) 
225         || setsockopt(sd, TcpProtoNumber(), TCP_NODELAY, (char *)&i, s)) {
226       WARNING0("setsockopt failed, cannot condition the accepted socket");
227     }
228  
229     i = ((gras_trp_tcp_sock_specific_t*)sock->specific)->buffsize;
230     if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&i, s)
231         || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&i, s)) {
232       WARNING0("setsockopt failed, cannot set buffsize");       
233     }
234  
235     res->plugin = sock->plugin;
236     res->incoming = 1;
237     res->sd = sd;
238     res->port= -1;
239     res->peer_port= peer_in.sin_port;
240
241     if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
242       res->peer_name = strdup("unknown");
243     } else {
244       struct in_addr addrAsInAddr;
245       char *tmp;
246  
247       addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
248       
249       tmp = inet_ntoa(addrAsInAddr);
250       if (tmp != NULL) {
251         res->peer_name = strdup(inet_ntoa(addrAsInAddr));
252       } else {
253         res->peer_name = strdup("unknown");
254       }
255     }
256
257     VERB3("accepted socket %d to %s:%d\n", sd, res->peer_name,res->peer_port);
258     
259     *dst = res;
260
261     return no_error;
262   }
263 }
264
265 void gras_trp_tcp_socket_close(gras_socket_t *sock){
266   gras_trp_tcp_specific_t *tcp;
267   
268   if (!sock) return; /* close only once */
269   tcp=sock->plugin->specific;
270
271   DEBUG1("close tcp connection %d\n", sock->sd);
272
273   /* FIXME: no pipe in GRAS so far  
274   if(!FD_ISSET(sd, &connectedPipes)) {
275     if(shutdown(sd, 2) < 0) {
276       GetNWSLock(&lock);
277       tmp_errno = errno;
278       ReleaseNWSLock(&lock);
279       
280       / * The other side may have beaten us to the reset. * /
281       if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
282         WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
283       }
284     }
285   } */
286
287   /* close the socket */
288   if(close(sock->sd) < 0) {
289     WARNING3("error while closing tcp socket %d: %d (%s)\n", sock->sd, errno, strerror(errno));
290   }
291
292   /* forget about it */
293   FD_CLR(sock->sd, &(tcp->incoming_socks));
294
295 }
296
297 /**
298  * gras_trp_tcp_chunk_send:
299  *
300  * Send data on a TCP socket
301  */
302 gras_error_t 
303 gras_trp_tcp_chunk_send(gras_socket_t *sock,
304                     char *data,
305                     size_t size) {
306   
307   /*  gras_assert0(sock && !sock->incoming,
308       "Asked to send stuff on an incomming socket");*/
309   gras_assert0(size >= 0, "Cannot send a negative amount of data");
310
311   while (size) {
312     int status = 0;
313     
314     status = write(sock->sd, data, (size_t)size);
315     DEBUG3("write(%d, %p, %ld);\n", sock->sd, data, size);
316     
317     if (status == -1) {
318       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
319              sock->sd, data, (int)size,
320              strerror(errno));
321     }
322     
323     if (status) {
324       size  -= status;
325       data  += status;
326     } else {
327       RAISE0(system_error,"file descriptor closed");
328     }
329   }
330
331   return no_error;
332 }
333 /**
334  * gras_trp_tcp_chunk_recv:
335  *
336  * Receive data on a TCP socket.
337  */
338 gras_error_t 
339 gras_trp_tcp_chunk_recv(gras_socket_t *sock,
340                         char *data,
341                         size_t size) {
342   gras_assert0(sock && !sock->incoming,
343                "Ascked to receive stuff on an outcomming socket");
344   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
345   
346   while (size) {
347     int status = 0;
348     
349     status = read(sock->sd, data, (size_t)size);
350     DEBUG3("read(%d, %p, %ld);\n", sock->sd, data, size);
351     
352     if (status == -1) {
353       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
354              sock->sd, data, (int)size,
355              strerror(errno));
356     }
357     
358     if (status) {
359       size  -= status;
360       data  += status;
361     } else {
362       RAISE0(system_error,"file descriptor closed");
363     }
364   }
365   
366   return no_error;
367 }
368
369
370 /*
371  * Returns the tcp protocol number from the network protocol data base.
372  *
373  * getprotobyname() is not thread safe. We need to lock it.
374  */
375 static int TcpProtoNumber(void) {
376   struct protoent *fetchedEntry;
377   static int returnValue = 0;
378   
379   if(returnValue == 0) {
380     fetchedEntry = getprotobyname("tcp");
381     gras_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
382     returnValue = fetchedEntry->p_proto;
383   }
384   
385   return returnValue;
386 }