Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit c2a2fcb

Browse files
committed
SocketLib -> NativeSocket, connectTcp -> create_connection
1 parent cc57bb8 commit c2a2fcb

6 files changed

Lines changed: 91 additions & 22 deletions

File tree

Shrine/mkdist.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ put /Tmp/SnailNet/Http.HC.Z SnailNet/Http.HC
7676
put /Tmp/SnailNet/Icmp.HC.Z SnailNet/Icmp.HC
7777
put /Tmp/SnailNet/IPv4.HC.Z SnailNet/IPv4.HC
7878
put /Tmp/SnailNet/MakeSnailNet.HC.Z SnailNet/MakeSnailNet.HC
79+
put /Tmp/SnailNet/NativeSocket.HC.Z SnailNet/NativeSocket.HC
7980
put /Tmp/SnailNet/Netcfg.HC.Z SnailNet/Netcfg.HC
8081
put /Tmp/SnailNet/NetFifo.HC.Z SnailNet/NetFifo.HC
8182
put /Tmp/SnailNet/NetHandlerTask.HC.Z SnailNet/NetHandlerTask.HC
8283
put /Tmp/SnailNet/SnailLib.HC.Z SnailNet/SnailLib.HC
8384
put /Tmp/SnailNet/Socket.HC.Z SnailNet/Socket.HC
84-
put /Tmp/SnailNet/SocketLib.HC.Z SnailNet/SocketLib.HC
8585
put /Tmp/SnailNet/Tcp.HC.Z SnailNet/Tcp.HC
8686
put /Tmp/SnailNet/Udp.HC.Z SnailNet/Udp.HC
8787
put /Tmp/SnailNet/Url.HC.Z SnailNet/Url.HC

