Skip to content

Commit

Permalink
Fix route.sh and some misc cleanup
Browse files Browse the repository at this point in the history
Check for fp NULL, review comment by Serge.

Signed-off-by: Anjali Kulkarni <[email protected]>
  • Loading branch information
anjalidk committed Feb 5, 2024
1 parent 41b251a commit aa317f5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 53 deletions.
35 changes: 23 additions & 12 deletions net_arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ static int handle_arp_resp(int net_sock, void **out)
struct rtattr *at[NDA_MAX + 1], *rta;
struct arp_info *arps = NULL, *iarps = NULL;
struct sockaddr_nl nladdr;
unsigned int type, family;
#ifdef TESTING
FILE *fp = NULL;
fp = fopen ("./arp_info.txt", "w");
if (!fp)
FILE *fp;
fp = fopen("./arp_info.txt", "w");
if (!fp) {
eprintf("Error opening file arp_info.txt with errno: %d", errno);
return -1;
}
#endif

msg.msg_name = &nladdr;
Expand Down Expand Up @@ -239,8 +242,7 @@ static int handle_arp_resp(int net_sock, void **out)
ecount++;
free(buf);
free(arps);
errno = nlmsg_err->error;
ret = -1;
ret = -(nlmsg_err->error);
goto out;
} else if (r->nlmsg_type == NLMSG_DONE) {
#ifdef PRINTLOGS
Expand All @@ -265,24 +267,33 @@ static int handle_arp_resp(int net_sock, void **out)
len -= NLMSG_LENGTH(sizeof(*nd_msg));
if (len < 0) {
eprintf("nlmsg_len incorrect");
errno = EINVAL;
free(buf);
free(arps);
ret = -1;
ret = -EINVAL;
goto out;
}
#ifdef PRINTLOGS
printf("len after sub %ld is %d\n",sizeof(*nd_msg), len);
#endif
rta = ((struct rtattr *) (((char *) (nd_msg)) + NLMSG_ALIGN(sizeof(struct ndmsg))));
parse_attr(at, rta, len);
family = nd_msg->ndm_family;
type = nd_msg->ndm_type;
if (family == AF_INET || family == AF_INET6) {
if ((type == RTN_BROADCAST) ||
(type == RTN_MULTICAST) ||
(type == RTN_LOCAL)) {
r = NLMSG_NEXT(r, recvl);
continue;
}
#ifdef TESTING
get_attr(at, iarps, nd_msg, fp);
get_attr(at, iarps, nd_msg, fp);
#else
get_attr(at, iarps, nd_msg);
get_attr(at, iarps, nd_msg);
#endif
iarps++;
aind++;
iarps++;
aind++;
}
r = NLMSG_NEXT(r, recvl);
count++;
}
Expand Down Expand Up @@ -314,7 +325,7 @@ int get_net_arp(void **out)
}

err = handle_arp_resp(net_sock, out);
if (err == -1) {
if (err < 0) {
close(net_sock);
return -1;
}
Expand Down
56 changes: 35 additions & 21 deletions net_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static int send_route_req(int net_sock)
#ifdef TESTING
#define IPA_F "%hhu.%hhu.%hhu.%hhu"
#define IPA_V(str) str[0], str[1], str[2], str[3]
#define V4_PREFIX_LEN 32

static inline bool is_zero(char *str, int len)
{
Expand Down Expand Up @@ -118,7 +119,8 @@ static void print_rt_info(struct rt_info *rt, FILE *fp)
fprintf(fp, "default");
} else {
fprintf(fp, IPA_F, IPA_V(rt->dest));
fprintf(fp, "/%02hhu", rt->dst_prefix_len);
if (rt->dst_prefix_len != V4_PREFIX_LEN)
fprintf(fp, "/%02hhu", rt->dst_prefix_len);
}
if (!is_zero(rt->gate, MAX_BYTES_IPV4)) {
fprintf(fp, " via ");
Expand All @@ -132,7 +134,10 @@ static void print_rt_info(struct rt_info *rt, FILE *fp)
fprintf(fp, " src ");
fprintf(fp, IPA_F, IPA_V(rt->prefsrc));
}
fprintf(fp, " metric %d \n", rt->metric);
if (rt->metric != 0)
fprintf(fp, " metric %d \n", rt->metric);
else
fprintf(fp, " \n");
}
}
#endif
Expand Down Expand Up @@ -248,8 +253,16 @@ static int handle_route_resp(int net_sock, void **out)
struct rtattr *at[RTA_MAX + 1];
struct rt_info *routes = NULL, *iroutes = NULL;
struct sockaddr_nl nladdr;
unsigned int family;
int ret = 0;

#ifdef TESTING
FILE *fp = NULL;
FILE *fp;
fp = fopen("./route_info.txt", "w");
if (!fp) {
eprintf("Error opening file route_info.txt with errno: %d", errno);
return -1;
}
#endif

