Skip to content

Commit e6cf365

Browse files
parth-opensrcgvisor-bot
authored andcommitted
nftables: Added uint8/16 and int8/16/64 BytesView converters.
PiperOrigin-RevId: 826652100
1 parent 2d04b04 commit e6cf365

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

pkg/sentry/socket/netlink/nlmsg/message.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ func (v *BytesView) String() string {
374374
return string(b)
375375
}
376376

377+
// Uint8 converts the raw attribute value to uint8.
378+
func (v *BytesView) Uint8() (uint8, bool) {
379+
attr := []byte(*v)
380+
val := primitive.Uint8(0)
381+
if len(attr) != val.SizeBytes() {
382+
return 0, false
383+
}
384+
val.UnmarshalBytes(attr)
385+
return uint8(val), true
386+
}
387+
388+
// Uint16 converts the raw attribute value to uint16.
389+
func (v *BytesView) Uint16() (uint16, bool) {
390+
attr := []byte(*v)
391+
val := primitive.Uint16(0)
392+
if len(attr) != val.SizeBytes() {
393+
return 0, false
394+
}
395+
val.UnmarshalBytes(attr)
396+
return uint16(val), true
397+
}
398+
377399
// Uint32 converts the raw attribute value to uint32.
378400
func (v *BytesView) Uint32() (uint32, bool) {
379401
attr := []byte(*v)
@@ -396,6 +418,28 @@ func (v *BytesView) Uint64() (uint64, bool) {
396418
return uint64(val), true
397419
}
398420

421+
// Int8 converts the raw attribute value to int8.
422+
func (v *BytesView) Int8() (int8, bool) {
423+
attr := []byte(*v)
424+
val := primitive.Int8(0)
425+
if len(attr) != val.SizeBytes() {
426+
return 0, false
427+
}
428+
val.UnmarshalBytes(attr)
429+
return int8(val), true
430+
}
431+
432+
// Int16 converts the raw attribute value to int32.
433+
func (v *BytesView) Int16() (int16, bool) {
434+
attr := []byte(*v)
435+
val := primitive.Int16(0)
436+
if len(attr) != val.SizeBytes() {
437+
return 0, false
438+
}
439+
val.UnmarshalBytes(attr)
440+
return int16(val), true
441+
}
442+
399443
// Int32 converts the raw attribute value to int32.
400444
func (v *BytesView) Int32() (int32, bool) {
401445
attr := []byte(*v)
@@ -407,6 +451,17 @@ func (v *BytesView) Int32() (int32, bool) {
407451
return int32(val), true
408452
}
409453

454+
// Int64 converts the raw attribute value to int32.
455+
func (v *BytesView) Int64() (int64, bool) {
456+
attr := []byte(*v)
457+
val := primitive.Int64(0)
458+
if len(attr) != val.SizeBytes() {
459+
return 0, false
460+
}
461+
val.UnmarshalBytes(attr)
462+
return int64(val), true
463+
}
464+
410465
// NetToHostU16 converts a uint16 in network byte order to
411466
// host byte order value.
412467
func NetToHostU16(v uint16) uint16 {

pkg/sentry/socket/netlink/nlmsg/message_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,54 @@ func TestBytesView(t *testing.T) {
342342
ok: false,
343343
value: 0,
344344
},
345+
bytesViewTest[uint16]{
346+
desc: "Convert BytesView to uint16",
347+
input: nlmsg.BytesView([]byte{7, 0}),
348+
ok: true,
349+
value: 7,
350+
},
351+
bytesViewTest[uint16]{
352+
desc: "Failed convert BytesView to uint16",
353+
input: nlmsg.BytesView([]byte{7}),
354+
ok: false,
355+
value: 0,
356+
},
357+
bytesViewTest[int16]{
358+
desc: "Convert BytesView to int16",
359+
input: nlmsg.BytesView([]byte{8, 0}),
360+
ok: true,
361+
value: 8,
362+
},
363+
bytesViewTest[int16]{
364+
desc: "Failed convert BytesView to int16",
365+
input: nlmsg.BytesView([]byte{8}),
366+
ok: false,
367+
value: 0,
368+
},
369+
bytesViewTest[uint8]{
370+
desc: "Convert BytesView to uint8",
371+
input: nlmsg.BytesView([]byte{7}),
372+
ok: true,
373+
value: 7,
374+
},
375+
bytesViewTest[uint8]{
376+
desc: "Failed convert BytesView to uint8",
377+
input: nlmsg.BytesView([]byte{}),
378+
ok: false,
379+
value: 0,
380+
},
381+
bytesViewTest[int8]{
382+
desc: "Convert BytesView to int8",
383+
input: nlmsg.BytesView([]byte{8 | uint8(0x1)<<7}),
384+
ok: true,
385+
value: 8,
386+
},
387+
bytesViewTest[int8]{
388+
desc: "Failed convert BytesView to int8",
389+
input: nlmsg.BytesView([]byte{}),
390+
ok: false,
391+
value: 0,
392+
},
345393
}
346394
for _, test := range tests {
347395
switch test.(type) {
@@ -369,6 +417,42 @@ func TestBytesView(t *testing.T) {
369417
if ok && value != tst.value {
370418
t.Errorf("%v: BytesView.Int32() got %v, want %v", tst.desc, value, tst.value)
371419
}
420+
case bytesViewTest[uint16]:
421+
tst := test.(bytesViewTest[uint16])
422+
value, ok := tst.input.Uint16()
423+
if ok != tst.ok {
424+
t.Errorf("%v: BytesView.Uint16() got ok = %v, want %v", tst.desc, ok, tst.ok)
425+
}
426+
if ok && value != tst.value {
427+
t.Errorf("%v: BytesView.Uint16() got %v, want %v", tst.desc, value, tst.value)
428+
}
429+
case bytesViewTest[int16]:
430+
tst := test.(bytesViewTest[int16])
431+
value, ok := tst.input.Int16()
432+
if ok != tst.ok {
433+
t.Errorf("%v: BytesView.Int16() got ok = %v, want %v", tst.desc, ok, tst.ok)
434+
}
435+
if ok && value != tst.value {
436+
t.Errorf("%v: BytesView.Int16() got %v, want %v", tst.desc, value, tst.value)
437+
}
438+
case bytesViewTest[uint8]:
439+
tst := test.(bytesViewTest[uint8])
440+
value, ok := tst.input.Uint8()
441+
if ok != tst.ok {
442+
t.Errorf("%v: BytesView.Uint8() got ok = %v, want %v", tst.desc, ok, tst.ok)
443+
}
444+
if ok && value != tst.value {
445+
t.Errorf("%v: BytesView.Uint8() got %v, want %v", tst.desc, value, tst.value)
446+
}
447+
case bytesViewTest[int8]:
448+
tst := test.(bytesViewTest[int8])
449+
value, ok := tst.input.Int8()
450+
if ok != tst.ok {
451+
t.Errorf("%v: BytesView.Int8() got ok = %v, want %v", tst.desc, ok, tst.ok)
452+
}
453+
if ok && value != tst.value {
454+
t.Errorf("%v: BytesView.Int8() got %v, want %v", tst.desc, value, tst.value)
455+
}
372456
default:
373457
t.Errorf("BytesView %T not support", t)
374458
}

0 commit comments

Comments
 (0)