File: socket.c

package info (click to toggle)
lash 0.5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,224 kB
  • ctags: 1,245
  • sloc: ansic: 11,677; sh: 8,933; makefile: 264; python: 36
file content (290 lines) | stat: -rw-r--r-- 6,418 bytes parent folder | download
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
/*
 *   LASH
 *    
 *   Copyright (C) 2002 Robert Ham <rah@bash.sh>
 *    
 *   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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#define _POSIX_SOURCE /* addrinfo */
#define _GNU_SOURCE

#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
extern int h_errno;

#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <lash/list.h> 
#include <lash/debug.h>

int
lash_sendall(int socket, const void *buf, size_t buf_size, int flags)
{
	int err;
	char *new_buf;
	size_t sent, new_buf_size;
	uint32_t *iptr;

	/* create the new buffer */
	new_buf_size = buf_size + sizeof(uint32_t);
	new_buf = lash_malloc(new_buf_size);

	/* create the packet header */
	iptr = (uint32_t *) new_buf;
	*iptr = htonl(new_buf_size);

	/* check to see if the size has been truncated
	 * (should never happen - we're not shifting much data around) */
	if (ntohl(*iptr) != new_buf_size) {
		fprintf(stderr, "%s: buf_size was truncated by htonl()!\n",
				__FUNCTION__);
		free(new_buf);
		return -1;
	}

	/* fill the packet */
	iptr++;
	memcpy(iptr, buf, buf_size);

	sent = 0;
	while (sent < new_buf_size) {
		err = send(socket, new_buf + sent, new_buf_size - sent, flags);

		if (err == 0) {			/* connection was closed */
			free(new_buf);
			return (-2);
		}

		if (err == -1) {
			if (errno == EINTR)
				continue;

			fprintf(stderr, "%s: error sending data: %s\n", __FUNCTION__,
					strerror(errno));
			free(new_buf);
			return (-1);
		}

		sent += err;
	}

	free(new_buf);
	return sent - sizeof(uint32_t);
}

int
lash_recvall(int socket, void **buf_ptr, size_t * buf_size_ptr, int flags)
{
	int err;
	char *buf = NULL;
	size_t recvd, buf_size;
	uint32_t *iptr;
	size_t packet_size;

	buf_size = sizeof(uint32_t);
	buf = lash_malloc(buf_size);

	recvd = 0;
	while (recvd < sizeof(uint32_t)) {
		err = recv(socket, buf + recvd, sizeof(uint32_t) - recvd, flags);

		/* check if the socket was closed */
		if (err == 0 && recvd == 0) {
			free(buf);
			return -2;
		}

		if (err == -1) {
			if (errno == EINTR)
				continue;

			fprintf(stderr, "%s: error recieving data: %s\n", __FUNCTION__,
					strerror(errno));
			free(buf);
			return (-1);
		}

		recvd += err;
	}

	iptr = (uint32_t *) buf;
	packet_size = ntohl(*iptr) - sizeof(uint32_t);
	if (buf_size != packet_size) {
		buf_size = packet_size;
		buf = lash_realloc(buf, buf_size);
	}

	recvd = 0;
	while (recvd < buf_size) {
		err = recv(socket, buf + recvd, buf_size - recvd, flags);
		if (err == -1) {
			fprintf(stderr, "%s: error recieving data: %s\n", __FUNCTION__,
					strerror(errno));
			free(buf);
			return (-1);
		}

		recvd += err;
	}

	*buf_ptr = buf;
	*buf_size_ptr = buf_size;

	return packet_size;
}

int
lash_open_socket(int *sockptr, const char *host, const char *service)
{
	struct addrinfo hints;
	struct addrinfo *addrs;
	struct addrinfo *addr;
	int sock = 0;
	int err;
	int connected = 0;

	LASH_DEBUGARGS("attempting to connect to host '%s', service '%s'", host,
				   service);

	memset(&hints, 0, sizeof(hints));
	hints.ai_socktype = SOCK_STREAM;

	err = getaddrinfo(host, service, &hints, &addrs);
	if (err) {
		fprintf(stderr, "%s: could not look up host '%s': %s\n",
				__FUNCTION__, host, gai_strerror(err));
		return -1;
	}

	for (addr = addrs; addr; addr = addr->ai_next) {
#ifdef LASH_DEBUG
		{
			char num_addr[NI_MAXHOST];
			char port_addr[NI_MAXSERV];

			err = getnameinfo(addr->ai_addr, addr->ai_addrlen,
							  num_addr, sizeof(num_addr),
							  port_addr, sizeof(port_addr),
							  NI_NUMERICHOST | NI_NUMERICSERV);
			if (err) {
				fprintf(stderr,
						"%s: error looking up numeric host/port names: %s\n",
						__FUNCTION__, strerror(errno));
			} else {
				LASH_DEBUGARGS("attempting to connect to %s:%s", num_addr,
							   port_addr);
			}
		}
#endif

		sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
		if (sock == -1) {
			LASH_DEBUGARGS
				("could not create socket with params domain=%d, type=%d, protocol=%d: %s",
				 addr->ai_family, addr->ai_socktype, addr->ai_protocol,
				 strerror(errno));
			continue;
		}

		err = connect(sock, addr->ai_addr, addr->ai_addrlen);
		if (err) {
			LASH_DEBUGARGS("error connecting: %s", strerror(errno));

			err = close(sock);
			if (err) {
				fprintf(stderr, "%s: error closing unconnected socket: %s\n",
						__FUNCTION__, strerror(errno));
			}
		} else {
			connected = 1;
			break;
		}
	}

	freeaddrinfo(addrs);

	if (!connected) {
		fprintf(stderr, "%s: could not connect to host '%s', service '%s'\n",
				__FUNCTION__, host, service);
		return -1;
	}

	LASH_PRINT_DEBUG("socket connected");
	*sockptr = sock;
	return 0;
}

static int
lash_lookup_peer_info(int sock,
					  char *host, size_t host_len,
					  char *port, size_t port_len)
{
	struct sockaddr_storage ss;
	socklen_t ss_len = sizeof(ss);
	int err;

	err = getpeername(sock, (struct sockaddr *)&ss, &ss_len);
	if (err) {
		fprintf(stderr, "%s: could not get peer address: %s\n", __FUNCTION__,
				strerror(errno));
		return -1;
	}

	err = getnameinfo((struct sockaddr *)&ss, ss_len,
					  host, host_len, port, port_len, 0);
	if (err) {
		fprintf(stderr, "%s: could not look up peer name: %s\n", __FUNCTION__,
				strerror(errno));
		return -1;
	}

	return 0;
}

const char *
lash_lookup_peer_name(int sock)
{
	static char host[NI_MAXHOST];
	int err;

	err = lash_lookup_peer_info(sock, host, sizeof(host), NULL, 0);
	if (err)
		return NULL;

	return host;
}

const char *
lash_lookup_peer_port(int sock)
{
	static char port[NI_MAXSERV];
	int err;

	err = lash_lookup_peer_info(sock, NULL, 0, port, sizeof(port));
	if (err)
		return NULL;

	return port;
}

/* EOF */