summaryrefslogtreecommitdiff
path: root/src/pso/TTcpSocket.cpp
blob: 7244ad6fedc196df9962903c0ae1c64ea7b220b8 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <global_types.h>
#include <stdio.h>
#include <string.h>
#include "pso/macros.h"
#include "pso/TArray.h"
#include "pso/TMainTask.h"
#include "pso/TObject.h"
#include "pso/TSocket.h"
#include "pso/TTcpSocket.h"

OBJECT_NAME(TTcpSocket);
TTcpSocket *tcp_socket_table[16] = {nullptr};

struct at_ip4_opt {
	u8 ttl;
	u8 svctype;
	u8 df_flag;
};

struct at_ip6_opt {
	u8 traffic_class;
	u32 flow_label;
	u8 hop_limit;
};

struct at_ip_option {
	u32 type;
	union {
		struct at_ip6_opt ip6;
		struct at_ip4_opt ip4;
	} ip46;
};

struct at_ip_addr {
	u32 type;
	union {
		u8 ip6[16];
		u32 ip4;
	} ip46;
};

struct send_buffs {
	short len;
	u8 *buff;
};

void func_80019aa0();
void controller_stuff();
char func_801a5d1c();
void render_tick();
void func_803d96a4();
short tcp_abort(short nh);
short tcp_bind(short nh, struct at_ip_addr *addr, u16 port);
short tcp_connect(short nh, struct at_ip_addr *addr, u16 port, struct at_ip_option *option);
short tcp_create();
short tcp_delete(short nh);
short tcp_get_opt(short nh, short type, u32 *opt);
short tcp_send(short nh, void (*notify)(short size, short sock_fd), char bufnum, struct send_buffs *sb);
short tcp_stat(short nh, short *stat, short *backlog, u32 *sendwin, u32 *recvwin);
short tcp_receive(short nh, void (*notify)(short size, short sock_fd), short len, u8 *buf);
int get_link_status();
char *get_sock_status_name(short code);


WEAK_FUNC TTcpSocket::TTcpSocket(TObject *parent) : TSocket(parent) {
	set_name(TTcpSocket_name);
}

TTcpSocket::~TTcpSocket() {
	close();
}

void TTcpSocket::recv() {
	if (sock_fd() != -1) {
		packet_buffer().fill(0);
		set_buffer_cleared(true);
		(int)tcp_receive(sock_fd(), notify, packet_buffer().size(), packet_buffer().data());
	}
}

void TTcpSocket::notify(short size, short sock_fd) {
	char tmp_str[64];
	TTcpSocket *socket = tcp_socket_table[sock_fd];

	if (socket != nullptr) {
		if (size > 0) {
			socket->set_buffer_cleared(false);
			socket->set_size(size);
			socket->set_buffer_offset(0);
			sprintf(tmp_str, "Rcv:%d byte", size);
			socket->log(tmp_str);
			if (socket->callback() != nullptr) {
				socket->callback()(socket);
			}
		} else {
			socket->set_flags(1);
		}
	}
}

int TTcpSocket::send(u8 *data, size_t size) {
	if (sock_fd() != -1) {
		if (!m_is_encrypted) {
			m_send_crypt.encrypt(data, size);
		}

		const s8 flags = sock_flags();
		if (flags || !get_link_status()) {
			return -1;
		} else {
			if (int status = stat()) {
				log(get_sock_status_name(status));
				set_flags(1);
				return 1;
			} else {
				if (send_window() <= size) {
					set_flags(1);
					return 1;
				} else {
					if (stat_val() < 0) {
						set_flags(1);
						return 1;
					} else {
						struct send_buffs sb;
						sb.buff = data;
						sb.len = size;
						set_unused(0);
						int ret = tcp_send(sock_fd(), nullptr, 1, &sb);
						if (ret >= 1 || ret < -1) {
							set_flags(1);
							log(get_sock_status_name(ret));
							close();
							return ret;
						}
					}
				}
			}
		}
	} else {
		return -1;
	}
}

short TTcpSocket::send(u8 *data) {
	if (sock_fd() != -1) {
		const s8 flags = sock_flags();
		if (flags || !get_link_status()) {
			return -1;
		} else {
			if (short status = stat()) {
				log(get_sock_status_name(status));
				set_flags(1);
				return 1;
			} else {
				if (stat_val() < 0) {
					set_flags(1);
					return 1;
				} else {
					s8 len = strlen(reinterpret_cast<const char *>(data));
					struct send_buffs sb;
					sb.buff = data;
					sb.len = len;
					if (send_window() <= len) {
						set_flags(1);
						return 1;
					} else {
						short ret = tcp_send(sock_fd(), nullptr, 1, &sb);
						if (ret) {
							close();
						}
						return ret;
					}
				}
			}
		}
	} else {
		return -1;
	}
}

