+int parse_rtlist(char *prgname, unsigned char *rxbuf, int len)
+{
+ char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */
+ struct rtcanmsg *rtc;
+ struct rtattr *rta;
+ struct nlmsghdr *nlh;
+ unsigned int src_ifindex = 0;
+ unsigned int dst_ifindex = 0;
+ __u32 handled, dropped, deleted;
+ int rtlen;
+
+
+ nlh = (struct nlmsghdr *)rxbuf;
+
+ while (1) {
+ if (!NLMSG_OK(nlh, len))
+ return 0;
+
+ if (nlh->nlmsg_type == NLMSG_ERROR) {
+ printf("NLMSG_ERROR\n");
+ return 1;
+ }
+
+ if (nlh->nlmsg_type == NLMSG_DONE) {
+ //printf("NLMSG_DONE\n");
+ return 1;
+ }
+
+ rtc = (struct rtcanmsg *)NLMSG_DATA(nlh);
+ if (rtc->can_family != AF_CAN) {
+ printf("received msg from unknown family %d\n", rtc->can_family);
+ return -EINVAL;
+ }
+
+ if (rtc->gwtype != CGW_TYPE_CAN_CAN) {
+ printf("received msg with unknown gwtype %d\n", rtc->gwtype);
+ return -EINVAL;
+ }
+
+ /*
+ * print list in a representation that
+ * can be used directly for start scripts.
+ *
+ * To order the mandatory and optional parameters in the
+ * output string, the NLMSG is parsed twice.
+ */
+
+ handled = 0;
+ dropped = 0;
+ deleted = 0;
+ src_ifindex = 0;
+ dst_ifindex = 0;
+
+ printf("%s -A ", basename(prgname));
+
+ /* first parse for mandatory options */
+ rta = (struct rtattr *) RTCAN_RTA(rtc);
+ rtlen = RTCAN_PAYLOAD(nlh);
+ for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
+ {
+ //printf("(A-%d)", rta->rta_type);
+ switch(rta->rta_type) {
+
+ case CGW_FILTER:
+ case CGW_MOD_AND:
+ case CGW_MOD_OR:
+ case CGW_MOD_XOR:
+ case CGW_MOD_SET:
+ case CGW_LIM_HOPS:
+ case CGW_CS_XOR:
+ case CGW_CS_CRC8:
+ break;
+
+ case CGW_SRC_IF:
+ src_ifindex = *(__u32 *)RTA_DATA(rta);
+ break;
+
+ case CGW_DST_IF:
+ dst_ifindex = *(__u32 *)RTA_DATA(rta);
+ break;
+
+ case CGW_HANDLED:
+ handled = *(__u32 *)RTA_DATA(rta);
+ break;
+
+ case CGW_DROPPED:
+ dropped = *(__u32 *)RTA_DATA(rta);
+ break;
+
+ case CGW_DELETED:
+ deleted = *(__u32 *)RTA_DATA(rta);
+ break;
+
+ default:
+ printf("Unknown attribute %d!", rta->rta_type);
+ return -EINVAL;
+ break;
+ }
+ }
+
+
+ printf("-s %s ", if_indextoname(src_ifindex, ifname));
+ printf("-d %s ", if_indextoname(dst_ifindex, ifname));
+
+ if (rtc->flags & CGW_FLAGS_CAN_ECHO)
+ printf("-e ");
+
+ if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
+ printf("-t ");
+
+ if (rtc->flags & CGW_FLAGS_CAN_IIF_TX_OK)
+ printf("-i ");
+
+ /* second parse for mod attributes */
+ rta = (struct rtattr *) RTCAN_RTA(rtc);
+ rtlen = RTCAN_PAYLOAD(nlh);
+ for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
+ {
+ //printf("(B-%d)", rta->rta_type);
+ switch(rta->rta_type) {
+
+ case CGW_FILTER:
+ printfilter(RTA_DATA(rta));
+ break;
+
+ case CGW_MOD_AND:
+ printmod("AND", RTA_DATA(rta));
+ break;
+
+ case CGW_MOD_OR:
+ printmod("OR", RTA_DATA(rta));
+ break;
+
+ case CGW_MOD_XOR:
+ printmod("XOR", RTA_DATA(rta));
+ break;
+
+ case CGW_MOD_SET:
+ printmod("SET", RTA_DATA(rta));
+ break;
+
+ case CGW_LIM_HOPS:
+ printf("-l %d ", *(__u8 *)RTA_DATA(rta));
+ break;
+
+ case CGW_CS_XOR:
+ print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta));
+ break;
+
+ case CGW_CS_CRC8:
+ print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta));
+ break;
+
+ case CGW_SRC_IF:
+ case CGW_DST_IF:
+ case CGW_HANDLED:
+ case CGW_DROPPED:
+ case CGW_DELETED:
+ break;
+
+ default:
+ printf("Unknown attribute %d!", rta->rta_type);
+ return -EINVAL;
+ break;
+ }
+ }
+
+ /* end of entry */
+ printf("# %d handled %d dropped %d deleted\n",
+ handled, dropped, deleted);
+
+ /* jump to next NLMSG in the given buffer */
+ nlh = NLMSG_NEXT(nlh, len);
+ }
+}
+