memset(&msg, 0, sizeof(msg));
Expand Down Expand Up @@ -309,19 +322,16 @@ static int handle_route_resp(int net_sock, void **out)
ecount++;
free(buf);
free(routes);
errno = nlmsg_err->error;
return -1;
ret = -(nlmsg_err->error);
goto out;
} else if (r->nlmsg_type == NLMSG_DONE) {
#ifdef PRINTLOGS
printf("DONE\n");
#endif
free(buf);
*(struct rt_info **)out = routes;
#ifdef TESTING
if (fp)
fclose(fp);
#endif
return rtind;
ret = rtind;
goto out;
}
rt = (struct rtmsg *)NLMSG_DATA(r);
len = r->nlmsg_len;
Expand All @@ -338,31 +348,33 @@ static int handle_route_resp(int net_sock, void **out)
len -= NLMSG_LENGTH(sizeof(*rt));
if (len < 0) {
eprintf("nlmsg_len incorrect");
errno = EINVAL;
free(buf);
free(routes);
return -1;
ret = -EINVAL;
goto out;
}
#ifdef PRINTLOGS
printf("len after sub %lu is %d\n",sizeof(*rt), len);
#endif
parse_attr(at, RTM_RTA(rt), len);
if (rt->rtm_family == AF_INET || rt->rtm_family == AF_INET6) {
family = rt->rtm_family;
if (family == AF_INET || family == AF_INET6) {
if ((rt->rtm_type == RTN_BROADCAST) ||
(rt->rtm_type == RTN_MULTICAST))
(rt->rtm_type == RTN_MULTICAST) ||
(rt->rtm_type == RTN_LOCAL)) {
r = NLMSG_NEXT(r, recvl);
continue;
}

if (rt->rtm_type == RTN_UNREACHABLE)
fprintf(fp, "RTN_UNREACHABLE \n");

get_attr(at, iroutes, rt);
#ifdef TESTING
fp = fopen ("./route_info.txt", "w");
print_rt_info(iroutes, fp);
#endif
iroutes++;
rtind++;
} else {
#ifdef PRINTLOGS
printf("Family is %d\n",rt->rtm_family);
#endif
}
r = NLMSG_NEXT(r, recvl);
count++;
Expand All @@ -373,11 +385,13 @@ static int handle_route_resp(int net_sock, void **out)
#endif
}
*(struct rt_info **)out = routes;
ret = rtind;
out:
#ifdef TESTING
if (fp)
fclose(fp);
#endif
return rtind;
return ret;
}

int get_net_route(void **out)
Expand All @@ -394,7 +408,7 @@ int get_net_route(void **out)
}

err = handle_route_resp(net_sock, out);
if (err == -1) {
if (err < 0) {
close(net_sock);
return -1;
}
Expand Down
20 changes: 0 additions & 20 deletions resmem_cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ int get_info_infile(char *fname, char *res, void *out)
return 0;
}

/*static inline void scan_mem_str(str, info, res)
{
char *loc = NULL;
loc = strstr(buf, str);
if (loc == NULL) {
eprintf("%s is not found in file %s", str, MEMINFO_FILE);
res->res_unit[i]->status = ENODATA;
} else {
sscanf(loc, "%*s%zu", &info);
(res->res_unit[i]->data).sz = info;
res->res_unit[i]->status = RES_STATUS_FILLED;
}
}*/

/* read information from a cgroup file.
*/
static inline size_t cgmemread(char *cg, char *name, char *elem)
Expand Down Expand Up @@ -182,8 +167,6 @@ int getmeminfo_cg(int res_id, void *out, size_t sz, void **hint, int pid, int fl
int populate_meminfo_cg(res_blk_t *res, int pid, int flags)
{
char *cg;
char buf[MEMBUF_2048];
int err;

cg = get_cgroup(pid, MEMCGNAME);
if (!cg) {
Expand All @@ -193,9 +176,6 @@ int populate_meminfo_cg(res_blk_t *res, int pid, int flags)

clean_init(cg);

if ((err = file_to_buf(MEMINFO_FILE, buf, MEMBUF_2048)) < 0)
return err;

for (int i = 0; i < res->res_count; i++) {
switch (res->res_unit[i]->res_id) {
case RES_MEM_FREE:
Expand Down
2 changes: 2 additions & 0 deletions tests/ROUTE/route_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ int main(int argc, char **argv)
exit(1);
}
rtn = rt;
#ifdef PRINTLOGS
for (int i=0;i<nroutes; i++) {
if (rt->dst_prefix_len != 0) {
printf("%hhu.%hhu.%hhu.%hhu/%02hhu\n",rt->dest[0], rt->dest[1], rt->dest[2], rt->dest[3], rt->dst_prefix_len);
}
rt++;
}
#endif
if (rtn)
free(rtn);
exit(0);
Expand Down

0 comments on commit aa317f5

Please sign in to comment.