forked from apache/ozone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDDS-11622. Support domain socket creation (apache#7397)
- Loading branch information
Showing
3 changed files
with
437 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/DomainPeer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hdds.scm.storage; | ||
|
||
import org.apache.hadoop.net.unix.DomainSocket; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.channels.ReadableByteChannel; | ||
|
||
/** | ||
* Represents a peer that we communicate with by using blocking I/O | ||
* on a UNIX domain socket. | ||
*/ | ||
public class DomainPeer implements Closeable { | ||
private final DomainSocket socket; | ||
private final OutputStream out; | ||
private final InputStream in; | ||
private final ReadableByteChannel channel; | ||
public static final Logger LOG = LoggerFactory.getLogger(DomainPeer.class); | ||
|
||
public DomainPeer(DomainSocket socket) { | ||
this.socket = socket; | ||
this.out = socket.getOutputStream(); | ||
this.in = socket.getInputStream(); | ||
this.channel = socket.getChannel(); | ||
} | ||
|
||
public ReadableByteChannel getInputStreamChannel() { | ||
return channel; | ||
} | ||
|
||
public void setReadTimeout(int timeoutMs) throws IOException { | ||
socket.setAttribute(DomainSocket.RECEIVE_TIMEOUT, timeoutMs); | ||
} | ||
|
||
public int getReceiveBufferSize() throws IOException { | ||
return socket.getAttribute(DomainSocket.RECEIVE_BUFFER_SIZE); | ||
} | ||
|
||
public void setWriteTimeout(int timeoutMs) throws IOException { | ||
socket.setAttribute(DomainSocket.SEND_TIMEOUT, timeoutMs); | ||
} | ||
|
||
public boolean isClosed() { | ||
return !socket.isOpen(); | ||
} | ||
|
||
public void close() throws IOException { | ||
socket.close(); | ||
LOG.info("{} is closed", socket); | ||
} | ||
|
||
public String getRemoteAddressString() { | ||
return "unix:{" + socket.toString() + "}"; | ||
} | ||
|
||
public String getLocalAddressString() { | ||
return "<local>"; | ||
} | ||
|
||
public InputStream getInputStream() throws IOException { | ||
return in; | ||
} | ||
|
||
public OutputStream getOutputStream() throws IOException { | ||
return out; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DomainPeer(" + getRemoteAddressString() + ")"; | ||
} | ||
|
||
public DomainSocket getDomainSocket() { | ||
return socket; | ||
} | ||
|
||
public boolean hasSecureChannel() { | ||
// | ||
// Communication over domain sockets is assumed to be secure, since it | ||
// doesn't pass over any network. We also carefully control the privileges | ||
// that can be used on the domain socket inode and its parent directories. | ||
// See #{java.org.apache.hadoop.net.unix.DomainSocket#validateSocketPathSecurity0} | ||
// for details. | ||
// | ||
// So unless you are running as root or the user launches the service, you cannot | ||
// launch a man-in-the-middle attach on UNIX domain socket traffic. | ||
// | ||
return true; | ||
} | ||
} |
Oops, something went wrong.