JNA call SetTcpEntry always returning 317

  java, jna, tcp, uac, windows

I am currently writing a testing/simulation software that shall imitate a certain behavior of a communication client using a proprietary TCP/IP protocol. To simulate a broken connection I want to just kick an open client socket out of the list. After some research I found that the SetTcpEntry method from the iphlpapi is a candidate to do this.
I have successfully implemented this approach in Java using JNA. But even if I run my simulation software as administrator the SetTcpEntry call returns 317. From its documentation I found that this indicates that the program is running non-elevated. But reading from some other doc which explains how to check manually for UAC elevation, I know that the program IS running elevated.
Has anyone had similar problems with this kind of call? And how could I solve this problem? Maybe there is some other way to get rid of my socket.

For completness, thi sis the Java code to delete a socket:

    public static boolean killSocket(final IoSession p_ioSession)
    {
        LogMF.info(LOGGER, "Attemtping to delete TCP connection {0} - {1}", p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress());

        final MIB_TCPROW.ByReference entry = new MIB_TCPROW.ByReference();
        entry.dwLocalAddr.setValue(CodecUtil.getInt(CodecUtil.skipOrder(((InetSocketAddress) p_ioSession.getLocalAddress()).getAddress().getAddress())));
        entry.dwLocalPort.setValue(htons(((InetSocketAddress) p_ioSession.getLocalAddress()).getPort()));
        entry.dwRemoteAddr.setValue(CodecUtil.getInt(CodecUtil.skipOrder(((InetSocketAddress) p_ioSession.getRemoteAddress()).getAddress().getAddress())));
        entry.dwRemotePort.setValue(htons(((InetSocketAddress) p_ioSession.getRemoteAddress()).getPort()));
        entry.state.State = MIB_TCP_STATE_DELETE_TCB;
        entry.state.setType("State");

        final int ret = library.SetTcpEntry(entry).intValue();

        if (ret == 0)
        {
            LogMF.info(LOGGER, "TCP connection {0} - {1} marked deleted", p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress());
            return true;
        }

        switch (ret)
        {
            case ERROR_ACCESS_DENIED:
                LogMF.error(LOGGER, "Failed to delete TCP connection {0} - {1}: Access is denied. Run as administrator.",
                        new Object[] { p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress() });
                break;
            case ERROR_NOT_SUPPORTED:
                LogMF.error(LOGGER, "Failed to delete TCP connection {0} - {1}: IPv4 is not configured.", new Object[] { p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress() });
                break;
            case ERROR_INVALID_PARAMETER:
                LogMF.error(LOGGER, "Failed to delete TCP connection {0} - {1}: Invaid function argument", new Object[] { p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress() });
                break;
            case ERROR_NOT_ELEVATED:
                LogMF.error(LOGGER, "Failed to delete TCP connection {0} - {1}: Running unelevated.", new Object[] { p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress() });
                break;
            default:
                LogMF.error(LOGGER, "Failed to delete TCP connection {0} - {1}: Unknown error.", new Object[] { p_ioSession.getLocalAddress(), p_ioSession.getRemoteAddress() });
                break;

        }

        return false;
    }

Source: Windows Questions

LEAVE A COMMENT