1840faa40SJérôme Duval /*
2840faa40SJérôme Duval * Copyright 2023, Haiku Inc. All rights reserved.
3840faa40SJérôme Duval * Distributed under the terms of the MIT License.
4840faa40SJérôme Duval */
5840faa40SJérôme Duval
6840faa40SJérôme Duval
7840faa40SJérôme Duval //#include <OS.h>
8840faa40SJérôme Duval #include <sys/socket.h>
9840faa40SJérôme Duval
10840faa40SJérôme Duval #include "strace.h"
11840faa40SJérôme Duval #include "Syscall.h"
12840faa40SJérôme Duval #include "TypeHandler.h"
13840faa40SJérôme Duval
14840faa40SJérôme Duval
15840faa40SJérôme Duval struct enum_info {
16840faa40SJérôme Duval unsigned int index;
17840faa40SJérôme Duval const char *name;
18840faa40SJérôme Duval };
19840faa40SJérôme Duval
20840faa40SJérôme Duval #define ENUM_INFO_ENTRY(name) \
21840faa40SJérôme Duval { name, #name }
22840faa40SJérôme Duval
23840faa40SJérôme Duval
24840faa40SJérôme Duval #define FLAG_INFO_ENTRY(name) \
25840faa40SJérôme Duval { name, #name }
26840faa40SJérôme Duval
27840faa40SJérôme Duval
28840faa40SJérôme Duval static const FlagsTypeHandler::FlagInfo kRecvFlagInfos[] = {
29840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_PEEK),
30840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_OOB),
31840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_DONTROUTE),
32840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_EOR),
33840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_TRUNC),
34840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_CTRUNC),
35840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_WAITALL),
36840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_DONTWAIT),
37840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_BCAST),
38840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_MCAST),
39840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_EOF),
40840faa40SJérôme Duval FLAG_INFO_ENTRY(MSG_NOSIGNAL),
41840faa40SJérôme Duval { 0, NULL }
42840faa40SJérôme Duval };
43840faa40SJérôme Duval
44840faa40SJérôme Duval
45840faa40SJérôme Duval static const enum_info kSocketFamily[] = {
46840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_UNSPEC),
47840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_INET),
48840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_APPLETALK),
49840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_ROUTE),
50840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_LINK),
51840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_INET6),
52840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_DLI),
53840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_IPX),
54840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_NOTIFY),
55840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_UNIX),
56840faa40SJérôme Duval ENUM_INFO_ENTRY(AF_BLUETOOTH),
57840faa40SJérôme Duval
58840faa40SJérôme Duval { 0, NULL }
59840faa40SJérôme Duval };
60840faa40SJérôme Duval
61840faa40SJérôme Duval
62840faa40SJérôme Duval static const enum_info kSocketType[] = {
63840faa40SJérôme Duval ENUM_INFO_ENTRY(SOCK_STREAM),
64840faa40SJérôme Duval ENUM_INFO_ENTRY(SOCK_DGRAM),
65840faa40SJérôme Duval ENUM_INFO_ENTRY(SOCK_RAW),
66840faa40SJérôme Duval ENUM_INFO_ENTRY(SOCK_SEQPACKET),
67840faa40SJérôme Duval ENUM_INFO_ENTRY(SOCK_MISC),
68840faa40SJérôme Duval
69840faa40SJérôme Duval { 0, NULL }
70840faa40SJérôme Duval };
71840faa40SJérôme Duval
72840faa40SJérôme Duval
73840faa40SJérôme Duval static const enum_info kShutdownHow[] = {
74840faa40SJérôme Duval ENUM_INFO_ENTRY(SHUT_RD),
75840faa40SJérôme Duval ENUM_INFO_ENTRY(SHUT_WR),
76840faa40SJérôme Duval ENUM_INFO_ENTRY(SHUT_RDWR),
77840faa40SJérôme Duval
78840faa40SJérôme Duval { 0, NULL }
79840faa40SJérôme Duval };
80840faa40SJérôme Duval
81840faa40SJérôme Duval
82*6beff0d1SJérôme Duval static const FlagsTypeHandler::FlagInfo kSocketFlagInfos[] = {
83*6beff0d1SJérôme Duval FLAG_INFO_ENTRY(SOCK_NONBLOCK),
84*6beff0d1SJérôme Duval FLAG_INFO_ENTRY(SOCK_CLOEXEC),
85*6beff0d1SJérôme Duval
86*6beff0d1SJérôme Duval { 0, NULL }
87*6beff0d1SJérôme Duval };
88*6beff0d1SJérôme Duval
89*6beff0d1SJérôme Duval
90840faa40SJérôme Duval static FlagsTypeHandler::FlagsList kRecvFlags;
91840faa40SJérôme Duval static EnumTypeHandler::EnumMap kSocketFamilyMap;
92840faa40SJérôme Duval static EnumTypeHandler::EnumMap kSocketTypeMap;
93840faa40SJérôme Duval static EnumTypeHandler::EnumMap kShutdownHowMap;
94*6beff0d1SJérôme Duval static FlagsTypeHandler::FlagsList kSocketFlags;
95840faa40SJérôme Duval
96840faa40SJérôme Duval
97840faa40SJérôme Duval void
patch_network()98840faa40SJérôme Duval patch_network()
99840faa40SJérôme Duval {
100840faa40SJérôme Duval for (int i = 0; kRecvFlagInfos[i].name != NULL; i++) {
101840faa40SJérôme Duval kRecvFlags.push_back(kRecvFlagInfos[i]);
102840faa40SJérôme Duval }
103840faa40SJérôme Duval for (int i = 0; kSocketFamily[i].name != NULL; i++) {
104840faa40SJérôme Duval kSocketFamilyMap[kSocketFamily[i].index] = kSocketFamily[i].name;
105840faa40SJérôme Duval }
106840faa40SJérôme Duval for (int i = 0; kSocketType[i].name != NULL; i++) {
107840faa40SJérôme Duval kSocketTypeMap[kSocketType[i].index] = kSocketType[i].name;
108840faa40SJérôme Duval }
109840faa40SJérôme Duval for (int i = 0; kShutdownHow[i].name != NULL; i++) {
110840faa40SJérôme Duval kShutdownHowMap[kShutdownHow[i].index] = kShutdownHow[i].name;
111840faa40SJérôme Duval }
112*6beff0d1SJérôme Duval for (int i = 0; kSocketFlagInfos[i].name != NULL; i++) {
113*6beff0d1SJérôme Duval kSocketFlags.push_back(kSocketFlagInfos[i]);
114*6beff0d1SJérôme Duval }
115840faa40SJérôme Duval
116840faa40SJérôme Duval Syscall *recv = get_syscall("_kern_recv");
117840faa40SJérôme Duval recv->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
118840faa40SJérôme Duval Syscall *recvfrom = get_syscall("_kern_recvfrom");
119840faa40SJérôme Duval recvfrom->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
120840faa40SJérôme Duval Syscall *recvmsg = get_syscall("_kern_recvmsg");
121840faa40SJérôme Duval recvmsg->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
122840faa40SJérôme Duval Syscall *send = get_syscall("_kern_send");
123840faa40SJérôme Duval send->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
124840faa40SJérôme Duval Syscall *sendmsg = get_syscall("_kern_sendmsg");
125840faa40SJérôme Duval sendmsg->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
126840faa40SJérôme Duval Syscall *sendto = get_syscall("_kern_sendto");
127840faa40SJérôme Duval sendto->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kRecvFlags));
128840faa40SJérôme Duval
129840faa40SJérôme Duval Syscall *socket = get_syscall("_kern_socket");
130840faa40SJérôme Duval socket->GetParameter("family")->SetHandler(
131840faa40SJérôme Duval new EnumTypeHandler(kSocketFamilyMap));
132840faa40SJérôme Duval socket->GetParameter("type")->SetHandler(
133*6beff0d1SJérôme Duval new EnumFlagsTypeHandler(kSocketTypeMap, kSocketFlags));
134840faa40SJérôme Duval
135840faa40SJérôme Duval Syscall *shutdown = get_syscall("_kern_shutdown_socket");
136840faa40SJérôme Duval shutdown->GetParameter("how")->SetHandler(
137840faa40SJérôme Duval new EnumTypeHandler(kShutdownHowMap));
138840faa40SJérôme Duval
139840faa40SJérôme Duval Syscall *socketPair = get_syscall("_kern_socketpair");
140840faa40SJérôme Duval socketPair->ParameterAt(3)->SetOut(true);
141840faa40SJérôme Duval socketPair->ParameterAt(3)->SetCount(2);
142840faa40SJérôme Duval socketPair->GetParameter("family")->SetHandler(
143840faa40SJérôme Duval new EnumTypeHandler(kSocketFamilyMap));
144840faa40SJérôme Duval socketPair->GetParameter("type")->SetHandler(
145*6beff0d1SJérôme Duval new EnumFlagsTypeHandler(kSocketTypeMap, kSocketFlags));
146*6beff0d1SJérôme Duval
147*6beff0d1SJérôme Duval Syscall *accept = get_syscall("_kern_accept");
148*6beff0d1SJérôme Duval accept->GetParameter("flags")->SetHandler(new FlagsTypeHandler(kSocketFlags));
149840faa40SJérôme Duval }
150