static bool session_exists(struct session_entry *session) { struct session_foreach_func func = { .cb = compare_session_foreach_cb, .arg = session, }; /* This is the closest we currently have to a find_session function. */ return bib_foreach_session(db, session->proto, &func, NULL); }
static bool assert_session_count(int expected, l4_protocol proto) { int counter = 0; struct session_foreach_func cb = { .cb = session_count_fn, .arg = &counter, }; bool success = true; success &= ASSERT_INT(0, bib_foreach_session(&jool, proto, &cb, NULL), "foreach result"); success &= ASSERT_INT(expected, counter, "computed count"); return success; }
static int session_exists(struct session_entry *session) { struct session_foreach_func func = { .cb = compare_session_foreach_cb, .arg = session, }; /* * This is the closest we have to a session finding function in the * current API. */ return bib_foreach_session(&jool, session->proto, &func, NULL); }
static int handle_session_display(struct bib *db, struct genl_info *info, struct request_session *request) { struct nlcore_buffer buffer; struct session_foreach_func func = { .cb = session_entry_to_userspace, .arg = &buffer, }; struct session_foreach_offset offset_struct; struct session_foreach_offset *offset = NULL; int error; if (verify_superpriv()) return nlcore_respond(info, -EPERM); log_debug("Sending session table to userspace."); error = nlbuffer_init_response(&buffer, info, nlbuffer_response_max_size()); if (error) return nlcore_respond(info, error); if (request->display.offset_set) { offset_struct.offset = request->display.offset; offset_struct.include_offset = false; offset = &offset_struct; } error = bib_foreach_session(db, request->l4_proto, &func, offset); nlbuffer_set_pending_data(&buffer, error > 0); error = (error >= 0) ? nlbuffer_send(info, &buffer) : nlcore_respond(info, error); nlbuffer_free(&buffer); return error; }
static bool test_foreach(void) { struct unit_iteration_args args; struct session_foreach_func func = { .cb = cb, .arg = &args, }; struct session_foreach_offset offset; int error; bool success = true; offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007102u);/* 203.0.113.2 */ offset.offset.src.l4 = 200; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000202u);/* 192.0.2.2 */ offset.offset.dst.l4 = 1200; /* Empty table, no offset. */ args.i = 0; args.offset = 0; error = bib_foreach_session(db, L4PROTO_UDP, &func, NULL); success &= ASSERT_INT(0, error, "call 1 result"); success &= ASSERT_UINT(0, args.i, "call 1 counter"); /* Empty table, offset, include offset, offset not found. */ args.i = 0; args.offset = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 2 result"); success &= ASSERT_UINT(0, args.i, "call 2 counter"); /* Empty table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 3 result"); success &= ASSERT_UINT(0, args.i, "call 3 counter"); /* ----------------------------------- */ if (!insert_test_sessions()) return false; /* Populated table, no offset. */ args.i = 0; args.offset = 0; error = bib_foreach_session(db, L4PROTO_UDP, &func, NULL); success &= ASSERT_INT(0, error, "call 4 result"); success &= ASSERT_UINT(9, args.i, "call 4 counter"); /* Populated table, offset, include offset, offset found. */ args.i = 0; args.offset = 4; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 5 result"); success &= ASSERT_UINT(5, args.i, "call 5 counter"); /* Populated table, offset, include offset, offset not found. */ args.i = 0; args.offset = 5; offset.include_offset = true; offset.offset.dst.l4 = 1250; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 6 result"); success &= ASSERT_UINT(4, args.i, "call 6 counter"); /* Populated table, offset, do not include offset, offset found. */ args.i = 0; args.offset = 5; offset.include_offset = false; offset.offset.dst.l4 = 1200; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 7 result"); success &= ASSERT_UINT(4, args.i, "call 7 counter"); /* Populated table, offset, do not include offset, offset not found. */ args.i = 0; args.offset = 5; offset.include_offset = false; offset.offset.dst.l4 = 1250; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 8 result"); success &= ASSERT_UINT(4, args.i, "call 8 counter"); /* ----------------------------------- */ /* Offset is before first, include offset. */ offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007101u);/* 203.0.113.1 */ offset.offset.src.l4 = 300; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000203u);/* 192.0.2.3 */ offset.offset.dst.l4 = 1200; args.i = 0; args.offset = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 9 result"); success &= ASSERT_UINT(9, args.i, "call 9 counter"); /* Offset is before first, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 10 result"); success &= ASSERT_UINT(9, args.i, "call 10 counter"); /* Offset is first, include offset. */ offset.offset.dst.l4 = 1300; args.i = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 11 result"); success &= ASSERT_UINT(9, args.i, "call 11 counter"); /* Offset is first, do not include offset. */ args.i = 0; args.offset = 1; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 12 result"); success &= ASSERT_UINT(8, args.i, "call 12 counter"); /* Offset is last, include offset. */ offset.offset.src.l3.s_addr = cpu_to_be32(0xcb007103u); /* 203.0.113.3 */ offset.offset.src.l4 = 100; offset.offset.dst.l3.s_addr = cpu_to_be32(0xc0000201u); /* 192.0.2.1 */ offset.offset.dst.l4 = 1100; args.i = 0; args.offset = 8; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 13 result"); success &= ASSERT_UINT(1, args.i, "call 13 counter"); /* Offset is last, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 14 result"); success &= ASSERT_UINT(0, args.i, "call 14 counter"); /* Offset is after last, include offset. */ offset.offset.src.l4 = 1200; args.i = 0; offset.include_offset = true; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 15 result"); success &= ASSERT_UINT(0, args.i, "call 15 counter"); /* Offset is after last, do not include offset. */ args.i = 0; offset.include_offset = false; error = bib_foreach_session(db, L4PROTO_UDP, &func, &offset); success &= ASSERT_INT(0, error, "call 16 result"); success &= ASSERT_UINT(0, args.i, "call 16 counter"); return success; }