summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-16 21:51:45 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-16 21:51:45 +0200
commit4f727471762d47a0d5690c18a98bb3682ea901b0 (patch)
treec00a55f9dad472504729503031e81aba9c03b957
parent1403daed9fea438a0a5d03c3efeeb414854c66d4 (diff)
Convert igmp to use debugging
-rw-r--r--igmp.c116
-rw-r--r--main.c3
-rw-r--r--main.h1
3 files changed, 61 insertions, 59 deletions
diff --git a/igmp.c b/igmp.c
index ab55aef..1c44769 100644
--- a/igmp.c
+++ b/igmp.c
@@ -170,7 +170,7 @@ igmp_match()
struct tm *tm = localtime(&t);
char s[64];
strftime(s, sizeof(s), "%c", tm);
- fprintf(stderr, " * Multicast request discovered at: %s\n", s);
+ debug(DBG_IGMP, "multicast request discovered at: %s\n", s);
//start announce
}
@@ -215,7 +215,7 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
switch (igmp_msg->common.type) {
case IGMP_V1_MEMBERSHIP_REPORT:
- fprintf(stderr, "%s: igmp_v1_membership_report\n", __func__);
+ debug(DBG_IGMP, "igmp_v1_membership_report\n");
/* fall through */
case IGMP_V2_MEMBERSHIP_REPORT: {
@@ -233,29 +233,29 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
grp.s_addr = htonl(igmp_msg->v2.addr);
inet_ntop(AF_INET, &grp, grp_str, sizeof(grp_str));
- fprintf(stderr, "%s: igmp_v2_membership_report %s -> %s (%s)\n",
- __func__, src_str, dst_str, grp_str);
+ debug(DBG_IGMP, "igmp_v2_membership_report %s -> %s (%s)\n",
+ src_str, dst_str, grp_str);
if (body_len != IGMP_MIN_LEN) {
- fprintf(stderr, "IGMPv2 invalid size\n");
+ error("IGMPv2 invalid size\n");
break;
}
if (!csum_valid((char *)igmp_msg, body_len)) {
- fprintf(stderr, "IGMPv2 invalid checksum\n");
+ error("IGMPv2 invalid checksum\n");
break;
}
- fprintf(stderr, "Inet addr: 0x%x\n", inet_addr("224.0.2.60"));
- fprintf(stderr, "Inet addr: 0x%x\n", cinet_addr(224,0,2,60));
- fprintf(stderr, "Inet addr: 0x%x\n", chtobe32(cinet_addr(224,0,2,60)));
+ debug(DBG_IGMP, "Inet addr: 0x%x\n", inet_addr("224.0.2.60"));
+ debug(DBG_IGMP, "Inet addr: 0x%x\n", cinet_addr(224,0,2,60));
+ debug(DBG_IGMP, "Inet addr: 0x%x\n", chtobe32(cinet_addr(224,0,2,60)));
if (htonl(hdr->dst) != cinet_addr(224,0,2,60)) {
- fprintf(stderr, "IGMPv2 invalid dst addr\n");
+ debug(DBG_IGMP, "IGMPv2 invalid dst addr\n");
break;
}
if (htonl(igmp_msg->v2.addr) != cinet_addr(224,0,2,60)) {
- fprintf(stderr, "IGMPv2 invalid grp addr\n");
+ debug(DBG_IGMP, "IGMPv2 invalid grp addr\n");
break;
}
@@ -275,30 +275,32 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
dst.s_addr = htonl(hdr->dst);
inet_ntop(AF_INET, &dst, dst_str, sizeof(dst_str));
- fprintf(stderr, "%s: igmp_v3_membership_report %s -> %s\n",
- __func__, src_str, dst_str);
-
- fprintf(stderr, "IGMPv3\n"
- " * Type: %x\n"
- " * Reserved: %u\n"
- " * Csum: %u\n"
- " * Reserved: %u\n"
- " * NRecs: %u\n"
- " * Size: %zu bytes\n",
- igmp_msg->v3.type,
- igmp_msg->v3.reserved1,
- igmp_msg->v3.checksum,
- igmp_msg->v3.reserved2,
- igmp_msg->v3.nrecs,
- sizeof(igmp_msg->v3));
+ debug(DBG_IGMP, "igmp_v3_membership_report %s -> %s\n",
+ src_str, dst_str);
+
+ debug(DBG_IGMP, "IGMPv3\n"
+ " * Type: %x\n"
+ " * Reserved: %u\n"
+ " * Csum: %u\n"
+ " * Reserved: %u\n"
+ " * NRecs: %u\n"
+ " * Size: %zu bytes\n",
+ igmp_msg->v3.type,
+ igmp_msg->v3.reserved1,
+ igmp_msg->v3.checksum,
+ igmp_msg->v3.reserved2,
+ igmp_msg->v3.nrecs,
+ sizeof(igmp_msg->v3));
if (!csum_valid(pos, body_len)) {
- fprintf(stderr, "IGMPv3 csum invalid\n");
+ error("IGMPv3 csum invalid\n");
break;
}
- if (htonl(hdr->dst) != cinet_addr(224,0,0,22))
+ if (htonl(hdr->dst) != cinet_addr(224,0,0,22)) {
+ debug(DBG_IGMP, "IGMPv2 invalid dst addr\n");
break;
+ }
body_len -= sizeof(igmp_msg->v3);
pos += sizeof(igmp_msg->v3);
@@ -309,32 +311,31 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
char grp_str[INET_ADDRSTRLEN];
if (body_len < sizeof(*record)) {
- fprintf(stderr, "IGMPv3 too short\n");
+ error("IGMPv3 too short\n");
break;
}
grp.s_addr = htonl(record->addr);
inet_ntop(AF_INET, &grp, grp_str, sizeof(grp_str));
- fprintf(stderr, "%s: received IGMPv3 record to %s\n",
- __func__, grp_str);
-
- fprintf(stderr, "IGMPv3 rec\n"
- " * Type: %u\n"
- " * Auxlen: %u\n"
- " * NSrcs: %u\n"
- " * Addr: %s\n"
- " * Size: %zu bytes\n",
- record->type,
- record->auxlen,
- record->nsrcs,
- grp_str,
- sizeof(*record));
+ debug(DBG_IGMP, "received IGMPv3 record to %s\n", grp_str);
+
+ debug(DBG_IGMP, "IGMPv3 rec\n"
+ " * Type: %u\n"
+ " * Auxlen: %u\n"
+ " * NSrcs: %u\n"
+ " * Addr: %s\n"
+ " * Size: %zu bytes\n",
+ record->type,
+ record->auxlen,
+ record->nsrcs,
+ grp_str,
+ sizeof(*record));
body_len -= sizeof(*record);
pos += sizeof(*record);
if (body_len < record->nsrcs * sizeof(uint32_t) + record->auxlen) {
- fprintf(stderr, "IGMPv3 too short\n");
+ error("IGMPv3 too short\n");
break;
}
@@ -344,8 +345,8 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
grp_src.s_addr = htonl(record->saddr[addr]);
inet_ntop(AF_INET, &grp_src, grp_src_str, sizeof(grp_src_str));
- fprintf(stderr, "%s: received IGMPv3 record src %s\n",
- __func__, grp_src_str);
+ debug(DBG_IGMP, "received IGMPv3 record src %s\n",
+ grp_src_str);
body_len -= sizeof(record->saddr[addr]);
pos += sizeof(record->saddr[addr]);
@@ -364,15 +365,15 @@ igmp_parse(struct cfg *cfg, struct igmp *igmp)
}
case IGMP_MEMBERSHIP_QUERY:
- fprintf(stderr, "%s: igmp_membership_query\n", __func__);
+ debug(DBG_IGMP, "igmp_membership_query\n");
break;
case IGMP_V2_LEAVE_GROUP:
- fprintf(stderr, "%s: igmp_v2_leave_group\n", __func__);
+ debug(DBG_IGMP, "igmp_v2_leave_group\n");
break;
default:
- fprintf(stderr, "%s: IGMP msg type %02hhx\n", __func__, igmp_msg->common.type);
+ debug(DBG_IGMP, "IGMP msg type %02hhx\n", igmp_msg->common.type);
break;
}
@@ -385,8 +386,7 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res)
{
struct igmp *igmp = container_of(task, struct igmp, task);
- fprintf(stderr, "%s: called (task %p, igmp %p, res %i)\n",
- __func__, task, igmp, res);
+ debug(DBG_IGMP, "task %p, igmp %p, res %i\n", task, igmp, res);
if (res < 0 || task->dead)
return;
@@ -397,7 +397,7 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res)
task->addr.ll.sll_protocol == htons(ETH_P_IP))
igmp_parse(cfg, igmp);
else
- fprintf(stderr, "Invalid packet type received\n");
+ debug(DBG_IGMP, "invalid packet type received\n");
uring_tbuf_read(cfg, &igmp->task, igmp_read_cb);
}
@@ -407,9 +407,7 @@ igmp_free(struct uring_task *task)
{
struct igmp *igmp = container_of(task, struct igmp, task);
- fprintf(stderr, "%s: called (task %p, igmp %p)\n",
- __func__, task, igmp);
-
+ debug(DBG_IGMP, "task %p, igmp %p\n", task, igmp);
xfree(igmp);
}
@@ -430,7 +428,7 @@ igmp_delete(struct cfg *cfg)
if (!igmp)
return;
- fprintf(stderr, "%s called, closing fd %i\n", __func__, igmp->task.fd);
+ debug(DBG_IGMP, "closing fd %i\n", igmp->task.fd);
uring_task_destroy(cfg, &igmp->task);
cfg->igmp = NULL;
}
@@ -506,7 +504,7 @@ igmp_init(struct cfg *cfg)
}
if (setsockopt(sfd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
- perror("igmp setsockopt");
+ error("igmp setsockopt: %m");
goto out_fd;
}
@@ -539,7 +537,7 @@ igmp_init(struct cfg *cfg)
/* can't set .sll_protocol to htons(ETH_P_IP), see comment above */
if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- perror("bind");
+ error("bind: %m");
goto out_fd;
}
diff --git a/main.c b/main.c
index 0fa4ea8..d7d31a5 100644
--- a/main.c
+++ b/main.c
@@ -113,6 +113,9 @@ const struct {
.name = "idle",
.val = DBG_IDLE
},{
+ .name = "igmp",
+ .val = DBG_IGMP
+ },{
.name = NULL,
.val = 0
}
diff --git a/main.h b/main.h
index 20fc231..132e81a 100644
--- a/main.h
+++ b/main.h
@@ -26,6 +26,7 @@ enum debug_category {
DBG_PROXY = (0x1 << 10),
DBG_RCON = (0x1 << 11),
DBG_IDLE = (0x1 << 12),
+ DBG_IGMP = (0x1 << 13),
};
static inline bool