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 syscall #3369

Merged
merged 3 commits into from
Sep 13, 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
8 changes: 6 additions & 2 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
# package name contains capital letter and such names are conventional.
---
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOi16$EOas_i32.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOi32$EOas_i64.java"
- "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"
3 changes: 3 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 @@ -32,6 +32,9 @@
[name args] > posix
0 > stdin-fileno
1 > stdout-fileno
2 > af-inet
1 > sock-stream
6 > ipproto-tcp

[] > @ /return

Expand Down
5 changes: 5 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/sys/win32.eo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
[name args] > win32
-10 > std-input-handle
-11 > std-output-handle
2 > af-inet
1 > sock-stream
6 > ipproto-tcp
-1 > invalid-socket
-1 > socket-error

[] > @ /return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
*/
package EOorg.EOeolang.EOsys; // NOPMD

import EOorg.EOeolang.EOsys.Posix.CloseSyscall;
import EOorg.EOeolang.EOsys.Posix.GetenvSyscall;
import EOorg.EOeolang.EOsys.Posix.GetpidSyscall;
import EOorg.EOeolang.EOsys.Posix.GettimeofdaySyscall;
import EOorg.EOeolang.EOsys.Posix.ReadSyscall;
import EOorg.EOeolang.EOsys.Posix.SocketSyscall;
import EOorg.EOeolang.EOsys.Posix.WriteSyscall;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +65,8 @@ public final class EOposix$EOφ extends PhDefault implements Atom {
EOposix$EOφ.SYS_CALLS.put("write", WriteSyscall::new);
EOposix$EOφ.SYS_CALLS.put("getenv", GetenvSyscall::new);
EOposix$EOφ.SYS_CALLS.put("gettimeofday", GettimeofdaySyscall::new);
EOposix$EOφ.SYS_CALLS.put("socket", SocketSyscall::new);
EOposix$EOφ.SYS_CALLS.put("close", CloseSyscall::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
*/
package EOorg.EOeolang.EOsys; // NOPMD

import EOorg.EOeolang.EOsys.Win32.ClosesocketFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetCurrentProcessIdFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetEnvironmentVariableFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetSystemTimeFuncCall;
import EOorg.EOeolang.EOsys.Win32.ReadFileFuncCall;
import EOorg.EOeolang.EOsys.Win32.SocketFuncCall;
import EOorg.EOeolang.EOsys.Win32.WSACleanupFuncCall;
import EOorg.EOeolang.EOsys.Win32.WSAStartupFuncCall;
import EOorg.EOeolang.EOsys.Win32.WriteFileFuncCall;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +67,10 @@ public final class EOwin32$EOφ extends PhDefault implements Atom {
EOwin32$EOφ.FUNCTIONS.put("WriteFile", WriteFileFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("GetEnvironmentVariable", GetEnvironmentVariableFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("GetSystemTime", GetSystemTimeFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("WSAStartup", WSAStartupFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("WSACleanup", WSACleanupFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("socket", SocketFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("closesocket", ClosesocketFuncCall::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ public interface CStdLib extends Library {
* @return Zero on success, -1 on error
*/
int gettimeofday(GettimeofdaySyscall.Timeval timeval, Pointer timezone);

/**
* Create an endpoint for communication.
* @param domain Socket domain
* @param type Socket type
* @param protocol Socket protocol
* @return New socket descriptor on success, -1 on error
*/
int socket(int domain, int type, int protocol);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.Syscall;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhDefault;
import org.eolang.Phi;

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

/**
* Ctor.
* @param posix Posix object
*/
public CloseSyscall(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.close(new Dataized(params[0]).asNumber().intValue()))
);
result.put(1, new PhDefault());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.Syscall;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhDefault;
import org.eolang.Phi;

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

/**
* Ctor.
* @param posix Posix object
*/
public SocketSyscall(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.socket(
new Dataized(params[0]).asNumber().intValue(),
new Dataized(params[1]).asNumber().intValue(),
new Dataized(params[2]).asNumber().intValue()
)
)
);
result.put(1, new PhDefault());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.Win32; // NOPMD

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

/**
* The closesocket WS2_32 function call.
* @see <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-closesocket">here for details</a>
* @since 0.40.0
*/
public final class ClosesocketFuncCall implements Syscall {
/**
* Win32 object.
*/
private final Phi win;

/**
* Ctor.
* @param win Win32 object
*/
public ClosesocketFuncCall(final Phi win) {
this.win = win;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.win.take("return").copy();
result.put(
0,
new Data.ToPhi(
Winsock.INSTANCE.closesocket(new Dataized(params[0]).asNumber().intValue())
)
);
result.put(1, new PhDefault());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.Win32; // NOPMD

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

/**
* The socket WS2_32 function call.
* @see <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket">here for details</a>
* @since 0.40.0
*/
public final class SocketFuncCall implements Syscall {
/**
* Win32 object.
*/
private final Phi win;

/**
* Ctor.
* @param win Win32 object
*/
public SocketFuncCall(final Phi win) {
this.win = win;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.win.take("return").copy();
result.put(
0,
new Data.ToPhi(
Winsock.INSTANCE.socket(
new Dataized(params[0]).asNumber().intValue(),
new Dataized(params[1]).asNumber().intValue(),
new Dataized(params[2]).asNumber().intValue()
)
)
);
result.put(1, new PhDefault());
return result;
}
}
Loading
Loading