client.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package client
  2. import (
  3. "git.giaever.org/joachimmg/go-log.git/log"
  4. "git.giaever.org/joachimmg/m-dns/config"
  5. "git.giaever.org/joachimmg/m-dns/connection"
  6. "git.giaever.org/joachimmg/m-dns/errors"
  7. "git.giaever.org/joachimmg/m-dns/host"
  8. )
  9. type Client interface {
  10. Close() error
  11. Lookup(service, domain string, instances chan<- *host.Host) error
  12. }
  13. type client struct {
  14. ipv4u connection.UDP
  15. ipv6u connection.UDP
  16. ipv4m connection.UDP
  17. ipv6m connection.UDP
  18. running bool
  19. runCh chan struct{}
  20. }
  21. func New() (Client, error) {
  22. c := new(client)
  23. if c == nil {
  24. log.Traceln(errors.Client, errors.OutOfMemory)
  25. return nil, errors.OutOfMemory
  26. }
  27. c.ipv4u = connection.New(4)
  28. c.ipv6u = connection.New(6)
  29. if err := c.ipv4u.Listen(config.ZeroIPv4Addr); err != nil {
  30. log.Traceln(errors.Client, config.ZeroIPv4Addr, err)
  31. }
  32. if err := c.ipv6u.Listen(config.ZeroIPv6Addr); err != nil {
  33. log.Traceln(errors.Client, config.ZeroIPv6Addr, err)
  34. }
  35. if !c.ipv4u.Listening() && !c.ipv6u.Listening() {
  36. log.Traceln(errors.Client, errors.ClientUDPuFailed, config.ZeroIPv4Addr, config.ZeroIPv6Addr)
  37. return nil, errors.ClientUDPuFailed
  38. }
  39. c.ipv4m = connection.New(4)
  40. c.ipv6m = connection.New(6)
  41. if err := c.ipv4m.ListenMulticast(nil, config.MdnsIPv4Addr); err != nil {
  42. log.Traceln(errors.Client, config.MdnsIPv4Addr, err)
  43. }
  44. if err := c.ipv6m.ListenMulticast(nil, config.MdnsIPv6Addr); err != nil {
  45. log.Traceln(errors.Client, config.MdnsIPv6Addr, err)
  46. }
  47. if !c.ipv4m.Listening() && !c.ipv6m.Listening() {
  48. log.Traceln(errors.Client, errors.ClientUDPmFailed, config.MdnsIPv4Addr, config.MdnsIPv6Addr)
  49. return nil, errors.ClientUDPmFailed
  50. }
  51. return c, nil
  52. }
  53. func (c *client) Close() error {
  54. if !c.running {
  55. return nil
  56. }
  57. log.Traceln(errors.Client, "Closing")
  58. c.ipv4u.Close()
  59. c.ipv6u.Close()
  60. c.ipv4m.Close()
  61. c.ipv6m.Close()
  62. c.running = nil
  63. return nil
  64. }
  65. func (c *client) Lookup(service, domain string, instances chan<- *host.Host) error {
  66. return nil
  67. }