static int nl802154_init(struct config *conf) { int err; conf->nl_sock = nl_socket_alloc(); if (!conf->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } nl_socket_set_buffer_size(conf->nl_sock, 8192, 8192); if (genl_connect(conf->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } conf->nl802154_id = genl_ctrl_resolve(conf->nl_sock, "nl802154"); if (conf->nl802154_id < 0) { fprintf(stderr, "nl802154 not found.\n"); err = -ENOENT; goto out_handle_destroy; } return 0; out_handle_destroy: nl_socket_free(conf->nl_sock); return err; }
bool TaskstatsSocket::Open() { std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl( nl_socket_alloc(), nl_socket_free); if (!nl.get()) { LOG(ERROR) << "Failed to allocate netlink socket"; return false; } int ret = genl_connect(nl.get()); if (ret < 0) { LOG(ERROR) << nl_geterror(ret) << std::endl << "Unable to open netlink socket (are you root?)"; return false; } int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME); if (family_id < 0) { LOG(ERROR) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)"; return false; } nl_ = std::move(nl); family_id_ = family_id; return true; }
int dhd_nl_sock_connect(struct dhd_netlink_info *dhd_nli) { dhd_nli->nl = nl_socket_alloc(); if (dhd_nli->nl == NULL) return -1; if (genl_connect(dhd_nli->nl) < 0) { fprintf(stderr, "netlink connection failed\n"); goto err; } dhd_nli->nl_id = genl_ctrl_resolve(dhd_nli->nl, "nl80211"); if (dhd_nli->nl_id < 0) { fprintf(stderr, "'nl80211' netlink not found\n"); goto err; } dhd_nli->cb = nl_cb_alloc(NL_CB_DEBUG); if (dhd_nli->cb == NULL) goto err; nl_socket_set_cb(dhd_nli->nl, dhd_nli->cb); return 0; err: nl_cb_put(dhd_nli->cb); nl_socket_free(dhd_nli->nl); fprintf(stderr, "nl80211 connection failed\n"); return -1; }
// Initialize netlink socket static int nl80211_init(struct nl80211_state *state) { int err; state->nl_sock = nl_socket_alloc(); if (!state->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } if (genl_connect(state->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); if (state->nl80211_id < 0) { fprintf(stderr, "nl80211 not found.\n"); err = -ENOENT; goto out_handle_destroy; } return 0; out_handle_destroy: nl_socket_free(state->nl_sock); return err; }
static bool nl80211_init() { int err; sock = nl_socket_alloc(); if (!sock) { fprintf(stderr, "failed to allocate netlink socket\n"); goto out; } err = genl_connect(sock); if (err) { nl_perror(err, "failed to make generic netlink connection"); goto out; } err = genl_ctrl_alloc_cache(sock, &cache); if (err) { nl_perror(err, "failed to allocate netlink controller cache"); goto out; } family = genl_ctrl_search_by_name(cache, NL80211_GENL_NAME); if (!family) { fprintf(stderr, "failed to find nl80211\n"); goto out; } return true; out: genl_family_put(family); nl_cache_free(cache); nl_socket_free(sock); return false; }
int unl_genl_init(struct unl *unl, const char *family) { memset(unl, 0, sizeof(*unl)); if (unl_init(unl)) goto error_out; unl->hdrlen = NLMSG_ALIGN(sizeof(struct genlmsghdr)); unl->family_name = strdup(family); if (!unl->family_name) goto error; if (genl_connect(unl->sock)) goto error; if (genl_ctrl_alloc_cache(unl->sock, &unl->cache)) goto error; unl->family = genl_ctrl_search_by_name(unl->cache, family); if (!unl->family) goto error; return 0; error: unl_free(unl); error_out: return -1; }
static int nl80211_init(void) { int err; state.nl_sock = nl_socket_alloc(); if (!state.nl_sock) { LOG_ERR_("Failed to allocate netlink socket.\n"); return -ENOMEM; } nl_socket_set_buffer_size(state.nl_sock, 8192, 8192); if (genl_connect(state.nl_sock)) { LOG_ERR_("Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } state.nl80211_id = genl_ctrl_resolve(state.nl_sock, "nl80211"); if (state.nl80211_id < 0) { LOG_ERR_("nl80211 not found.\n"); err = -ENOENT; goto out_handle_destroy; } return 0; out_handle_destroy: nl_socket_free(state.nl_sock); return err; }
static int nl80211_init(void) { int err, fd; if (!nls) { nls = malloc(sizeof(struct nl80211_state)); if (!nls) { err = -ENOMEM; goto err; } memset(nls, 0, sizeof(*nls)); nls->nl_sock = nl_socket_alloc(); if (!nls->nl_sock) { err = -ENOMEM; goto err; } if (genl_connect(nls->nl_sock)) { err = -ENOLINK; goto err; } fd = nl_socket_get_fd(nls->nl_sock); if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) { err = -EINVAL; goto err; } if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) { err = -ENOMEM; goto err; } nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211"); if (!nls->nl80211) { err = -ENOENT; goto err; } nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl"); if (!nls->nlctrl) { err = -ENOENT; goto err; } } return 0; err: nl80211_close(); return err; }
void lowpansocket::getAddress(struct ieee802154_addr *ret, const vinterface &iface) { #if defined HAVE_LIBNL || HAVE_LIBNL3 #ifdef HAVE_LIBNL3 struct nl_sock *nl = nl_socket_alloc(); #else struct nl_handle *nl = nl_handle_alloc(); #endif unsigned char *buf = NULL; struct sockaddr_nl nla; struct nlattr *attrs[IEEE802154_ATTR_MAX+1]; struct genlmsghdr *ghdr; struct nlmsghdr *nlh; struct nl_msg *msg; int family; if (!nl) return; genl_connect(nl); /* Build and send message */ msg = nlmsg_alloc(); family = genl_ctrl_resolve(nl, "802.15.4 MAC"); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, IEEE802154_LIST_IFACE, 1); nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, iface.toString().c_str()); nl_send_auto_complete(nl, msg); nlmsg_free(msg); /* Receive and parse answer */ nl_recv(nl, &nla, &buf, NULL); nlh = (struct nlmsghdr*)buf; genlmsg_parse(nlh, 0, attrs, IEEE802154_ATTR_MAX, ieee802154_policy); ghdr = (genlmsghdr*)nlmsg_data(nlh); if (!attrs[IEEE802154_ATTR_SHORT_ADDR] || !attrs[IEEE802154_ATTR_SHORT_ADDR]) return; // We only handle short addresses right now ret->addr_type = IEEE802154_ADDR_SHORT; ret->pan_id = nla_get_u16(attrs[IEEE802154_ATTR_PAN_ID]); ret->short_addr = nla_get_u16(attrs[IEEE802154_ATTR_SHORT_ADDR]); free(buf); nl_close(nl); #ifdef HAVE_LIBNL3 nl_socket_free(nl); #else nl_handle_destroy(nl); #endif #endif }
int main() { struct nl_sock * sk; int cbarg; // nl_debug = 4; // setup netlink socket sk = nl_socket_alloc(); nl_socket_disable_seq_check(sk); // disable sequence number check genl_connect(sk); int id = genl_ctrl_resolve(sk, DEMO_FAMILY_NAME); struct nl_msg * msg; // create a messgae msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, id, 0, // hdrlen 0, // flags DEMO_CMD, // numeric command identifier DEMO_VERSION // interface version ); nla_put_string(msg, DEMO_ATTR1_STRING, "hola"); nla_put_u16(msg, DEMO_ATTR2_UINT16, 0xf1); // send it nl_send_auto(sk, msg); // handle reply struct nl_cb * cb = NULL; cb = nl_cb_alloc(NL_CB_CUSTOM); //nl_cb_set_all(cb, NL_CB_DEBUG, NULL, NULL); nl_cb_set_all(cb, NL_CB_CUSTOM, cb_handler, &cbarg); nl_cb_err(cb, NL_CB_DEBUG, NULL, NULL); int nrecv = nl_recvmsgs_report(sk, cb); printf("cbarg %d nrecv %d\n", cbarg, nrecv); // cleanup nlmsg_free(msg); nl_close(sk); nl_socket_free(sk); return 0; }
int ipvs_nl_send_message(struct nl_msg *msg, nl_recvmsg_msg_cb_t func, void *arg) { int err = EINVAL; sock = nl_socket_alloc(); if (!sock) { nlmsg_free(msg); return -1; } if (genl_connect(sock) < 0) goto fail_genl; family = genl_ctrl_resolve(sock, IPVS_GENL_NAME); if (family < 0) goto fail_genl; /* To test connections and set the family */ if (msg == NULL) { nl_socket_free(sock); sock = NULL; return 0; } if (nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, func, arg) != 0) goto fail_genl; if (nl_send_auto_complete(sock, msg) < 0) goto fail_genl; if ((err = -nl_recvmsgs_default(sock)) > 0) goto fail_genl; nlmsg_free(msg); nl_socket_free(sock); return 0; fail_genl: nl_socket_free(sock); sock = NULL; nlmsg_free(msg); errno = err; #ifndef FALLBACK_LIBNL1 errno = nlerr2syserr(err); #endif return -1; }
static struct nl_sock* nl_create_handle(struct nl_cb* cb) { struct nl_sock* handle; handle = nl_socket_alloc_cb(cb); if (!handle) { return NULL; } if (genl_connect(handle)) { nl_socket_free(handle); return NULL; } return handle; }
int main() { struct nl_handle *handle; struct nl_msg *msg; struct myhdr { char mychar[20]; } *hdr; int id; handle = nl_handle_alloc(); if (handle == NULL) goto open_failure; if (genl_connect(handle) != 0) goto open_failure; id = genl_ctrl_resolve(handle, "CONTROL_EXMPL"); if(id < 0) { perror("genl_ctrl_resolve\n"); goto open_failure; } else printf("id %i\n", id); msg = nlmsg_alloc(); memset(msg, 0, 16); int pid = getpid(); int seq = 1; hdr = genlmsg_put(msg, pid, seq, id, sizeof(struct myhdr), NLM_F_REQUEST, 1, 1); memcpy(hdr->mychar, "hello world", strlen("hello world") + 1); int ret = nl_send(handle, msg); printf("message sent %i\n", ret); nlmsg_free(msg); return 0; open_failure: if (handle) { nl_close(handle); nl_handle_destroy(handle); } printf("Erreur open_failure\n"); return 0; }
static struct nl_handle *init_netlink(void) { struct nl_handle *handle; int ret, multicast_id; handle = nl_handle_alloc(); if (!handle){ elog("Cannot allocate netlink handle!\n"); return NULL; } nl_disable_sequence_check(handle); ret = genl_connect(handle); if (ret < 0){ elog("Cannot connect to netlink socket\n"); return NULL; } /* familyid = genl_ctrl_resolve(handle, SAMPLE_NL_FAMILY_NAME); if (!familyid){ elog("Cannot resolve family name(%s)\n", SAMPLE_NL_FAMILY_NAME); return NULL; } dlog("familyid %d\n", familyid); */ multicast_id = nl_get_multicast_id(handle, SAMPLE_NL_FAMILY_NAME, SAMPLE_NL_GRP_NAME); if (multicast_id < 0){ elog("Cannot resolve grp name(%d)\n", SAMPLE_NL_GRP_NAME); return NULL; } ret = nl_socket_add_membership(handle, multicast_id); if (ret < 0){ elog("Cannot join fs multicast group\n"); return NULL; } ret = nl_socket_modify_cb(handle, NL_CB_VALID, NL_CB_CUSTOM, sample_nl_cb, NULL); if (ret < 0){ elog("Cannot register callback for" " netlink messages\n"); return NULL; } return handle; }
/** * Query the wireless device for information * about the current connection */ bool wireless_network::query(bool accumulate) { if (!network::query(accumulate)) { return false; } struct nl_sock* sk = nl_socket_alloc(); if (sk == nullptr) { return false; } if (genl_connect(sk) < 0) { return false; } int driver_id = genl_ctrl_resolve(sk, "nl80211"); if (driver_id < 0) { nl_socket_free(sk); return false; } if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, scan_cb, this) != 0) { nl_socket_free(sk); return false; } struct nl_msg* msg = nlmsg_alloc(); if (msg == nullptr) { nl_socket_free(sk); return false; } if ((genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) == nullptr) || nla_put_u32(msg, NL80211_ATTR_IFINDEX, m_ifid) < 0) { nlmsg_free(msg); nl_socket_free(sk); return false; } // nl_send_sync always frees msg if (nl_send_sync(sk, msg) < 0) { nl_socket_free(sk); return false; } nl_socket_free(sk); return true; }
int netlink_init(void) { int error; sk = nl_socket_alloc(); if (!sk) { log_err("Could not allocate the socket to kernelspace; it seems we're out of memory."); return -ENOMEM; } /* * We handle ACKs ourselves. The reason is that Netlink ACK errors do * not contain the friendly error string, so they're useless to us. * https://github.com/NICMx/Jool/issues/169 */ nl_socket_disable_auto_ack(sk); error = genl_connect(sk); if (error) { log_err("Could not open the socket to kernelspace."); goto fail; } family = genl_ctrl_resolve(sk, GNL_JOOL_FAMILY_NAME); if (family < 0) { log_err("Jool's socket family doesn't seem to exist."); log_err("(This probably means Jool hasn't been modprobed.)"); error = family; goto fail; } /* error = nl_socket_modify_err_cb(sk, NL_CB_CUSTOM, error_handler, NULL); if (error) { log_err("Could not register the error handler function."); log_err("This means the socket to kernelspace cannot be used."); goto fail; } */ return 0; fail: nl_socket_free(sk); return netlink_print_error(error); }
static struct nl_sock * create_genl_socket(void) { int ret; struct nl_sock *sk = nl_socket_alloc(); if (sk == NULL) { fprintf(stderr, "Failed to allocate netlink socket\n"); abort(); } if ((ret = genl_connect(sk)) != 0) { fprintf(stderr, "Failed to connect netlink socket: %s\n", nl_geterror(ret)); abort(); } return sk; }
int nl80211_connect(const char *interface, void **handle, void **cache, void **family, char *errstr) { #ifndef HAVE_LINUX_NETLINK snprintf(errstr, LORCON_STATUS_MAX, "LORCON was not compiled with netlink/nl80211 " "support, check the output of ./configure for why"); return -1; #else struct nl_sock *nl_handle; struct nl_cache *nl_cache; struct genl_family *nl80211; if ((nl_handle = nl_socket_alloc()) == NULL) { snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate nlhandle", __FUNCTION__); return -1; } if (genl_connect(nl_handle)) { snprintf(errstr, LORCON_STATUS_MAX, "%s failed to connect to generic netlink", __FUNCTION__); nl_socket_free(nl_handle); return -1; } if (genl_ctrl_alloc_cache(nl_handle, &nl_cache) != 0) { snprintf(errstr, LORCON_STATUS_MAX, "%s failed to allocate " "generic netlink cache", __FUNCTION__); nl_socket_free(nl_handle); return -1; } if ((nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211")) == NULL) { snprintf(errstr, LORCON_STATUS_MAX, "%s failed to find " "nl80211 controls, kernel may be too old", __FUNCTION__); nl_socket_free(nl_handle); return -1; } (*handle) = (void *) nl_handle; (*cache) = (void *) nl_cache; (*family) = (void *) nl80211; return 1; #endif }
static void nl80211_init(struct nl80211_state *state) { int ret; state->nl_sock = nl80211_nl_socket_xalloc(); ret = genl_connect(state->nl_sock); if (ret) panic("Cannot connect generic netlink!\n"); ret = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache); if (ret < 0) panic("Failed to allocate generic netlink cache: %s!", nl_geterror(-ret)); state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); if (!state->nl80211) panic("nl80211 not found in netlink cache!\n"); }
struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg) { struct nl_handle *handle; handle = nl80211_handle_alloc(cb); if (handle == NULL) { wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " "callbacks (%s)", dbg); return NULL; } if (genl_connect(handle)) { wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " "netlink (%s)", dbg); nl80211_handle_destroy(handle); return NULL; } return handle; }
static int netlink_init(void) { info("Starting experimental netlink support"); handle = nl_handle_alloc(); if (!handle) { error("Failed to allocate netlink handle"); return -ENOMEM; } if (genl_connect(handle) < 0) { error("Failed to connect to generic netlink"); nl_handle_destroy(handle); return -ENOLINK; } cache = genl_ctrl_alloc_cache(handle); if (!cache) { error("Failed to allocate generic netlink cache"); return -ENOMEM; nl_handle_destroy(handle); } family = genl_ctrl_search_by_name(cache, "bluetooth"); if (!family) { error("Failed to find Bluetooth netlink family"); nl_cache_free(cache); nl_handle_destroy(handle); return -ENOENT; } if (create_channel(nl_socket_get_fd(handle)) < 0) { error("Failed to create netlink IO channel"); genl_family_put(family); nl_cache_free(cache); nl_handle_destroy(handle); return -ENOMEM; } return 0; }
int ws80211_init(void) { int err; #ifdef HAVE_NL80211_SPLIT_WIPHY_DUMP int features = 0; #endif /* HAVE_NL80211_SPLIT_WIPHY_DUMP */ struct nl80211_state *state = &nl_state; state->nl_sock = nl_socket_alloc(); if (!state->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } if (genl_connect(state->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); if (state->nl80211_id < 0) { fprintf(stderr, "nl80211 not found.\n"); err = -ENOENT; goto out_handle_destroy; } #ifdef HAVE_NL80211_SPLIT_WIPHY_DUMP ws80211_get_protocol_features(&features); if (features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) state->have_split_wiphy = TRUE; #endif /* HAVE_NL80211_SPLIT_WIPHY_DUMP */ return 0; out_handle_destroy: nl_socket_free(state->nl_sock); state->nl_sock = 0; return err; }
static int driver_connect() { int err; nf10_genl_sock = nl_socket_alloc(); if(nf10_genl_sock == NULL) return -NLE_NOMEM; err = genl_connect(nf10_genl_sock); if(err) { nl_socket_free(nf10_genl_sock); return err; } nf10_genl_family = genl_ctrl_resolve(nf10_genl_sock, NF10_GENL_FAMILY_NAME); if(nf10_genl_family < 0) { nl_socket_free(nf10_genl_sock); return nf10_genl_family; } return 0; }
int main() { /* create socket */ struct nl_sock *sk = NULL; sk = nl_socket_alloc(); genl_connect(sk); nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, get_cw, NULL); /* setup message */ int driver_id, if_index; struct nl_msg *msg = NULL; driver_id = genl_ctrl_resolve(sk, "nl80211"); if_index = if_nametoindex("wlan0"); msg = nlmsg_alloc(); genlmsg_put(msg, 0, 0, driver_id, 0, 0, NL80211_CMD_GET_WIPHY, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_index); //struct nlattr *nested; //nested = nla_nest_start(msg,NL80211_ATTR_WIPHY_TXQ_PARAMS); //NLA_PUT_U16(msg,NL80211_TXQ_ATTR_CWMIN,cw_min); //NLA_PUT_U16(msg,NL80211_TXQ_ATTR_CWMAX,cw_max); //nla_nest_end(msg,nested); /* send/recv/ack */ int r; r = nl_send_auto_complete(sk, msg); printf("atlasd: nl_send_auto_complete returned %d\n", r); nl_recvmsgs_default(sk); nl_wait_for_ack(sk); nla_put_failure: /* from NLA_PUT_* macros */ fail: if (sk) nl_socket_free(sk); if (msg) nlmsg_free(msg); }
int main(int argc, char *argv[]) { struct nl_sock *sock; struct nl_cache *cache; struct nl_dump_params params = { .dp_type = NL_DUMP_LINE, .dp_fd = stdout, }; if ((sock = nl_socket_alloc()) == 0) ERRX("Failed nl_socket_alloc"); if (genl_connect(sock)) ERRX("Failed genl_connect"); if (genl_ctrl_alloc_cache(sock, &cache)) ERRX("Failed genl_ctrl_alloc_cache"); nl_cache_dump(cache, ¶ms); return 0; }
static int init_nl() { int err; nl_soc = nl_socket_alloc(); if (!nl_soc) { ALOGE("Failed to allocate netlink socket."); return -ENOMEM; } if (genl_connect(nl_soc)) { ALOGE("Failed to connect to generic netlink."); err = -ENOLINK; goto out_handle_destroy; } genl_ctrl_alloc_cache(nl_soc, &nl_cache); if (!nl_cache) { ALOGE("Failed to allocate generic netlink cache."); err = -ENOMEM; goto out_handle_destroy; } nl80211 = genl_ctrl_search_by_name(nl_cache, "nl80211"); if (!nl80211) { ALOGE("nl80211 not found."); err = -ENOENT; goto out_cache_free; } return 0; out_cache_free: nl_cache_free(nl_cache); out_handle_destroy: nl_socket_free(nl_soc); return err; }
static int nl80211_init(struct nl80211_state *state) { int err; state->nl_sock = nl_socket_alloc(); if (!state->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } if (genl_connect(state->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) { fprintf(stderr, "Failed to allocate generic netlink cache.\n"); err = -ENOMEM; goto out_handle_destroy; } state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); if (!state->nl80211) { fprintf(stderr, "nl80211 not found.\n"); err = -ENOENT; goto out_cache_free; } return 0; out_cache_free: nl_cache_free(state->nl_cache); out_handle_destroy: nl_socket_free(state->nl_sock); return err; }
bool CNL80211::open() { if (m_connected) return true; m_nlCallback = nl_cb_alloc(NL_CB_DEFAULT); m_nlSocket = nl_socket_alloc(); if(!m_nlSocket) { emit message("Could not create netlink socket"); return false; } if (!genl_connect(m_nlSocket)) { emit message("Could not connect to generic netlink interface"); close(); return false; } if (genl_ctrl_alloc_cache(m_nlSocket, &m_nlCache)) { emit message("Could not allocate generic netlink cache."); close(); return false; } m_nlFamily = genl_ctrl_search_by_name(m_nlCache, "nl80211"); if (!m_nlFamily) { emit message("Could not find nl80211"); close(); return false; } m_nlFd = nl_socket_get_fd(m_nlSocket); m_nlSn = new QSocketNotifier(m_nlFd,QSocketNotifier::Read,this); connect(m_nlSn,SIGNAL(activated(int)), this, SLOT(readNlMessage(void))); //Start Signal Quality polling m_sqTimerId = startTimer(m_sqPollrate); m_connected = true; return true; }
static struct nl_handle *gen_handle(struct nl_cb* cb){ struct nl_handle *handle; uint32_t pid = getpid() & 0x3FFFFF; int i; handle = nl_handle_alloc_cb(cb); for (i = 0; i < 1024; i++) { if (port_bitmap[i / 32] & (1 << (i % 32))) continue; port_bitmap[i / 32] |= 1 << (i % 32); pid += i << 22; break; } nl_socket_set_local_port(handle, pid); if (!handle){ flooder_log(FLOODER_DEBUG, "Failed to allocate a handle"); return handle; } if(genl_connect(handle)){ flooder_log(FLOODER_DEBUG, "Cannot connect to handle"); handle_destroy(handle); return NULL; } if ((handle_id = genl_ctrl_resolve(handle, "nl80211")) < 0){ flooder_log(FLOODER_DEBUG, "Cannot resolve nl80211"); handle_destroy(handle); return NULL; } return handle; }
static int wprobe_init(void) { int ret; if (n_devs++ > 0) return 0; handle = nl_socket_alloc(); if (!handle) { DPRINTF("Failed to create handle\n"); goto err; } if (genl_connect(handle)) { DPRINTF("Failed to connect to generic netlink\n"); goto err; } ret = genl_ctrl_alloc_cache(handle, &cache); if (ret < 0) { DPRINTF("Failed to allocate netlink cache\n"); goto err; } family = genl_ctrl_search_by_name(cache, "wprobe"); if (!family) { DPRINTF("wprobe API not present\n"); goto err; } return 0; err: wprobe_free(); return -EINVAL; }