Java Code Examples for io.netty.util.internal.SocketUtils#socketAddress()

The following examples show how to use io.netty.util.internal.SocketUtils#socketAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Socks5ProxyServer.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean handleProxyProtocol(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!authenticated) {
        authenticated = authenticate(ctx, msg);
        return false;
    }

    Socks5CommandRequest req = (Socks5CommandRequest) msg;
    assertThat(req.type(), is(Socks5CommandType.CONNECT));

    Socks5CommandResponse res =
            new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4);
    intermediaryDestination = SocketUtils.socketAddress(req.dstAddr(), req.dstPort());

    ctx.write(res);

    ctx.pipeline().remove(ENCODER);
    ctx.pipeline().remove(DECODER);

    return true;
}
 
Example 2
Source File: DnsQueryTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void writeQueryTest() throws Exception {
    InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53);
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder());
    List<DnsQuery> queries = new ArrayList<DnsQuery>(5);
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
            DnsSection.QUESTION,
            new DefaultDnsQuestion("1.0.0.127.in-addr.arpa", DnsRecordType.PTR)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
            DnsSection.QUESTION,
            new DefaultDnsQuestion("www.example.com", DnsRecordType.A)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
            DnsSection.QUESTION,
            new DefaultDnsQuestion("example.com", DnsRecordType.AAAA)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
            DnsSection.QUESTION,
            new DefaultDnsQuestion("example.com", DnsRecordType.MX)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
            DnsSection.QUESTION,
            new DefaultDnsQuestion("example.com", DnsRecordType.CNAME)));

    for (DnsQuery query: queries) {
        assertThat(query.count(DnsSection.QUESTION), is(1));
        assertThat(query.count(DnsSection.ANSWER), is(0));
        assertThat(query.count(DnsSection.AUTHORITY), is(0));
        assertThat(query.count(DnsSection.ADDITIONAL), is(0));

        embedder.writeOutbound(query);

        DatagramPacket packet = embedder.readOutbound();
        Assert.assertTrue(packet.content().isReadable());
        packet.release();
        Assert.assertNull(embedder.readOutbound());
    }
}
 
Example 3
Source File: IpSubnetFilterTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static EmbeddedChannel newEmbeddedInetChannel(final String ipAddress, ChannelHandler... handlers) {
    return new EmbeddedChannel(handlers) {
        @Override
        protected SocketAddress remoteAddress0() {
            return isActive()? SocketUtils.socketAddress(ipAddress, 5421) : null;
        }
    };
}
 
Example 4
Source File: DatagramPacketEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncode() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    assertTrue(channel.writeOutbound(
            new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender)));
    DatagramPacket packet = channel.readOutbound();
    try {
        assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8));
        assertEquals(recipient, packet.recipient());
        assertEquals(sender, packet.sender());
    } finally {
        packet.release();
    }
}
 
Example 5
Source File: DatagramPacketEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnmatchedMessageType() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    DefaultAddressedEnvelope<Long, InetSocketAddress> envelope =
            new DefaultAddressedEnvelope<Long, InetSocketAddress>(1L, recipient, sender);
    assertTrue(channel.writeOutbound(envelope));
    DefaultAddressedEnvelope<Long, InetSocketAddress> output = channel.readOutbound();
    try {
        assertSame(envelope, output);
    } finally {
        output.release();
    }
}
 
Example 6
Source File: DatagramPacketDecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecode() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    ByteBuf content = Unpooled.wrappedBuffer("netty".getBytes(CharsetUtil.UTF_8));
    assertTrue(channel.writeInbound(new DatagramPacket(content, recipient, sender)));
    assertEquals("netty", channel.readInbound());
}
 
Example 7
Source File: MsgEchoPeerOne.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    final int messageSize = 64 * 1024;
    final InetSocketAddress self = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
    final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
    new MsgEchoPeerOne(self, peer, messageSize).run();
}
 
Example 8
Source File: MsgEchoPeerTwo.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    final int messageSize = 64 * 1024;
    final InetSocketAddress self = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
    final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
    new MsgEchoPeerTwo(self, peer, messageSize).run();
}
 
Example 9
Source File: ByteEchoPeerOne.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int messageSize = 64 * 1024;
    final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
    final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
    new ByteEchoPeerOne(messageSize, myAddress, peerAddress).run();
}
 
Example 10
Source File: ByteEchoPeerTwo.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int messageSize = 64 * 1024;
    final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
    final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
    new ByteEchoPeerTwo(messageSize, myAddress, peerAddress).run();
}
 
Example 11
Source File: DatagramMulticastTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable {
    MulticastTestHandler mhandler = new MulticastTestHandler();

    sb.handler(new SimpleChannelInboundHandler<Object>() {
        @Override
        public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
            // Nothing will be sent.
        }
    });

    cb.handler(mhandler);

    sb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
    sb.option(ChannelOption.SO_REUSEADDR, true);
    cb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
    cb.option(ChannelOption.SO_REUSEADDR, true);

    Channel sc = sb.bind(newSocketAddress()).sync().channel();

    InetSocketAddress addr = (InetSocketAddress) sc.localAddress();
    cb.localAddress(addr.getPort());

    if (sc instanceof OioDatagramChannel) {
        // skip the test for OIO, as it fails because of
        // No route to host which makes no sense.
        // Maybe a JDK bug ?
        sc.close().awaitUninterruptibly();
        return;
    }
    DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel();

    String group = "230.0.0.1";
    InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort());

    cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();

    sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
    assertTrue(mhandler.await());

    // leave the group
    cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();

    // sleep a second to make sure we left the group
    Thread.sleep(1000);

    // we should not receive a message anymore as we left the group before
    sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
    mhandler.await();

    sc.close().awaitUninterruptibly();
    cc.close().awaitUninterruptibly();
}
 
Example 12
Source File: IpSubnetFilterTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static InetSocketAddress newSockAddress(String ipAddress) {
    return SocketUtils.socketAddress(ipAddress, 1234);
}