Skip to content

Commit 7dc5fea

Browse files
committed
Refactor daemon_unpackapplyfilter() for improved readability and consistency
1 parent dc28ffe commit 7dc5fea

1 file changed

Lines changed: 82 additions & 82 deletions

File tree

rpcapd/daemon.c

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,95 +2336,95 @@ daemon_msg_endcap_req(uint8_t ver, struct daemon_slpars *pars,
23362336
static int
23372337
daemon_unpackapplyfilter(PCAP_SOCKET sockctrl, SSL *ctrl_ssl, struct session *session, uint32_t *plenp, char *errmsgbuf)
23382338
{
2339-
int status;
2340-
struct rpcap_filter filter;
2341-
struct rpcap_filterbpf_insn insn;
2342-
struct bpf_insn *bf_insn = NULL;
2343-
struct bpf_program bf_prog;
2344-
unsigned int i;
2345-
int ret = 0;
2346-
2347-
status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &filter,
2348-
sizeof(struct rpcap_filter), plenp, errmsgbuf);
2349-
if (status == -1)
2350-
{
2351-
return -1;
2352-
}
2353-
if (status == -2)
2354-
{
2355-
return -2;
2356-
}
2357-
2358-
bf_prog.bf_len = ntohl(filter.nitems);
2359-
2360-
if (ntohs(filter.filtertype) != RPCAP_UPDATEFILTER_BPF)
2361-
{
2362-
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Only BPF/NPF filters are currently supported");
2363-
return -2;
2364-
}
2365-
2366-
if (bf_prog.bf_len > RPCAP_BPF_MAXINSNS)
2367-
{
2368-
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE,
2369-
"Filter program is larger than the maximum size of %d instructions",
2370-
RPCAP_BPF_MAXINSNS);
2371-
return -2;
2372-
}
2373-
bf_insn = (struct bpf_insn *) malloc (sizeof(struct bpf_insn) * bf_prog.bf_len);
2374-
if (bf_insn == NULL)
2375-
{
2376-
pcapint_fmt_errmsg_for_errno(errmsgbuf, PCAP_ERRBUF_SIZE,
2377-
errno, "malloc() failed");
2378-
return -2;
2379-
}
2380-
2381-
bf_prog.bf_insns = bf_insn;
2382-
2383-
for (i = 0; i < bf_prog.bf_len; i++)
2384-
{
2385-
status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &insn,
2386-
sizeof(struct rpcap_filterbpf_insn), plenp, errmsgbuf);
2387-
if (status == -1)
2388-
{
2389-
ret = -1;
2390-
goto cleanup;
2391-
}
2392-
if (status == -2)
2393-
{
2394-
ret = -2;
2395-
goto cleanup;
2396-
}
2397-
2398-
bf_insn[i].code = ntohs(insn.code);
2399-
bf_insn[i].jf = insn.jf;
2400-
bf_insn[i].jt = insn.jt;
2401-
bf_insn[i].k = ntohl(insn.k);
2402-
}
2339+
int status;
2340+
struct rpcap_filter filter;
2341+
struct rpcap_filterbpf_insn insn;
2342+
struct bpf_insn *bf_insn = NULL;
2343+
struct bpf_program bf_prog;
2344+
unsigned int i;
2345+
int ret = 0;
2346+
2347+
status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &filter,
2348+
sizeof(struct rpcap_filter), plenp, errmsgbuf);
2349+
if (status == -1)
2350+
{
2351+
return -1;
2352+
}
2353+
if (status == -2)
2354+
{
2355+
return -2;
2356+
}
2357+
2358+
bf_prog.bf_len = ntohl(filter.nitems);
2359+
2360+
if (ntohs(filter.filtertype) != RPCAP_UPDATEFILTER_BPF)
2361+
{
2362+
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Only BPF/NPF filters are currently supported");
2363+
return -2;
2364+
}
2365+
2366+
if (bf_prog.bf_len > RPCAP_BPF_MAXINSNS)
2367+
{
2368+
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE,
2369+
"Filter program is larger than the maximum size of %d instructions",
2370+
RPCAP_BPF_MAXINSNS);
2371+
return -2;
2372+
}
2373+
bf_insn = (struct bpf_insn *) malloc (sizeof(struct bpf_insn) * bf_prog.bf_len);
2374+
if (bf_insn == NULL)
2375+
{
2376+
pcapint_fmt_errmsg_for_errno(errmsgbuf, PCAP_ERRBUF_SIZE,
2377+
errno, "malloc() failed");
2378+
return -2;
2379+
}
2380+
2381+
bf_prog.bf_insns = bf_insn;
2382+
2383+
for (i = 0; i < bf_prog.bf_len; i++)
2384+
{
2385+
status = rpcapd_recv(sockctrl, ctrl_ssl, (char *) &insn,
2386+
sizeof(struct rpcap_filterbpf_insn), plenp, errmsgbuf);
2387+
if (status == -1)
2388+
{
2389+
ret = -1;
2390+
goto cleanup;
2391+
}
2392+
if (status == -2)
2393+
{
2394+
ret = -2;
2395+
goto cleanup;
2396+
}
2397+
2398+
bf_insn[i].code = ntohs(insn.code);
2399+
bf_insn[i].jf = insn.jf;
2400+
bf_insn[i].jt = insn.jt;
2401+
bf_insn[i].k = ntohl(insn.k);
2402+
}
24032403

24042404
//
24052405
// XXX - pcap_setfilter() should do the validation for us.
24062406
//
2407-
if (bpf_validate(bf_prog.bf_insns, bf_prog.bf_len) == 0)
2408-
{
2409-
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "The filter contains invalid instructions");
2410-
ret = -2;
2411-
goto cleanup;
2412-
}
2413-
2414-
if (pcap_setfilter(session->fp, &bf_prog))
2415-
{
2416-
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "RPCAP error: %s", pcap_geterr(session->fp));
2417-
ret = -2;
2418-
goto cleanup;
2419-
}
2420-
2421-
ret = 0;
2407+
if (bpf_validate(bf_prog.bf_insns, bf_prog.bf_len) == 0)
2408+
{
2409+
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "The filter contains invalid instructions");
2410+
ret = -2;
2411+
goto cleanup;
2412+
}
2413+
2414+
if (pcap_setfilter(session->fp, &bf_prog))
2415+
{
2416+
snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "RPCAP error: %s", pcap_geterr(session->fp));
2417+
ret = -2;
2418+
goto cleanup;
2419+
}
2420+
2421+
ret = 0;
24222422

24232423
cleanup:
2424-
if (ret != 0 && bf_insn != NULL)
2425-
free(bf_insn);
2424+
if (ret != 0 && bf_insn != NULL)
2425+
free(bf_insn);
24262426

2427-
return ret;
2427+
return ret;
24282428
}
24292429

24302430
static int

0 commit comments

Comments
 (0)