udpManifold.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. func startManifold(listenSocket *net.UDPConn, sendSockets []*net.UDPConn) error {
  11. var buffer [10000]byte
  12. for {
  13. length, _, err := listenSocket.ReadFromUDP(buffer[:])
  14. if err != nil {
  15. return err
  16. }
  17. for _, conn := range sendSockets {
  18. go conn.Write(buffer[:length])
  19. }
  20. }
  21. }
  22. func openReceiveSocket(listenPort int, multicastGroup string) (*net.UDPConn, error) {
  23. var err error
  24. var listenSocket *net.UDPConn
  25. if len(multicastGroup) > 0 {
  26. resolvedMulticastGroup, err := net.ResolveIPAddr("ip4", multicastGroup)
  27. if err != nil {
  28. return nil, err
  29. }
  30. listenSocket, err = net.ListenMulticastUDP("udp4", nil, &net.UDPAddr{
  31. IP: resolvedMulticastGroup.IP, Zone: resolvedMulticastGroup.Zone, Port: listenPort})
  32. } else {
  33. resolvedIP, err := net.ResolveIPAddr("ip4", "0.0.0.0")
  34. if err != nil {
  35. return nil, err
  36. }
  37. listenSocket, err = net.ListenUDP("udp4", &net.UDPAddr{
  38. IP: resolvedIP.IP, Zone: resolvedIP.Zone, Port: listenPort})
  39. }
  40. if err != nil {
  41. return nil, err
  42. }
  43. return listenSocket, nil
  44. }
  45. func printError(err error) {
  46. log.WithFields(log.Fields{
  47. "error": err.Error(),
  48. }).Error("Unhandled error")
  49. }
  50. func createSendConnections(sendAddressString string) ([]*net.UDPConn, error) {
  51. var connections []*net.UDPConn
  52. for _, rawString := range strings.Split(sendAddressString, ",") {
  53. parts := strings.Split(rawString, ":")
  54. if len(parts) != 2 {
  55. return nil, fmt.Errorf("%[1]s is an invalid address", rawString)
  56. }
  57. ip, err := net.ResolveIPAddr("ip4", parts[0])
  58. if err != nil {
  59. return nil, err
  60. }
  61. port, err := strconv.Atoi(parts[1])
  62. if err != nil {
  63. return nil, err
  64. }
  65. conn, err := net.DialUDP("udp", nil, &net.UDPAddr{IP: ip.IP, Port: port})
  66. if err != nil {
  67. return nil, err
  68. }
  69. connections = append(connections, conn)
  70. }
  71. return connections, nil
  72. }
  73. func main() {
  74. listenPort := flag.Int("listenPort", 4000, "Udp port to listen on")
  75. multicastAddress := flag.String("multicastGroup", "230.0.0.1", "Optional mutlicast group to subscribe to")
  76. sendAddresses := flag.String("sendAddresses", "localhost:4001", "Udp addresses to send out on (comma separated")
  77. flag.Parse()
  78. receiveSocket, err := openReceiveSocket(*listenPort, *multicastAddress)
  79. if err != nil {
  80. printError(err)
  81. return
  82. }
  83. sendSockets, err := createSendConnections(*sendAddresses)
  84. if err != nil {
  85. printError(err)
  86. return
  87. }
  88. err = startManifold(receiveSocket, sendSockets)
  89. if err != nil {
  90. printError(err)
  91. return
  92. }
  93. }