Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#3251): socket.connect syscall #3386

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
# package name contains capital letter and such names are conventional.
---
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/CloseSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/SocketSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/SocketFuncCall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/ClosesocketFuncCall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/WSAStartupFuncCall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/WSACleanupFuncCall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/ConnectSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/InetAddrSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/StrerrorSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/ErrnoSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/SockaddrIn.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOnet/EOsocketTest.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOnet/package-info.java"
2 changes: 2 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/i16.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
as-bytes > @
$ > as-i16
$.times -1.as-i64.as-i32.as-i16 > neg
$.as-i32.as-i64 > as-i64
$.as-i64.as-number > as-number

# Convert this `i16` to `i32`.
# The object is an atom because it's not possible to check what
Expand Down
1 change: 1 addition & 0 deletions eo-runtime/src/main/eo/org/eolang/i32.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
as-bytes > @
$ > as-i32
$.times -1.as-i64.as-i32 > neg
$.as-i64.as-number > as-number

# Convert this `i32` to `i64`.
# The object is an atom because it's not possible to check what
Expand Down
1 change: 1 addition & 0 deletions eo-runtime/src/main/eo/org/eolang/i64.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
as-bytes > @
$ > as-i64
$.times -1.as-i64 > neg
$.as-i32.as-i16 > as-i16

# Convert this `i64` to `i32`.
# The `error` is returned if the `i64` number is more than
Expand Down
113 changes: 113 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/net/socket.eo
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2024 Objectionary.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# 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 NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS 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.

+alias org.eolang.sys.posix
+alias org.eolang.sys.os
+alias org.eolang.txt.sprintf
+architect [email protected]
+home https://github.com/objectionary/eo
+package org.eolang.net
+rt jvm org.eolang:eo-runtime:0.0.0
+rt node eo2js-runtime:0.0.0
+version 0.0.0

# Socket.
[address port] > socket
if. > @
os.is-windows
win-socket address port
posix-socket address port

# Host TO Network Short.
# It is used to convert a `number` port from host byte order to
# to `i16` in network byte order (big-endian).
# This conversion is necessary because network protocols like TCP/IP use
# big-endian byte order, regardless of the host machine's architecture.
[port] > htons
port.as-i16.as-bytes > bts
as-i16. > @
or.
(bts.and FF-).left 8
(bts.right 8).and FF-

[address port] > posix-socket
[scope] > connect
code. > sd
posix
"socket"
* posix.af-inet posix.sock-stream posix.ipproto-tcp
code. > inet-addr
posix
"inet_addr"
* ^.address
if. > inet-addr-as-int
inet-addr.eq posix.inaddr-none
error
sprintf
"Couldn't convert an IPv4 address '%s' into a 32-bit integer via 'inet_addr' posix syscall, reason: '%s'"
* ^.address strerror
inet-addr.as-i32
posix.sockaddr-in > sockaddr
posix.af-inet.as-i16
^.^.htons port
inet-addr-as-int
code. > connected
posix
"connect"
* sd sockaddr sockaddr.size
code. > closed
posix
"close"
* sd
code. > strerror
posix
"strerror"
* (posix "errno" *).code
if. > @
sd.eq -1
error
sprintf
"Couldn't create a posix socket, reason: '%s'"
* strerror
try
if.
connected.eq -1
error
sprintf
"Couldn't connect to '%s:%d' on posix socket '%d', reason: '%s'"
* ^.address ^.port sd strerror
as-bytes.
dataized
scope
^.scoped-posix-socket sd
ex > [ex]
if.
closed.eq -1
error
sprintf
"Couldn't close a posix socket '%d' connected to '%s:%d'"
* sd ^.address ^.port
true

[descriptor] > scoped-posix-socket

[address port] > win-socket
2 changes: 2 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/number.eo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
as-bytes > @
$ > as-number
$.times -1 > neg
$.as-i64.as-i32 > as-i32
$.as-i32.as-i16 > as-i16

# Convert this `number` to `i64` object.
[] > as-i64 /i64
Expand Down
12 changes: 12 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/sys/posix.eo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
2 > af-inet
1 > sock-stream
6 > ipproto-tcp
-1 > inaddr-none

[] > @ /return

Expand All @@ -46,3 +47,14 @@
# Here `tv-sec` is seconds since Jan 1, 1970, and `tv-usec` - microseconds.
[tv-sec tv-usec] > timeval
$ > self

