summaryrefslogtreecommitdiff
path: root/network.c
blob: 59e14a98a289766b4e269f3c6634d8227d94737d (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
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
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003 jeremian <jeremian@poczta.fm>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "network.h"
#include <string.h>
#include <errno.h>
#include <signal.h>

static void
sig_alrm(int signo)
{
	return;
}

int
ip_listen(const char *host, const char *serv, socklen_t *addrlenp, const char *type)
{
	int				listenfd, n, typ;
	const int		on = 1;
	struct addrinfo	hints, *res, *ressave;

	if (strcmp(type, "udp") == 0)
		typ = 0; /* this is udp_listen */
	else
		typ = 1; /* default: tcp_listen */
	
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = AF_UNSPEC;
	
	if (typ)
		hints.ai_socktype = SOCK_STREAM;
	else
		hints.ai_socktype = SOCK_DGRAM;

	if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
		printf("%s_listen error for %s, %s: %s\n",
				 (typ)?"tcp":"udp", host, serv, gai_strerror(n));
		exit(1);
	}
	ressave = res;

	do {
		listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (listenfd < 0)
			continue;		/* error, try next one */

		if (typ) /* tcp_listen */
			setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
		if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
			break;			/* success */

		close(listenfd);	/* bind error, close and try next one */
	} while ( (res = res->ai_next) != NULL);

	if (res == NULL) {	/* errno from final socket() or bind() */
		printf("%s_listen error for %s, %s\n", (typ)?"tcp":"udp", host, serv);
		exit(1);
	}

	if (typ) /* tcp_listen */
		listen(listenfd, 1);

	if (addrlenp)
		*addrlenp = res->ai_addrlen;	/* return size of protocol address */

	freeaddrinfo(ressave);

	return(listenfd);
}

int
ip_connect(const char *host, const char *serv, const char* type)
{
	int				sockfd, n, typ;
	struct addrinfo	hints, *res, *ressave;

	if (strcmp(type, "udp") == 0)
		typ = 0; /* this is udp_listen */
	else
		typ = 1; /* default: tcp_listen */
	
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	if (typ)
		hints.ai_socktype = SOCK_STREAM;
	else
		hints.ai_socktype = SOCK_DGRAM;

	if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
		printf("%s_connect error for %s, %s: %s\n",
				(typ)?"tcp":"udp", host, serv, gai_strerror(n));
		exit(1);
	}
	ressave = res;

	do {
		sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (sockfd < 0)
			continue;	/* ignore this one */

		if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
			break;		/* success */

		close(sockfd);	/* ignore this one */
	} while ( (res = res->ai_next) != NULL);

	if (res == NULL) {	/* errno set from final connect() */
		printf("%s_connect error for %s, %s\n", (typ)?"tcp":"udp", host, serv);
		exit(1);
	}

	freeaddrinfo(ressave);

	return(sockfd);
}

char *
sock_ntop(const struct sockaddr *sa, socklen_t salen)
{
    char		portstr[7];
    static char str[128];		/* Unix domain is largest */

	switch (sa->sa_family) {
	case AF_INET: {
		struct sockaddr_in	*sin = (struct sockaddr_in *) sa;

		if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL)
			return(NULL);
		if (ntohs(sin->sin_port) != 0) {
			snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin->sin_port));
			strcat(str, portstr);
		}
		return(str);
	}

#ifdef	IPV6
	case AF_INET6: {
		struct sockaddr_in6	*sin6 = (struct sockaddr_in6 *) sa;

		if (inet_ntop(AF_INET6, &sin6->sin6_addr, str, sizeof(str)) == NULL)
			return(NULL);
		if (ntohs(sin6->sin6_port) != 0) {
			snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin6->sin6_port));
			strcat(str, portstr);
		}
		return(str);
	}
#endif

	default:
		snprintf(str, sizeof(str), "sock_ntop: unknown AF_xxx: %d, len %d",
				 sa->sa_family, salen);
		return(str);
	}
    return (NULL);
}

int
SSL_writen(SSL* fd, unsigned char* buf, int amount)
{
	int sent, n;
	sent = 0;
	while (sent < amount) {
		signal(SIGALRM, sig_alrm);
		alarm(5);
		n = SSL_write(fd, buf+sent, amount - sent);
		alarm(0);
		if (n != -1) {
			sent += n;
		}
		if (n == -1) {
			if (errno != EAGAIN)
				return 0;
		}
	}
	return amount;
}

int
SSL_readn(SSL* fd, unsigned char* buf, int amount)
{
	int sent, n;
	sent = 0;
	while (sent < amount) {
		n = SSL_read(fd, buf+sent, amount - sent);
		if (n != -1) {
			sent += n;
		}
		if (n == 0)
			return 0;
		if (n == -1) {
			if (errno != EAGAIN)
				return 0;
		}
	}
	return amount;
} 

int
writen(int fd, unsigned char* buf, int amount)
{
	int sent, n;
	sent = 0;
	while (sent < amount) {
		signal(SIGALRM, sig_alrm);
		alarm(5);
		n = write(fd, buf+sent, amount - sent);
		alarm(0);
		if (n != -1) {
			sent += n;
		}
		if (n == -1) {
			if (errno != EAGAIN)
				return 0;
		}
	}
	return amount;
}

int
readn(int fd, unsigned char* buf, int amount)
{
	int sent, n;
	sent = 0;
	while (sent < amount) {
		n = read(fd, buf+sent, amount - sent);
		if (n != -1) {
			sent += n;
		}
		if (n == 0)
			return 0;
		if (n == -1) {
			if (errno != EAGAIN)
				return 0;
		}
	}
	return amount;

}