Make endianity swapping simpler.

This commit is contained in:
Ondrej Zajicek 2009-09-08 17:06:47 +02:00
parent be86240662
commit c15e569065
3 changed files with 42 additions and 192 deletions

View file

@ -105,7 +105,7 @@ htonlsah(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
n->sn = htonl(h->sn);
n->checksum = htons(h->checksum);
n->length = htons(h->length);
};
}
void
ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
@ -120,150 +120,29 @@ ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
h->sn = ntohl(n->sn);
h->checksum = ntohs(n->checksum);
h->length = ntohs(n->length);
};
}
void
htonlsab(void *h, void *n, u16 type, u16 len)
{
unsigned int i;
u32 *hid = h;
u32 *nid = n;
int i;
switch (type)
{
case LSA_T_RT:
{
struct ospf_lsa_rt *hrt, *nrt;
struct ospf_lsa_rt_link *hrtl, *nrtl;
u16 links;
nrt = n;
hrt = h;
#ifdef OSPFv2
links = hrt->links;
nrt->options = htons(hrt->options);
nrt->links = htons(hrt->links);
#else /* OSPFv3 */
nrt->options = htonl(hrt->options);
links = (len - sizeof(struct ospf_lsa_rt)) /
sizeof(struct ospf_lsa_rt_link);
#endif
nrtl = (struct ospf_lsa_rt_link *) (nrt + 1);
hrtl = (struct ospf_lsa_rt_link *) (hrt + 1);
for (i = 0; i < links; i++)
{
#ifdef OSPFv2
nrtl[i].id = htonl(hrtl[i].id);
nrtl[i].data = htonl(hrtl[i].data);
nrtl[i].type = hrtl[i].type;
nrtl[i].notos = hrtl[i].notos;
nrtl[i].metric = htons(hrtl[i].metric);
#else /* OSPFv3 */
nrtl[i].type = hrtl[i].type;
nrtl[i].padding = 0;
nrtl[i].metric = htons(hrtl[i].metric);
nrtl[i].lif = htonl(hrtl[i].lif);
nrtl[i].nif = htonl(hrtl[i].nif);
nrtl[i].id = htonl(hrtl[i].id);
#endif
}
break;
}
case LSA_T_NET:
case LSA_T_SUM_NET:
case LSA_T_SUM_RT:
case LSA_T_EXT:
#ifdef OSPFv3
case LSA_T_LINK:
case LSA_T_PREFIX:
#endif
{
u32 *hid, *nid;
nid = n;
hid = h;
for (i = 0; i < (len / sizeof(u32)); i++)
{
*(nid + i) = htonl(*(hid + i));
}
break;
}
default:
bug("(hton): Unknown LSA");
}
};
for (i = 0; i < (len / sizeof(u32)); i++)
nid[i] = htonl(hid[i]);
}
void
ntohlsab(void *n, void *h, u16 type, u16 len)
{
unsigned int i;
switch (type)
{
case LSA_T_RT:
{
struct ospf_lsa_rt *hrt, *nrt;
struct ospf_lsa_rt_link *hrtl, *nrtl;
u16 links;
u32 *nid = n;
u32 *hid = h;
int i;
nrt = n;
hrt = h;
#ifdef OSPFv2
hrt->options = ntohs(nrt->options);
links = hrt->links = ntohs(nrt->links);
#else /* OSPFv3 */
hrt->options = ntohl(nrt->options);
links = (len - sizeof(struct ospf_lsa_rt)) /
sizeof(struct ospf_lsa_rt_link);
#endif
nrtl = (struct ospf_lsa_rt_link *) (nrt + 1);
hrtl = (struct ospf_lsa_rt_link *) (hrt + 1);
for (i = 0; i < links; i++)
{
#ifdef OSPFv2
hrtl[i].id = ntohl(nrtl[i].id);
hrtl[i].data = ntohl(nrtl[i].data);
hrtl[i].type = nrtl[i].type;
hrtl[i].notos = nrtl[i].notos;
hrtl[i].metric = ntohs(nrtl[i].metric);
#else /* OSPFv3 */
hrtl[i].type = nrtl[i].type;
hrtl[i].padding = 0;
hrtl[i].metric = ntohs(nrtl[i].metric);
hrtl[i].lif = ntohl(nrtl[i].lif);
hrtl[i].nif = ntohl(nrtl[i].nif);
hrtl[i].id = ntohl(nrtl[i].id);
#endif
}
break;
}
case LSA_T_NET:
case LSA_T_SUM_NET:
case LSA_T_SUM_RT:
case LSA_T_EXT:
#ifdef OSPFv3
case LSA_T_LINK:
case LSA_T_PREFIX:
#endif
{
u32 *hid, *nid;
hid = h;
nid = n;
for (i = 0; i < (len / sizeof(u32)); i++)
{
hid[i] = ntohl(nid[i]);
}
break;
}
default:
bug("(ntoh): Unknown LSA");
}
};
for (i = 0; i < (len / sizeof(u32)); i++)
hid[i] = ntohl(nid[i]);
}
void
buf_dump(const char *hdr, const byte *buf, int blen)

