Skip to content

Commit

Permalink
fix: network ip match recode to fix dameng INET_ATON support (#19654)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
  • Loading branch information
swordqiu and Qiu Jian authored Mar 5, 2024
1 parent 0d67795 commit e4b5bd9
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 82 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ require (
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240305034450-d50f47051d06
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900
yunion.io/x/pkg v1.10.1-0.20240303050651-73685b15a96e
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/sqlchemy v1.1.3-0.20240304110946-16faa82225e6
yunion.io/x/sqlchemy v1.1.3-0.20240305180726-ccd03e6aa837
yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,8 @@ yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401 h1:4l6ELFSQ0MBVInscZ8
yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401/go.mod h1:VK4Z93dgiKgAijcSqbMKmGaBMJuHulR16Hz4K015ZPo=
yunion.io/x/log v0.0.0-20190514041436-04ce53b17c6b/go.mod h1:+gauLs73omeJAPlsXcevLsJLKixV+sR/E7WSYTSx1fE=
yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U=
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361 h1:c5LyNdhbvBe/92pXs9jgXOU/S+RgYh/DHe538LpT/Mo=
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U=
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91 h1:inY5o3LDa/zgsIZuPN0HmpzKIsu/lLgsBmMttuDPGj4=
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U=
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 h1:Hu/4ERvoWaN6aiFs4h4/yvVBMeVzb3eDaYTxkVx9Kdo=
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900/go.mod h1:0vLkNEhlmA64HViPBAnSTUMrx5QP1CLsxXmxDKQ80tc=
yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
Expand All @@ -1223,7 +1223,7 @@ yunion.io/x/pkg v1.10.1-0.20240303050651-73685b15a96e h1:NuYu+z0dOT93aoNjg3W3PkL
yunion.io/x/pkg v1.10.1-0.20240303050651-73685b15a96e/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v1.1.3-0.20240304110946-16faa82225e6 h1:3R34JW8CxuweUn4mCrFMSTwU5Njwh5aCB5x9x6l5GBA=
yunion.io/x/sqlchemy v1.1.3-0.20240304110946-16faa82225e6/go.mod h1:5W8ghvJ4TNt/r2yDjjD3i4QsZgIiJX45dhRQBGWPHsQ=
yunion.io/x/sqlchemy v1.1.3-0.20240305180726-ccd03e6aa837 h1:2KhiZJdbKVK6ksl542z5BzDsD+bgpHEtYHCEuA8Y48A=
yunion.io/x/sqlchemy v1.1.3-0.20240305180726-ccd03e6aa837/go.mod h1:5W8ghvJ4TNt/r2yDjjD3i4QsZgIiJX45dhRQBGWPHsQ=
yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c h1:QuLab2kSRECZRxo4Lo2KcYn6XjQFDGaZ1+x0pYDVVwQ=
yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=
60 changes: 41 additions & 19 deletions pkg/compute/models/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2633,31 +2633,53 @@ func (manager *SNetworkManager) ListItemFilter(

if len(ips) > 0 {
conditions := []sqlchemy.ICondition{}
for _, ip := range ips {
if len(ip) == 0 {
for _, ipstr := range ips {
if len(ipstr) == 0 {
continue
}
ipIa, err := parseIpToIntArray(ip)
if err != nil {
return nil, err
}

ipSa := []string{"0", "0", "0", "0"}
for i := range ipIa {
ipSa[i] = strconv.Itoa(ipIa[i])
}
fullIp := strings.Join(ipSa, ".")

ipField := sqlchemy.INET_ATON(sqlchemy.NewStringField(fullIp))
ipStart := sqlchemy.INET_ATON(q.Field("guest_ip_start"))
ipEnd := sqlchemy.INET_ATON(q.Field("guest_ip_end"))

var ipCondtion sqlchemy.ICondition
if exactIpMatch {
ipCondtion = sqlchemy.Between(ipField, ipStart, ipEnd)
if ip4Addr, err := netutils.NewIPV4Addr(ipstr); err == nil {
// ipv4 address, exactly
ipStart := sqlchemy.INET_ATON(q.Field("guest_ip_start"))
ipEnd := sqlchemy.INET_ATON(q.Field("guest_ip_end"))

ipCondtion = sqlchemy.AND(
sqlchemy.GE(ipEnd, uint32(ip4Addr)),
sqlchemy.LE(ipStart, uint32(ip4Addr)),
)
if !exactIpMatch {
ipCondtion = sqlchemy.OR(
ipCondtion,
sqlchemy.Contains(q.Field("guest_ip_start"), ipstr),
sqlchemy.Contains(q.Field("guest_ip_end"), ipstr),
)
}
} else if ip6Addr, err := netutils.NewIPV6Addr(ipstr); err == nil {
// ipv6 address, exactly
ipStart := q.Field("guest_ip6_start")
ipEnd := q.Field("guest_ip6_end")

ipCondtion = sqlchemy.AND(
sqlchemy.GE(ipEnd, ip6Addr.String()),
sqlchemy.LE(ipStart, ip6Addr.String()),
)
if !exactIpMatch {
ipCondtion = sqlchemy.OR(
ipCondtion,
sqlchemy.Contains(q.Field("guest_ip6_start"), ipstr),
sqlchemy.Contains(q.Field("guest_ip6_end"), ipstr),
)
}
} else {
ipCondtion = sqlchemy.OR(sqlchemy.Between(ipField, ipStart, ipEnd), sqlchemy.Contains(q.Field("guest_ip_start"), ip), sqlchemy.Contains(q.Field("guest_ip_end"), ip))
ipCondtion = sqlchemy.OR(
sqlchemy.Contains(q.Field("guest_ip_start"), ipstr),
sqlchemy.Contains(q.Field("guest_ip_end"), ipstr),
sqlchemy.Contains(q.Field("guest_ip6_start"), ipstr),
sqlchemy.Contains(q.Field("guest_ip6_end"), ipstr),
)
}

conditions = append(conditions, ipCondtion)
}
q = q.Filter(sqlchemy.OR(conditions...))
Expand Down
4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ yunion.io/x/executor/server
# yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401
## explicit; go 1.18
yunion.io/x/jsonutils
# yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
# yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
## explicit; go 1.12
yunion.io/x/log
yunion.io/x/log/hooks
Expand Down Expand Up @@ -1626,7 +1626,7 @@ yunion.io/x/pkg/utils
# yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
## explicit; go 1.12
yunion.io/x/s3cli
# yunion.io/x/sqlchemy v1.1.3-0.20240304110946-16faa82225e6
# yunion.io/x/sqlchemy v1.1.3-0.20240305180726-ccd03e6aa837
## explicit; go 1.17
yunion.io/x/sqlchemy
yunion.io/x/sqlchemy/backends
Expand Down
3 changes: 3 additions & 0 deletions vendor/yunion.io/x/log/log.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/yunion.io/x/sqlchemy/backends/dameng/dameng.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions vendor/yunion.io/x/sqlchemy/backends/dameng/functions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 6 additions & 29 deletions vendor/yunion.io/x/sqlchemy/functions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/yunion.io/x/sqlchemy/querydefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions vendor/yunion.io/x/sqlchemy/queryfield.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions vendor/yunion.io/x/sqlchemy/subquery.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions vendor/yunion.io/x/sqlchemy/table.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions vendor/yunion.io/x/sqlchemy/union.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e4b5bd9

Please sign in to comment.