Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 166445de2aee
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d0f83875206a
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Jan 27, 2015

  1. Copy the full SHA
    727721b View commit details

Commits on Jan 30, 2015

  1. Merge pull request #2522 from GennadySpb/master

    Implement pack & unpacking ipv6 address in sockaddr
    enebo committed Jan 30, 2015
    Copy the full SHA
    d0f8387 View commit details
Showing with 36 additions and 8 deletions.
  1. +36 −8 core/src/main/java/org/jruby/util/io/Sockaddr.java
44 changes: 36 additions & 8 deletions core/src/main/java/org/jruby/util/io/Sockaddr.java
Original file line number Diff line number Diff line change
@@ -88,14 +88,27 @@ public static IRubyObject unpack_sockaddr_in(ThreadContext context, IRubyObject

int port = ((val.get(2)&0xff) << 8) + (val.get(3)&0xff);

StringBuilder sb = new StringBuilder()
.append(val.get(4)&0xff)
AddressFamily af = getAddressFamilyFromSockaddr(runtime, val);

StringBuilder sb = new StringBuilder();

if (af == AddressFamily.AF_INET) {
sb.append(val.get(4) & 0xff)
.append(".")
.append(val.get(5)&0xff)
.append(val.get(5) & 0xff)
.append(".")
.append(val.get(6)&0xff)
.append(val.get(6) & 0xff)
.append(".")
.append(val.get(7)&0xff);
.append(val.get(7) & 0xff);

} else { // if af == AddressFamily.AF_INET6
for (int i = 4; i <= 19; i++) {
if (i != 4 && i % 2 == 0) {
sb.append(":");
}
sb.append(Integer.toHexString(val.get(i) & 0xff | 0x100).substring(1));
}
}

IRubyObject[] result = new IRubyObject[]{
runtime.newFixnum(port),
@@ -117,15 +130,23 @@ public static IRubyObject pack_sockaddr_in(ThreadContext context, int iport, Str
try {
DataOutputStream ds = new DataOutputStream(bufS);

writeSockaddrHeader(AddressFamily.AF_INET, ds);
writeSockaddrPort(ds, iport);

try {
if(host != null && "".equals(host)) {
writeSockaddrHeader(AddressFamily.AF_INET, ds);
writeSockaddrPort(ds, iport);
ds.writeInt(0);
} else {
InetAddress[] addrs = InetAddress.getAllByName(host);
byte[] addr = addrs[0].getAddress();

if (addr.length == 4) {
writeSockaddrHeader(AddressFamily.AF_INET, ds);
} else {
writeSockaddrHeader(AddressFamily.AF_INET6, ds);
}

writeSockaddrPort(ds, iport);

ds.write(addr, 0, addr.length);
}
} catch (UnknownHostException e) {
@@ -207,6 +228,13 @@ public static void validateSockaddr(Ruby runtime, ByteList val) {
}
}

public static AddressFamily getAddressFamilyFromSockaddr(Ruby runtime, ByteList val) {
int high = val.get(0) & 0xff;
int low = val.get(1) & 0xff;

return AddressFamily.valueOf((high << 8) + low);
}

private static RuntimeException sockerr(Ruby runtime, String msg) {
return new RaiseException(runtime, runtime.getClass("SocketError"), msg, true);
}