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 java.nio.charset.Charset;
14 import com.sun.jna.Structure;
15 import com.sun.jna.Structure.FieldOrder;
18 * Common things for socket communication.
20 public final class Sockets {
27 * Default SSH agent socket environment variable name.
29 public static final String ENV_SSH_AUTH_SOCK = "SSH_AUTH_SOCK"; //$NON-NLS-1$
32 * Domain for Unix domain sockets.
34 public static final int AF_UNIX = 1;
37 * Socket type for duplex sockets.
39 public static final int SOCK_STREAM = 1;
42 * Default protocol selector.
44 public static final int DEFAULT_PROTOCOL = 0;
47 * Very simple representation of the C SockAddr type.
49 @FieldOrder(value = { "sa_family", "sa_data" })
50 public static class SockAddr extends Structure {
51 // This is a "variable length struct" in C.
53 // Why 108 is apparently lost in time. But the file path for a Unix
54 // domain socket cannot be longer (including the terminating NUL).
55 private static final int MAX_DATA_LENGTH = 108;
58 public short sa_family = AF_UNIX;
60 /** Unix domain socket path. */
61 public byte[] sa_data = new byte[MAX_DATA_LENGTH];
64 * Creates a new {@link SockAddr} for the given {@code path}.
69 * to use to decode the {@code path} to a byte sequence
71 public SockAddr(String path, Charset encoding) {
72 byte[] bytes = path.getBytes(encoding);
73 int toCopy = Math.min(sa_data.length - 1, bytes.length);
74 System.arraycopy(bytes, 0, sa_data, 0, toCopy);