1
// Socket class - implements BaseSocket for INET domain sockets
3
//Please refer to http://dansguardian.org/?page=copyright2
4
//for the license for this code.
6
// This program is free software; you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation; either version 2 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
#include <netinet/tcp.h>
38
// constructor - create an INET socket & clear address structs
41
sck = socket(AF_INET, SOCK_STREAM, 0);
42
memset(&my_adr, 0, sizeof my_adr);
43
memset(&peer_adr, 0, sizeof peer_adr);
44
my_adr.sin_family = AF_INET;
45
peer_adr.sin_family = AF_INET;
46
peer_adr_length = sizeof(struct sockaddr_in);
48
setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
51
// create socket from pre-existing FD (address structs will be invalid!)
52
Socket::Socket(int fd):BaseSocket(fd)
54
memset(&my_adr, 0, sizeof my_adr);
55
memset(&peer_adr, 0, sizeof peer_adr);
56
my_adr.sin_family = AF_INET;
57
peer_adr.sin_family = AF_INET;
58
peer_adr_length = sizeof(struct sockaddr_in);
60
setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
63
// create socket from pre-existing FD, storing local & remote IPs
64
Socket::Socket(int newfd, struct sockaddr_in myip, struct sockaddr_in peerip):BaseSocket(newfd)
66
memset(&my_adr, 0, sizeof my_adr); // ***
67
memset(&peer_adr, 0, sizeof peer_adr); // ***
68
my_adr.sin_family = AF_INET; // *** Fix suggested by
69
peer_adr.sin_family = AF_INET; // *** Christopher Weimann
72
peer_adr_length = sizeof(struct sockaddr_in);
74
setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
77
// find the ip to which the client has connected
78
std::string Socket::getLocalIP()
80
return inet_ntoa(my_adr.sin_addr);
83
// find the ip of the client connecting to us
84
std::string Socket::getPeerIP()
86
return inet_ntoa(peer_adr.sin_addr);
89
// find the port of the client connecting to us
90
int Socket::getPeerSourcePort()
92
return ntohs(peer_adr.sin_port);
95
// return the address of the client connecting to us
96
unsigned long int Socket::getPeerSourceAddr()
98
return (unsigned long int)ntohl(peer_adr.sin_addr.s_addr);
101
// close connection & wipe address structs
105
sck = socket(AF_INET, SOCK_STREAM, 0);
106
memset(&my_adr, 0, sizeof my_adr);
107
memset(&peer_adr, 0, sizeof peer_adr);
108
my_adr.sin_family = AF_INET;
109
peer_adr.sin_family = AF_INET;
110
peer_adr_length = sizeof(struct sockaddr_in);
113
// connect to given IP & port (following default constructor)
114
int Socket::connect(const std::string &ip, int port)
116
int len = sizeof my_adr;
117
peer_adr.sin_port = htons(port);
118
inet_aton(ip.c_str(), &peer_adr.sin_addr);
119
return ::connect(sck, (struct sockaddr *) &peer_adr, len);
121
// bind socket to given port
122
int Socket::bind(int port)
124
int len = sizeof my_adr;
126
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
127
my_adr.sin_port = htons(port);
128
return ::bind(sck, (struct sockaddr *) &my_adr, len);
131
// bind socket to given port & IP
132
int Socket::bind(const std::string &ip, int port)
134
int len = sizeof my_adr;
136
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
137
my_adr.sin_port = htons(port);
138
my_adr.sin_addr.s_addr = inet_addr(ip.c_str());
139
return ::bind(sck, (struct sockaddr *) &my_adr, len);
142
// accept incoming connections & return new Socket
143
Socket* Socket::accept()
145
peer_adr_length = sizeof(struct sockaddr_in);
146
int newfd = this->baseAccept((struct sockaddr*) &peer_adr, &peer_adr_length);
147
Socket* s = new Socket(newfd, my_adr, peer_adr);