int TTcpSocket::test_connection() {
	for (int i = 0; i < 1800; ++i) {
		if (int status = stat()) {
			log(get_sock_status_name(status));
			set_flags(1);
			return 1;
		} else if (send_window() <= 128) {
			set_flags(1);
			return 1;
		} else if (stat_val() < 0) {
			set_flags(1);
			return 1;
		} else if (!get_link_status()) {
			set_flags(1);
			return 1;
		} else if (send_window() > 4095) {
			return 0;
		} else {
			main_task.some_empty_func();
			main_task.run_tl_camera_tasks();
			main_task.render();
			main_task.render_screen_overlay();
			func_80019aa0();
			render_tick();
		}
	}

	set_flags(1);
	return 1;
}

void TTcpSocket::some_stub() {}

short TTcpSocket::stat() {
	if (sock_fd() != -1) {
		return tcp_stat(sock_fd(), &stat_val(), nullptr, &send_window(), &recv_window());
	} else {
		return 0;
	}
}

short TTcpSocket::close() {
	if (sock_fd() != -1) {
		(short)tcp_abort(sock_fd());
		(short)tcp_delete(sock_fd());
		tcp_socket_table[sock_fd()] = nullptr;
		set_sock_fd(-1);
		m_is_encrypted = 0;
		set_size(0);
		set_buffer_offset(0);
		set_buffer_cleared(true);
	}
	return sock_fd();
}

short TTcpSocket::open() {
	set_sock_fd(-1);
	m_is_encrypted = 0;
	set_size(0);
	set_buffer_offset(0);
	set_buffer_cleared(true);
	set_unused(0);
	set_sock_fd(tcp_create());

	if (sock_fd() < 0) {
		log(get_sock_status_name(sock_fd()));
		return -1;
	} else {
		struct at_ip_option connect_option;
		struct at_ip_addr connect_addr;
		struct at_ip_addr bind_addr;

		u32 opt = 1;

		tcp_get_opt(sock_fd(), 0x2001, &opt);

		bind_addr.type = 4;
		bind_addr.ip46.ip4 = 0;

		tcp_bind(sock_fd(), &bind_addr, src_port());

		connect_addr.type = 4;
		connect_addr.ip46.ip4 = dst_addr().addr;

		connect_option.type = 4;
		connect_option.ip46.ip4.ttl = 120;
		connect_option.ip46.ip4.svctype = 0;
		connect_option.ip46.ip4.df_flag = 0;

		if (tcp_connect(sock_fd(), &connect_addr, dst_port(), &connect_option)) {
			log(get_sock_status_name(sock_fd()));
			return -1;
		} else {
			for (;;) {
				func_803d96a4();
				controller_stuff();
				if (short status = stat()) {
					log(get_sock_status_name(status));
					return -2;
				} else if (stat_val() < 0) {
					return -2;
				} else {
					some_stub();
					if (stat_val() < 4) {
						if (!get_link_status()) {
							return -3;
						} else if (func_801a5d1c()) {
							return -2;
						} else {
							main_task.some_empty_func();
							main_task.run_tl_camera_tasks();
							main_task.render();
							main_task.render_screen_overlay();
							func_80019aa0();
							render_tick();
						}
					} else {
						tcp_socket_table[sock_fd()] = this;
						return sock_fd();
					}
				}
			}
		}
	}
}

void func_80019aa0() {}
void controller_stuff() {}
char func_801a5d1c() {}
void render_tick() {}
void func_803d96a4() {}
short tcp_abort(short nh) {}
short tcp_bind(short nh, struct at_ip_addr *addr, u16 port) {}
short tcp_connect(short nh, struct at_ip_addr *addr, u16 port, struct at_ip_option *option) {}
short tcp_create() {}
short tcp_delete(short nh) {}
short tcp_get_opt(short nh, short type, u32 *opt) {}
short tcp_send(short nh, void (*notify)(short size, short sock_fd), char bufnum, struct send_buffs *sb) {}
short tcp_stat(short nh, short *stat, short *backlog, u32 *sendwin, u32 *recvwin) {}
short tcp_receive(short nh, void (*notify)(short size, short sock_fd), short len, u8 *buf) {}
int get_link_status() {}
char *get_sock_status_name(short code) {}