2 * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
8 * SPDX-License-Identifier: BSD-3-Clause
10 package org.eclipse.jgit.internal.transport.sshd.agent.connector;
12 import com.sun.jna.LastErrorException;
13 import com.sun.jna.Library;
14 import com.sun.jna.Structure;
15 import com.sun.jna.platform.unix.LibCAPI;
18 * Low-level Unix/Linux JNA socket API.
20 interface UnixSockets extends LibCAPI, Library {
23 * Library to load. These functions live in libc.
25 String LIBRARY_NAME = "c"; //$NON-NLS-1$
28 * Command to set the close-on-exec flag on a file descriptor via
29 * {@link #fcntl(int, int, int)}.
34 * Specifies that a file descriptor shall not be inherited by child
40 * Creates a socket and returns a file descriptor for it.
43 * socket domain; use {@link Sockets#AF_UNIX}
45 * socket type; use {@link Sockets#SOCK_STREAM}
47 * socket communication protocol; use
48 * {@link Sockets#DEFAULT_PROTOCOL}.
49 * @return file descriptor for the socket; should be closed eventually, or
51 * @throws LastErrorException
53 * @see LibCAPI#close(int)
55 int socket(int domain, int type, int protocol) throws LastErrorException;
58 * Simple binding to fcntl; used to set the FD_CLOEXEC flag. On OS X, we
59 * cannot include SOCK_CLOEXEC in the socket() call.
62 * file descriptor to operate on
64 * set to {@link #F_SETFD}
66 * zero to clear the close-on-exec flag, {@link #FD_CLOEXEC} to
68 * @return -1 on error, otherwise a value >= 0
69 * @throws LastErrorException
71 int fcntl(int fd, int command, int flag) throws LastErrorException;
74 * Connects a file descriptor, which must refer to a socket, to a
75 * {@link Sockets.SockAddr}.
78 * file descriptor of the socket, as returned by
79 * {@link #socket(int, int, int)}
81 * address to connect to
83 * Length of {@code addr}, use {@link Structure#size()}
84 * @return 0 on success; -1 otherwise
85 * @throws LastErrorException
88 int connect(int fd, Sockets.SockAddr addr, int addrLen)
89 throws LastErrorException;
92 * Read data from a file descriptor.
95 * file descriptor to read from
99 * maximum number of bytes to read; at most length of {@code buf}
100 * @return number of bytes actually read; zero for EOF, -1 on error
101 * @throws LastErrorException
104 LibCAPI.ssize_t read(int fd, byte[] buf, LibCAPI.size_t bufLen)
105 throws LastErrorException;
108 * Write data to a file descriptor.
111 * file descriptor to write to
115 * number of bytes to write
116 * @return number of bytes actually written; -1 on error
117 * @throws LastErrorException
120 LibCAPI.ssize_t write(int fd, byte[] data, LibCAPI.size_t dataLen)
121 throws LastErrorException;