1 //
2 // $Id: socket.hh,v 1.3 1999/01/19 11:00:50 voeckler Exp $
3 //
4 // Author:  Jens-S. V�ckler <voeckler@rvs.uni-hannover.de>
5 //
6 // File:    socket.hh
7 //          Sun May  3 1998
8 //
9 // (c) 1998 Lehrgebiet Rechnernetze und Verteilte Systeme
10 //          Universit�t Hannover, Germany
11 //
12 // Books:   W. Richard Steven, "Advanced Programming in the UNIX Environment",
13 //          Addison-Wesley, 1992.
14 //
15 // Permission to use, copy, modify, distribute, and sell this software
16 // and its documentation for any purpose is hereby granted without fee,
17 // provided that (i) the above copyright notices and this permission
18 // notice appear in all copies of the software and related documentation,
19 // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte
20 // Systeme and the University of Hannover may not be used in any
21 // advertising or publicity relating to the software without the
22 // specific, prior written permission of Lehrgebiet Rechnernetze und
23 // Verteilte Systeme and the University of Hannover.
24 //
25 // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
27 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
28 //
29 // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR
30 // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
31 // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
32 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
33 // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
34 // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 // SOFTWARE.
36 //
37 // $Log: socket.hh,v $
38 // Revision 1.3  1999/01/19 11:00:50  voeckler
39 // added bool type workaround.
40 //
41 // Revision 1.2  1998/08/27 15:23:24  voeckler
42 // added TCP_NODELAY options at several places.
43 //
44 // Revision 1.1  1998/08/13 21:52:55  voeckler
45 // Initial revision
46 //
47 //
48 #ifndef _SOCKET_HH
49 #define _SOCKET_HH
50 
51 #if defined(__GNUC__) || defined(__GNUG__)
52 #pragma interface
53 #else
54 #ifndef HAS_BOOL
55 #define HAS_BOOL
56 typedef int bool;
57 #define false 0
58 #define true  1
59 #endif
60 #endif
61 
62 #include <sys/types.h>
63 #include <sys/socket.h>	// SOMAXCONN
64 #include <netinet/in.h>
65 
66 #if SOMAXCONN <= 5
67 #undef SOMAXCONN
68 #endif
69 
70 #ifndef SOMAXCONN
71 #if defined(SOLARIS)
72 #if MAJOR < 5
73 #define SOMAXCONN 32
74 #else
75 #define SOMAXCONN 128
76 #endif
77 #elif defined(LINUX)
78 #define SOMAXCONN 128
79 #else
80 #define SOMAXCONN 5 // BSD
81 #endif // OS selection
82 #endif // !SOMAXCONN
83 
84 #ifndef SA
85 #define SA struct sockaddr
86 #endif
87 
88 int
89 setSocketBuffers( int fd, int size );
90   // purpose: set socket buffers for both directions to the specified size
91   // paramtr: size (IN): new socket buffer size
92   // returns: -1 on setsockopt() errors, 0 otherwise
93   // warning: prints error message on stderr, errno will be changed
94 
95 int
96 getSocketNoDelay( int sockfd );
97   // purpose: get state of the TCP_NODELAY of the socket
98   // paramtr: sockfd (IN): socket descriptor
99   // returns: 1, if TCP_NODELAY is set,
100   //          0, if TCP_NODELAY is not set,
101   //         -1, if an error occurred (e.g. datagram socket)
102 
103 
104 int
105 setSocketNoDelay( int sockfd, bool nodelay = true );
106   // purpose: get state of the TCP_NODELAY of the socket
107   // paramtr: sockfd (IN): socket descriptor
108   //          nodelay (IN): true, if TCP_NODELAY is to be set, false otherwise.
109   // returns: 0, if everything worked out o.k.
110   //         -1, if an error occurred (e.g. datagram socket)
111 
112 int
113 connectTo( struct in_addr host, unsigned short port, bool nodelay = false,
114 	   int sendBufferSize = -1, int recvBufferSize = -1 );
115   // purpose: connect to a server as a client
116   // paramtr: host (IN): address describing the server
117   //          port (IN): port to connect at the server
118   //          nodelay (IN): true=set TCP_NODELAY option.
119   //          sendBufferSize (IN): don't set (use sys defaults) if < 0
120   //          recvBufferSize (IN): don't set (use sys defaults) if < 0
121   // returns: >=0 is the descriptor of the opened, connected socket,
122   //          -1  is an indication of an error (errno may have been reset).
123 
124 int
125 serverSocket( struct in_addr host, unsigned short port,
126 	      int backlog = SOMAXCONN, bool reuse = true, bool nodelay = false,
127 	      int sendBufferSize = -1, int recvBufferSize = -1 );
128   // purpose: open a server socket for listening
129   // paramtr: host (IN): host to bind locally to, use INADDRY_ANY for *
130   //          port (IN): port to bind to, use 0 for system assigned
131   //          backlog (IN): listen backlog queue length
132   //          reuse (IN): set SO_REUSEADDR option - default usefully
133   //          nodelay (IN): true=set TCP_NODELAY option.
134   //            SETTING TCP_NODELAY ON A SERVER SOCKET DOES NOT MAKE SENSE!
135   //          sendBufferSize (IN): don't set (use sys defaults) if < 0
136   //          recvBufferSize (IN): don't set (use sys defaults) if < 0
137   // returns: opened listening fd, or -1 on error.
138   // warning: error message will be printed on stderr and errno reset.
139 
140 #endif // _SOCKET_HH
141