1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
//
// Connection.h
//
// Connection: This class forms a easy to use interface to the berkeley
// tcp socket library. All the calls are basically the same,
// but the parameters do not have any stray _addr or _in
// mixed in...
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: Connection.h,v 1.10 2004/05/28 13:15:22 lha Exp $
//
#ifndef _Connection_h_
#define _Connection_h_
#include "Object.h"
#include "htString.h"
#include <stdlib.h>
#include <sys/types.h>
#ifdef _MSC_VER /* _WIN32 */
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
class Connection : public Object
{
public:
// Constructors & Destructors
Connection(int socket = -1); // Default constructor
Connection(const Connection& rhs); // Copy constructor
~Connection();
// (De)initialization
int Win32Socket_Init(void);
int Open(int priv = 0);
virtual int Close();
int Ndelay();
int Nondelay();
int Timeout(int value);
int Retries(int value);
int WaitTime(unsigned int _wt);
// Port stuff
int Assign_Port(int port = 0);
int Assign_Port(const String& service);
int Get_Port();
inline int Is_Privileged();
// Host stuff
int Assign_Server(const String& name);
int Assign_Server(unsigned int addr = INADDR_ANY);
const String &Get_Server() const { return server_name; }
const String &Get_Server_IPAddress() const { return server_ip_address; }
// Connection establishment
virtual int Connect();
Connection *Accept(int priv = 0);
Connection *Accept_Privileged();
// Registration things
int Bind();
int Listen(int n = 5);
// IO
String* Read_Line(String &, char *terminator = (char *)"\n");
char* Read_Line(char *buffer, int maxlength, char *terminator=(char *)"\n");
String* Read_Line(char *terminator = (char *)"\n");
virtual int Read_Char();
int Write_Line(char *buffer, char *eol = (char *)"\n");
int Write(char *buffer, int maxlength = -1);
int Read(char *buffer, int maxlength);
virtual int Read_Partial(char *buffer, int maxlength);
virtual int Write_Partial(char *buffer, int maxlength);
void Stop_IO() {need_io_stop = 1;}
// Access to socket number
char *Socket_as_String();
int Get_Socket() { return sock; }
int IsOpen() { return sock >= 0; }
int IsConnected() { return connected; }
// Access to info about remote socket
const char* Get_PeerIP() const;
const char* Get_Peername();
// A method to re-initialize the buffer
virtual void Flush();
private:
//
// For buffered IO we will need a buffer
//
enum {BUFFER_SIZE = 8192};
char buffer[BUFFER_SIZE];
int pos, pos_max;
// Assignment operator declared private for preventing any use
Connection& operator+ (const Connection& rhs) { return *this; }
protected:
int sock;
struct sockaddr_in server;
int connected;
String peer;
String server_name;
String server_ip_address;
int need_io_stop;
int timeout_value;
int retry_value;
unsigned int wait_time; // time to wait after an
// unsuccessful connection
};
//*************************************************************************
// inline int Connection::Is_Privileged()
// PURPOSE:
// Return whether the port is priveleged or not.
//
inline int Connection::Is_Privileged()
{
return server.sin_port < 1023;
}
//
// Get arround the lack of gethostip() library call... There is a gethostname()
// call but we want the IP address, not the name!
// The call will put the ASCII string representing the IP address in the supplied
// buffer and it will also return the 4 byte unsigned long equivalent of it.
// The ip buffer can be null...
//
unsigned int gethostip(char *ip = 0, int length = 0);
#endif
|