udp_linux_32.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build linux && (386 || amd64p32 || arm || mips || mipsle) && !android && !e2e_testing
  2. // +build linux
  3. // +build 386 amd64p32 arm mips mipsle
  4. // +build !android
  5. // +build !e2e_testing
  6. package udp
  7. import (
  8. "golang.org/x/sys/unix"
  9. )
  10. type iovec struct {
  11. Base *byte
  12. Len uint32
  13. }
  14. type msghdr struct {
  15. Name *byte
  16. Namelen uint32
  17. Iov *iovec
  18. Iovlen uint32
  19. Control *byte
  20. Controllen uint32
  21. Flags int32
  22. }
  23. type rawMessage struct {
  24. Hdr msghdr
  25. Len uint32
  26. }
  27. func (u *StdConn) PrepareRawMessages(n int) ([]rawMessage, [][]byte, [][]byte, [][]byte) {
  28. controlLen := int(u.controlLen.Load())
  29. msgs := make([]rawMessage, n)
  30. buffers := make([][]byte, n)
  31. names := make([][]byte, n)
  32. var controls [][]byte
  33. if controlLen > 0 {
  34. controls = make([][]byte, n)
  35. }
  36. for i := range msgs {
  37. size := MTU
  38. if defaultGROReadBufferSize > size {
  39. size = defaultGROReadBufferSize
  40. }
  41. buffers[i] = make([]byte, size)
  42. names[i] = make([]byte, unix.SizeofSockaddrInet6)
  43. vs := []iovec{
  44. {Base: &buffers[i][0], Len: uint32(len(buffers[i]))},
  45. }
  46. msgs[i].Hdr.Iov = &vs[0]
  47. msgs[i].Hdr.Iovlen = uint32(len(vs))
  48. msgs[i].Hdr.Name = &names[i][0]
  49. msgs[i].Hdr.Namelen = uint32(len(names[i]))
  50. if controlLen > 0 {
  51. controls[i] = make([]byte, controlLen)
  52. msgs[i].Hdr.Control = &controls[i][0]
  53. msgs[i].Hdr.Controllen = controllen(len(controls[i]))
  54. } else {
  55. msgs[i].Hdr.Control = nil
  56. msgs[i].Hdr.Controllen = controllen(0)
  57. }
  58. }
  59. return msgs, buffers, names, controls
  60. }