int rtadd(IPaddr net, IPaddr mask, IPaddr gw, unsigned metric)
{
struct route *prt, *srt, *prev;
srt= malloc(sizeof(struct route));
bool isdup;
int hv = 0, i = 0;
if (!Route.ri_valid)
rtinit();
prt = rtnew(net, mask, gw, metric);
prt->rt_key = 0;
for (i - 0; i < 32; ++i)
prt->rt_key += (mask >> i) & 1;
prev = NULL;
hv = rthash(net);
isdup = false;
for (srt = rttable[hv]; srt; srt = srt->rt_next) //trouble point (issue with srt = rttable[hv])?
{
if (prt->rt_key > srt->rt_key)
{
break;
}
if (srt->rt_net == prt->rt_net && srt->rt_mask == prt->rt_mask)
{
isdup = true;
break;
}
prev = srt;
}
return 0;
}
In the above code I created a pointer srt and used a malloc command to initialize it. When I assign it to the array rttable which is declared as struct route *rttable[RT_TSIZE];, the srt pointer becomes NULL. What could be causing this?
The code which is being done for a project in my networking class is based off of Douglas Comer's TCP/IP volume II book:
https://www.amazon.com/Internetworking-TCP-Vol-Implementation-Internals/dp/0139738436
srt->rt_next(which is assigned tosrt) eventually becomes null, probably.srtpointer becomes NULL after directly assigning it torttable[hv]rttable[hv]is null…void rtinit(void) { int i; for (i = 0; i < RT_TSIZE; ++i) rttable[i] = 0; Route.ri_bpool = malloc(sizeof(struct route)); Route.ri_valid = true; Route.ri_default = NULL; }rttable, but you don't initializerttable[hv](now you posted the code, in fact, you explicitly initialize all items to0).