You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
855 B

  1. package xmpp
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // ensurePort adds a port to an address if none are provided.
  7. // It handles both IPV4 and IPV6 addresses.
  8. func ensurePort(addr string, port int) string {
  9. // This is an IPV6 address literal
  10. if strings.HasPrefix(addr, "[") {
  11. // if address has no port (behind his ipv6 address) - add default port
  12. if strings.LastIndex(addr, ":") <= strings.LastIndex(addr, "]") {
  13. return addr + ":" + strconv.Itoa(port)
  14. }
  15. return addr
  16. }
  17. // This is either an IPV6 address without bracket or an IPV4 address
  18. switch strings.Count(addr, ":") {
  19. case 0:
  20. // This is IPV4 without port
  21. return addr + ":" + strconv.Itoa(port)
  22. case 1:
  23. // This is IPV6 with port
  24. return addr
  25. default:
  26. // This is IPV6 without port, as you need to use bracket with port in IPV6
  27. return "[" + addr + "]:" + strconv.Itoa(port)
  28. }
  29. }