File: sockprot.cpp

package info (click to toggle)
tcpreen 1.4.4-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,316 kB
  • sloc: sh: 4,324; cpp: 2,142; ansic: 1,870; makefile: 34; sed: 16
file content (298 lines) | stat: -rw-r--r-- 6,158 bytes parent folder | download | duplicates (2)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * sockprot.cpp - protocol-independant socket addresses handling
 * $Id: sockprot.cpp 198 2006-07-07 14:39:25Z remi $
 */

/***********************************************************************
 *  Copyright (C) 2002-2004 Remi Denis-Courmont.                       *
 *  This program is free software; you can redistribute and/or modify  *
 *  it under the terms of the GNU General Public License as published  *
 *  by the Free Software Foundation; version 2 of the license.         *
 *                                                                     *
 *  This program is distributed in the hope that it will be useful,    *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               *
 *  See the GNU General Public License for more details.               *
 *                                                                     *
 *  You should have received a copy of the GNU General Public License  *
 *  along with this program; if not, you can get it from:              *
 *  http://www.gnu.org/copyleft/gpl.html                               *
 ***********************************************************************/

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include "gettext.h"
#include "secstr.h" // secure_strncpy()

#include <sys/types.h>
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#if HAVE_SYS_UN_H
# include <sys/un.h> // struct sockaddr_un
#endif

#include <errno.h> // errno
#include <unistd.h> // close(), unlink()
#include <fcntl.h> // fcntl()
#if HAVE_SYS_SELECT_H
# include <sys/select.h> // select()
#endif

#include "solve.h"
#include "sockprot.h"

/*** SocketAddress class implementation ***/
int SocketAddress::SetFromSocket (int fd, int flags, int side)
{
	if (res != NULL)
		freeai (res);
	ClearError ();

	struct sockaddr_storage addr;
	socklen_t len = sizeof (struct sockaddr_storage);

	if ((side)	? getpeername (fd, (struct sockaddr *)&addr, &len)
			: getsockname (fd, (struct sockaddr *)&addr, &len))
	{
		res = NULL;
		return SetError ();
	}

	res = makeai ((struct sockaddr *)&addr, len);
	if (res == NULL)
		return SetError ();

	if (SetError (getnamebyaddr ((struct sockaddr *)res->ai_addr,
					res->ai_addrlen,
					myhost, sizeof (myhost),
					myserv, sizeof (myserv), flags)))
	{
		secure_strncpy (myhost, _("unknown_node"), sizeof (myhost));
		secure_strncpy (myserv, _("unknown_service"), sizeof (myserv));
		return GetError ();
	}

	return 0;
}


int SocketAddress::SetByName (const char *host, const char *service,
				int flags, int af, int type, int proto)
{
	if (res != NULL)
		freeai (res);
	ClearError ();

	if (host != NULL)
		strncpy (myhost, host, sizeof (myhost));
	else
		*myhost = 0;
	secure_strncpy (myserv, (service != NULL) ? service : "0",
			sizeof (myserv));

	struct addrinfo info;
	memset (&info, 0, sizeof (info));
	if (flags)
		info.ai_flags = flags;
	if (af)
		info.ai_family = af;
	if (type)
		info.ai_socktype = type;
	if (proto)
		info.ai_protocol = proto;

	if (SetError (getaddrbyname (host, service, &info, &res)))
	{
		res = NULL;
		return GetError ();
	}

	//if ((flags & AI_CANONNAME) && (res != NULL))
	// -- broken if multiple results
	//	secure_strncpy (myhost, sizeof (myhost), res->ai_canonname);

	return 0;
}


SocketAddress::~SocketAddress (void)
{
	if (res != NULL)
		freeai (res);
}


SocketAddress::SocketAddress (const SocketAddress& src)
	: err_ai (src.err_ai), err_sys (src.err_sys)
{
	secure_strncpy (myhost, src.myhost, sizeof (myhost));
	secure_strncpy (myserv, src.myserv, sizeof (myserv));

	if (src.res != NULL)
	{
		res = copyai (src.res);
		if (res == NULL)
			SetError ();
	}
	else
		res = NULL;
}


int SocketAddress::Bind (void)
{
	for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next)
	{
		int fd = socket (ai->ai_family, ai->ai_socktype,
					ai->ai_protocol);

		if (fd != -1)
		{
			int t = 1;

			setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &t,
					sizeof (t));
			if (bind (fd, ai->ai_addr, ai->ai_addrlen))
			{
				SetError ();
				close (fd);
			}
			else
			{
				ClearError ();
				return fd; // success!
			}
		}
		else
			SetError ();
	}

	return -1;
}


const char *
SocketAddress::StrError (void) const
{
	return (err_ai == EAI_SYSTEM)
		? strerror (err_sys)
		: gai_strerror (err_ai);
}



int
SocketAddress::SetError (int ai)
{
	if (ai == EAI_SYSTEM)
		err_sys = errno;
	return err_ai = ai;
}


int SocketAddress::Connect (int nonblock)
{
	for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next)
	{
		int fd = socket (ai->ai_family, ai->ai_socktype,
				ai->ai_protocol), t = 1;

		setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &t, sizeof (t));

		if (fd != -1)
		{
#ifdef O_NONBLOCK
			int flags = fcntl (fd, F_GETFL);
			fcntl (fd, F_SETFL, O_NONBLOCK);
#endif

			if (connect (fd, ai->ai_addr, ai->ai_addrlen) == 0)
			{
				ClearError ();
#ifdef O_NONBLOCK
				if (flags != -1)
					fcntl (fd, F_SETFL, flags);
#endif
				return fd;
			}

			if (errno != EINPROGRESS)
			{
				SetError ();
				close (fd);
				continue;
			}

			if (nonblock)
				return fd;

#ifndef WIN32
			if (fd >= FD_SETSIZE)
			{
				close (fd);
				errno = EMFILE;
			}
#endif

			/* Waits until connection is established */
			fd_set s;
			FD_ZERO (&s);
			FD_SET (fd, &s);

			if (select (fd + 1, NULL, &s, NULL, NULL) != 1)
			{
				SetError ();
				close (fd);

				if (err_sys == EINTR)
					// aborts if interrupted
					return -1;
				continue;
			}

			int err = 0;
			socklen_t len = sizeof (err);

			if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &err, &len))
			{
				SetError ();
				close (fd);
				continue;
			}

			if (len != sizeof (err))
				continue; // impossible error

			if (err)
			{
				errno = err;
				SetError ();
				close (fd);
				continue;
			}

			ClearError ();
#ifdef O_NONBLOCK
			if (flags != -1)
				fcntl (fd, F_SETFL, flags);
#endif
			return fd;
		}
	}

	return -1;
}


void
SocketAddress::CleanUp (void) const
{
#if HAVE_SYS_UN_H
	for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next)
		if (ai->ai_family == AF_LOCAL)
			unlink (((const struct sockaddr_un *)ai->ai_addr)->sun_path);
#endif
}