| 217 | } |
| 218 | |
| 219 | struct ifmediareq * |
| 220 | ifmedia_getstate(int s) |
| 221 | { |
| 222 | static struct ifmediareq *ifmr = NULL; |
| 223 | int *mwords; |
| 224 | int xmedia = 1; |
| 225 | |
| 226 | if (ifmr == NULL) { |
| 227 | ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq)); |
| 228 | if (ifmr == NULL) |
| 229 | err(1, "malloc"); |
| 230 | |
| 231 | (void) memset(ifmr, 0, sizeof(struct ifmediareq)); |
| 232 | (void) strlcpy(ifmr->ifm_name, name, |
| 233 | sizeof(ifmr->ifm_name)); |
| 234 | |
| 235 | ifmr->ifm_count = 0; |
| 236 | ifmr->ifm_ulist = NULL; |
| 237 | |
| 238 | /* |
| 239 | * We must go through the motions of reading all |
| 240 | * supported media because we need to know both |
| 241 | * the current media type and the top-level type. |
| 242 | */ |
| 243 | |
| 244 | if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0) { |
| 245 | xmedia = 0; |
| 246 | } |
| 247 | if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) { |
| 248 | err(1, "SIOCGIFMEDIA"); |
| 249 | } |
| 250 | |
| 251 | if (ifmr->ifm_count == 0) |
| 252 | errx(1, "%s: no media types?", name); |
| 253 | |
| 254 | mwords = (int *)malloc(ifmr->ifm_count * sizeof(int)); |
| 255 | if (mwords == NULL) |
| 256 | err(1, "malloc"); |
| 257 | |
| 258 | ifmr->ifm_ulist = mwords; |
| 259 | if (xmedia) { |
| 260 | if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0) |
| 261 | err(1, "SIOCGIFXMEDIA"); |
| 262 | } else { |
| 263 | if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) |
| 264 | err(1, "SIOCGIFMEDIA"); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return ifmr; |
| 269 | } |
| 270 | |
| 271 | static void |
| 272 | setifmediacallback(int s, void *arg) |
no test coverage detected