问题描述:

As part of my project, I have added a new socket option and called the option properly after the socket gets connected after checking the response header values for a HTTP GET request.

HttpEngine#readNetworkResponse()

In this function, I check the response headers for a specific header and value and accordingly call the socket option.

private Response readNetworkResponse() throws IOException

{

transport.finishRequest();

Response networkResponse = transport.readResponseHeaders()

.request(networkRequest)

.handshake(connection.getHandshake())

.header(OkHeaders.SENT_MILLIS, Long.toString(sentRequestMillis))

.header(OkHeaders.RECEIVED_MILLIS, Long.toString(System.currentTimeMillis()))

.build();

// Check for the response header and call setsocketoption as follows:

if(connection != null) {

logger.i("enableCustomOption called from HE");

connection.enableCustomOption();

}

}

Connection#enableCustomOption

public void enableCustomOption()

{

if (!connected) {

return;

}

if (httpConnection != null) {

logger.i(" enableCustomOption() from Connection called ");

httpConnection.enableCustomOption();

}

}

HttpConnection#enableCustomOption

public void enableCustomOption() {

try {

logger.i(" HC:: SOCKET DETAILS stored in HC.java ::: " + socket.toString());

logger.i(" HC:: SOCKET DETAILS retrieved using Connection ::: " + connection.getSocket().toString());

connection.getSocket().enableCustomOption();

}

catch (SocketException ignored) {

ignored.printStackTrace();

}

}

Socket#enableCustomOption

public void enableCustomOption() throws SocketException {

checkOpenAndCreate(true);

impl.setOption(SocketOptions.CUSTOM_ENABLE, Integer.valueOf(1));

}

IOBridge#getSocketOptionErrno

private static Object getSocketOptionErrno(FileDescriptor fd, int option) throws ErrnoException, SocketException {

......................

case SocketOptions.CUSTOM_ENABLE:

return Libcore.os.getsockoptInt(fd, IPPROTO_TCP, CUSTOM_ENABLE);

......................

}

IoBridge#setSocketOptionErrno

private static void setSocketOptionErrno(FileDescriptor fd, int option, Object value) throws ErrnoException, SocketException {

.........

case SocketOptions.CUSTOM_ENABLE:

Libcore.os.setsockoptInt(fd, IPPROTO_TCP,CUSTOM_ENABLE, (Integer) value);

return;

.........

}

net/ipv4/tcp.c

static int do_tcp_setsockopt(struct sock *sk, int level,

int optname, char __user *optval, unsigned int optlen)

{

.....

case CUSTOM_ENABLE:

if(val >= 0 && val <= 1) {

}

else

err = -EINVAL;

printk(KERN_ALERT "Custom option ENABLE [%d] SPORT [%d] DPORT [%d]\n", val, ntohs(tp->inet_conn.icsk_inet.inet_sport), ntohs(tp->inet_conn.icsk_inet.inet_dport));

printk(KERN_ALERT "Custom option INIT TCP SOCK (TP) POINTER [%p] ", tp);

printk(KERN_ALERT "Custom option INIT SOCK (SK) POINTER [%p] ", sk);

break;

.......

}

Issue:

After setting the socket options correctly on the Java Layer, If i check the C layer for socket values, I see there is a mismatch:

// SPORT and DPORT values of the socket are always zero

17:38:10.439 5340 11894 I System.out: (HTTPLog)-Static: enableCustomOption() from Connection called

17:38:10.439 5340 11894 I System.out: (HTTPLog)-ConnThread-175: SOCKET DETAILS stored in Connection.java (using getSocket) ::: SSL socket over Socket[address=r1---sn-ci5gup-qxas.gvt1.com/182.79.221.12,port=443,localPort=45723, fd=FileDescriptor[52]]

17:38:10.439 5340 11894 I System.out: (HTTPLog)-Static: HC:: SOCKET DETAILS stored in HC.java ::: SSL socket over Socket[address=r1---sn-ci5gup-qxas.gvt1.com/182.79.221.12,port=443,localPort=45723, fd=FileDescriptor[52]]

17:38:10.439 5340 11894 I System.out: (HTTPLog)-Static: HC:: SOCKET DETAILS retrieved using Connection ::: SSL socket over Socket[address=r1---sn-ci5gup-qxas.gvt1.com/182.79.221.12,port=443,localPort=45723, fd=FileDescriptor[52]]

17:38:10.439 5340 11894 I System.out: (HTTPLog)-Static: HC:: SOCKET DETAILS RPORT ::: 443

17:38:10.439 5340 11894 I System.out: (HTTPLog)-Static: enableCustomOption() from HTTPConnection called

// SPORT/DPORT are always ZERO

<1>[ 145.034928] [6:pool-9-thread-1:11894] Set Sock Custom option ENABLE [1] SPORT [0] DPORT [0]

<1>[ 145.034977] [6:pool-9-thread-1:11894] Custom option INIT TCP SOCK (TP) POINTER [ffffffc84594b400]

<1>[ 145.034995] [6:pool-9-thread-1:11894] Custom option INIT SOCK (SK) POINTER [ffffffc84594b400]

Anyone faced the same issue? If so, Please help.

Thank you.

相关阅读:
Top