netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Graf <tgraf@suug.ch>
To: "Tomá? Macek" <maca02@atlas.cz>
Cc: netdev@oss.sgi.com
Subject: Re: receive only one record from the routing table
Date: Sat, 18 Jun 2005 22:23:59 +0200	[thread overview]
Message-ID: <20050618202359.GP22463@postel.suug.ch> (raw)
In-Reply-To: <Pine.LNX.4.61.0506182042540.29813@localhost.localdomain>

* Tom?? Macek <Pine.LNX.4.61.0506182042540.29813@localhost.localdomain> 2005-06-18 20:55
> The 'rtm_dst_len = 16' should mean the mask of the route I'm looking for, correct?

Yes.

> The whole code before sending the packet is below:
> 
> 
>      /* Create Socket */
>      if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
>          perror("Socket Creation: ");
> 
>      /* Initialize the buffer */
>      memset(msgBuf, 0, BUFSIZE);
> 
>      /* point the header and the msg structure pointers into the buffer */
>      nlMsg = (struct nlmsghdr *)msgBuf;
>      rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
>      rtMsg->rtm_family = AF_INET;
>      rtMsg->rtm_dst_len = 16;
> 
>      /* Fill in the nlmsg header*/
>      nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message.
>      nlMsg->nlmsg_type = RTM_GETROUTE;   // Get the routes from kernel routing table .
>      nlMsg->nlmsg_flags = NLM_F_REQUEST;   // The message is a request for dump.
>      nlMsg->nlmsg_seq = msgSeq++;   // Sequence of the message packet.
>      nlMsg->nlmsg_pid = getpid();   // PID of process sending the request.
> 
>      char *cp;
>      unsigned int xx[4]; int i = 0;
>      unsigned char *ap = (unsigned char *)xx;
>      for (cp = argv[1], i = 0; *cp; cp++) {
>          if (*cp <= '9' && *cp >= '0') {
>              ap[i] = 10*ap[i] + (*cp-'0');
>              continue;
>          }
>          if (*cp == '.' && ++i <= 3)
>              continue;
>          return -1;
>      }
> 
>      NetlinkAddAttr(nlMsg, sizeof(nlMsg), RTA_DST, &xx, 4);

This looks good but your NetlinkAddAttr is bogus, it should
be something like this:

int nl_msg_append_tlv(struct nlmsghdr *n, int type, void *data, size_t len)
{
	int tlen;
	struct rtattr *rta;
	
	tlen = NLMSG_ALIGN(n->nlmsg_len) + RTA_LENGTH(NLMSG_ALIGN(len));

	rta = (struct rtattr *) NLMSG_TAIL(n);
	rta->rta_type = type;
	rta->rta_len = RTA_LENGTH(NLMSG_ALIGN(len));
	memcpy(RTA_DATA(rta), data, len);
	n->nlmsg_len = tlen;
	
	return 0;
}

Your code is missing various alignment requirements. I can't tell
whether this is the last bug. I recommend you to read ip/iproute.c
in the iproute2 source or give libnl a second chance.

  reply	other threads:[~2005-06-18 20:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-17 13:51 receive only one record from the routing table Tomáš Macek
2005-06-17 14:15 ` Thomas Graf
2005-06-17 18:57   ` Tomáš Macek
2005-06-17 19:13     ` Thomas Graf
2005-06-18 18:55       ` Tomáš Macek
2005-06-18 20:23         ` Thomas Graf [this message]
2005-06-22 10:07           ` Tomáš Macek
2005-06-22 13:53           ` Print one record only - addition Tomáš Macek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050618202359.GP22463@postel.suug.ch \
    --to=tgraf@suug.ch \
    --cc=maca02@atlas.cz \
    --cc=netdev@oss.sgi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).