Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/netgraph/util.c
Show All 28 Lines | |||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <atf-c.h> | #include <atf-c.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | |||||
#include <string.h> | #include <string.h> | ||||
#include <sys/select.h> | #include <sys/select.h> | ||||
#include <sys/queue.h> | #include <sys/queue.h> | ||||
#include "util.h" | #include "util.h" | ||||
static int cs = -1, ds = -1; | static int cs = -1, ds = -1; | ||||
static ng_error_t error_handling = FAIL; | static ng_error_t error_handling = FAIL; | ||||
#define CHECK(r, x) do { \ | #define CHECK(r, x) do { \ | ||||
if (error_handling == FAIL) \ | if (error_handling == FAIL) \ | ||||
ATF_REQUIRE(x); \ | ATF_REQUIRE(x); \ | ||||
else if(!(x)) \ | else if(!(x)) \ | ||||
return r; \ | return r; \ | ||||
} while(0) | } while(0) | ||||
struct data_handler { | struct data_handler { | ||||
char const *hook; | char const *hook; | ||||
ng_data_handler_t handler; | ng_data_handler_t handler; | ||||
SLIST_ENTRY(data_handler) next; | SLIST_ENTRY(data_handler) next; | ||||
}; | }; | ||||
static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head); | static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head); | ||||
static ng_msg_handler_t msg_handler = NULL; | |||||
static void handle_data(void *ctx); | static void handle_data(void *ctx); | ||||
static void handle_msg(void); | static void handle_msg(void *ctx); | ||||
void | void | ||||
ng_connect(char const *path1, char const *hook1, | ng_connect(char const *path1, char const *hook1, | ||||
char const *path2, char const *hook2) | char const *path2, char const *hook2) | ||||
{ | { | ||||
struct ngm_connect c; | struct ngm_connect c; | ||||
strncpy(c.ourhook, hook1, sizeof(c.ourhook)); | strncpy(c.ourhook, hook1, sizeof(c.ourhook)); | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | |||||
void | void | ||||
ng_send_data(char const *hook, | ng_send_data(char const *hook, | ||||
void const *data, size_t len) | void const *data, size_t len) | ||||
{ | { | ||||
CHECK(, -1 != NgSendData(ds, hook, data, len)); | CHECK(, -1 != NgSendData(ds, hook, data, len)); | ||||
} | } | ||||
void | |||||
ng_register_msg(ng_msg_handler_t proc) { | |||||
msg_handler = proc; | |||||
} | |||||
static void | static void | ||||
handle_msg(void) { | handle_msg(void *ctx) { | ||||
struct ng_mesg *m; | struct ng_mesg *m; | ||||
char path[NG_PATHSIZ]; | char path[NG_PATHSIZ]; | ||||
ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path)); | ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path)); | ||||
printf("Got message from %s\n", path); | if(msg_handler != NULL) | ||||
(*msg_handler)(path, m, ctx); | |||||
free(m); | free(m); | ||||
} | } | ||||
static void | static void | ||||
handle_data(void *ctx) { | handle_data(void *ctx) { | ||||
char hook[NG_HOOKSIZ]; | char hook[NG_HOOKSIZ]; | ||||
struct data_handler *hnd; | struct data_handler *hnd; | ||||
u_char *data; | u_char *data; | ||||
Show All 24 Lines | retry: | ||||
switch (select(maxfd+1, &fds, NULL, NULL, &timeout)) { | switch (select(maxfd+1, &fds, NULL, NULL, &timeout)) { | ||||
case -1: | case -1: | ||||
ATF_REQUIRE_ERRNO(EINTR, 1); | ATF_REQUIRE_ERRNO(EINTR, 1); | ||||
goto retry; | goto retry; | ||||
case 0: /* timeout */ | case 0: /* timeout */ | ||||
return 0; | return 0; | ||||
default: /* something to do */ | default: /* something to do */ | ||||
if (FD_ISSET(cs, &fds)) | if (FD_ISSET(cs, &fds)) | ||||
handle_msg(); | handle_msg(context); | ||||
if (FD_ISSET(ds, &fds)) | if (FD_ISSET(ds, &fds)) | ||||
handle_data(context); | handle_data(context); | ||||
return 1; | return 1; | ||||
} | } | ||||
} | } | ||||
void | void | ||||
ng_handle_events(unsigned int ms, void *context) | ng_handle_events(unsigned int ms, void *context) | ||||
Show All 31 Lines |