summaryrefslogtreecommitdiff
path: root/tools/socket.h
blob: ceaad782c1deee0a7ddc8a748903a6b7837bc69f (plain)
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
#ifndef TOOLBOX_SOCKET_H
#define TOOLBOX_SOCKET_H

#include "tools.h"
#include "source.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>

/* cTBSocket provides a cTBSource-derived interface for input and output on
   TCP/IPv4-sockets. */

class cTBSocket: public cTBSource {
private:
	struct sockaddr_in m_LocalAddr;
	struct sockaddr_in m_RemoteAddr;

	int m_Type;

public:
	cTBSocket(int Type = SOCK_STREAM);
	virtual ~cTBSocket();
	
	/* See cTBSource::SysRead() 
	   Reimplemented for TCP/IPv4 sockets. */
	virtual ssize_t SysRead(void *Buffer, size_t Length) const;

	/* See cTBSource::SysWrite() 
	   Reimplemented for TCP/IPv4 sockets. */
	virtual ssize_t SysWrite(const void *Buffer, size_t Length) const;

	/* Connect() tries to connect an available local socket to the port given
	   by Port of the target host given by Host in numbers-and-dots notation
	   (i.e. "212.43.45.21"). Returns true if the connection attempt was 
	   successful and false otherwise, setting errno appropriately. */
	virtual bool Connect(const std::string &Host, uint Port);

	/* Shutdown() shuts down one or both ends of a socket. If called with How
	   set to SHUT_RD, further reads on this socket will be denied. If called
	   with SHUT_WR, all writes are denied. Called with SHUT_RDWR, all firther
	   action on this socket will be denied. Returns true on success and false
	   otherwise, setting errno appropriately. */
	virtual bool Shutdown(int How);

	/* OpenUDP() Open the associated socket. 
	   Returns true on success and false otherwise, setting errno 
	   appropriately. */
	virtual bool OpenUDP(const std::string &Ip, uint Port);
	
	/* Close() closes the associated socket and releases all structures. 
	   Returns true on success and false otherwise, setting errno 
	   appropriately. The object is in the closed state afterwards, regardless
	   of any errors. */
	virtual bool Close(void);

	/* Listen() listens on the local port Port for incoming connections. The 
	   BackLog parameter defines the maximum length the queue of pending 
	   connections may grow to. Returns true if the object is listening on
	   the specified port and false otherwise, setting errno appropriately. */
	virtual bool Listen(const std::string &Ip, uint Port, int BackLog);

	/* Accept() returns a newly created cTBSocket, which is connected to the 
	   first connection request on the queue of pending connections of a
	   listening socket. If no connection request was pending, or if any other
	   error occured, the resulting cTBSocket is closed. */
	virtual cTBSocket Accept(void) const;

	/* Accept() extracts the first connection request on the queue of pending
	   connections of the listening socket Listener and connects it to this
	   object. Returns true on success and false otherwise, setting errno to
	   an appropriate value. */
	virtual bool Accept(const cTBSocket &Listener);

	/* LocalPort() returns the port number this socket is connected to locally.
	   The result is undefined for a non-open socket. */
	int LocalPort(void) const { return ntohs(m_LocalAddr.sin_port); }

	/* RemotePort() returns the port number this socket is connected to on the
	   remote side. The result is undefined for a non-open socket. */
	int RemotePort(void) const { return ntohs(m_RemoteAddr.sin_port); }

	/* LocalIp() returns the internet address in numbers-and-dots notation of
	   the interface this socket is connected to locally. This can be 
	   "0.0.0.0" for a listening socket listening to all interfaces. If the
	   socket is in its closed state, the result is undefined. */
	std::string LocalIp(void) const { return inet_ntoa(m_LocalAddr.sin_addr); }

	/* RemoteIp() returns the internet address in numbers-and-dots notation of
	   the interface this socket is connected to on the remote side. If the
	   socket is in its closed state, the result is undefined. */
	std::string RemoteIp(void) const { return inet_ntoa(m_RemoteAddr.sin_addr); }

	in_addr_t LocalIpAddr(void) const { return m_LocalAddr.sin_addr.s_addr; }
	in_addr_t RemoteIpAddr(void) const { return m_RemoteAddr.sin_addr.s_addr; }

	int Type(void) const { return m_Type; }
};

inline ssize_t cTBSocket::SysRead(void *Buffer, size_t Length) const {
	if (m_Type == SOCK_DGRAM) {
		socklen_t len = sizeof(m_RemoteAddr);
		return ::recvfrom(*this, Buffer, Length, 0, (sockaddr*)&m_RemoteAddr, &len);
	} else
		return ::recv(*this, Buffer, Length, 0);
}

inline ssize_t cTBSocket::SysWrite(const void *Buffer, size_t Length) const {
	if ((m_Type == SOCK_DGRAM)) {
		socklen_t len = sizeof(m_RemoteAddr);
		return ::sendto(*this, Buffer, Length, 0, (sockaddr*)&m_RemoteAddr, len);
	} else
		return ::send(*this, Buffer, Length, 0);
}

#endif // TOOLBOX_SOCKET_H