File: common.c

package info (click to toggle)
ayttm 0.6.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,368 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 561; yacc: 294; lex: 53; sed: 16
file content (163 lines) | stat: -rw-r--r-- 3,587 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
/*
 * Ayttm
 *
 * Copyright (C) 2009 the Ayttm team
 * 
 * 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
 *
 */

/* Basic functions like hostname lookup, connect and getting data */

#include "intl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include "common.h"
#include "net_constants.h"

struct addrinfo *lookup_address(const char *host, int port_num, int family)
{
	struct addrinfo hints;
	struct addrinfo *result = NULL;
	char port[10];

	memset(&hints, 0, sizeof(struct addrinfo));

	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_CANONNAME;

	snprintf(port, sizeof(port), "%d", port_num);

	if (getaddrinfo(host, port, &hints, &result) < 0) {
		fprintf(stderr, "Lookup: unable to create socket for %s:%s\n",
			host, port);
	} else {
		return result;
	}

	return NULL;
}

int connect_address(const char *host, int port_num, void *info)
{
	int fd;
	struct addrinfo hints;
	struct addrinfo *result = NULL, *rp = NULL;
	char port[10];
	int ret = -1;

	memset(&hints, 0, sizeof(struct addrinfo));

	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	snprintf(port, sizeof(port), "%d", port_num);

	if ((ret = getaddrinfo(host, port, &hints, &result)) < 0) {
		fprintf(stderr,
			"Connect: unable to create socket for %s:%s(%s)\n",
			host, port, gai_strerror(ret));
		ret = AY_HOSTNAME_LOOKUP_FAIL;
	} else {
		for (rp = result; rp; rp = rp->ai_next) {
			fd = socket(rp->ai_family, rp->ai_socktype,
				rp->ai_protocol);

			if (fd == -1)
				continue;

			if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1) {
				/* Preserve the connection info if requested */
				if(info)
					memcpy(info, rp->ai_addr, 
						rp->ai_addrlen);

				freeaddrinfo(result);
				return fd;
			} else {
				perror("connect:");
				ret = AY_CONNECT_FAILED;
			}
		}
	}

	if (result)
		freeaddrinfo(result);

	return ret;
}

/* this code is borrowed from cvs 1.10 */
int ay_recv_line(int sock, char **resultp)
{
	int c;
	char *result;
	size_t input_index = 0;
	size_t result_size = 80;

	result = (char *)malloc(result_size);

	while (1) {
		char ch;
		if (recv(sock, &ch, 1, 0) < 0) {
			fprintf(stderr, "recv() error from  server\n");
			return -1;
		}
		c = ch;

		if (c == EOF) {
			free(result);

			/* It's end of file.  */
			fprintf(stderr, "end of file from  server\n");
		}

		if (c == '\012')
			break;

		result[input_index++] = c;
		while (input_index + 1 >= result_size) {
			result_size *= 2;
			result = (char *)realloc(result, result_size);
		}
	}

	if (resultp)
		*resultp = result;

	/* Terminate it just for kicks, but we *can* deal with embedded NULs.  */
	result[input_index] = '\0';

	if (resultp == NULL)
		free(result);

	return input_index;
}