network_clients_1942.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package adapters
  2. import "strings"
  3. // Atribución de clientes a redes (#1942). Hasta ahora la columna de clientes
  4. // del panel de redes solo se llenaba cuando el cliente traía VLAN, así que en
  5. // un br-lan sin VLAN filtering, que es el montaje normal en casa, quedaba
  6. // vacía. Aquí se cruza lo que ya sabe el servidor: el puerto del FDB, la VLAN
  7. // observada y el mapa wifi-iface → red que declara `uci show wireless`.
  8. //
  9. // Reglas: un cliente aparece en exactamente UNA red o en ninguna. Nunca se
  10. // adivina: un puerto trunk presente en varias redes sin VLAN conocida no
  11. // atribuye, y un cliente sin puerto, sin VLAN y sin wifi-iface mapeado tampoco.
  12. // wifiNetworkMap: ifname inalámbrico (wlan0, phy1-ap0...) → nombre de red UCI
  13. // (lan, guest...), desde las secciones wifi-iface ya parseadas (#541).
  14. func wifiNetworkMap(ifaces []Dot11rIface) map[string]string {
  15. out := map[string]string{}
  16. for _, ic := range ifaces {
  17. if ic.Ifname == "" || ic.Network == "" {
  18. continue
  19. }
  20. out[ic.Ifname] = ic.Network
  21. }
  22. if len(out) == 0 {
  23. return nil
  24. }
  25. return out
  26. }
  27. // netPortBase: nombre base de un miembro de red: eth0.999 y eth0 son el mismo
  28. // puerto físico, y el FDB aprende siempre sobre el base.
  29. func netPortBase(port string) string {
  30. if i := strings.IndexByte(port, '.'); i > 0 {
  31. return port[:i]
  32. }
  33. return port
  34. }
  35. // attributeNetworkClients devuelve las redes con ClientMacs relleno. El slice
  36. // de entrada no se modifica. Una red sin ningún cliente atribuible queda sin
  37. // el campo (la UI muestra "-" como antes), no una lista vacía inventada.
  38. func attributeNetworkClients(networks []NetworkInfo, clients []Device, wifiNet map[string]string) []NetworkInfo {
  39. if len(networks) == 0 {
  40. return networks
  41. }
  42. out := make([]NetworkInfo, len(networks))
  43. copy(out, networks)
  44. for _, c := range clients {
  45. candidates := make([]int, 0, len(out))
  46. // La config wireless es autoritativa: la iface está declarada en una
  47. // red concreta, sin la ambigüedad de un puerto trunk.
  48. if netName, ok := wifiNet[c.WifiIface]; ok {
  49. for i, n := range out {
  50. if n.Name == netName {
  51. candidates = append(candidates, i)
  52. }
  53. }
  54. } else if c.Port != "" {
  55. base := netPortBase(c.Port)
  56. for i, n := range out {
  57. for _, p := range n.Ports {
  58. if netPortBase(p) == base {
  59. candidates = append(candidates, i)
  60. break
  61. }
  62. }
  63. }
  64. }
  65. // La VLAN observada desempata un puerto presente en varias redes y,
  66. // sin puerto ni iface, es la única pista disponible (comportamiento
  67. // que ya tenía la UI).
  68. if c.Vlan > 0 {
  69. byVlan := make([]int, 0, len(out))
  70. for _, i := range candidates {
  71. if out[i].Vlan == c.Vlan {
  72. byVlan = append(byVlan, i)
  73. }
  74. }
  75. if len(candidates) == 0 {
  76. for i, n := range out {
  77. if n.Vlan == c.Vlan {
  78. byVlan = append(byVlan, i)
  79. }
  80. }
  81. }
  82. candidates = byVlan
  83. }
  84. if len(candidates) != 1 || c.MAC == "" {
  85. continue
  86. }
  87. i := candidates[0]
  88. out[i].ClientMacs = append(out[i].ClientMacs, c.MAC)
  89. }
  90. return out
  91. }