SnailNet/Http.HC

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ I64 HttpOpenGet(U8* host, U16 port = 0, U8* path, I64* len_out) {
1212
U8 line[256];
1313
I64 error = 0;
1414

15-
I64 sock = socket(AF_INET, SOCK_STREAM);
16-
1715
if (!port)
1816
port = 80;
1917

@@ -22,9 +20,9 @@ I64 HttpOpenGet(U8* host, U16 port = 0, U8* path, I64* len_out) {
2220
path = "/";
2321

2422
//"Connect(%s:%d)\n", host, port;
25-
error = connectTcp(sock, host, port);
26-
//"connect: %d\n", error;
27-
if (error == 0) {
23+
I64 sock = create_connection(host, port);
24+
//"create_connection: %d\n", sock;
25+
if (sock >= 0) {
2826
StrPrint(line, "GET %s HTTP/1.1\r\n", path);
2927
sendString(sock, line, 0);
3028
StrPrint(line, "Host: %s\r\n", host);

SnailNet/MakeSnailNet.HC

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
StreamPrint("#include \"::/Adam/HwSupp/%s\"", native_driver);
1919

2020
// Contains a lot of common definitions, probably should be cleaned up
21-
StreamPrint("#include \"::/Adam/Net/SocketLib\"");
21+
StreamPrint("#include \"::/Adam/Net/NativeSocket\"");
2222

2323
// L2
2424
StreamPrint("#include \"::/Adam/Net/Ethernet\"");
Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// vim: set ft=c:
22

3-
#define SOCK_STREAM 1
4-
#define SOCK_DGRAM 2
5-
#define SOCK_RAW 3
3+
#define SOCK_STREAM 1
4+
#define SOCK_DGRAM 2
5+
#define SOCK_RAW 3
66

7-
#define AF_UNSPEC 0
8-
#define AF_INET 2
9-
#define AF_INET6 10
7+
#define AF_UNSPEC 0
8+
#define AF_INET 2
9+
#define AF_INET6 10
1010

11-
#define INADDR_ANY 0
11+
#define INADDR_ANY 0
1212

13-
#define SOL_SOCKET 1
13+
#define SOL_SOCKET 1
1414

1515
// optval = I64*
16-
#define SO_RCVTIMEO_MS 1
16+
#define SO_RCVTIMEO_MS 1
1717

18-
#define AI_CACHED 0x8000
18+
#define AI_CACHED 0x8000
1919

2020
class in_addr {
2121
U32 s_addr;
@@ -159,8 +159,6 @@ I64 setsockopt(I64 sockfd, I64 level, I64 optname, U8* optval, I64 optlen) {
159159
else return -1;
160160
}
161161

162-
I64 connectTcp(I64, U8*, U16) { return -1; }
163-
I64 recvall(I64, U8*, I64, I64) { return -1; }
164162
I64 getaddrinfo(U8* node, U8* service, addrinfo* hints, addrinfo** res) {
165163
if (socket_addr_resolver) return socket_addr_resolver->getaddrinfo(node, service, hints, res);
166164
else return -1;
@@ -194,6 +192,67 @@ U8* gai_strerror(I64 errcode) {
194192
return "Unspecified error";
195193
}
196194

195+
// Inspired by https://docs.python.org/3.7/library/socket.html#socket.create_connection
196+
I64 create_connection(U8* hostname, U16 port) {
197+
sockaddr_in addr;
198+
addr.sin_family = AF_INET;
199+
addr.sin_port = htons(port);
200+
addr.sin_addr.s_addr = 0;
201+
202+
addrinfo* res;
203+
I64 error = getaddrinfo(hostname, NULL, NULL, &res);
204+
205+
if (error < 0) {
206+
"$FG,4$getaddrinfo: error %d\n$FG$", error;
207+
}
208+
else {
209+
addrinfo* curr = res;
210+
211+
while (curr) {
212+
if (curr->ai_family == AF_INET && (curr->ai_socktype == 0 || curr->ai_socktype == SOCK_STREAM)) {
213+
addr.sin_addr.s_addr = (curr->ai_addr(sockaddr_in*))->sin_addr.s_addr;
214+
freeaddrinfo(res);
215+
216+
I64 sockfd = socket(AF_INET, SOCK_STREAM);
217+
218+
if (sockfd < 0)
219+
return sockfd;
220+
221+
error = connect(sockfd, &addr, sizeof(addr));
222+
223+
if (error < 0) {
224+
close(sockfd);
225+
return error;
226+
}
227+
228+
return sockfd;
229+
}
230+
231+
curr = curr->ai_next;
232+
}
233+
234+
"$FG,4$create_connection: no suitable address\n$FG$";
235+
}
236+
237+
freeaddrinfo(res);
238+
return -1;
239+
}
240+
241+
I64 recvall(I64 sockfd, U8* buf, I64 len, I64 flags) {
242+
I64 total = 0;
243+
244+
while (len) {
245+
I64 got = recv(sockfd, buf + total, len, flags);
246+
247+
if (got < 0)
248+
break;
249+
250+
len -= got;
251+
total += got;
252+
}
253+
254+
return total;
255+
}
197256

198257
U0 RegisterSocketClass(U16 domain, U16 type, CSocket* (*socket)(U16 domain, U16 type)) {
199258
CSocketClass* cls = MAlloc(sizeof(CSocketClass));

SnailNet/SnailLib.HC

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,26 @@ I64 close(I64 sockfd) {
9999
return ReadI8();
100100
}
101101

102-
I64 connectTcp(I64 sockfd, U8* addr, U16 port) {
102+
I64 create_connection(U8* addr, U16 port) {
103+
I64 sockfd = socket(AF_INET, SOCK_STREAM);
104+
105+
if (sockfd < 0)
106+
return sockfd;
107+
103108
CommPutChar(SNAIL_COM, CMD_CONNECT_TCP);
104109
CommPutChar(SNAIL_COM, sockfd);
105110
CommPutChar(SNAIL_COM, StrLen(addr));
106111
CommPutS(SNAIL_COM, addr);
107112
CommPutChar(SNAIL_COM, port & 0xff);
108113
CommPutChar(SNAIL_COM, port >> 8);
109-
return ReadI8();
114+
115+
I64 error = ReadI8();
116+
if (error < 0) {
117+
close(sockfd);
118+
return error;
119+
}
120+
121+
return sockfd;
110122
}
111123

112124
I64 recvall(I64 sockfd, U8* buf, I64 len, I64 flags) {

SnailNet/SocketDummy.HC

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
I64 SocketInit() { throw; return -1; }
1212
I64 socket(I64, I64) { return -1; }
1313
I64 close(I64) { return -1; }
14-
I64 connectTcp(I64, U8*, U16) { return -1; }
14+
I64 create_connection(U8*, U16) { return -1; }
1515
I64 recvall(I64, U8*, I64, I64) { return -1; }
1616
I64 send(I64, U8*, I64, I64) { return -1; }
1717
I64 recvLine(I64, U8*, I64, I64) { return -1; }

0 commit comments

Comments
 (0)