* Search for interface with name "ifn", and fill n accordingly: * * n->ip ip address of interface "ifn" * n->if_name copy of interface name "ifn" */
| 85 | * n->if_name copy of interface name "ifn" |
| 86 | */ |
| 87 | static void |
| 88 | set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n) |
| 89 | { |
| 90 | size_t needed; |
| 91 | int mib[6]; |
| 92 | char *buf, *lim, *next; |
| 93 | struct if_msghdr *ifm; |
| 94 | struct ifa_msghdr *ifam; |
| 95 | struct sockaddr_dl *sdl; |
| 96 | struct sockaddr_in *sin; |
| 97 | int ifIndex; |
| 98 | |
| 99 | mib[0] = CTL_NET; |
| 100 | mib[1] = PF_ROUTE; |
| 101 | mib[2] = 0; |
| 102 | mib[3] = AF_INET; |
| 103 | mib[4] = NET_RT_IFLIST; |
| 104 | mib[5] = 0; |
| 105 | /* |
| 106 | * Get interface data. |
| 107 | */ |
| 108 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) |
| 109 | err(1, "iflist-sysctl-estimate"); |
| 110 | buf = safe_calloc(1, needed); |
| 111 | if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) |
| 112 | err(1, "iflist-sysctl-get"); |
| 113 | lim = buf + needed; |
| 114 | /* |
| 115 | * Loop through interfaces until one with |
| 116 | * given name is found. This is done to |
| 117 | * find correct interface index for routing |
| 118 | * message processing. |
| 119 | */ |
| 120 | ifIndex = 0; |
| 121 | next = buf; |
| 122 | while (next < lim) { |
| 123 | ifm = (struct if_msghdr *)next; |
| 124 | next += ifm->ifm_msglen; |
| 125 | if (ifm->ifm_version != RTM_VERSION) { |
| 126 | if (g_co.verbose) |
| 127 | warnx("routing message version %d " |
| 128 | "not understood", ifm->ifm_version); |
| 129 | continue; |
| 130 | } |
| 131 | if (ifm->ifm_type == RTM_IFINFO) { |
| 132 | sdl = (struct sockaddr_dl *)(ifm + 1); |
| 133 | if (strlen(ifn) == sdl->sdl_nlen && |
| 134 | strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) { |
| 135 | ifIndex = ifm->ifm_index; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | if (!ifIndex) |
| 141 | errx(1, "unknown interface name %s", ifn); |
| 142 | /* |
| 143 | * Get interface address. |
| 144 | */ |
no test coverage detected