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

localSocketAddress implementation #2152

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,17 @@ private Socket getConnectedSocket(InetSocketAddress addr, int timeoutInMilliSeco
if (addr.isUnresolved())
throw new java.net.UnknownHostException();
selectedSocket = getSocketFactory().createSocket();

String localSocketAddress = this.conn.activeConnectionProperties.getProperty(SQLServerDriverStringProperty.LOCAL_SOCKET_ADDRESS.toString());

if (null != localSocketAddress && !localSocketAddress.isEmpty()) {
selectedSocket.bind(new InetSocketAddress(InetAddress.getByName(localSocketAddress), 0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we validate this user specified loicalSocketAddress as a valid IP somewhere before coming here ? If not would it be good to handle an exception thrown by InetSocketAddress/bind call and raise an error in that case ?


if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + " binding socket to: " + localSocketAddress);
}
}

if (!selectedSocket.isConnected()) {
selectedSocket.connect(addr, timeoutInMilliSeconds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,4 +1283,18 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
* @param accessTokenCallbackClass
*/
void setAccessTokenCallbackClass(String accessTokenCallbackClass);

/**
* Returns the specified address to explicitly to use on the client side for TCP/IP when connecting.
*
* @return localSocketAddress
*/
String getLocalSocketAddress();

/**
* Sets the address to explicitly use on the client side for TCP/IP when connecting.
*
* @param localSocketAddress
*/
void setLocalSocketAddress(String localSocketAddress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ public String getAccessToken() {
return getStringProperty(connectionProps, SQLServerDriverStringProperty.ACCESS_TOKEN.toString(), null);
}

@Override
public void setLocalSocketAddress(String localSocketAddress) {
setStringProperty(connectionProps, SQLServerDriverStringProperty.LOCAL_SOCKET_ADDRESS.toString(), localSocketAddress);
}

@Override
public String getLocalSocketAddress() {
return getStringProperty(connectionProps, SQLServerDriverStringProperty.LOCAL_SOCKET_ADDRESS.toString(), null);
}

/**
* Sets the Column Encryption setting. If lastUpdateCount is set to true, the driver will return only the last
* update count from all the update counts returned by a batch. The default of false will return all update counts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ enum SQLServerDriverStringProperty {
ENCRYPT("encrypt", EncryptOption.TRUE.toString()),
SERVER_CERTIFICATE("serverCertificate", ""),
DATETIME_DATATYPE("datetimeParameterType", DatetimeType.DATETIME2.toString()),
ACCESS_TOKEN_CALLBACK_CLASS("accessTokenCallbackClass", "");
ACCESS_TOKEN_CALLBACK_CLASS("accessTokenCallbackClass", ""),
LOCAL_SOCKET_ADDRESS("localSocketAddress", "");

private final String name;
private final String defaultValue;
Expand Down Expand Up @@ -931,6 +932,8 @@ public final class SQLServerDriver implements java.sql.Driver {
SQLServerDriverStringProperty.AAD_SECURE_PRINCIPAL_SECRET.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.MAX_RESULT_BUFFER.toString(),
SQLServerDriverStringProperty.MAX_RESULT_BUFFER.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.LOCAL_SOCKET_ADDRESS.toString(),
SQLServerDriverStringProperty.LOCAL_SOCKET_ADDRESS.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.CONNECT_RETRY_COUNT.toString(),
Integer.toString(SQLServerDriverIntProperty.CONNECT_RETRY_COUNT.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.CONNECT_RETRY_INTERVAL.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ protected Object[][] getContents() {
{"R_AADSecurePrincipalSecretPropertyDescription", "A Secret defined for a registered application which has been granted permission to the database connected."},
{"R_accessTokenCallbackClassPropertyDescription", "The class to instantiate as the SQLServerAccessTokenCallback for acquiring tokens."},
{"R_accessTokenCallbackPropertyDescription", "A SQLServerAccessTokenCallback object which is used to call a callback method to return an access token."},
{"R_localSocketAddressPropertyDescription", "A specified IPv4, IPv6 or DNS name to explicitly configure on the client side of TCP/IP when connecting."},
{"R_noParserSupport", "An error occurred while instantiating the required parser. Error: \"{0}\""},
{"R_writeOnlyXML", "Cannot read from this SQLXML instance. This instance is for writing data only."},
{"R_dataHasBeenReadXML", "Cannot read from this SQLXML instance. The data has already been read."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public void testDataSource() throws SQLServerException {
ds.setTrustStorePassword(stringPropValue);
assertEquals(stringPropValue, ds.getTrustStorePassword(), TestResource.getResource("R_valuesAreDifferent"));

ds.setLocalSocketAddress(stringPropValue);
assertEquals(stringPropValue, ds.getLocalSocketAddress(), TestResource.getResource("R_valuesAreDifferent"));

// verify encrypt=true options
ds.setEncrypt(EncryptOption.MANDATORY.toString());
assertEquals("True", EncryptOption.valueOfString(ds.getEncrypt()).toString(),
Expand Down