Skip to content

Commit

Permalink
Merge pull request #79 from ChainSafe/cayman/fix-ip-parse
Browse files Browse the repository at this point in the history
Fix ip parsing
  • Loading branch information
wemeetagain committed Aug 3, 2020
2 parents a774fce + 0390bdd commit d8d0d1c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/chai": "^4.2.0",
"@types/debug": "^4.1.5",
"@types/eslint": "^6.1.3",
"@types/ip6addr": "^0.2.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.0.10",
"@typescript-eslint/eslint-plugin": "^2.7.0",
Expand All @@ -58,6 +59,7 @@
"bigint-buffer": "^1.1.5",
"debug": "^4.1.1",
"dgram": "^1.0.1",
"ip6addr": "^0.2.3",
"is-ip": "^3.1.0",
"libp2p-crypto": "^0.17.5",
"multiaddr": "^7.4.2",
Expand Down
8 changes: 7 additions & 1 deletion src/message/decode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as RLP from "rlp";
import { toString as ipBufferToString } from "multiaddr/src/ip";
import { toBigIntBE } from "bigint-buffer";
import * as ip6addr from "ip6addr";
import {
IPingMessage,
IPongMessage,
Expand Down Expand Up @@ -58,11 +59,16 @@ function decodePong(data: Buffer): IPongMessage {
if (!Array.isArray(rlpRaw) || rlpRaw.length !== 4) {
throw new Error(ERR_INVALID_MESSAGE);
}
let stringIpAddr = ipBufferToString(rlpRaw[2]);
const parsedIp = ip6addr.parse(stringIpAddr);
if (parsedIp.kind() === "ipv4") {
stringIpAddr = parsedIp.toString({ format: "v4" });
}
return {
type: MessageType.PONG,
id: toBigIntBE(rlpRaw[0]),
enrSeq: toBigIntBE(rlpRaw[1]),
recipientIp: ipBufferToString(rlpRaw[2]),
recipientIp: stringIpAddr,
recipientPort: rlpRaw[3].length ? rlpRaw[3].readUIntBE(0, rlpRaw[3].length) : 0,
};
}
Expand Down
59 changes: 59 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/ip6addr@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@types/ip6addr/-/ip6addr-0.2.0.tgz#51d89902c98910b95455a17398a727ad6593904d"
integrity sha512-DXTacUIOHG+p4fRLhgNbPAjbcWkU5DGWS/HCAId/xlv1shJd+pF4gfJLbOlwejKHuu6R3nsEfmSNNUUyoEmhTw==
dependencies:
"@types/node" "*"

"@types/json-schema@*", "@types/json-schema@^7.0.3":
version "7.0.3"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
Expand Down Expand Up @@ -301,6 +308,11 @@ asn1.js@^5.0.1:
minimalistic-assert "^1.0.0"
safer-buffer "^2.1.0"

assert-plus@1.0.0, assert-plus@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
integrity sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=

assertion-error@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
Expand Down Expand Up @@ -696,6 +708,11 @@ cookie@0.3.1:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
integrity sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=

core-util-is@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

cp-file@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/cp-file/-/cp-file-6.2.0.tgz#40d5ea4a1def2a9acdd07ba5c0b0246ef73dc10d"
Expand Down Expand Up @@ -1094,6 +1111,16 @@ external-editor@^3.0.3:
iconv-lite "^0.4.24"
tmp "^0.0.33"

extsprintf@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"
integrity sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=

extsprintf@^1.2.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=

fast-deep-equal@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"
Expand Down Expand Up @@ -1467,6 +1494,14 @@ ip-regex@^4.0.0:
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-4.1.0.tgz#5ad62f685a14edb421abebc2fff8db94df67b455"
integrity sha512-pKnZpbgCTfH/1NLIlOduP/V+WRXzC2MOz3Qo8xmxk8C5GudJLgK5QyLVXOSWy3ParAH7Eemurl3xjv/WXYFvMA==

ip6addr@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/ip6addr/-/ip6addr-0.2.3.tgz#660df0d27092434f0aadee025aba8337c6d7d4d4"
integrity sha512-qA9DXRAUW+lT47/i/4+Q3GHPwZjGt/atby1FH/THN6GVATA6+Pjp2nztH7k6iKeil7hzYnBwfSsxjthlJ8lJKw==
dependencies:
assert-plus "^1.0.0"
jsprim "^1.4.0"

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
Expand Down Expand Up @@ -1667,6 +1702,11 @@ json-schema-traverse@^0.4.1:
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
integrity sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==

json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
integrity sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=

json-stable-stringify-without-jsonify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
Expand All @@ -1679,6 +1719,16 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsprim@^1.4.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
integrity sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=
dependencies:
assert-plus "1.0.0"
extsprintf "1.3.0"
json-schema "0.2.3"
verror "1.10.0"

karma@^4.3.0:
version "4.4.1"
resolved "https://registry.yarnpkg.com/karma/-/karma-4.4.1.tgz#6d9aaab037a31136dc074002620ee11e8c2e32ab"
Expand Down Expand Up @@ -3040,6 +3090,15 @@ varint@^5.0.0, varint@~5.0.0:
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf"
integrity sha1-2Ca4n3SQcy+rwMDtaT7Uddyynr8=

verror@1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"
integrity sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=
dependencies:
assert-plus "^1.0.0"
core-util-is "1.0.2"
extsprintf "^1.2.0"

void-elements@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-2.0.1.tgz#c066afb582bb1cb4128d60ea92392e94d5e9dbec"
Expand Down

0 comments on commit d8d0d1c

Please sign in to comment.