static int nl_msg_out_handler_debug(struct nl_msg *msg, void *arg) { FILE *ofd = arg ? arg : stderr; fprintf(ofd, "-- Debug: Sent Message:\n"); nl_msg_dump(msg, ofd); return NL_OK; }
static int message_dumper (struct nl_msg *msg, void *arg) { int protocol; protocol = *(int *) arg; fprintf (stderr, "Setting msg proto to %d\n", protocol); nlmsg_set_proto (msg, protocol); nl_msg_dump (msg, stderr); return NL_OK; }
static int cb_handler(struct nl_msg * msg, void * arg) { * (int*) arg = 123; // cbarg struct nlmsghdr * hdr = nlmsg_hdr(msg); struct genlmsghdr * gnlh = nlmsg_data(hdr); int valid = genlmsg_validate(hdr, 0, DEMO_ATTR_MAX, demo_gnl_policy); printf("valid %d %s\n", valid, valid ? "ERROR" : "OK"); // one way struct nlattr * attrs[DEMO_ATTR_MAX + 1]; if (genlmsg_parse(hdr, 0, attrs, DEMO_ATTR_MAX, demo_gnl_policy) < 0) { printf("genlsmg_parse ERROR\n"); } else { printf("genlsmg_parse OK\n"); printf("attr1 %s\n", nla_get_string(attrs[DEMO_ATTR1_STRING])); printf("attr2 %x\n", nla_get_u16(attrs[DEMO_ATTR2_UINT16])); struct attr_custom * cp = (struct attr_custom *) nla_data(attrs[DEMO_ATTR3_CUSTOM]); printf("attr3 %d %ld %f %lf\n", cp->a, cp->b, cp->c,cp->d); } // another way printf("gnlh->cmd %d\n", gnlh->cmd); //--- DEMO_CMD_ECHO int remaining = genlmsg_attrlen(gnlh, 0); struct nlattr * attr = genlmsg_attrdata(gnlh, 0); while (nla_ok(attr, remaining)) { printf("remaining %d\n", remaining); printf("attr @ %p\n", attr); // nla_get_string(attr) attr = nla_next(attr, &remaining); } nl_msg_dump(msg, stderr); return NL_STOP; }
static int event_input(struct nl_msg *msg, void *arg) { struct nl_cache_mngr *mngr = arg; int protocol = nlmsg_get_proto(msg); int type = nlmsg_hdr(msg)->nlmsg_type; struct nl_cache_ops *ops; int i, n; struct nl_parser_param p = { .pp_cb = include_cb, }; NL_DBG(2, "Cache manager %p, handling new message %p as event\n", mngr, msg); #ifdef NL_DEBUG if (nl_debug >= 4) nl_msg_dump(msg, stderr); #endif if (mngr->cm_protocol != protocol) BUG(); for (i = 0; i < mngr->cm_nassocs; i++) { if (mngr->cm_assocs[i].ca_cache) { ops = mngr->cm_assocs[i].ca_cache->c_ops; for (n = 0; ops->co_msgtypes[n].mt_id >= 0; n++) if (ops->co_msgtypes[n].mt_id == type) goto found; } } return NL_SKIP; found: NL_DBG(2, "Associated message %p to cache %p\n", msg, mngr->cm_assocs[i].ca_cache); p.pp_arg = &mngr->cm_assocs[i]; return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p); }
static int rtnl_raw_parse_cb(struct nl_msg *msg, void *arg) { struct nl_lookup_arg *lookup_arg = (struct nl_lookup_arg *)arg; struct usnic_rtnl_sk *unlsk = lookup_arg->unlsk; struct nlmsghdr *nlm_hdr = nlmsg_hdr(msg); struct rtmsg *rtm; struct nlattr *tb[RTA_MAX + 1]; int found = 0; int err; #if WANT_DEBUG_MSGS nl_msg_dump(msg, stderr); #endif /* WANT_DEBUG_MSGS */ lookup_arg->nh_addr = 0; lookup_arg->found = 0; lookup_arg->replied = 0; lookup_arg->msg_count++; if (nlm_hdr->nlmsg_pid != nl_socket_get_local_port(unlsk->nlh) || nlm_hdr->nlmsg_seq != unlsk->seq) { usnic_err("Not an expected reply msg pid: %u local pid: %u " "msg seq: %u expected seq: %u\n", nlm_hdr->nlmsg_pid, nl_socket_get_local_port(unlsk->nlh), nlm_hdr->nlmsg_seq, unlsk->seq); return NL_SKIP; } lookup_arg->replied = 1; if (nlm_hdr->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *e = (struct nlmsgerr *)nlmsg_data(nlm_hdr); if (nlm_hdr->nlmsg_len >= (__u32)nlmsg_msg_size(sizeof(*e))) { usnic_err("Received a netlink error message %d\n", e->error); } else { usnic_err("Received a truncated netlink error message\n"); } return NL_STOP; } if (nlm_hdr->nlmsg_type != RTM_NEWROUTE) { usnic_err("Received an invalid route request reply message\n"); return NL_STOP; } rtm = nlmsg_data(nlm_hdr); if (rtm->rtm_family != AF_INET) { usnic_err("RTM message contains invalid AF family\n"); return NL_STOP; } init_route_policy(route_policy); err = nlmsg_parse(nlm_hdr, sizeof(struct rtmsg), tb, RTA_MAX, route_policy); if (err < 0) { usnic_err("nlmsg parse error %d\n", err); return NL_STOP; } if (tb[RTA_OIF]) { if (nla_get_u32(tb[RTA_OIF]) == (uint32_t)lookup_arg->oif) found = 1; else usnic_err("Retrieved route has a different outgoing interface %d (expected %d)\n", nla_get_u32(tb[RTA_OIF]), lookup_arg->oif); } if (found && tb[RTA_METRICS]) { lookup_arg->metric = (int)nla_get_u32(tb[RTA_METRICS]); } if (found && tb[RTA_GATEWAY]) lookup_arg->nh_addr = nla_get_u32(tb[RTA_GATEWAY]); lookup_arg->found = found; return NL_STOP; }