aboutsummaryrefslogtreecommitdiff
path: root/plaintext_network.c
blob: 9ba899987a6074c9e52940931b86aae9b7b1ca21 (plain) (blame)
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
// One of the code files for HaxServ
//
// Written by: Test_User <hax@andrewyu.org>
//
// This is free and unencumbered software released into the public
// domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <arpa/inet.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include "config.h"
#include "general_network.h"
#include "haxstring.h"
#include "plaintext_network.h"

int init_plaintext_network(void) {
	return 0;
}

int plaintext_send(void *fd, struct string msg) {
	while (msg.len > 0) {
		ssize_t res;
		do {
			res = send(*((int*)fd), msg.data, msg.len, 0);
		} while (res == -1 && (errno == EINTR));

		if (res < 0 || (size_t)res > msg.len) { // res > len shouldn't be possible, but is still an error
			return 1;
		} else if (res > 0) {
			msg.len -= (size_t)res;
			msg.data += (size_t)res;
		}
	}

	return 0;
}

size_t plaintext_recv(void *fd, char *data, size_t len, char *err) {
	ssize_t res;
	do {
		res = recv(*((int*)fd), data, len, 0);
	} while (res == -1 && (errno == EINTR));

	if (res == -1) {
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			*err = 1;
		} else {
			*err = 3;
		}
		return 0;
	} else if (res == 0) {
		*err = 2;
		return 0;
	}
	*err = 0;

	return (size_t)res;
}

int plaintext_connect(void **handle, struct string address, struct string port, struct string *addr_out) {
	struct sockaddr sockaddr;
	if (resolve(address, port, &sockaddr) != 0)
		return -1;

	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (fd == -1)
		return -1;

	{
		struct timeval timeout = {
			.tv_sec = PING_INTERVAL,
			.tv_usec = 0,
		};

		setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
	}

	int res;
	do {
		res = connect(fd, &sockaddr, sizeof(sockaddr));
	} while (res < 0 && errno == EINTR);
	if (res < 0) {
		close(fd);
		return -1;
	}

	*handle = malloc(sizeof(fd));
	if (!handle) {
		close(fd);
		return -1;
	}
	*((int*)*handle) = fd;

	addr_out->data = malloc(sizeof(sockaddr));
	if (!addr_out->data) {
		free(handle);
		close(fd);
		return -1;
	}
	memcpy(addr_out->data, &sockaddr, sizeof(sockaddr));
	addr_out->len = sizeof(sockaddr);

	return fd;
}

int plaintext_accept(int listen_fd, void **handle, struct string *addr) {
	struct sockaddr address;
	socklen_t address_len = sizeof(address);

	int con_fd;
	do {
		con_fd = accept(listen_fd, &address, &address_len);
	} while (con_fd == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK || errno == ENETDOWN || errno == EPROTO || errno == ENOPROTOOPT || errno == EHOSTDOWN || errno == ENONET || errno == EHOSTUNREACH || errno == EOPNOTSUPP || errno == ENETUNREACH));

	if (con_fd == -1)
		return -1;

	addr->data = malloc(address_len);
	if (addr->data == 0 && address_len != 0) {
		close(con_fd);
		return -1;
	}

	memcpy(addr->data, &address, address_len);
	addr->len = address_len;

	*handle = malloc(sizeof(con_fd));
	if (!handle) {
		free(addr->data);
		close(con_fd);
		return -1;
	}
	*((int*)*handle) = con_fd;

	return con_fd;
}

void plaintext_shutdown(void *handle) {
	shutdown(*((int*)handle), SHUT_RDWR);
}

void plaintext_close(int fd, void *handle) {
	free(handle);
	close(fd);
}