summaryrefslogtreecommitdiffstats
path: root/sockutil.c
blob: db22f67531921705211075c3b2ac0ac43ecf9ba1 (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
/*
 * Copyright © 2005,2006  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 <netinet/in.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"

/* Some generic socket related functions to make things easier */

int
getsockport(int sd)
{
	struct sockaddr_in addr;
	socklen_t addrlen;

	addrlen = sizeof addr;
	if (getsockname(sd, (struct sockaddr *)&addr, &addrlen) == 0) {
		return ntohs(addr.sin_port);
	} else {
		return -1;
	}
}

int
bind_any(int minport)
{
	int sd;
	struct sockaddr_in addr;
	
	sd = socket(AF_INET, SOCK_STREAM, 0);
	if (sd == -1) return -1;
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	do {
		addr.sin_port = htons(minport++);
	} while (bind(sd, (struct sockaddr *)&addr, sizeof addr) != 0);
	if (listen(sd, 0) == -1) {
		syslog(LOG_WARNING, "listen error %d, %s", errno,
				strerror(errno));
	}
	return sd;
}

int
connect_to_host(char *hostname, int port)
{
	struct hostent *h;
	struct sockaddr_in haddr;
	int sd;

	if ((h = gethostbyname(hostname)) == NULL) {
		return -1;
	}
	haddr.sin_family = h->h_addrtype;
	haddr.sin_port = htons(port);
	memcpy(&haddr.sin_addr, h->h_addr, h->h_length);

	sd = socket(PF_INET, SOCK_STREAM, 0);
	if (sd == -1) return -1;
	if (connect(sd, (struct sockaddr *)&haddr, sizeof haddr) == -1) {
		return -1;
	}
	return sd;
}

int
connect_to_peer(struct sockaddr_in *peer, int port)
{
	struct sockaddr_in in_peer;
	int sd;
	int fdflags;
       
	in_peer.sin_family = AF_INET;
	in_peer.sin_port = htons(port);
	in_peer.sin_addr = peer->sin_addr;

	sd = socket(PF_INET, SOCK_STREAM, 0);
	if (sd == -1) return -1;

	/* Set close-on-exec for these sds */
	if ((fdflags = fcntl(sd, F_GETFD, 0)) < 0) {
		return -1;
	}

	fdflags |= FD_CLOEXEC;
	if (fcntl(sd, F_SETFD, fdflags) < 0) {
		return -1;
	}

	if (connect(sd, (struct sockaddr *)&in_peer, sizeof in_peer) == -1) {
		return -1;
	}
	return sd;
}


struct qa_packet *
recv_packet(int fd)
{
	char *packetbuf;
	int packetsize;
	struct qa_packet *qp = NULL;

	packetbuf = malloc(QARSH_MAX_PACKET_SIZE);
	memset(packetbuf, 0, QARSH_MAX_PACKET_SIZE);

	packetsize = read(fd, packetbuf, QARSH_MAX_PACKET_SIZE);
	if (packetsize > 0) {
		qp = parse_packets(packetbuf, packetsize);
	}
	free(packetbuf);
	return qp;
}

int
send_packet(int fd, struct qa_packet *qp)
{
	char *packetbuf;
	int packetsize;
	ssize_t ret;
	
	packetbuf = malloc(QARSH_MAX_PACKET_SIZE);
	memset(packetbuf, 0, QARSH_MAX_PACKET_SIZE);
	packetbuf = qptostr(qp, &packetbuf, &packetsize);

	ret = write(fd, packetbuf, packetsize);
	free(packetbuf);
	return ret;
}