~ubuntu-branches/ubuntu/intrepid/dansguardian/intrepid-security

« back to all changes in this revision

Viewing changes to src/Socket.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2008-04-06 14:47:06 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080406144706-2r26l1rougdmb1sd
Tags: 2.9.9.3-2
This time build with gcc 4.3 (Closes: #454889) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Socket class - implements BaseSocket for INET domain sockets
 
2
 
 
3
//Please refer to http://dansguardian.org/?page=copyright2
 
4
//for the license for this code.
 
5
 
 
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.
 
10
//
 
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.
 
15
//
 
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
 
19
 
 
20
 
 
21
// INCLUDES
 
22
 
 
23
#include "Socket.hpp"
 
24
 
 
25
#include <syslog.h>
 
26
#include <csignal>
 
27
#include <fcntl.h>
 
28
#include <sys/time.h>
 
29
#include <pwd.h>
 
30
#include <cerrno>
 
31
#include <unistd.h>
 
32
#include <stdexcept>
 
33
#include <netinet/tcp.h>
 
34
 
 
35
 
 
36
// IMPLEMENTATION
 
37
 
 
38
// constructor - create an INET socket & clear address structs
 
39
Socket::Socket()
 
40
{
 
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);
 
47
        int f = 1;
 
48
        setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
 
49
}
 
50
 
 
51
// create socket from pre-existing FD (address structs will be invalid!)
 
52
Socket::Socket(int fd):BaseSocket(fd)
 
53
{
 
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);
 
59
        int f = 1;
 
60
        setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
 
61
}
 
62
 
 
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)
 
65
{
 
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
 
70
        my_adr = myip;
 
71
        peer_adr = peerip;
 
72
        peer_adr_length = sizeof(struct sockaddr_in);
 
73
        int f = 1;
 
74
        setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
 
75
}
 
76
 
 
77
// find the ip to which the client has connected
 
78
std::string Socket::getLocalIP()
 
79
{
 
80
        return inet_ntoa(my_adr.sin_addr);
 
81
}
 
82
 
 
83
// find the ip of the client connecting to us
 
84
std::string Socket::getPeerIP()
 
85
{
 
86
        return inet_ntoa(peer_adr.sin_addr);
 
87
}
 
88
 
 
89
// find the port of the client connecting to us
 
90
int Socket::getPeerSourcePort()
 
91
{
 
92
        return ntohs(peer_adr.sin_port);
 
93
}
 
94
 
 
95
// return the address of the client connecting to us
 
96
unsigned long int Socket::getPeerSourceAddr()
 
97
{
 
98
        return (unsigned long int)ntohl(peer_adr.sin_addr.s_addr);
 
99
}
 
100
 
 
101
// close connection & wipe address structs
 
102
void Socket::reset()
 
103
{
 
104
        this->baseReset();
 
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);
 
111
}
 
112
 
 
113
// connect to given IP & port (following default constructor)
 
114
int Socket::connect(const std::string &ip, int port)
 
115
{
 
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);
 
120
}
 
121
// bind socket to given port
 
122
int Socket::bind(int port)
 
123
{
 
124
        int len = sizeof my_adr;
 
125
        int i = 1;
 
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);
 
129
}
 
130
 
 
131
// bind socket to given port & IP
 
132
int Socket::bind(const std::string &ip, int port)
 
133
{
 
134
        int len = sizeof my_adr;
 
135
        int i = 1;
 
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);
 
140
}
 
141
 
 
142
// accept incoming connections & return new Socket
 
143
Socket* Socket::accept()
 
144
{
 
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);
 
148
        return s;
 
149
}