3 /* tcp trp (transport) - send/receive a bunch of bytes from a tcp socket */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 # include <unistd.h> /* close() pipe() read() write() */
14 # include <signal.h> /* close() pipe() read() write() */
15 # include <netinet/in.h> /* sometimes required for #include <arpa/inet.h> */
16 # include <netinet/tcp.h> /* TCP_NODELAY */
17 # include <arpa/inet.h> /* inet_ntoa() */
18 # include <netdb.h> /* getprotobyname() */
19 # include <sys/time.h> /* struct timeval */
20 # include <errno.h> /* errno */
21 # include <sys/wait.h> /* waitpid() */
22 # include <sys/socket.h> /* getpeername() socket() */
24 # include <string.h> /* memset */
28 #include "transport_private.h"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,"TCP transport");
35 xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
37 xbt_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
39 xbt_error_t gras_trp_tcp_socket_accept(gras_socket_t sock,
42 void gras_trp_tcp_socket_close(gras_socket_t sd);
44 xbt_error_t gras_trp_tcp_chunk_send(gras_socket_t sd,
48 xbt_error_t gras_trp_tcp_chunk_recv(gras_socket_t sd,
52 void gras_trp_tcp_exit(gras_trp_plugin_t *plug);
55 static int TcpProtoNumber(void);
57 *** Specific plugin part
63 } gras_trp_tcp_plug_data_t;
66 *** Specific socket part
71 } gras_trp_tcp_sock_data_t;
77 xbt_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
79 gras_trp_tcp_plug_data_t *data = xbt_new(gras_trp_tcp_plug_data_t,1);
81 FD_ZERO(&(data->msg_socks));
82 FD_ZERO(&(data->raw_socks));
84 plug->socket_client = gras_trp_tcp_socket_client;
85 plug->socket_server = gras_trp_tcp_socket_server;
86 plug->socket_accept = gras_trp_tcp_socket_accept;
87 plug->socket_close = gras_trp_tcp_socket_close;
89 plug->chunk_send = gras_trp_tcp_chunk_send;
90 plug->chunk_recv = gras_trp_tcp_chunk_recv;
92 plug->flush = NULL; /* nothing's cached */
94 plug->data = (void*)data;
95 plug->exit = gras_trp_tcp_exit;
100 void gras_trp_tcp_exit(gras_trp_plugin_t *plug) {
101 DEBUG1("Exit plugin TCP (free %p)", plug->data);
102 xbt_free(plug->data);
105 xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
108 struct sockaddr_in addr;
110 struct in_addr *haddr;
111 int size = sock->bufSize * 1024;
113 sock->incoming = 1; /* TCP sockets are duplex'ed */
115 sock->sd = socket (AF_INET, SOCK_STREAM, 0);
119 "Failed to create socket: %s",
123 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
124 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
125 WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
128 he = gethostbyname (sock->peer_name);
131 "Failed to lookup hostname %s: %s",
132 sock->peer_name, sock_errstr);
135 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
137 memset(&addr, 0, sizeof(struct sockaddr_in));
138 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
139 addr.sin_family = AF_INET;
140 addr.sin_port = htons (sock->peer_port);
142 if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
145 "Failed to connect socket to %s:%d (%s)",
146 sock->peer_name, sock->peer_port, sock_errstr);
153 * gras_trp_tcp_socket_server:
155 * Open a socket used to receive messages.
157 xbt_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
158 /* OUT */ gras_socket_t sock){
159 int size = sock->bufSize * 1024;
161 struct sockaddr_in server;
163 gras_trp_tcp_plug_data_t *tcp=(gras_trp_tcp_plug_data_t*)self->data;
165 sock->outgoing = 1; /* TCP => duplex mode */
167 server.sin_port = htons((u_short)sock->port);
168 server.sin_addr.s_addr = INADDR_ANY;
169 server.sin_family = AF_INET;
170 if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
171 RAISE1(system_error,"Socket allocation failed: %s", sock_errstr);
174 if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
175 RAISE1(system_error,"setsockopt failed, cannot condition the socket: %s",
179 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
180 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
181 WARN1("setsockopt failed, cannot set buffer size: %s",
185 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
187 RAISE2(system_error,"Cannot bind to port %d: %s",sock->port, sock_errstr);
190 if (listen(sock->sd, 5) < 0) {
192 RAISE2(system_error,"Cannot listen to port %d: %s",sock->port,sock_errstr);
196 FD_SET(sock->sd, &(tcp->raw_socks));
198 FD_SET(sock->sd, &(tcp->msg_socks));
200 DEBUG2("Openned a server socket on port %d (sock %d)",sock->port,sock->sd);
206 gras_trp_tcp_socket_accept(gras_socket_t sock,
207 gras_socket_t *dst) {
210 struct sockaddr_in peer_in;
211 socklen_t peer_in_len = sizeof(peer_in);
218 gras_trp_socket_new(1,&res);
220 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
224 gras_socket_close(sock);
226 "Accept failed (%s). Droping server socket.", sock_errstr);
229 socklen_t s = sizeof(int);
231 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
232 || setsockopt(sd, TcpProtoNumber(), TCP_NODELAY, (char *)&i, s)) {
233 RAISE1(system_error,"setsockopt failed, cannot condition the socket: %s",
237 res->bufSize = sock->bufSize;
238 size = sock->bufSize * 1024;
239 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
240 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
241 WARN1("setsockopt failed, cannot set buffer size: %s",
245 res->plugin = sock->plugin;
246 res->incoming = sock->incoming;
247 res->outgoing = sock->outgoing;
251 res->peer_port = peer_in.sin_port;
253 /* FIXME: Lock to protect inet_ntoa */
254 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
255 res->peer_name = (char*)strdup("unknown");
257 struct in_addr addrAsInAddr;
260 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
262 tmp = inet_ntoa(addrAsInAddr);
264 res->peer_name = (char*)strdup(tmp);
266 res->peer_name = (char*)strdup("unknown");
270 VERB3("accepted socket %d to %s:%d", sd, res->peer_name,res->peer_port);
279 void gras_trp_tcp_socket_close(gras_socket_t sock){
280 gras_trp_tcp_plug_data_t *tcp;
282 if (!sock) return; /* close only once */
283 tcp=sock->plugin->data;
285 DEBUG1("close tcp connection %d", sock->sd);
287 /* FIXME: no pipe in GRAS so far
288 if(!FD_ISSET(sd, &connectedPipes)) {
289 if(shutdown(sd, 2) < 0) {
292 ReleaseNWSLock(&lock);
294 / * The other side may have beaten us to the reset. * /
295 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
296 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
301 #ifndef HAVE_WINSOCK_H
302 /* forget about the socket
303 ... but not when using winsock since accept'ed socket can not fit
306 FD_CLR(sock->sd, &(tcp->raw_socks));
308 FD_CLR(sock->sd, &(tcp->msg_socks));
312 /* close the socket */
313 if(tcp_close(sock->sd) < 0) {
314 WARN3("error while closing tcp socket %d: %d (%s)\n",
315 sock->sd, sock_errno, sock_errstr);
321 * gras_trp_tcp_chunk_send:
323 * Send data on a TCP socket
326 gras_trp_tcp_chunk_send(gras_socket_t sock,
330 /* TCP sockets are in duplex mode, don't check direction */
331 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
336 status = tcp_write(sock->sd, data, (size_t)size);
337 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
340 RAISE4(system_error,"write(%d,%p,%ld) failed: %s",
341 sock->sd, data, size,
349 RAISE0(system_error,"file descriptor closed");
356 * gras_trp_tcp_chunk_recv:
358 * Receive data on a TCP socket.
361 gras_trp_tcp_chunk_recv(gras_socket_t sock,
365 /* TCP sockets are in duplex mode, don't check direction */
366 xbt_assert0(sock, "Cannot recv on an NULL socket");
367 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
372 status = tcp_read(sock->sd, data, (size_t)size);
373 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
376 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
377 sock->sd, data, (int)size,
385 RAISE0(system_error,"file descriptor closed");
394 * Returns the tcp protocol number from the network protocol data base.
396 * getprotobyname() is not thread safe. We need to lock it.
398 static int TcpProtoNumber(void) {
399 struct protoent *fetchedEntry;
400 static int returnValue = 0;
402 if(returnValue == 0) {
403 fetchedEntry = getprotobyname("tcp");
404 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
405 returnValue = fetchedEntry->p_proto;
411 /* Data exchange over raw sockets. Placing this in there is a kind of crude hack.
412 It means that the only possible raw are TCP where we may want to do UDP for them.
413 But I fail to find a good internal organization for now. We may want to split
414 raw and regular sockets more efficiently.
416 xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
418 unsigned int timeout,
419 unsigned long int exp_size,
420 unsigned long int msg_size) {
422 int res_last, msg_sofar, exp_sofar;
427 struct timeval timeOut;
429 chunk = xbt_malloc(msg_size);
431 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
432 for(msg_sofar=0; msg_sofar < msg_size; msg_sofar += res_last) {
435 res_last = send(peer->sd, chunk, msg_size - msg_sofar, 0);
439 FD_SET(peer->sd,&rd_set);
440 timeOut.tv_sec = timeout;
443 if (0 < select(peer->sd+1,&rd_set,NULL,NULL,&timeOut))
444 res_last = recv(peer->sd, chunk, msg_size-msg_sofar, 0);
448 /* No progress done, bail out */
450 RAISE0(unknown_error,"Not exchanged a single byte, bailing out");
460 #ifdef HAVE_WINSOCK_H
461 #define RETSTR( x ) case x: return #x
463 const char *gras_wsa_err2string( int err ) {
471 RETSTR( WSAEWOULDBLOCK );
472 RETSTR( WSAEINPROGRESS );
473 RETSTR( WSAEALREADY );
474 RETSTR( WSAENOTSOCK );
475 RETSTR( WSAEDESTADDRREQ );
476 RETSTR( WSAEMSGSIZE );
477 RETSTR( WSAEPROTOTYPE );
478 RETSTR( WSAENOPROTOOPT );
479 RETSTR( WSAEPROTONOSUPPORT );
480 RETSTR( WSAESOCKTNOSUPPORT );
481 RETSTR( WSAEOPNOTSUPP );
482 RETSTR( WSAEPFNOSUPPORT );
483 RETSTR( WSAEAFNOSUPPORT );
484 RETSTR( WSAEADDRINUSE );
485 RETSTR( WSAEADDRNOTAVAIL );
486 RETSTR( WSAENETDOWN );
487 RETSTR( WSAENETUNREACH );
488 RETSTR( WSAENETRESET );
489 RETSTR( WSAECONNABORTED );
490 RETSTR( WSAECONNRESET );
491 RETSTR( WSAENOBUFS );
492 RETSTR( WSAEISCONN );
493 RETSTR( WSAENOTCONN );
494 RETSTR( WSAESHUTDOWN );
495 RETSTR( WSAETOOMANYREFS );
496 RETSTR( WSAETIMEDOUT );
497 RETSTR( WSAECONNREFUSED );
499 RETSTR( WSAENAMETOOLONG );
500 RETSTR( WSAEHOSTDOWN );
501 RETSTR( WSAEHOSTUNREACH );
502 RETSTR( WSAENOTEMPTY );
503 RETSTR( WSAEPROCLIM );
507 RETSTR( WSAEREMOTE );
508 RETSTR( WSASYSNOTREADY );
509 RETSTR( WSAVERNOTSUPPORTED );
510 RETSTR( WSANOTINITIALISED );
511 RETSTR( WSAEDISCON );
514 RETSTR( WSAENOMORE );
515 RETSTR( WSAECANCELLED );
516 RETSTR( WSAEINVALIDPROCTABLE );
517 RETSTR( WSAEINVALIDPROVIDER );
518 RETSTR( WSASYSCALLFAILURE );
519 RETSTR( WSASERVICE_NOT_FOUND );
520 RETSTR( WSATYPE_NOT_FOUND );
521 RETSTR( WSA_E_NO_MORE );
522 RETSTR( WSA_E_CANCELLED );
523 RETSTR( WSAEREFUSED );
524 #endif /* HAVE_WINSOCK2 */
526 RETSTR( WSAHOST_NOT_FOUND );
527 RETSTR( WSATRY_AGAIN );
528 RETSTR( WSANO_RECOVERY );
529 RETSTR( WSANO_DATA );
531 return "unknown WSA error";
533 #endif /* HAVE_WINSOCK_H */