forked from Mirror/frr
ospfd: Rework ospf_read_packet into 2 functions
The indentation level for ospf_read was starting to be pretty extremene. Rework into 2 functions for improved readability. Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
parent
4392cc4337
commit
0263751346
|
@ -2937,48 +2937,44 @@ static int ospf_verify_header(struct stream *ibuf, struct ospf_interface *oi,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Starting point of packet process function. */
|
enum ospf_read_return_enum {
|
||||||
int ospf_read(struct thread *thread)
|
OSPF_READ_ERROR,
|
||||||
|
OSPF_READ_CONTINUE,
|
||||||
|
};
|
||||||
|
|
||||||
|
static enum ospf_read_return_enum ospf_read_helper(struct ospf *ospf)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct stream *ibuf;
|
struct stream *ibuf;
|
||||||
struct ospf *ospf;
|
|
||||||
struct ospf_interface *oi;
|
struct ospf_interface *oi;
|
||||||
struct ip *iph;
|
struct ip *iph;
|
||||||
struct ospf_header *ospfh;
|
struct ospf_header *ospfh;
|
||||||
uint16_t length;
|
uint16_t length;
|
||||||
struct interface *ifp = NULL;
|
|
||||||
struct connected *c;
|
struct connected *c;
|
||||||
int32_t count = 0;
|
struct interface *ifp = NULL;
|
||||||
|
|
||||||
/* first of all get interface pointer. */
|
|
||||||
ospf = THREAD_ARG(thread);
|
|
||||||
|
|
||||||
/* prepare for next packet. */
|
|
||||||
thread_add_read(master, ospf_read, ospf, ospf->fd, &ospf->t_read);
|
|
||||||
|
|
||||||
while (count < ospf->write_oi_count) {
|
|
||||||
count++;
|
|
||||||
stream_reset(ospf->ibuf);
|
stream_reset(ospf->ibuf);
|
||||||
ibuf = ospf_recv_packet(ospf, ospf->fd, &ifp, ospf->ibuf);
|
ibuf = ospf_recv_packet(ospf, ospf->fd, &ifp, ospf->ibuf);
|
||||||
if (ibuf == NULL)
|
if (ibuf == NULL)
|
||||||
return -1;
|
return OSPF_READ_ERROR;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This raw packet is known to be at least as big as its
|
* This raw packet is known to be at least as big as its
|
||||||
* IP header.
|
* IP header. Note that there should not be alignment problems with
|
||||||
* Note that there should not be alignment problems with
|
|
||||||
* this assignment because this is at the beginning of the
|
* this assignment because this is at the beginning of the
|
||||||
* stream data buffer.
|
* stream data buffer.
|
||||||
*/
|
*/
|
||||||
iph = (struct ip *)STREAM_DATA(ibuf);
|
iph = (struct ip *)STREAM_DATA(ibuf);
|
||||||
/* Note that sockopt_iphdrincl_swab_systoh was called in
|
/*
|
||||||
* ospf_recv_packet. */
|
* Note that sockopt_iphdrincl_swab_systoh was called in
|
||||||
|
* ospf_recv_packet.
|
||||||
|
*/
|
||||||
if (ifp == NULL) {
|
if (ifp == NULL) {
|
||||||
/* Handle cases where the platform does not support
|
/*
|
||||||
* retrieving the ifindex, and also platforms (such
|
* Handle cases where the platform does not support
|
||||||
* as Solaris 8) that claim to support ifindex
|
* retrieving the ifindex, and also platforms (such as
|
||||||
* retrieval but do not.
|
* Solaris 8) that claim to support ifindex retrieval but do
|
||||||
|
* not.
|
||||||
*/
|
*/
|
||||||
c = if_lookup_address((void *)&iph->ip_src, AF_INET,
|
c = if_lookup_address((void *)&iph->ip_src, AF_INET,
|
||||||
ospf->vrf_id);
|
ospf->vrf_id);
|
||||||
|
@ -2991,7 +2987,7 @@ int ospf_read(struct thread *thread)
|
||||||
__PRETTY_FUNCTION__,
|
__PRETTY_FUNCTION__,
|
||||||
inet_ntoa(iph->ip_src),
|
inet_ntoa(iph->ip_src),
|
||||||
ospf_get_name(ospf));
|
ospf_get_name(ospf));
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3002,7 +2998,7 @@ int ospf_read(struct thread *thread)
|
||||||
"ospf_read[%s]: Dropping self-originated packet",
|
"ospf_read[%s]: Dropping self-originated packet",
|
||||||
inet_ntoa(iph->ip_src));
|
inet_ntoa(iph->ip_src));
|
||||||
}
|
}
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -3013,20 +3009,21 @@ int ospf_read(struct thread *thread)
|
||||||
|
|
||||||
ospfh = (struct ospf_header *)stream_pnt(ibuf);
|
ospfh = (struct ospf_header *)stream_pnt(ibuf);
|
||||||
if (MSG_OK
|
if (MSG_OK
|
||||||
!= ospf_packet_examin(ospfh,
|
!= ospf_packet_examin(ospfh, stream_get_endp(ibuf)
|
||||||
stream_get_endp(ibuf)
|
|
||||||
- stream_get_getp(ibuf)))
|
- stream_get_getp(ibuf)))
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
/* Now it is safe to access all fields of OSPF packet header. */
|
/* Now it is safe to access all fields of OSPF packet header. */
|
||||||
|
|
||||||
/* associate packet with ospf interface */
|
/* associate packet with ospf interface */
|
||||||
oi = ospf_if_lookup_recv_if(ospf, iph->ip_src, ifp);
|
oi = ospf_if_lookup_recv_if(ospf, iph->ip_src, ifp);
|
||||||
|
|
||||||
/* ospf_verify_header() relies on a valid "oi" and thus can
|
/*
|
||||||
* be called only after the passive/backbone/other checks
|
* ospf_verify_header() relies on a valid "oi" and thus can be called
|
||||||
* below are passed. These checks in turn access the fields
|
* only after the passive/backbone/other checks below are passed.
|
||||||
* of unverified "ospfh" structure for their own purposes and
|
* These checks in turn access the fields of unverified "ospfh"
|
||||||
* must remain very accurate in doing this. */
|
* structure for their own purposes and must remain very accurate
|
||||||
|
* in doing this.
|
||||||
|
*/
|
||||||
|
|
||||||
/* If incoming interface is passive one, ignore it. */
|
/* If incoming interface is passive one, ignore it. */
|
||||||
if (oi && OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE) {
|
if (oi && OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE) {
|
||||||
|
@ -3034,14 +3031,12 @@ int ospf_read(struct thread *thread)
|
||||||
|
|
||||||
if (IS_DEBUG_OSPF_EVENT)
|
if (IS_DEBUG_OSPF_EVENT)
|
||||||
zlog_debug(
|
zlog_debug(
|
||||||
"ignoring packet from router %s sent to %s, "
|
"ignoring packet from router %s sent to %s, received on a passive interface, %s",
|
||||||
"received on a passive interface, %s",
|
inet_ntop(AF_INET, &ospfh->router_id, buf[0],
|
||||||
inet_ntop(AF_INET, &ospfh->router_id,
|
sizeof(buf[0])),
|
||||||
buf[0], sizeof(buf[0])),
|
|
||||||
inet_ntop(AF_INET, &iph->ip_dst, buf[1],
|
inet_ntop(AF_INET, &iph->ip_dst, buf[1],
|
||||||
sizeof(buf[1])),
|
sizeof(buf[1])),
|
||||||
inet_ntop(AF_INET,
|
inet_ntop(AF_INET, &oi->address->u.prefix4,
|
||||||
&oi->address->u.prefix4,
|
|
||||||
buf[2], sizeof(buf[2])));
|
buf[2], sizeof(buf[2])));
|
||||||
|
|
||||||
if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) {
|
if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) {
|
||||||
|
@ -3052,7 +3047,7 @@ int ospf_read(struct thread *thread)
|
||||||
OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
|
OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
|
||||||
ospf_if_set_multicast(oi);
|
ospf_if_set_multicast(oi);
|
||||||
}
|
}
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3063,16 +3058,13 @@ int ospf_read(struct thread *thread)
|
||||||
if ((oi == NULL)
|
if ((oi == NULL)
|
||||||
|| (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
|
|| (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
|
||||||
&& !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))) {
|
&& !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))) {
|
||||||
if ((oi = ospf_associate_packet_vl(ospf, ifp, iph,
|
if ((oi = ospf_associate_packet_vl(ospf, ifp, iph, ospfh))
|
||||||
ospfh))
|
|
||||||
== NULL) {
|
== NULL) {
|
||||||
if (!ospf->instance && IS_DEBUG_OSPF_EVENT)
|
if (!ospf->instance && IS_DEBUG_OSPF_EVENT)
|
||||||
zlog_debug(
|
zlog_debug(
|
||||||
"Packet from [%s] received on link %s"
|
"Packet from [%s] received on link %s but no ospf_interface",
|
||||||
" but no ospf_interface",
|
inet_ntoa(iph->ip_src), ifp->name);
|
||||||
inet_ntoa(iph->ip_src),
|
return OSPF_READ_CONTINUE;
|
||||||
ifp->name);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3082,18 +3074,16 @@ int ospf_read(struct thread *thread)
|
||||||
*/
|
*/
|
||||||
else if (oi->ifp != ifp) {
|
else if (oi->ifp != ifp) {
|
||||||
if (IS_DEBUG_OSPF_EVENT)
|
if (IS_DEBUG_OSPF_EVENT)
|
||||||
flog_warn(
|
flog_warn(EC_OSPF_PACKET,
|
||||||
EC_OSPF_PACKET,
|
|
||||||
"Packet from [%s] received on wrong link %s",
|
"Packet from [%s] received on wrong link %s",
|
||||||
inet_ntoa(iph->ip_src), ifp->name);
|
inet_ntoa(iph->ip_src), ifp->name);
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
} else if (oi->state == ISM_Down) {
|
} else if (oi->state == ISM_Down) {
|
||||||
char buf[2][INET_ADDRSTRLEN];
|
char buf[2][INET_ADDRSTRLEN];
|
||||||
|
|
||||||
flog_warn(
|
flog_warn(
|
||||||
EC_OSPF_PACKET,
|
EC_OSPF_PACKET,
|
||||||
"Ignoring packet from %s to %s received on interface that is "
|
"Ignoring packet from %s to %s received on interface that is down [%s]; interface flags are %s",
|
||||||
"down [%s]; interface flags are %s",
|
|
||||||
inet_ntop(AF_INET, &iph->ip_src, buf[0],
|
inet_ntop(AF_INET, &iph->ip_src, buf[0],
|
||||||
sizeof(buf[0])),
|
sizeof(buf[0])),
|
||||||
inet_ntop(AF_INET, &iph->ip_dst, buf[1],
|
inet_ntop(AF_INET, &iph->ip_dst, buf[1],
|
||||||
|
@ -3106,7 +3096,7 @@ int ospf_read(struct thread *thread)
|
||||||
OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
|
OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
|
||||||
if (oi->multicast_memberships)
|
if (oi->multicast_memberships)
|
||||||
ospf_if_set_multicast(oi);
|
ospf_if_set_multicast(oi);
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -3122,12 +3112,11 @@ int ospf_read(struct thread *thread)
|
||||||
EC_OSPF_PACKET,
|
EC_OSPF_PACKET,
|
||||||
"Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
|
"Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
|
||||||
inet_ntoa(iph->ip_src), IF_NAME(oi),
|
inet_ntoa(iph->ip_src), IF_NAME(oi),
|
||||||
lookup_msg(ospf_ism_state_msg, oi->state,
|
lookup_msg(ospf_ism_state_msg, oi->state, NULL));
|
||||||
NULL));
|
|
||||||
/* Try to fix multicast membership. */
|
/* Try to fix multicast membership. */
|
||||||
SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
|
SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
|
||||||
ospf_if_set_multicast(oi);
|
ospf_if_set_multicast(oi);
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Verify more OSPF header fields. */
|
/* Verify more OSPF header fields. */
|
||||||
|
@ -3138,7 +3127,7 @@ int ospf_read(struct thread *thread)
|
||||||
"ospf_read[%s]: Header check failed, "
|
"ospf_read[%s]: Header check failed, "
|
||||||
"dropping.",
|
"dropping.",
|
||||||
inet_ntoa(iph->ip_src));
|
inet_ntoa(iph->ip_src));
|
||||||
continue;
|
return OSPF_READ_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show debug receiving packet. */
|
/* Show debug receiving packet. */
|
||||||
|
@ -3150,8 +3139,7 @@ int ospf_read(struct thread *thread)
|
||||||
}
|
}
|
||||||
|
|
||||||
zlog_debug("%s received from [%s] via [%s]",
|
zlog_debug("%s received from [%s] via [%s]",
|
||||||
lookup_msg(ospf_packet_type_str, ospfh->type,
|
lookup_msg(ospf_packet_type_str, ospfh->type, NULL),
|
||||||
NULL),
|
|
||||||
inet_ntoa(ospfh->router_id), IF_NAME(oi));
|
inet_ntoa(ospfh->router_id), IF_NAME(oi));
|
||||||
zlog_debug(" src [%s],", inet_ntoa(iph->ip_src));
|
zlog_debug(" src [%s],", inet_ntoa(iph->ip_src));
|
||||||
zlog_debug(" dst [%s]", inet_ntoa(iph->ip_dst));
|
zlog_debug(" dst [%s]", inet_ntoa(iph->ip_dst));
|
||||||
|
@ -3191,6 +3179,33 @@ int ospf_read(struct thread *thread)
|
||||||
IF_NAME(oi), ospf_get_name(ospf), ospfh->type);
|
IF_NAME(oi), ospf_get_name(ospf), ospfh->type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return OSPF_READ_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Starting point of packet process function. */
|
||||||
|
int ospf_read(struct thread *thread)
|
||||||
|
{
|
||||||
|
struct ospf *ospf;
|
||||||
|
int32_t count = 0;
|
||||||
|
enum ospf_read_return_enum ret;
|
||||||
|
|
||||||
|
/* first of all get interface pointer. */
|
||||||
|
ospf = THREAD_ARG(thread);
|
||||||
|
|
||||||
|
/* prepare for next packet. */
|
||||||
|
thread_add_read(master, ospf_read, ospf, ospf->fd, &ospf->t_read);
|
||||||
|
|
||||||
|
while (count < ospf->write_oi_count) {
|
||||||
|
count++;
|
||||||
|
ret = ospf_read_helper(ospf);
|
||||||
|
switch (ret) {
|
||||||
|
case OSPF_READ_ERROR:
|
||||||
|
return -1;
|
||||||
|
break;
|
||||||
|
case OSPF_READ_CONTINUE:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue