From 57e9207c8a809ca8014b1d4a4213ce91b334b493 Mon Sep 17 00:00:00 2001 From: Andrew Elder Date: Fri, 21 Sep 2012 10:25:16 -0400 Subject: [PATCH] mrpq: split client interface from example application. add windows version. --- examples/mrpdclient.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ examples/mrpdclient.h | 45 ++++++++++++++++ examples/mrpq_win.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 316 insertions(+) create mode 100644 examples/mrpdclient.c create mode 100644 examples/mrpdclient.h create mode 100644 examples/mrpq_win.c diff --git a/examples/mrpdclient.c b/examples/mrpdclient.c new file mode 100644 index 0000000..cf61a88 --- /dev/null +++ b/examples/mrpdclient.c @@ -0,0 +1,140 @@ +/****************************************************************************** + + Copyright (c) 2012, Intel Corporation + Copyright (c) 2012, AudioScience, Inc + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the Intel Corporation nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +******************************************************************************/ + +#include +#include +#include + +#if defined WIN32 +#include +#include +typedef int socklen_t; +static int rcv_timeout = 100; /* 100 ms */ +#elif defined __linux__ +#include +typedef int SOCKET; +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#define closesocket(s) close(s); +#define mrpd_send send +static struct timeval rcv_timeout = { + .tv_sec = 0, + .tv_usec = 100 * 1000 /* 100 ms */ +}; +#endif + +#include "mrpd.h" +#include "mrpdclient.h" + +/* global variables */ +static SOCKET control_socket = SOCKET_ERROR; + +int mrpdclient_init(void) { + struct sockaddr_in addr; + SOCKET sock_fd = SOCKET_ERROR; + int rc; + + control_socket = socket(AF_INET, SOCK_DGRAM, 0); + if (control_socket == INVALID_SOCKET) goto out; + + rc = setsockopt(control_socket, SOL_SOCKET, SO_RCVTIMEO, + (const char *)&rcv_timeout, sizeof(rcv_timeout)); + if (rc != NO_ERROR) + goto out; + rc = setsockopt(control_socket, SOL_SOCKET, SO_SNDTIMEO, + (const char *)&rcv_timeout, sizeof(rcv_timeout)); + if (rc != NO_ERROR) + goto out; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(0); + + rc = bind(control_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr)); + if (rc <= SOCKET_ERROR) goto out; + + control_socket = sock_fd; + return(0); + +out: + if (control_socket != SOCKET_ERROR) + closesocket(control_socket); + control_socket = SOCKET_ERROR; + return (-1); +} + +int mprdclient_close(void) +{ + if (control_socket != SOCKET_ERROR) + closesocket(control_socket); + return 0; +} + +int mrpdclient_recv(ptr_process_msg fn) { + char *msgbuf; + int bytes = 0; + + msgbuf = (char *)malloc(MAX_FRAME_SIZE); + if (NULL == msgbuf) + return -1; + + memset(msgbuf, 0, MAX_FRAME_SIZE); + bytes = recv(control_socket, msgbuf, MAX_FRAME_SIZE, 0); + if (bytes <= SOCKET_ERROR) { + goto out; + } + + return fn(msgbuf, bytes); +out: + free (msgbuf); + return(-1); +} + +int mprdclient_sendto( char *notify_data, int notify_len) +{ + struct sockaddr_in addr; + socklen_t addr_len; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(MRPD_PORT_DEFAULT); + addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr_len = sizeof(addr); + + if (control_socket != SOCKET_ERROR) + return(sendto(control_socket, notify_data, notify_len, 0, (struct sockaddr *)&addr, addr_len)); + else + return(0); +} + diff --git a/examples/mrpdclient.h b/examples/mrpdclient.h new file mode 100644 index 0000000..f44f06c --- /dev/null +++ b/examples/mrpdclient.h @@ -0,0 +1,45 @@ +/****************************************************************************** + + Copyright (c) 2012, Intel Corporation + Copyright (c) 2012, AudioScience, Inc + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the Intel Corporation nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +******************************************************************************/ + +#ifndef _MRPDCLIENT_H_ +#define _MRPDCLIENT_H_ + +typedef int (*ptr_process_msg)(char *buf, int buflen); + +int mrpdclient_init(void); +int mrpdclient_recv(ptr_process_msg fn); +int mprdclient_sendto(char *notify_data, int notify_len); +int mprdclient_close(void); + +#endif diff --git a/examples/mrpq_win.c b/examples/mrpq_win.c new file mode 100644 index 0000000..4309cda --- /dev/null +++ b/examples/mrpq_win.c @@ -0,0 +1,131 @@ +/****************************************************************************** + + Copyright (c) 2012, Intel Corporation + Copyright (c) 2012, AudioScience, Inc + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the Intel Corporation nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#if defined WIN32 +#include +#endif + +#include "mrpd.h" +#include "mrpdclient.h" + + +/* global variables */ +#define VERSION_STR "0.0" + +static const char *version_str = +"mrpq v" VERSION_STR "\n" +"Copyright (c) 2012, Intel Corporation\n"; + + +int process_ctl_msg(char *buf, int buflen) { + + /* + * M?? - query MMRP Registrar MAC Address database + * M+? - JOIN_MT a MAC address + * -- note M++ doesn't exist apparently- MMRP doesn't use 'New' -- + * M-- - LV a MAC address + * V?? - query MVRP Registrar VID database + * V++ - JOIN_IN a VID (VLAN ID) + * V+? - JOIN_MT a VID (VLAN ID) + * V-- - LV a VID (VLAN ID) + */ + + /* XXX */ + printf("ret %s\n", buf); + fflush(stdout); + free(buf); + return(0); +} + +void process_events( void ) { + + /* wait for events, demux the received packets, process packets */ +} + +int main(int argc, char *argv[]) +{ + int rc = 0; + char *msgbuf; + +#if defined WIN32 + WSADATA wsa_data; + WSAStartup(MAKEWORD(1,1), &wsa_data); +#endif + + rc = mrpdclient_init(); + if (rc) { + printf("init failed\n"); + return -1; + } + + msgbuf = (char *)malloc(1500); + + memset(msgbuf,0,1500); + sprintf(msgbuf,"M??"); + printf("M??\n"); + rc = mprdclient_sendto(msgbuf, 1500); + rc = mrpdclient_recv(process_ctl_msg); + if (rc <= SOCKET_ERROR) + printf("recv error\n"); + memset(msgbuf,0,1500); + sprintf(msgbuf,"V??"); + printf("V??\n"); + rc = mprdclient_sendto(msgbuf, 1500 ); + rc =mrpdclient_recv(process_ctl_msg); + if (rc <= SOCKET_ERROR) + printf("recv error\n"); + memset(msgbuf,0,1500); + sprintf(msgbuf,"S??"); + printf("S??\n"); + rc = mprdclient_sendto(msgbuf, 1500 ); + rc = mrpdclient_recv(process_ctl_msg); + if (rc <= SOCKET_ERROR) + printf("recv error\n"); + + sprintf(msgbuf,"BYE"); + rc = mprdclient_sendto(msgbuf, 1500 ); + mprdclient_close(); + +#if defined WIN32 + WSACleanup(); +#endif + return(rc); +} -- 2.7.4