-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wrong mapping of libc function gethostname, sethostname, getdomainname and setdomainname. The functions are defined as: int getdomainname(char *name, size_t len); int setdomainname(const char *name, size_t len); int gethostname(char *name, size_t len); int sethostname(const char *name, size_t len); and a C char[] maps by default to a byte[] in java. In addition bind the winsock gethostname function: com.sun.jna.platform.win32.Winsock2.gethostname(byte[], int) The sethostname and setdomainname were tested manually, as changing the hostname is a privileged operation and is not regularly possible in unittests. This closes #871
- Loading branch information
1 parent
24b2108
commit 4d980d6
Showing
5 changed files
with
167 additions
and
14 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
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
81 changes: 81 additions & 0 deletions
81
contrib/platform/src/com/sun/jna/platform/win32/Winsock2.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,81 @@ | ||
/* Copyright (c) 2017 Matthias Bläsing, All Rights Reserved | ||
* | ||
* The contents of this file is dual-licensed under 2 | ||
* alternative Open Source/Free licenses: LGPL 2.1 or later and | ||
* Apache License 2.0. (starting with JNA version 4.0.0). | ||
* | ||
* You can freely decide which license you want to apply to | ||
* the project. | ||
* | ||
* You may obtain a copy of the LGPL License at: | ||
* | ||
* http://www.gnu.org/licenses/licenses.html | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "LGPL2.1". | ||
* | ||
* You may obtain a copy of the Apache License at: | ||
* | ||
* http://www.apache.org/licenses/ | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "AL2.0". | ||
*/ | ||
package com.sun.jna.platform.win32; | ||
|
||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
import com.sun.jna.win32.W32APIOptions; | ||
|
||
public interface Winsock2 extends Library { | ||
|
||
Winsock2 INSTANCE = (Winsock2) Native.loadLibrary("ws2_32", Winsock2.class, W32APIOptions.ASCII_OPTIONS); | ||
|
||
/** | ||
* The gethostname function retrieves the standard host name for the local | ||
* computer. | ||
* | ||
* <p> | ||
* <strong>Remarks</strong></p> | ||
* | ||
* <p> | ||
* The gethostname function returns the name of the local host into the | ||
* buffer specified by the name parameter. The host name is returned as a | ||
* null-terminated string. The form of the host name is dependent on the | ||
* Windows Sockets provider—it can be a simple host name, or it can be a | ||
* fully qualified domain name. However, it is guaranteed that the name | ||
* returned will be successfully parsed by gethostbyname and | ||
* WSAAsyncGetHostByName.</p> | ||
* | ||
* <p> | ||
* The maximum length of the name returned in the buffer pointed to by the | ||
* name parameter is dependent on the namespace provider.</p> | ||
* | ||
* <p> | ||
* If the gethostname function is used on a cluster resource on Windows | ||
* Server 2008, Windows Server 2003, or Windows 2000 Server and the | ||
* _CLUSTER_NETWORK_NAME_ environment variable is defined, then the value in | ||
* this environment variable overrides the actual hostname and is returned. | ||
* On a cluster resource, the _CLUSTER_NETWORK_NAME_ environment variable | ||
* contains the name of the cluster.</p> | ||
* | ||
* <p> | ||
* The gethostname function queries namespace providers to determine the | ||
* local host name using the SVCID_HOSTNAME GUID defined in the Svgguid.h | ||
* header file. If no namespace provider responds, then the gethostname | ||
* function returns the NetBIOS name of the local computer.</p> | ||
* | ||
* <p> | ||
* The maximum length, in bytes, of the string returned in the buffer | ||
* pointed to by the name parameter is dependent on the namespace provider, | ||
* but this string must be 256 bytes or less. So if a buffer of 256 bytes is | ||
* passed in the name parameter and the namelen parameter is set to 256, the | ||
* buffer size will always be adequate.</p> | ||
* | ||
* @param name A bytearray that receives the local host name. | ||
* @param namelen The length, in bytes, of the buffer pointed to by the name parameter. | ||
* @return If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved by calling WSAGetLastError. | ||
*/ | ||
public int gethostname(byte[] name, int namelen); | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
contrib/platform/test/com/sun/jna/platform/win32/Winsock2Test.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,41 @@ | ||
/* Copyright (c) 2017 Matthias Bläsing, All Rights Reserved | ||
* | ||
* The contents of this file is dual-licensed under 2 | ||
* alternative Open Source/Free licenses: LGPL 2.1 or later and | ||
* Apache License 2.0. (starting with JNA version 4.0.0). | ||
* | ||
* You can freely decide which license you want to apply to | ||
* the project. | ||
* | ||
* You may obtain a copy of the LGPL License at: | ||
* | ||
* http://www.gnu.org/licenses/licenses.html | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "LGPL2.1". | ||
* | ||
* You may obtain a copy of the Apache License at: | ||
* | ||
* http://www.apache.org/licenses/ | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "AL2.0". | ||
*/ | ||
package com.sun.jna.platform.win32; | ||
|
||
import com.sun.jna.Native; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class Winsock2Test { | ||
@Test | ||
public void testGethostname() { | ||
// This needs visual inspection ... | ||
byte[] buffer = new byte[256]; | ||
Winsock2.INSTANCE.gethostname(buffer, buffer.length); | ||
String hostname = Native.toString(buffer); | ||
System.out.println("Hostname: " + hostname); | ||
assertTrue(hostname.length() > 0); | ||
} | ||
|
||
} |