aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/posix/generic/services.cpp
blob: 8ae065633a53f756f467f20b8745e64e91cb9414 (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
213
214
215
216
217
218
219
220
221
222
#include <mlibc/services.hpp>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <mlibc/debug.hpp>

namespace mlibc {

static int parse_rest(service_buf &buf, char *end, int proto) {
	if (!strncmp(end, "/udp", 4)) {
		if (proto == IPPROTO_TCP && proto != -1)
			return 0;
		buf.protocol = IPPROTO_UDP;
		buf.socktype = SOCK_DGRAM;
	} else if (!strncmp(end, "/tcp", 4)) {
		if (proto == IPPROTO_UDP && proto != -1)
			return 0;
		buf.protocol = IPPROTO_TCP;
		buf.socktype = SOCK_STREAM;
	} else {
		return 0;
	}

	//TODO(geert): also parse aliases.

	return 1;
}

static int lookup_serv_file_port(service_result &buf, int proto, int port) {
	auto file = fopen(_PATH_SERVICES, "r");
	if (!file) {
		switch (errno) {
			case ENOENT:
			case ENOTDIR:
			case EACCES:
				return -EAI_SERVICE;
			default:
				return -EAI_SYSTEM;
		}
	}

	char line_buf[129] = {0};
	char *line = line_buf + 1;
	while(fgets(line, 128, file)) {
		int name_length = 0;
		char *pos;
		// easy way to handle comments, just move the end of the line
		// to the beginning of the comment
		if ((pos = strchr(line, '#'))) {
			*pos++ = '\n';
			*pos = '\0';
		}

		char *end = NULL;
		for (pos = line; *pos; pos++) {
			for (; isalpha(*pos); pos++);
			int rport = strtoul(pos, &end, 10);
			if (rport != port || rport > 65535) {
				pos = end;
				continue;
			}

			// We have found the port, time to rewind to the start
			// of the line.
			for (; pos[-1]; pos--)
				if(!isspace(pos[-1]))
					name_length++;
			break;
		}

		if (!pos)
			continue;

		if (!name_length)
			continue;

		auto name = frg::string<MemoryAllocator>(pos, name_length,
				getAllocator());

		struct service_buf sbuf = {};
		sbuf.port = port;
		sbuf.name = std::move(name);
		if (!parse_rest(sbuf, end, proto))
			continue;
		buf.push_back(std::move(sbuf));
	}

	fclose(file);
	return buf.size();
}

static int lookup_serv_file_name(service_result &buf, const char *name,
		int proto) {
	auto file = fopen(_PATH_SERVICES, "r");
	if (!file) {
		switch (errno) {
			case ENOENT:
			case ENOTDIR:
			case EACCES:
				return -EAI_SERVICE;
			default:
				return -EAI_SYSTEM;
		}
	}

	char line[128];
	int name_length = strlen(name);
	while(fgets(line, 128, file)) {
		char *pos;
		// easy way to handle comments, just move the end of the line
		// to the beginning of the comment
		if ((pos = strchr(line, '#'))) {
			*pos++ = '\n';
			*pos = '\0';
		}

		for (pos = line; (pos = strstr(pos, name)); pos++) {
			// the name must start and end with a space
			if (pos > line && !isspace(pos[-1]))
				continue;
			if (pos[name_length] && !isspace(pos[name_length]))
				continue;
			break;
		}
		if (!pos)
			continue;

		// Skip the name at the beginning of the line.
		for(pos = line; *pos && !isspace(*pos); pos++)
			;

		char *end = NULL;
		int port = strtoul(pos, &end, 10);
		if (port > 65535 || end == pos)
			continue;

		struct service_buf sbuf;
		sbuf.port = port;
		sbuf.name = frg::string<MemoryAllocator>(name, getAllocator());
		if (!parse_rest(sbuf, end, proto))
			continue;

		buf.push_back(sbuf);

	}

	fclose(file);
	return buf.size();
}


// This function returns a negative error code, since a positive
// return code means success.
int lookup_serv_by_name(service_result &buf, const char *name, int proto,
		int socktype, int flags) {
	switch(socktype) {
		case SOCK_STREAM:
			if (!proto)
				proto = IPPROTO_TCP;
			else if (proto != IPPROTO_TCP)
				return -EAI_SERVICE;
			break;
		case SOCK_DGRAM:
			if (!proto)
				proto = IPPROTO_UDP;
			else if (proto != IPPROTO_UDP)
				return -EAI_SERVICE;
			break;
		case 0:
			break;
		default:
			if (name)
				return -EAI_SERVICE;
			buf[0].port = 0;
			buf[0].socktype = socktype;
			buf[0].protocol = proto;
			return 1;
	}

	char *end = nullptr;
	unsigned int port = 0;
	int count = 0;

	if (name) {
		if (!*name)
			return -EAI_SERVICE;
		port = strtoul(name, &end, 10);
	}
	// The end pointer is a null pointer so the name was a port
	// or the name was not specified.
	if (!end || !*end) {
		if (proto != IPPROTO_UDP) {
			buf[count].port = port;
			buf[count].protocol = IPPROTO_TCP;
			buf[count].socktype = SOCK_STREAM;
			count++;
		}
		if (proto != IPPROTO_TCP) {
			buf[count].port = port;
			buf[count].protocol = IPPROTO_UDP;
			buf[count].socktype = SOCK_DGRAM;
			count++;
		}
		return count;
	}

	if (flags & AI_NUMERICSERV)
		return -EAI_NONAME;

	return lookup_serv_file_name(buf, name, proto);
}

int lookup_serv_by_port(service_result &buf, int proto, int port) {
	return lookup_serv_file_port(buf, proto, port);
}

} // namespace mlibc