Skip to content

Commit 689b85a

Browse files
committed
Emulated implementation of dgram and stream sockets
1 parent 955e19c commit 689b85a

File tree

7 files changed

+1350
-157
lines changed

7 files changed

+1350
-157
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <netdb.h>
5757
#include <netinet/in.h>
5858
#include <signal.h>
59+
#include <stddef.h>
5960
#include <stdio.h>
6061
#include <stdint.h>
6162
#include <stdlib.h>
@@ -640,7 +641,7 @@ int32_t call_recvfrom(int32_t sockfd, void *buf, int32_t offset, int32_t len, in
640641
if (res != -1) {
641642
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
642643
len_and_family[0] = l; // ...so this unsigned->signed conversion is well defined
643-
len_and_family[1] = sa.ss_family;
644+
len_and_family[1] = l < offsetof(struct sockaddr_storage, ss_family) + sizeof(sa.ss_family) ? AF_UNSPEC : sa.ss_family;
644645
memcpy(src_addr, &sa, l);
645646
}
646647
return res;

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/SocketTests.java

Lines changed: 525 additions & 106 deletions
Large diffs are not rendered by default.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/OSErrorEnum.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242

4343
import java.io.FileNotFoundException;
4444
import java.io.IOException;
45+
import java.nio.channels.AlreadyConnectedException;
4546
import java.nio.channels.ClosedChannelException;
4647
import java.nio.channels.NonReadableChannelException;
4748
import java.nio.channels.NonWritableChannelException;
49+
import java.nio.channels.NotYetConnectedException;
4850
import java.nio.file.AccessDeniedException;
4951
import java.nio.file.DirectoryNotEmptyException;
5052
import java.nio.file.FileAlreadyExistsException;
@@ -165,13 +167,13 @@ public enum OSErrorEnum {
165167
ESTRPIPE(86, "Streams pipe error"),
166168
EUSERS(87, "Too many users"),
167169
ENOTSOCK(88, "Socket operation on non-socket"),
168-
EDESTADDRREQ(89, "Destination address required"),
170+
EDESTADDRREQ(platformSpecific(89, 39), "Destination address required"),
169171
EMSGSIZE(90, "Message too long"),
170172
EPROTOTYPE(91, "Protocol wrong type for socket"),
171173
ENOPROTOOPT(92, "Protocol not available"),
172174
EPROTONOSUPPORT(93, "Protocol not supported"),
173175
ESOCKTNOSUPPORT(94, "Socket type not supported"),
174-
EOPNOTSUPP(95, "Operation not supported on transport endpoint"),
176+
EOPNOTSUPP(platformSpecific(95, 102), "Operation not supported on transport endpoint"),
175177
EPFNOSUPPORT(96, "Protocol family not supported"),
176178
EAFNOSUPPORT(platformSpecific(97, 47), "Address family not supported by protocol"),
177179
EADDRINUSE(98, "Address already in use"),
@@ -182,7 +184,7 @@ public enum OSErrorEnum {
182184
ECONNABORTED(103, "Software caused connection abort"),
183185
ECONNRESET(104, "Connection reset by peer"),
184186
ENOBUFS(105, "No buffer space available"),
185-
EISCONN(106, "Transport endpoint is already connected"),
187+
EISCONN(platformSpecific(106, 56), "Transport endpoint is already connected"),
186188
ENOTCONN(platformSpecific(107, 57), "Transport endpoint is not connected"),
187189
ESHUTDOWN(108, "Cannot send after transport endpoint shutdown"),
188190
ETOOMANYREFS(109, "Too many references: cannot splice"),
@@ -309,6 +311,13 @@ public static ErrorAndMessagePair fromException(Exception e) {
309311
return new ErrorAndMessagePair(OSErrorEnum.EOPNOTSUPP, OSErrorEnum.EOPNOTSUPP.getMessage());
310312
} else if (e instanceof NonReadableChannelException || e instanceof NonWritableChannelException) {
311313
return new ErrorAndMessagePair(OSErrorEnum.EBADF, OSErrorEnum.EBADF.getMessage());
314+
} else if (e instanceof OperationWouldBlockException) {
315+
return new ErrorAndMessagePair(OSErrorEnum.EWOULDBLOCK, OSErrorEnum.EWOULDBLOCK.getMessage());
316+
} else if (e instanceof NotYetConnectedException) {
317+
// TODO for UDP send without connect it should probably be EDESTADDRREQ
318+
return new ErrorAndMessagePair(OSErrorEnum.ENOTCONN, OSErrorEnum.ENOTCONN.getMessage());
319+
} else if (e instanceof AlreadyConnectedException) {
320+
return new ErrorAndMessagePair(OSErrorEnum.EISCONN, OSErrorEnum.EISCONN.getMessage());
312321
} else if (e instanceof RuntimeException) {
313322
throw (RuntimeException) e;
314323
} else {
@@ -355,4 +364,8 @@ public ErrorAndMessagePair(OSErrorEnum oserror, String message) {
355364
private static int platformSpecific(int linuxValue, int darwinValue) {
356365
return getPythonOSName().equals(PLATFORM_DARWIN) ? darwinValue : linuxValue;
357366
}
367+
368+
public static class OperationWouldBlockException extends IllegalStateException {
369+
private static final long serialVersionUID = -6947337041526311362L;
370+
}
358371
}

0 commit comments

Comments
 (0)