Beispiel #1
0
int
send_clear_command ()
{
  set_nl_header (SR_C_CLEAR);
  set_attributes ();
  if (sendto_fd (sd, (char *) &req, req.n.nlmsg_len) < 0)
    return -1;
  receive_response ();
  return 0;
}
/*
 * Probe the controller in genetlink to find the family id
 */
int ResetManager::get_family_id(int sk, const char *family_name)
{
    struct nlattr *na;
    int rep_len;
    int id = -1;
    GENERIC_NETLINK_PACKET family_req, ans;

    /* Get family name */
    family_req.n.nlmsg_type = GENL_ID_CTRL;
    family_req.n.nlmsg_flags = NLM_F_REQUEST;
    family_req.n.nlmsg_seq = 0;
    family_req.n.nlmsg_pid = getpid();
    family_req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
    family_req.g.cmd = CTRL_CMD_GETFAMILY;
    family_req.g.version = 0x1;

    na = (struct nlattr *) GENLMSG_DATA(&family_req);
    na->nla_type = CTRL_ATTR_FAMILY_NAME;
    na->nla_len = strlen(family_name) + 1 + NLA_HDRLEN;
    strcpy((char *)NLA_DATA(na), family_name);
 
    family_req.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);

    if (sendto_fd(sk, (char *) &family_req, family_req.n.nlmsg_len) < 0) {
        return -1;
    }

    rep_len = recv(sk, &ans, sizeof(ans), 0);
    if (rep_len < 0){
        LOGE("no response\n");
        return -1;
    }
    /* Validate response message */
    else if (!NLMSG_OK((&ans.n), (unsigned int)rep_len)){
        LOGE("invalid reply message\n");
        return -1;
    }
    else if (ans.n.nlmsg_type == NLMSG_ERROR) { /* error */
        LOGE("received error\n");
        return -1;
    }

    na = (struct nlattr *) GENLMSG_DATA(&ans);
    na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
    if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
        id = *(__u16 *) NLA_DATA(na);
    }

    return id;
}
Beispiel #3
0
int
get_family_id ()
{
  int id;
  struct nlattr *na;

  if (strlen (SR_GNL_FAMILY_NAME) > 16)
    {
      printf ("get_family_id - hostname too long.");
      exit (0);
    }

  set_nl_header (CTRL_CMD_GETFAMILY);

  req.n.nlmsg_type = GENL_ID_CTRL;
  req.n.nlmsg_seq = 0;
  req.g.version = 0x1;

  na = (struct nlattr *) GENLMSG_DATA (&req);
  set_nl_attr (na, CTRL_ATTR_FAMILY_NAME, SR_GNL_FAMILY_NAME,
	       strlen (SR_GNL_FAMILY_NAME));

  req.n.nlmsg_len += NLMSG_ALIGN (na->nla_len);

  if (sendto_fd (sd, (char *) &req, req.n.nlmsg_len) < 0)
    return -1;

  while (do_receive_response ());

  na = (struct nlattr *) GENLMSG_DATA (&ans);
  na = (struct nlattr *) GENLMSG_NLA_NEXT (na);
  if (na->nla_type == CTRL_ATTR_FAMILY_ID)
    {
      id = *(__u16 *) GENLMSG_NLA_DATA (na);
    }

  srext_fam_id = id;
  return 0;
}
Beispiel #4
0
int get_family_id(int sd)
{
    struct {
        struct nlmsghdr n;
        struct genlmsghdr g;
        char buf[256];
    } family_req;

    struct {
        struct nlmsghdr n;
        struct genlmsghdr g;
        char buf[256];
    } ans;

    int id;
    struct nlattr *na;
    int rep_len;

//         Get family name
    family_req.n.nlmsg_type = GENL_ID_CTRL;
    family_req.n.nlmsg_flags = NLM_F_REQUEST;
    family_req.n.nlmsg_seq = 0;
    family_req.n.nlmsg_pid = getpid();
    family_req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
    family_req.g.cmd = CTRL_CMD_GETFAMILY;
    family_req.g.version = 0x1;

    na = (struct nlattr *)GENLMSG_DATA(&family_req);

    na->nla_type = CTRL_ATTR_FAMILY_NAME;

    //------change here--------

    na->nla_len = strlen("CONTROL_EXMPL") + 1 + NLA_HDRLEN;
    strcpy(NLA_DATA(na), "CONTROL_EXMPL");

    family_req.n.nlmsg_len += NLMSG_ALIGN(na->nla_len);

    if (sendto_fd(sd, (char *) &family_req, family_req.n.nlmsg_len) < 0)
        return -1;

    rep_len = recv(sd, &ans, sizeof(ans), 0);
    if (rep_len < 0) {
        perror("recv");
        return -1;
    }

    //  Validate response message
    if (!NLMSG_OK((&ans.n), rep_len)) {
        fprintf(stderr, "invalid reply message\n");
        return -1;
    }

    if (ans.n.nlmsg_type == NLMSG_ERROR) { // error
        fprintf(stderr, "received error\n");
        return -1;
    }

    na = (struct nlattr *)( GENLMSG_DATA(&ans));
    na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
    if (na->nla_type == CTRL_ATTR_FAMILY_ID) {
        id = *(__u16 *) NLA_DATA(na);
    }
    return id;
}