static void reconf_reboot(int action, int argc, char **argv, int oi) { struct if_head *ifs; struct interface *ifn, *ifp; ifs = discover_interfaces(argc - oi, argv + oi); if (ifs == NULL) return; while ((ifp = TAILQ_FIRST(ifs))) { TAILQ_REMOVE(ifs, ifp, next); ifn = find_interface(ifp->name); if (ifn) { if (action) if_reboot(ifn, argc, argv); else ipv4_applyaddr(ifn); free_interface(ifp); } else { init_state(ifp, argc, argv); TAILQ_INSERT_TAIL(ifaces, ifp, next); start_interface(ifp); } } free(ifs); sort_interfaces(); }
void handle_carrier(int carrier, int flags, const char *ifname) { struct interface *ifp; if (!(options & DHCPCD_LINK)) return; ifp = find_interface(ifname); if (ifp == NULL) { handle_interface(1, ifname); return; } if (!(ifp->options->options & DHCPCD_LINK)) return; if (carrier == LINK_UNKNOWN) carrier = carrier_status(ifp); /* will set ifp->flags */ else ifp->flags = flags; if (carrier == LINK_UNKNOWN) syslog(LOG_ERR, "%s: carrier_status: %m", ifname); /* IFF_RUNNING is checked, if needed, earlier and is OS dependant */ else if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) { if (ifp->carrier != LINK_DOWN) { if (ifp->carrier == LINK_UP) syslog(LOG_INFO, "%s: carrier lost", ifp->name); ifp->carrier = LINK_DOWN; dhcp_close(ifp); dhcp6_drop(ifp, "EXPIRE6"); ipv6rs_drop(ifp); /* Don't blindly delete our knowledge of LL addresses. * We need to listen to what the kernel does with * them as some OS's will remove, mark tentative or * do nothing. */ ipv6_free_ll_callbacks(ifp); dhcp_drop(ifp, "NOCARRIER"); } } else if (carrier == LINK_UP && ifp->flags & IFF_UP) { if (ifp->carrier != LINK_UP) { syslog(LOG_INFO, "%s: carrier acquired", ifp->name); ifp->carrier = LINK_UP; #if !defined(__linux__) && !defined(__NetBSD__) /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the * hardware address changes so we have to go * through the disovery process to work it out. */ handle_interface(0, ifp->name); #endif if (ifp->wireless) getifssid(ifp->name, ifp->ssid); configure_interface(ifp, margc, margv); script_runreason(ifp, "CARRIER"); start_interface(ifp); } } }
void handle_interface(int action, const char *ifname) { struct if_head *ifs; struct interface *ifp, *ifn, *ifl = NULL; const char * const argv[] = { ifname }; int i; if (action == -1) { ifp = find_interface(ifname); if (ifp != NULL) { ifp->options->options |= DHCPCD_DEPARTED; stop_interface(ifp); } return; } /* If running off an interface list, check it's in it. */ if (ifc) { for (i = 0; i < ifc; i++) if (strcmp(ifv[i], ifname) == 0) break; if (i >= ifc) return; } ifs = discover_interfaces(-1, UNCONST(argv)); TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) { if (strcmp(ifp->name, ifname) != 0) continue; /* Check if we already have the interface */ ifl = find_interface(ifp->name); if (ifl) { /* The flags and hwaddr could have changed */ ifl->flags = ifp->flags; ifl->hwlen = ifp->hwlen; if (ifp->hwlen != 0) memcpy(ifl->hwaddr, ifp->hwaddr, ifl->hwlen); } else { TAILQ_REMOVE(ifs, ifp, next); TAILQ_INSERT_TAIL(ifaces, ifp, next); } if (action == 1) { init_state(ifp, margc, margv); start_interface(ifp); } } /* Free our discovered list */ while ((ifp = TAILQ_FIRST(ifs))) { TAILQ_REMOVE(ifs, ifp, next); free_interface(ifp); } free(ifs); }
static void if_reboot(struct interface *ifp, int argc, char **argv) { int oldopts; oldopts = ifp->options->options; script_runreason(ifp, "RECONFIGURE"); configure_interface(ifp, argc, argv); dhcp_reboot_newopts(ifp, oldopts); dhcp6_reboot(ifp); start_interface(ifp); }
int main (int argc, char ** argv) { uint16_t *data, max = 0, min = 0; unsigned int i, data_size; /* Initialize SDL */ if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO)) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Initialize libavcodec and all of the codecs */ av_register_all(); if (argc > 1) { data = load_file(argv[1], &data_size); if ((data) && (data_size > 0)) { printf("Managed to load file correctly!\n"); printf("Data size: %d\n", data_size); min = max = data[0]; } else { return 1; } for (i = 0; i < data_size; i++) { if (data[i] > max) max = data[i]; if (data[i] < min) min = data[i]; } printf("max: %d, min: %d\n", max, min); } if (data) { start_interface(); } else { fprintf(stderr, "No data to display!\n"); return 1; } SDL_Quit(); return 0; }
int loopback_setup(void) { int r, if_loopback; union { struct sockaddr sa; struct sockaddr_nl nl; struct sockaddr_storage storage; } sa; unsigned requests = 0, i; int fd; errno = 0; if ((if_loopback = (int) if_nametoindex("lo")) <= 0) return errno ? -errno : -ENODEV; if ((fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) return -errno; zero(sa); sa.nl.nl_family = AF_NETLINK; if (bind(fd, &sa.sa, sizeof(sa)) < 0) { r = -errno; goto finish; } if ((r = add_adresses(fd, if_loopback, &requests)) < 0) goto finish; if ((r = start_interface(fd, if_loopback, &requests)) < 0) goto finish; for (i = 0; i < requests; i++) { if ((r = read_response(fd, requests)) < 0) goto finish; } r = 0; finish: if (r < 0) log_warning("Failed to configure loopback device: %s", strerror(-r)); if (fd >= 0) close_nointr_nofail(fd); return r; }
static int add_adresses(int fd, int if_loopback, unsigned *requests) { union { struct sockaddr sa; struct sockaddr_nl nl; } sa = { .nl.nl_family = AF_NETLINK, }; union { struct nlmsghdr header; uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + RTA_LENGTH(sizeof(struct in6_addr))]; } request = { .header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), .header.nlmsg_type = RTM_NEWADDR, .header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK, .header.nlmsg_seq = *requests + 1, }; struct ifaddrmsg *ifaddrmsg; uint32_t ipv4_address = htonl(INADDR_LOOPBACK); int r; ifaddrmsg = NLMSG_DATA(&request.header); ifaddrmsg->ifa_family = AF_INET; ifaddrmsg->ifa_prefixlen = 8; ifaddrmsg->ifa_flags = IFA_F_PERMANENT; ifaddrmsg->ifa_scope = RT_SCOPE_HOST; ifaddrmsg->ifa_index = if_loopback; r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address)); if (r < 0) return r; if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) return -errno; (*requests)++; if (!socket_ipv6_is_supported()) return 0; request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); request.header.nlmsg_seq = *requests + 1; ifaddrmsg->ifa_family = AF_INET6; ifaddrmsg->ifa_prefixlen = 128; r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback)); if (r < 0) return r; if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) return -errno; (*requests)++; return 0; } static int start_interface(int fd, int if_loopback, unsigned *requests) { union { struct sockaddr sa; struct sockaddr_nl nl; } sa = { .nl.nl_family = AF_NETLINK, }; union { struct nlmsghdr header; uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))]; } request = { .header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), .header.nlmsg_type = RTM_NEWLINK, .header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK, .header.nlmsg_seq = *requests + 1, }; struct ifinfomsg *ifinfomsg; ifinfomsg = NLMSG_DATA(&request.header); ifinfomsg->ifi_family = AF_UNSPEC; ifinfomsg->ifi_index = if_loopback; ifinfomsg->ifi_flags = IFF_UP; ifinfomsg->ifi_change = IFF_UP; if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) return -errno; (*requests)++; return 0; } static int read_response(int fd, unsigned requests_max) { union { struct sockaddr sa; struct sockaddr_nl nl; } sa; union { struct nlmsghdr header; uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct nlmsgerr))]; } response; ssize_t l; socklen_t sa_len = sizeof(sa); struct nlmsgerr *nlmsgerr; l = recvfrom_loop(fd, &response, sizeof(response), 0, &sa.sa, &sa_len); if (l < 0) return -errno; if (sa_len != sizeof(sa.nl) || sa.nl.nl_family != AF_NETLINK) return -EIO; if (sa.nl.nl_pid != 0) return 0; if ((size_t) l < sizeof(struct nlmsghdr)) return -EIO; if (response.header.nlmsg_type != NLMSG_ERROR || (pid_t) response.header.nlmsg_pid != getpid() || response.header.nlmsg_seq >= requests_max) return 0; if ((size_t) l < NLMSG_LENGTH(sizeof(struct nlmsgerr)) || response.header.nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) return -EIO; nlmsgerr = NLMSG_DATA(&response.header); if (nlmsgerr->error < 0 && nlmsgerr->error != -EEXIST) return nlmsgerr->error; return response.header.nlmsg_seq; } static int check_loopback(void) { int r; _cleanup_close_ int fd; union { struct sockaddr sa; struct sockaddr_in in; } sa = { .in.sin_family = AF_INET, .in.sin_addr.s_addr = INADDR_LOOPBACK, }; /* If we failed to set up the loop back device, check whether * it might already be set up */ fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); if (fd < 0) return -errno; if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0) r = 1; else r = errno == EADDRNOTAVAIL ? 0 : -errno; return r; } int loopback_setup(void) { int r, if_loopback; union { struct sockaddr sa; struct sockaddr_nl nl; } sa = { .nl.nl_family = AF_NETLINK, }; unsigned requests = 0, i; _cleanup_close_ int fd = -1; bool eperm = false; errno = 0; if_loopback = (int) if_nametoindex("lo"); if (if_loopback <= 0) return errno ? -errno : -ENODEV; fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (fd < 0) return -errno; if (bind(fd, &sa.sa, sizeof(sa)) < 0) { r = -errno; goto error; } r = add_adresses(fd, if_loopback, &requests); if (r < 0) goto error; r = start_interface(fd, if_loopback, &requests); if (r < 0) goto error; for (i = 0; i < requests; i++) { r = read_response(fd, requests); if (r == -EPERM) eperm = true; else if (r < 0) goto error; } if (eperm && check_loopback() < 0) { r = -EPERM; goto error; } return 0; error: log_warning("Failed to configure loopback device: %s", strerror(-r)); return r; }