View file

@ -389,17 +389,28 @@ struct ospf_lsa_header
struct ospf_lsa_rt
{
#ifdef CPU_BIG_ENDIAN
u16 options; /* VEB flags only */
u16 links;
#else
u16 links;
u16 options; /* VEB flags only */
#endif
};
struct ospf_lsa_rt_link
{
u32 id;
u32 data;
#ifdef CPU_BIG_ENDIAN
u8 type;
u8 notos;
u8 padding;
u16 metric;
#else
u16 metric;
u8 padding;
u8 type;
#endif
};
struct ospf_lsa_net
@ -438,9 +449,15 @@ struct ospf_lsa_rt
struct ospf_lsa_rt_link
{
#ifdef CPU_BIG_ENDIAN
u8 type;
u8 padding;
u16 metric;
#else
u16 metric;
u8 padding;
u8 type;
#endif
u32 lif; /* Local interface ID */
u32 nif; /* Neighbor interface ID */
u32 id; /* Neighbor router ID */
@ -481,8 +498,13 @@ struct ospf_lsa_link
struct ospf_lsa_prefix
{
#ifdef CPU_BIG_ENDIAN
u16 pxcount;
u16 ref_type;
#else
u16 ref_type;
u16 pxcount;
#endif
u32 ref_id;
u32 ref_rt;
u32 rest[];
@ -514,57 +536,6 @@ static inline unsigned lsa_net_count(struct ospf_lsa_header *lsa)
}
/*
struct ospf_lsa_ext_etos
{
#ifdef CPU_BIG_ENDIAN
u8 ebit:1;
u8 tos:7;
u8 padding1;
u16 padding2;
#else
u16 padding2;
u8 padding1;
u8 tos:7;
u8 ebit:1;
#endif
};
struct ospf_lsa_sum_tos
{
#ifdef CPU_BIG_ENDIAN
u8 tos;
u8 padding1;
u16 padding2;
#else
u16 padding2;
u8 padding1;
u8 tos;
#endif
};
union ospf_lsa_sum_tm
{
struct ospf_lsa_sum_tos tos;
u32 metric;
};
union ospf_lsa_ext_etm
{
struct ospf_lsa_ext_etos etos;
u32 metric;
};
struct ospf_lsa_ext_tos
{
union ospf_lsa_ext_etm etm;
ip_addr fwaddr;
u32 tag;
};
*/
struct ospf_lsreq_header
{
u32 type;

View file

@ -211,7 +211,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->data = (ifa->iface->addr->flags & IA_UNNUMBERED) ?
ifa->iface->index : ipa_to_u32(ifa->iface->addr->ip);
ln->metric = ifa->cost;
ln->notos = 0;
ln->padding = 0;
i++;
master = 1;
}
@ -226,7 +226,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->id = ipa_to_u32(ifa->drip);
ln->data = ipa_to_u32(ifa->iface->addr->ip);
ln->metric = ifa->cost;
ln->notos = 0;
ln->padding = 0;
i++;
master = 1;
}
@ -241,7 +241,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->id = neigh->rid;
ln->data = ipa_to_u32(ifa->iface->addr->ip);
ln->metric = ifa->cost;
ln->notos = 0;
ln->padding = 0;
i++;
master = 1;
}
@ -268,7 +268,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->id = ipa_to_u32(a->prefix);
ln->data = ipa_to_u32(ipa_mkmask(a->pxlen));
ln->metric = ifa->cost;
ln->notos = 0;
ln->padding = 0;
i++;
}
}
@ -282,7 +282,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->id = ipa_to_u32(sn->px.addr);
ln->data = ipa_to_u32(ipa_mkmask(sn->px.len));
ln->metric = sn->cost;
ln->notos = 0;
ln->padding = 0;
i++;
}