summaryrefslogtreecommitdiff
path: root/util.c
blob: be62ac1617de5f14ab1af9d838247c6f5e7edd7a (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
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <errno.h>

#include <poll.h>

#include "util.h"

uint32_t
sock_get_ipv4_addr(int fd)
{
	struct sockaddr_storage storage;
	struct sockaddr_in *own = (struct sockaddr_in *) &storage;
	socklen_t addrlen = sizeof(struct sockaddr_storage);

	assert(getsockname(fd, (struct sockaddr *) &storage, &addrlen) == 0);
	
	return own->sin_addr.s_addr;
}

uint32_t
sock_get_netmask(int fd)
{
	struct ifreq ifreq;

	memset(&ifreq, 0, sizeof ifreq);

	/* FIXME: do not hardcode interface name */
	strcpy(ifreq.ifr_name, "wlp3s0");

	if (ioctl(fd, SIOCGIFNETMASK, &ifreq) < 0) {
		fprintf(stderr, "get netmask failed: %s\n", strerror(errno));
		return 0;
	}

	return ((struct sockaddr_in *) &ifreq.ifr_netmask)->sin_addr.s_addr;
}

uint32_t
sock_get_peer_ipv4_addr(int fd)
{
	struct sockaddr_storage storage;
	struct sockaddr_in *own = (struct sockaddr_in *) &storage;
	socklen_t addrlen = sizeof(struct sockaddr_storage);

	assert(getpeername(fd, (struct sockaddr *) &storage, &addrlen) == 0);
	
	return own->sin_addr.s_addr;
}

#if 0
uint32_t
sock_get_peer_mac_addr(int fd)
{

}
#endif

void
set_ip(uint8_t *ipbuf, uint32_t ip)
{
	ipbuf[0] = (ip & 0xFF);
	ipbuf[1] = (ip & 0xFF00) >> 8;
	ipbuf[2] = (ip & 0xFF0000) >> 16;
	ipbuf[3] = (ip & 0xFF000000) >> 24;
}


int
bind_socket(int socktype, const char *host, const char *port)
{
	struct addrinfo hints, *result, *rp;
	int reuseaddr = 1, s;
	int fd;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET;
	hints.ai_socktype = socktype;

	printf("bind to host: %s, port: %s\n", host, port);

	s = getaddrinfo(host, port, &hints, &result);
	if (s != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
		return -1;
	}
	for (rp = result; rp != NULL; rp = rp->ai_next) {
		fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (fd == -1)
			continue;
		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
			       &reuseaddr, sizeof(reuseaddr)) == -1)
			continue;

		/*ip = ((struct sockaddr_in *)rp->ai_addr)->sin_addr.s_addr;*/
		if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0)
			break;

		close(fd);
	}
	freeaddrinfo(result);
	if (rp == NULL) {
		fprintf(stderr, "Failed to bind: %s\n", strerror(errno));
		return -1;
	}

	if (socktype == SOCK_STREAM &&
	    listen(fd, 1) < 0) {
		fprintf(stderr, "Failed to listen: %s\n", strerror(errno));
		close(fd);
		return -1;
	}

	return fd;
}

int
connect_to_host(int socktype, const char *host, const char *port)
{
	struct addrinfo hints, *result, *rp;
	int fd, s;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET;
	hints.ai_socktype = socktype;

	printf("connect to host: %s, port: %s\n", host, port);

	s = getaddrinfo(host, port, &hints, &result);
	if (s != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
		return -1;
	}
	for (rp = result; rp != NULL; 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)
			break;

		close(fd);
	}
	freeaddrinfo(result);
	if (rp == NULL) {
		fprintf(stderr, "Failed to bind: %s\n", strerror(errno));
		return -1;
	}

	return fd;
}

/* From systemd:src/shared/util.c */
ssize_t
loop_read(int fd, void *buf, size_t nbytes, uint8_t do_poll)
{
        uint8_t *p;
        ssize_t n = 0;

        assert(fd >= 0);
        assert(buf);

        p = buf;

        while (nbytes > 0) {
                ssize_t k;

                if ((k = read(fd, p, nbytes)) <= 0) {

                        if (k < 0 && errno == EINTR)
                                continue;

                        if (k < 0 && errno == EAGAIN && do_poll) {
                                struct pollfd pollfd;

                                memset(&pollfd, 0, sizeof pollfd);
                                pollfd.fd = fd;
                                pollfd.events = POLLIN;

                                if (poll(&pollfd, 1, -1) < 0) {
                                        if (errno == EINTR)
                                                continue;

                                        return n > 0 ? n : -errno;
                                }

                                if (pollfd.revents != POLLIN)
                                        return n > 0 ? n : -EIO;

                                continue;
                        }

                        return n > 0 ? n : (k < 0 ? -errno : 0);
                }

                p += k;
                nbytes -= k;
                n += k;
        }

        return n;
}