| "Utils.c": "/*@@\n @file Utils.c\n @date 1991\n @author John Shalf\n @desc\n Routines which deal with sockets.\n @enddesc\n @history\n @hdate Thu May 25 13:45:29 2000\n @hauthor Tom Goodale\n @hdesc Moved this file into Cactus\n @hdate Thu 14 May 2002\n @hauthor Thomas Radke\n @hdesc Merged with HTTPD socket code\n @endhistory\n @version $Id$\n @@*/\n\n#include \"SocketUtils.h\"\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <errno.h>\n\n#ifdef HAVE_NETDB_H\n#include <netdb.h>\n#endif\n#ifdef HAVE_UNISTD_H\n#include <unistd.h>\n#endif\n#ifdef HAVE_WINSOCK2_H\n#include <windows.h>\n#include <winsock2.h>\n#endif\n#ifdef HAVE_SYS_TYPES_H\n#include <sys/types.h>\n#endif\n#ifdef HAVE_SYS_FILIO_H\n#include <sys/filio.h>\n#endif\n#ifdef HAVE_SYS_IOCTL_H\n#include <sys/ioctl.h>\n#endif\n#ifdef HAVE_SYS_SOCKET_H\n#include <sys/socket.h>\n#endif\n#ifdef HAVE_NETINET_IN_H\n#include <netinet/in.h>\n#endif\n#ifdef HAVE_ARPA_INET_H\n#include <arpa/inet.h>\n#endif\n\nstatic const char *rcsid = \"$Header$\";\nCCTK_FILEVERSION(CactusConnect_Socket_Utils_c)\n\n/********************************************************************\n ********************* Macro Definitions **********************\n ********************************************************************/\n/* SunOS doesn't know INADDR_NONE */\n#ifndef INADDR_NONE\n#define INADDR_NONE (-1)\n#endif\n\n#ifndef MSG_NOSIGNAL\n#define MSG_NOSIGNAL 0\n#endif\n\n#ifdef SOCKET_HAVE_UNIX_SOCKETS\n#define IOCTL_SOCKET(a, b, c) ioctl(a, b, c)\n#else\n#define errno WSAGetLastError()\n#define IOCTL_SOCKET(a, b, c) ioctlsocket (a, b, (u_long *) (c))\n#endif\n\n#define GET_INADDR(hostname, sin_addr) \\\n { \\\n struct hostent *host_entry; \\\n \\\n \\\n host_entry = gethostbyname (hostname); \\\n if (host_entry) \\\n { \\\n memcpy (&sin_addr, host_entry->h_addr_list[0], host_entry->h_length); \\\n } \\\n else \\\n { \\\n sin_addr.s_addr = inet_addr (hostname); \\\n if (sin_addr.s_addr == INADDR_NONE) \\\n { \\\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING, \\\n \"Can't find network address for host '%s'\", \\\n hostname); \\\n return (INVALID_SOCKET); \\\n } \\\n } \\\n }\n\n\n/********************************************************************\n ********************* Internal Functions *********************\n ********************************************************************/\nstatic SOCKET TCPOpenSocket (void);\nstatic SOCKET UDPOpenSocket (struct in_addr sin_addr, unsigned int port);\n\n\n /*@@\n @routine Socket_InitializeSocketLayer\n @date Wed Sep 13 20:39:15 2000\n @author Tom Goodale\n @desc\n Special code for starting up the socket layer.\n This is only neccessary for Windows.\n @enddesc\n\n @returntype int\n @returndesc\n 0 for success, negative otherwise\n @endreturndesc\n@@*/\nint Socket_InitializeSocketLayer (void)\n{\n int retval;\n#ifdef HAVE_WINSOCK2_H\n WSADATA wsaData;\n\n\n retval = WSAStartup (MAKEWORD (2, 0), &wsaData);\n#else\n retval = 0;\n#endif /* HAVE_WINSOCK2_H */\n\n return (retval == 0 ? 0 : -1);\n}\n\n\n /*@@\n @routine Socket_TCPOpenClientSocket\n @date 1991\n @author John Shalf\n @desc\n Opens a TCP client socket by connecting to a server host with\n given port number.\n @enddesc\n\n @var hostname\n @vdesc hostname of the server to connect to\n @vtype const char *\n @vio in\n @endvar\n @var port\n @vdesc port number on the server to connect to\n @vtype unsigned int\n @vio in\n @endvar\n\n @returntype SOCKET\n @returndesc\n a valid socket descriptor, or INVALID_SOCKET for failure\n @endreturndesc\n@@*/\nSOCKET Socket_TCPOpenClientSocket (const char *hostname, unsigned int port)\n{\n SOCKET s;\n struct sockaddr_in server;\n\n\n memset (&server, 0, sizeof (server));\n server.sin_family = AF_INET;\n server.sin_port = htons (port);\n GET_INADDR (hostname, server.sin_addr);\n\n s = TCPOpenSocket ();\n if (s != INVALID_SOCKET &&\n connect (s, (struct sockaddr *) &server, sizeof (server)) == SOCKET_ERROR)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't connect to host '%s' port %u: %s\",\n hostname, port, strerror (errno));\n Socket_CloseSocket (s);\n s = INVALID_SOCKET;\n }\n\n return (s);\n}\n\n\n/*@@\n @routine Socket_TCPOpenServerSocket\n @date 1991\n @author John Shalf\n @desc\n Opens a TCP server socket on the given port.\n If the port is already taken, it will hunt for the next\n available port. The port is set up to be listened on.\n @enddesc\n\n @var port\n @vdesc port number to bind server socket\n @vtype unsigned int\n @vio in\n @endvar\n\n @returntype SOCKET\n @returndesc\n a valid socket descriptor, or INVALID_SOCKET for failure\n @endreturndesc\n@@*/\nSOCKET Socket_TCPOpenServerSocket (unsigned int port, unsigned int *hunt,\n int backlog)\n{\n SOCKET s;\n int is_bound;\n const int on = 1;\n struct sockaddr_in addr;\n\n\n s = TCPOpenSocket ();\n\n#ifdef SO_REUSEADDR\n /* try to reuse the port if possible */\n if (s != INVALID_SOCKET)\n {\n if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) ==\n SOCKET_ERROR)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't set SO_REUSEADDR to port %u: %s\",\n port, strerror (errno));\n Socket_CloseSocket (s);\n s = INVALID_SOCKET;\n }\n }\n#endif\n\n if (s != INVALID_SOCKET)\n {\n do\n {\n /* give the socket a name */\n addr.sin_family = AF_INET;\n addr.sin_port = htons (port);\n addr.sin_addr.s_addr = htonl (INADDR_ANY);\n is_bound = bind (s, (struct sockaddr *) &addr, sizeof (addr)) !=\n SOCKET_ERROR;\n if (! is_bound && hunt)\n {\n /* hunt for a new port */\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Port %u taken, trying next port\", port++);\n }\n } while (! is_bound && hunt);\n\n if (is_bound)\n {\n if (listen (s, backlog) == SOCKET_ERROR)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't listen on port %u: %s\", port, strerror (errno));\n Socket_CloseSocket (s);\n s = INVALID_SOCKET;\n }\n else if (hunt)\n {\n *hunt = port;\n }\n }\n else\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't bind socket on port %u: %s\", port, strerror(errno));\n Socket_CloseSocket (s);\n s = INVALID_SOCKET;\n }\n }\n\n return (s);\n}\n\n\n/*@@\n @routine Socket_CloseSocket\n @date Tue 15 May 2002\n @author Thomas Radke\n @desc\n Closes the given socket descriptor.\n @enddesc\n\n @var s\n @vdesc socket to close\n @vtype SOCKET\n @vio in\n @endvar\n@@*/\nvoid Socket_CloseSocket (SOCKET s)\n{\n#ifdef SOCKET_HAVE_UNIX_SOCKETS\n close (s);\n#else\n closesocket (s);\n#endif\n}\n\n\n/*@@\n @routine Socket_TCPBlockingWrite\n @date 1991\n @author John Shalf\n @desc\n** purpose: Handles error conditions when writing to a file descriptor.\n** If a buffer is too large to write in one block, this routine will\n** divide the buffer into two segments and recursively call itself to\n** send each of the halves.\n @enddesc\n@@*/\nint Socket_TCPBlockingWrite (SOCKET s, const char *buffer, int buflen)\n{\n int n;\n int nstore;\n\n\n n = send (s, buffer, buflen, MSG_NOSIGNAL);\n\n if(n != SOCKET_ERROR)\n {\n return n;\n }\n else\n {\n switch(errno) /* note use of global variable errno */\n {\n case EBADF:\n#ifdef PERRORS\n perror(\"invalid file descriptor\");\n#endif\n break;\n case EPIPE:\n#ifdef PERRORS\n perror(\"attemped to write to an unconnected socket\");\n#endif\n break;\n case EFBIG:\n#ifdef PERRORS\n perror(\"datasize too large to write.\");\n perror(\"will attempt recovery by sending in smaller pieces\");\n#endif\n /* subdivide buffer and call TCPBlockingWrite() recursively */\n nstore=n; /* preserve variable */\n Socket_TCPBlockingWrite(s,buffer,buflen>>1);\n Socket_TCPBlockingWrite(s,buffer+(buflen>>1),buflen-(buflen>>1));\n n=nstore; /* restore variable */\n break;\n case EFAULT:\n#ifdef PERRORS\n perror(\"invalid buffer address\");\n#endif\n break;\n case EINVAL:\n#ifdef PERRORS\n perror(\"file descriptor points to unusable device.\");\n#endif\n break;\n case EIO:\n#ifdef PERRORS\n perror(\"an io error occured\");\n#endif\n break;\n#ifdef EWOULDBLOCK\n case EWOULDBLOCK:\n#ifdef PERRORS\n perror(\"Non-blocking I/O is specified by ioctl\");\n perror(\"but socket has no data (would have blocked).\");\n#endif\n#endif\n break;\n }\n }\n return n; /* default, don't know what happened */\n}\n\n/*@@\n @routine Socket_TCPBlockingRead\n @date 1991\n @author John Shalf\n @desc\n** purpose: Handles error conditions when reading from a file descriptor.\n** With TCP stream connections, the record size of the recieved stream\n** may not coincide with the record written. This routine assembles\n** a fragmented record into a single array by blocking until buflen\n** characters are recieved, or write returns a premature EOF.\n @enddesc\n@@*/\nint Socket_TCPBlockingRead(SOCKET s,char *buffer, int buflen)\n{\n int n,accum=0;\n\n\n while ((n = recv (s, buffer, buflen, MSG_NOSIGNAL)) != SOCKET_ERROR)\n {\n buffer = buffer + n;\n buflen -= n;\n accum+=n;\n }\n if (n == SOCKET_ERROR)\n {\n switch(errno) /* note use of global variable errno */\n {\n case EBADF:\n#ifdef PERRORS\n perror(\"invalid file descriptor\");\n#endif\n break;\n case EFAULT:\n#ifdef PERRORS\n perror(\"invalid buffer address\");\n#endif\n break;\n case EINTR:\n#ifdef PERRORS\n perror(\"operation interrupted by a signal\");\n perror(\"disable signals and try again\");\n#endif\n break;\n#ifdef EWOULDBLOCK\n case EWOULDBLOCK:\n#ifdef PERRORS\n perror(\"Non-blocking I/O is specified by ioctl\");\n perror(\"but socket has no data (would have blocked).\");\n#endif\n#endif\n break;\n }\n }\n\n if(n<0)\n {\n return n;\n }\n else\n {\n return accum;\n }\n}\n\nint Socket_SetNonBlocking (SOCKET s)\n{\n int retval;\n unsigned int on = 1;\n\n\n retval = IOCTL_SOCKET (s, FIONBIO, &on) != SOCKET_ERROR ? 0 : -1;\n\n return(retval);\n}\n\n\nSOCKET Socket_UDPOpenClientSocket (const char *hostname, unsigned int port)\n{\n SOCKET s;\n struct in_addr sin_addr;\n\n\n GET_INADDR (hostname, sin_addr);\n\n s = UDPOpenSocket (sin_addr, port);\n\n return (s);\n}\n\n\nSOCKET Socket_UDPOpenServerSocket (unsigned int port)\n{\n SOCKET s;\n struct in_addr sin_addr;\n\n\n sin_addr.s_addr = INADDR_ANY;\n\n s = UDPOpenSocket (sin_addr, port);\n\n return (s);\n}\n\n\n/********************************************************************\n ********************* Internal Routines **********************\n ********************************************************************/\nstatic SOCKET TCPOpenSocket (void)\n{\n SOCKET s;\n struct protoent *protocol;\n\n\n protocol = getprotobyname (\"tcp\");\n if (! protocol)\n {\n CCTK_WARN (3, \"Can't find TCP protocol\");\n return (INVALID_SOCKET);\n }\n\n s = socket (PF_INET, SOCK_STREAM, protocol->p_proto);\n if (s == INVALID_SOCKET)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't create socket: %s\", strerror (errno));\n }\n\n return (s);\n}\n\n\nstatic SOCKET UDPOpenSocket (struct in_addr sin_addr, unsigned int port)\n{\n SOCKET s;\n struct protoent *protocol;\n struct sockaddr_in addr;\n\n\n memset (&addr, 0, sizeof (addr));\n addr.sin_family = AF_INET;\n addr.sin_port = htons (port);\n addr.sin_addr = sin_addr;\n\n protocol = getprotobyname (\"udp\");\n if (! protocol)\n {\n CCTK_WARN (3, \"Can't find UDP protocol\");\n return (INVALID_SOCKET);\n }\n\n s = socket (PF_INET, SOCK_DGRAM, protocol->p_proto);\n if (s == INVALID_SOCKET)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't create socket on port %u: %s\",\n port, strerror (errno));\n }\n else if (bind (s, (struct sockaddr *) &addr, sizeof (addr)) == SOCKET_ERROR)\n {\n CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,\n \"Couldn't bind to port %u: %s\", port, strerror (errno));\n Socket_CloseSocket (s);\n s = INVALID_SOCKET;\n }\n\n return (s);\n}\n" |
| "documentation.tex": "% *======================================================================*\n% Cactus Thorn template for ThornGuide documentation\n% Author: Ian Kelley\n% Date: Sun Jun 02, 2002\n% $Header$ \n%\n% Thorn documentation in the latex file doc/documentation.tex \n% will be included in ThornGuides built with the Cactus make system.\n% The scripts employed by the make system automatically include \n% pages about variables, parameters and scheduling parsed from the \n% relevent thorn CCL files.\n% \n% This template contains guidelines which help to assure that your \n% documentation will be correctly added to ThornGuides. More \n% information is available in the Cactus UsersGuide.\n% \n% Guidelines:\n% - Do not change anything before the line\n% % BEGIN CACTUS THORNGUIDE\",\n% except for filling in the title, author, date etc. fields.\n% - You can define your own macros are OK, but they must appear after\n% the BEGIN CACTUS THORNGUIDE line, and do not redefine standard \n% latex commands.\n% - To avoid name clashes with other thorns, 'labels', 'citations', \n% 'references', and 'image' names should conform to the following \n% convention: \n% ARRANGEMENT_THORN_LABEL\n% For example, an image wave.eps in the arrangement CactusWave and \n% thorn WaveToyC should be renamed to CactusWave_WaveToyC_wave.eps\n% - Graphics should only be included using the graphix package. \n% More specifically, with the \"includegraphics\" command. Do\n% not specify any graphic file extensions in your .tex file. This \n% will allow us (later) to create a PDF version of the ThornGuide\n% via pdflatex. |\n% - References should be included with the latex \"bibitem\" command. \n% - For the benefit of our Perl scripts, and for future extensions, \n% please use simple latex. \n%\n% *======================================================================* \n% \n% Example of including a graphic image:\n% \\begin{figure}[ht]\n% \t\\begin{center}\n% \t \\includegraphics[width=6cm]{MyArrangement_MyThorn_MyFigure}\n% \t\\end{center}\n% \t\\caption{Illustration of this and that}\n% \t\\label{MyArrangement_MyThorn_MyLabel}\n% \\end{figure}\n%\n% Example of using a label:\n% \\label{MyArrangement_MyThorn_MyLabel}\n%\n% Example of a citation:\n% \\cite{MyArrangement_MyThorn_Author99}\n%\n% Example of including a reference\n% \\bibitem{MyArrangement_MyThorn_Author99}\n% {J. Author, {\\em The Title of the Book, Journal, or periodical}, 1 (1999), \n% 1--16. {\\tt http://www.nowhere.com/}}\n%\n% *======================================================================* \n\n% If you are using CVS use this line to give version information\n% $Header$\n\n\\documentclass{article}\n\n% Use the Cactus ThornGuide style file\n% (Automatically used from Cactus distribution, if you have a \n% thorn without the Cactus Flesh download this from the Cactus\n% homepage at www.cactuscode.org)\n\\usepackage{../../../../doc/latex/cactus}\n\n\\begin{document}\n\n% The author of the documentation\n\\author{} \n\n% The title of the document (not necessarily the name of the Thorn)\n\\title{Socket}\n\n% the date your document was last changed, if your document is in CVS, \n% please us:\n% \\date{$ $Date$ $}\n\\date{}\n\n\\maketitle\n\n% Do not delete next line\n% START CACTUS THORNGUIDE\n\n% Add all definitions used in this documentation here \n% \\def\\mydef etc\n\n% Add an abstract for this thorn's documentation\n\\begin{abstract}\n\n\\end{abstract}\n\n% The following sections are suggestive only.\n% Remove them or add your own.\n\n\\section{Introduction}\n\n\\section{Physical System}\n\n\\section{Numerical Implementation}\n\n\\section{Using This Thorn}\n\n\\subsection{Obtaining This Thorn}\n\n\\subsection{Basic Usage}\n\n\\subsection{Special Behaviour}\n\n\\subsection{Interaction With Other Thorns}\n\n\\subsection{Support and Feedback}\n\n\\section{History}\n\n\\subsection{Thorn Source Code}\n\n\\subsection{Thorn Documentation}\n\n\\subsection{Acknowledgements}\n\n\n\\begin{thebibliography}{9}\n\n\\end{thebibliography}\n\n% Do not delete next line\n% END CACTUS THORNGUIDE\n\n\\end{document}\n" |