/* tcp trp (transport) - send/receive a bunch of bytes from a tcp socket */
-/* Authors: Martin Quinson */
-/* Copyright (C) 2004 Martin Quinson. */
+/* Copyright (c) 2004 Martin Quinson. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
- under the terms of the license (GNU LGPL) which comes with this package. */
-
-#include <unistd.h> /* close() pipe() read() write() */
-#include <signal.h> /* close() pipe() read() write() */
-#include <netinet/in.h> /* sometimes required for #include <arpa/inet.h> */
-#include <netinet/tcp.h> /* TCP_NODELAY */
-#include <arpa/inet.h> /* inet_ntoa() */
-#include <netdb.h> /* getprotobyname() */
-#include <sys/time.h> /* struct timeval */
-#include <errno.h> /* errno */
-#include <sys/wait.h> /* waitpid() */
-#include <sys/socket.h> /* getpeername() socket() */
-#include <stdlib.h>
-#include <string.h> /* memset */
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "portable.h"
+
+#if 0
+# include <signal.h> /* close() pipe() read() write() */
+# include <sys/wait.h> /* waitpid() */
+#endif
+
#include "transport_private.h"
plug->data = (void*)data;
plug->exit = gras_trp_tcp_exit;
-
+
return no_error;
}
if (sock->sd < 0) {
RAISE1(system_error,
"Failed to create socket: %s",
- strerror (errno));
+ sock_errstr);
}
if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
- WARN1("setsockopt failed, cannot set buffer size: %s",
- strerror(errno));
+ WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
}
he = gethostbyname (sock->peer_name);
if (he == NULL) {
RAISE2(system_error,
"Failed to lookup hostname %s: %s",
- sock->peer_name, strerror (errno));
+ sock->peer_name, sock_errstr);
}
haddr = ((struct in_addr *) (he->h_addr_list)[0]);
addr.sin_port = htons (sock->peer_port);
if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
- close(sock->sd);
+ tcp_close(sock->sd);
RAISE3(system_error,
"Failed to connect socket to %s:%d (%s)",
- sock->peer_name, sock->peer_port, strerror (errno));
+ sock->peer_name, sock->peer_port, sock_errstr);
}
return no_error;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- RAISE1(system_error,"Socket allocation failed: %s", strerror(errno));
+ RAISE1(system_error,"Socket allocation failed: %s", sock_errstr);
}
if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
RAISE1(system_error,"setsockopt failed, cannot condition the socket: %s",
- strerror(errno));
+ sock_errstr);
}
if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
WARN1("setsockopt failed, cannot set buffer size: %s",
- strerror(errno));
+ sock_errstr);
}
if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
- close(sock->sd);
- RAISE2(system_error,"Cannot bind to port %d: %s",sock->port, strerror(errno));
+ tcp_close(sock->sd);
+ RAISE2(system_error,"Cannot bind to port %d: %s",sock->port, sock_errstr);
}
if (listen(sock->sd, 5) < 0) {
- close(sock->sd);
- RAISE2(system_error,"Cannot listen to port %d: %s",sock->port,strerror(errno));
+ tcp_close(sock->sd);
+ RAISE2(system_error,"Cannot listen to port %d: %s",sock->port,sock_errstr);
}
if (sock->raw)
int sd;
int tmp_errno;
int size;
-
+
+ XBT_IN;
gras_trp_socket_new(1,&res);
sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
if(sd == -1) {
gras_socket_close(sock);
RAISE1(system_error,
- "Accept failed (%s). Droping server socket.", strerror(tmp_errno));
+ "Accept failed (%s). Droping server socket.", sock_errstr);
} else {
int i = 1;
socklen_t s = sizeof(int);
if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
|| setsockopt(sd, TcpProtoNumber(), TCP_NODELAY, (char *)&i, s)) {
RAISE1(system_error,"setsockopt failed, cannot condition the socket: %s",
- strerror(errno));
+ sock_errstr);
}
-
- (*dst)->bufSize = sock->bufSize;
+
+ res->bufSize = sock->bufSize;
size = sock->bufSize * 1024;
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
|| setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
WARN1("setsockopt failed, cannot set buffer size: %s",
- strerror(errno));
+ sock_errstr);
}
res->plugin = sock->plugin;
*dst = res;
+ XBT_OUT;
return no_error;
}
}
}
} */
- /* forget about the socket */
- if (sock->raw)
+#ifndef HAVE_WINSOCK_H
+ /* forget about the socket
+ ... but not when using winsock since accept'ed socket can not fit
+ into the fd_set*/
+ if (sock->raw){
FD_CLR(sock->sd, &(tcp->raw_socks));
- else
+ } else {
FD_CLR(sock->sd, &(tcp->msg_socks));
-
+ }
+#endif
+
/* close the socket */
- if(close(sock->sd) < 0) {
+ if(tcp_close(sock->sd) < 0) {
WARN3("error while closing tcp socket %d: %d (%s)\n",
- sock->sd, errno, strerror(errno));
+ sock->sd, sock_errno, sock_errstr);
}
+
}
/**
while (size) {
int status = 0;
- status = write(sock->sd, data, (size_t)size);
+ status = tcp_write(sock->sd, data, (size_t)size);
DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
- if (status == -1) {
+ if (status <= 0) {
RAISE4(system_error,"write(%d,%p,%ld) failed: %s",
sock->sd, data, size,
- strerror(errno));
+ sock_errstr);
}
if (status) {
while (size) {
int status = 0;
- status = read(sock->sd, data, (size_t)size);
+ status = tcp_read(sock->sd, data, (size_t)size);
DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
- if (status == -1) {
+ if (status <= 0) {
RAISE4(system_error,"read(%d,%p,%d) failed: %s",
sock->sd, data, (int)size,
- strerror(errno));
+ sock_errstr);
}
if (status) {
xbt_free(chunk);
return no_error;
}
+
+
+#ifdef HAVE_WINSOCK_H
+#define RETSTR( x ) case x: return #x
+
+const char *gras_wsa_err2string( int err ) {
+ switch( err ) {
+ RETSTR( WSAEINTR );
+ RETSTR( WSAEBADF );
+ RETSTR( WSAEACCES );
+ RETSTR( WSAEFAULT );
+ RETSTR( WSAEINVAL );
+ RETSTR( WSAEMFILE );
+ RETSTR( WSAEWOULDBLOCK );
+ RETSTR( WSAEINPROGRESS );
+ RETSTR( WSAEALREADY );
+ RETSTR( WSAENOTSOCK );
+ RETSTR( WSAEDESTADDRREQ );
+ RETSTR( WSAEMSGSIZE );
+ RETSTR( WSAEPROTOTYPE );
+ RETSTR( WSAENOPROTOOPT );
+ RETSTR( WSAEPROTONOSUPPORT );
+ RETSTR( WSAESOCKTNOSUPPORT );
+ RETSTR( WSAEOPNOTSUPP );
+ RETSTR( WSAEPFNOSUPPORT );
+ RETSTR( WSAEAFNOSUPPORT );
+ RETSTR( WSAEADDRINUSE );
+ RETSTR( WSAEADDRNOTAVAIL );
+ RETSTR( WSAENETDOWN );
+ RETSTR( WSAENETUNREACH );
+ RETSTR( WSAENETRESET );
+ RETSTR( WSAECONNABORTED );
+ RETSTR( WSAECONNRESET );
+ RETSTR( WSAENOBUFS );
+ RETSTR( WSAEISCONN );
+ RETSTR( WSAENOTCONN );
+ RETSTR( WSAESHUTDOWN );
+ RETSTR( WSAETOOMANYREFS );
+ RETSTR( WSAETIMEDOUT );
+ RETSTR( WSAECONNREFUSED );
+ RETSTR( WSAELOOP );
+ RETSTR( WSAENAMETOOLONG );
+ RETSTR( WSAEHOSTDOWN );
+ RETSTR( WSAEHOSTUNREACH );
+ RETSTR( WSAENOTEMPTY );
+ RETSTR( WSAEPROCLIM );
+ RETSTR( WSAEUSERS );
+ RETSTR( WSAEDQUOT );
+ RETSTR( WSAESTALE );
+ RETSTR( WSAEREMOTE );
+ RETSTR( WSASYSNOTREADY );
+ RETSTR( WSAVERNOTSUPPORTED );
+ RETSTR( WSANOTINITIALISED );
+ RETSTR( WSAEDISCON );
+
+#ifdef HAVE_WINSOCK2
+ RETSTR( WSAENOMORE );
+ RETSTR( WSAECANCELLED );
+ RETSTR( WSAEINVALIDPROCTABLE );
+ RETSTR( WSAEINVALIDPROVIDER );
+ RETSTR( WSASYSCALLFAILURE );
+ RETSTR( WSASERVICE_NOT_FOUND );
+ RETSTR( WSATYPE_NOT_FOUND );
+ RETSTR( WSA_E_NO_MORE );
+ RETSTR( WSA_E_CANCELLED );
+ RETSTR( WSAEREFUSED );
+#endif /* HAVE_WINSOCK2 */
+
+ RETSTR( WSAHOST_NOT_FOUND );
+ RETSTR( WSATRY_AGAIN );
+ RETSTR( WSANO_RECOVERY );
+ RETSTR( WSANO_DATA );
+ }
+ return "unknown WSA error";
+}
+#endif /* HAVE_WINSOCK_H */