# The posix `sockaddr_in` structure.
[sin-family sin-port sin-addr] > sockaddr-in
00-00-00-00-00-00-00-00 > sin-zero
plus. > size
plus.
plus.
sin-family.size
sin-port.size
sin-addr.size
sin-zero.size
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
package EOorg.EOeolang.EOsys; // NOPMD

import EOorg.EOeolang.EOsys.Posix.CloseSyscall;
import EOorg.EOeolang.EOsys.Posix.ConnectSyscall;
import EOorg.EOeolang.EOsys.Posix.ErrnoSyscall;
import EOorg.EOeolang.EOsys.Posix.GetenvSyscall;
import EOorg.EOeolang.EOsys.Posix.GetpidSyscall;
import EOorg.EOeolang.EOsys.Posix.GettimeofdaySyscall;
import EOorg.EOeolang.EOsys.Posix.InetAddrSyscall;
import EOorg.EOeolang.EOsys.Posix.ReadSyscall;
import EOorg.EOeolang.EOsys.Posix.SocketSyscall;
import EOorg.EOeolang.EOsys.Posix.StrerrorSyscall;
import EOorg.EOeolang.EOsys.Posix.WriteSyscall;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -67,6 +71,10 @@ public final class EOposix$EOφ extends PhDefault implements Atom {
EOposix$EOφ.SYS_CALLS.put("gettimeofday", GettimeofdaySyscall::new);
EOposix$EOφ.SYS_CALLS.put("socket", SocketSyscall::new);
EOposix$EOφ.SYS_CALLS.put("close", CloseSyscall::new);
EOposix$EOφ.SYS_CALLS.put("connect", ConnectSyscall::new);
EOposix$EOφ.SYS_CALLS.put("inet_addr", InetAddrSyscall::new);
EOposix$EOφ.SYS_CALLS.put("errno", ErrnoSyscall::new);
EOposix$EOφ.SYS_CALLS.put("strerror", StrerrorSyscall::new);
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/CStdLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package EOorg.EOeolang.EOsys.Posix; // NOPMD

import EOorg.EOeolang.EOsys.SockaddrIn;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
Expand Down Expand Up @@ -62,6 +63,21 @@ public interface CStdLib extends Library {
*/
int O_RDWR = 2;

/**
* TCP connection family.
*/
int AF_INET = 2;

/**
* The "Socket as stream" type.
*/
int SOCK_STREAM = 1;

/**
* Protocol for TCP connection.
*/
int IPPROTO_TCP = 6;

/**
* Duplicates file descriptor.
* @param descriptor Old file descriptor
Expand Down Expand Up @@ -140,4 +156,29 @@ public interface CStdLib extends Library {
* @return New socket descriptor on success, -1 on error
*/
int socket(int domain, int type, int protocol);

/**
* Connects to the server at the specified IP address and port.
* @param sockfd Socket descriptor
* @param addr Address structure
* @param addrlen The size of the address structure
* @return Zero on success, -1 on error
*/
int connect(int sockfd, SockaddrIn addr, int addrlen);

/**
* Convert IP string to binary form.
* @param address IP address
* @return IP address in binary form
* @checkstyle MethodNameCheck (5 lines)
*/
@SuppressWarnings("PMD.MethodNamingConventions")
int inet_addr(String address);

/**
* Converts {@code errno} to a human-readable string.
* @param errno The error number
* @return Error as string
*/
String strerror(int errno);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* 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 NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS 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.
*/
/*
* @checkstyle PackageNameCheck (4 lines)
* @checkstyle TrailingCommentCheck (3 lines)
*/
package EOorg.EOeolang.EOsys.Posix; // NOPMD

import EOorg.EOeolang.EOsys.SockaddrIn;
import EOorg.EOeolang.EOsys.Syscall;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhDefault;
import org.eolang.Phi;

/**
* Connect syscall.
* @since 0.40
*/
public final class ConnectSyscall implements Syscall {
/**
* Posix object.
*/
private final Phi posix;

/**
* Ctor.
* @param posix Posix object
*/
public ConnectSyscall(final Phi posix) {
this.posix = posix;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.posix.take("return").copy();
result.put(
0,
new Data.ToPhi(
CStdLib.INSTANCE.connect(
new Dataized(params[0]).asNumber().intValue(),
new SockaddrIn(
new Dataized(params[1].take("sin-family")).take(Short.class),
new Dataized(params[1].take("sin-port")).take(Short.class),
new Dataized(params[1].take("sin-addr")).take(Integer.class),
new Dataized(params[1].take("sin-zero")).take()
),
new Dataized(params[2]).asNumber().intValue()
)
)
);
result.put(1, new PhDefault());
return result;
}
}
Loading
Loading