udp_linux_32.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 := int(u.groBufSize.Load())
  38. if size < MTU {
  39. size = MTU
  40. }
  41. buf := u.borrowRxBuffer(size)
  42. buffers[i] = buf
  43. names[i] = make([]byte, unix.SizeofSockaddrInet6)
  44. vs := []iovec{
  45. {Base: &buf[0], Len: uint32(len(buf))},
  46. }
  47. msgs[i].Hdr.Iov = &vs[0]
  48. msgs[i].Hdr.Iovlen = uint32(len(vs))
  49. msgs[i].Hdr.Name = &names[i][0]
  50. msgs[i].Hdr.Namelen = uint32(len(names[i]))
  51. if controlLen > 0 {
  52. controls[i] = make([]byte, controlLen)
  53. msgs[i].Hdr.Control = &controls[i][0]
  54. msgs[i].Hdr.Controllen = controllen(len(controls[i]))
  55. } else {
  56. msgs[i].Hdr.Control = nil
  57. msgs[i].Hdr.Controllen = controllen(0)
  58. }
  59. }
  60. return msgs, buffers, names, controls
  61. }
  62. func setIovecBase(msg *rawMessage, buf []byte) {
  63. iov := (*iovec)(msg.Hdr.Iov)
  64. iov.Base = &buf[0]
  65. iov.Len = uint32(len(buf))
  66. }