summaryrefslogtreecommitdiffstats
path: root/sockutil.c
blob: 9d2da1d4ef047717850a92de6cad0715ece4bfe7 (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
/*
 * Copyright © 2005,2008 Red Hat, Inc. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions of the
 * GNU General Public License v.2.  This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
 * including the implied warranties 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
 * trademarks that are incorporated in the source code or documentation are not
 * subject to the GNU General Public License and may only be used or replicated
 * with the express permission of Red Hat, Inc.
 *
 * Red Hat Author(s): Nathan Straz <nstraz@redhat.com>
 *                    Dean Jansa <djansa@redhat.com>
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include "qarsh_packet.h"

/* Logging provided by qarshd, qarsh, or qacp */
extern void lprintf(int priority, const char *format, ...);

static int packet_seq = 1;
/* Some generic socket related functions to make things easier */

int
connect_to_host(char *hostname, int port, unsigned short *ss_family)
{
	struct addrinfo *ail;
	struct addrinfo *aip;
	struct addrinfo hints;
	char portstr[NI_MAXSERV];
	int sd;
	int err;

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

	memset(portstr, 0, sizeof portstr);
	snprintf(portstr, NI_MAXSERV, "%d", port);

	if ((err = getaddrinfo(hostname, portstr, &hints, &ail)) != 0) {
		lprintf(0, "Could not resolve hostname %s: %s\n", 
		        hostname, gai_strerror(err));
		return -1;
	}

	/* TBD -- do we loop over all of the addrinfos returned trying to 
  	 * connect, or just pick the first one? */ 

	for (aip = ail; aip != NULL; aip = aip->ai_next) {
#if 0
		char hname[NI_MAXHOST] = "";
		char nname[NI_MAXHOST] = "";
		
		err = getnameinfo(aip->ai_addr, aip->ai_addrlen, hname, NI_MAXHOST, NULL, 0, 0); 
		err = getnameinfo(aip->ai_addr, aip->ai_addrlen, nname, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); 
 
		if (err != 0) {
			printf("error in getnameinfo: %s\n", gai_strerror(err));
			continue;
		}
 
		if (*hname && *nname) {
			printf("Trying: %s (%s) -- ", hname, nname);
			if (aip->ai_family == AF_INET6) puts("IPv6");
			if (aip->ai_family == AF_INET) puts("IPv4");
		}
#endif

		sd = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
		if (sd == -1) return -1;
		if (connect(sd, aip->ai_addr, aip->ai_addrlen) == -1) {
			close(sd);
			continue;
		} else {
			*ss_family=aip->ai_family;
			freeaddrinfo(ail);
			return sd;
		}
	}

	freeaddrinfo(ail);
	return -1;
}

/* 
 * All incoming and outgoing packets go through this function.
 * Caller should make sure the fd is ready to read.
 */
struct qa_packet *
recv_packet(int fd)
{
	char buf[QARSH_MAX_PACKET_SIZE];
	uint32_t packetsize;
	void *psbuf = &packetsize;
	int bufused = 0;
	int ret = 0;

	do {
		if ((ret = read(fd, (char *)psbuf+bufused, sizeof packetsize - bufused)) < 0) {
			lprintf(0, "Read error while reading packet size: %s\n", strerror(errno));
			return NULL;
		} else if (ret == 0) {
			return NULL;
		}
		bufused += ret;
	} while (bufused < sizeof packetsize);

	packetsize = ntohl(packetsize);
	if (packetsize > QARSH_MAX_PACKET_SIZE) {
		lprintf(0, "Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
		return NULL;
	}
	/* Keep reading until we get the whole packet and nothing but the packet, so help me socket */
	bufused = 0;
	do {
		if ((ret = read(fd, buf+bufused, packetsize-bufused)) < 0) {
			lprintf(0, "Read error while reading packet data: %s\n", strerror(errno));
			return NULL;
		}
		bufused += ret;
	} while (bufused < packetsize);

	return parse_packet(buf, packetsize);
}

int
send_packet(int fd, struct qa_packet *qp)
{
	char buf[QARSH_MAX_PACKET_SIZE];
	uint32_t netsize;
	int len;
	ssize_t ret = -1;
	struct iovec iovs[2];

	qp->qp_seq = packet_seq++;

	len = qptostr(qp, buf, QARSH_MAX_PACKET_SIZE - sizeof netsize);

	if (len > 0) {
		netsize = htonl(len);
		iovs[0].iov_base = &netsize;
		iovs[0].iov_len = sizeof netsize;
		iovs[1].iov_base = buf;
		iovs[1].iov_len = len;

		ret = writev(fd, iovs, 2);
	}
	return ret;
}