libsocket
1.5
Main Page
Namespaces
Classes
Files
File List
File Members
socket.hh
Go to the documentation of this file.
1
/*
2
** socket.hh
3
** Login : Julien Lemoine <speedblue@happycoders.org>
4
** Started on Sat Mar 1 23:20:24 2003 Julien Lemoine
5
** $Id: socket.hh,v 1.11 2004/11/14 19:37:46 speedblue Exp $
6
**
7
** Copyright (C) 2003,2004 Julien Lemoine
8
** This program is free software; you can redistribute it and/or modify
9
** it under the terms of the GNU Lesser General Public License as published by
10
** the Free Software Foundation; either version 2 of the License, or
11
** (at your option) any later version.
12
**
13
** This program is distributed in the hope that it will be useful,
14
** but WITHOUT ANY WARRANTY; without even the implied warranty of
15
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
** GNU Lesser General Public License for more details.
17
**
18
** You should have received a copy of the GNU Lesser General Public License
19
** along with this program; if not, write to the Free Software
20
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
*/
22
23
#ifndef SOCKET_HH_
24
# define SOCKET_HH_
25
26
#include <iostream>
27
#include <list>
28
#include <string>
29
#include <cstring>
30
31
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
32
# define LIBSOCKET_WIN
33
#endif
34
35
#ifdef LIBSOCKET_WIN
36
# include <winsock.h>
37
#else
38
# include <sys/types.h>
// for connect(), bind(), accept(),
39
# include <sys/time.h>
// for struct timeval
40
#endif
41
42
#ifdef LIBSOCKET_WIN
43
# define SENDTO_FLAGS 0
44
#else
45
# if defined(__APPLE__) && defined(__MACH__)
46
# define SENDTO_FLAGS 0
47
# else
48
# define SENDTO_FLAGS MSG_NOSIGNAL
49
# endif
50
# include <sys/socket.h>
// for connect(), listen(), bind() accept(),
51
# include <netinet/in.h>
// for ntons(), htonl(), etc...
52
# include <arpa/inet.h>
// for inet_addre()
53
# include <netdb.h>
// for gethostbyname()
54
# include <unistd.h>
// for read()
55
#endif
56
57
#ifdef TLS
58
# include <gnutls/gnutls.h>
59
#endif
60
61
#include "
socketexception.hh
"
62
64
namespace
Network
65
{
66
typedef
enum
e_gnutls_kind
67
{
68
LIBSOCKET_TLS
,
69
LIBSOCKET_SSL
70
}
GnuTLSKind
;
71
72
typedef
enum
e_pkind
73
{
74
text
,
75
binary
76
}
PROTO_KIND
;
77
78
typedef
enum
e_kind
79
{
80
TCP
,
81
UDP
,
82
LOCAL
83
}
SOCKET_KIND
;
84
85
typedef
enum
e_version
86
{
87
V4
,
88
V6
89
}
SOCKET_VERSION
;
90
91
// ip header 20 bytes
92
// udp header 8 bytes
93
static
const
int
MAXPKTSIZE = 65507;
94
95
static
const
char
DEFAULT_DELIM =
'\0'
;
96
100
class
Socket
101
{
102
public
:
103
Socket
(
SOCKET_KIND
kind,
SOCKET_VERSION
version =
V4
);
104
Socket
(
SOCKET_KIND
kind,
PROTO_KIND
pkind,
SOCKET_VERSION
version =
V4
);
105
virtual
~Socket
();
106
107
public
:
110
void
write
(
const
std::string& str);
111
113
bool
connected
()
const
;
114
117
int
get_socket
();
119
void
add_delim
(
const
std::string& delim);
121
void
del_delim
(
const
std::string& delim);
124
void
allow_empty_lines
();
125
126
// Initialize GNUTLS support
128
void
init_tls
(
GnuTLSKind
kind,
129
unsigned
size = 1024,
130
const
std::string &certfile =
""
,
131
const
std::string &keyfile =
""
,
132
const
std::string &trustfile =
""
,
133
const
std::string &crlfile =
""
);
134
136
void
enable_tls
();
137
140
virtual
std::string
read
() = 0;
142
virtual
std::string
read
(
int
timeout) = 0;
145
virtual
std::string
readn
(
unsigned
int
size) = 0;
148
virtual
std::string
readn
(
int
timeout,
unsigned
int
size) = 0;
149
150
protected
:
154
void
_close
(
int
socket)
const
;
158
void
_listen
(
int
socket)
const
;
162
virtual
std::string
_read_line
(
int
socket) = 0;
166
virtual
std::string
_read_line_bin
(
int
socket,
unsigned
int
size) = 0;
170
void
_write_str
(
int
socket,
const
std::string& str)
const
;
174
void
_write_str_bin
(
int
socket,
const
std::string& str)
const
;
180
void
_set_timeout
(
bool
enable,
int
socket,
int
timeout);
181
// @brief find the index and the size of the delimiter.
182
std::pair<int, int>
_find_delim
(
const
std::string& str,
int
start)
const
;
185
bool
_update_buffer
(std::pair<int, int> &delim,
int
&i, std::string &str);
187
bool
_check_answer
(
int
res, std::string &str);
188
189
protected
:
190
SOCKET_KIND
_kind
;
191
SOCKET_VERSION
_version
;
192
unsigned
_state_timeout
;
193
int
_socket
;
194
int
_recv_flags
;
195
struct
sockaddr_in
_addr
;
196
# ifdef IPV6_ENABLED
197
struct
sockaddr_in6 _addr6;
198
# endif
199
PROTO_KIND
_proto_kind
;
200
std::list<std::string>
_delim
;
201
bool
_empty_lines
;
202
std::string
_buffer
;
203
bool
_tls
;
204
# ifdef TLS
205
gnutls_session _session;
206
gnutls_certificate_credentials _x509_cred;
207
unsigned
_nbbits;
208
bool
_tls_main;
209
# endif
210
};
211
213
Socket
&
operator<<
(
Socket
& s,
const
std::string& str);
215
Socket
&
operator>>
(
Socket
& s, std::string& str);
216
}
217
218
#endif
/* !SOCKET_HH_ */
src
socket.hh
Generated on Thu Oct 24 2013 09:48:13 for libsocket by
1.8.4