LibCTDB
[sahlberg/ctdb.git] / ib / ibwrapper_test.c
index dc07a1c313bdaacc3b5038264443d2ca27bd678b..eb0d58858ff745e01159550c79100c1680c98c08 100644 (file)
@@ -8,7 +8,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdlib.h>
@@ -38,7 +37,7 @@
 #include <time.h>
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "ib/ibwrapper.h"
 
 struct ibwtest_ctx {
@@ -81,16 +80,18 @@ enum testopcode {
 
 int ibwtest_connect_everybody(struct ibwtest_ctx *tcx)
 {
-       struct ibwtest_conn     *pconn = talloc_zero(tcx, struct ibwtest_conn);
+       struct ibw_conn         *conn;
+       struct ibwtest_conn     *tconn = talloc_zero(tcx, struct ibwtest_conn);
        int     i;
 
        for(i=0; i<tcx->naddrs; i++) {
-               if (ibw_connect(tcx->ibwctx, &tcx->addrs[i], pconn)) {
+               conn = ibw_conn_new(tcx->ibwctx, tconn);
+               if (ibw_connect(conn, &tcx->addrs[i], tconn)) {
                        fprintf(stderr, "ibw_connect error at %d\n", i);
                        return -1;
                }
        }
-       DEBUG(10, ("sent %d connect request...\n", tcx->naddrs));
+       DEBUG(DEBUG_DEBUG, ("sent %d connect request...\n", tcx->naddrs));
 
        return 0;
 }
@@ -102,19 +103,20 @@ int ibwtest_send_id(struct ibw_conn *conn)
        void *key;
        uint32_t        len;
 
-       DEBUG(10, ("ibwtest_send_id\n"));
+       DEBUG(DEBUG_DEBUG, ("ibwtest_send_id\n"));
        len = sizeof(uint32_t)+strlen(tcx->id)+2;
        if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
-               DEBUG(0, ("send_id: ibw_alloc_send_buf failed\n"));
+               DEBUG(DEBUG_ERR, ("send_id: ibw_alloc_send_buf failed\n"));
                return -1;
        }
 
        /* first sizeof(uint32_t) size bytes are for length */
+       *((uint32_t *)buf) = len;
        buf[sizeof(uint32_t)] = (char)TESTOP_SEND_ID;
        strcpy(buf+sizeof(uint32_t)+1, tcx->id);
 
        if (ibw_send(conn, buf, key, len)) {
-               DEBUG(0, ("send_id: ibw_send error\n"));
+               DEBUG(DEBUG_ERR, ("send_id: ibw_send error\n"));
                return -1;
        }
        tcx->nsent++;
@@ -137,6 +139,7 @@ int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const
                return -1;
        }
 
+       *((uint32_t *)buf) = len;
        p = buf;
        p += sizeof(uint32_t);
        p[0] = (char)TESTOP_SEND_TEXT;
@@ -144,7 +147,7 @@ int ibwtest_send_test_msg(struct ibwtest_ctx *tcx, struct ibw_conn *conn, const
        strcpy(p, msg);
 
        if (ibw_send(conn, buf, key, len)) {
-               DEBUG(0, ("send_test_msg: ibw_send error\n"));
+               DEBUG(DEBUG_ERR, ("send_test_msg: ibw_send error\n"));
                return -1;
        }
        tcx->nsent++;
@@ -187,14 +190,15 @@ int ibwtest_do_varsize_scenario_conn_size(struct ibwtest_ctx *tcx, struct ibw_co
 
        len = sizeof(uint32_t) + 1 + size + 1;
        if (ibw_alloc_send_buf(conn, (void **)&buf, &key, len)) {
-               DEBUG(0, ("varsize/ibw_alloc_send_buf failed\n"));
+               DEBUG(DEBUG_ERR, ("varsize/ibw_alloc_send_buf failed\n"));
                return -1;
        }
+       *((uint32_t *)buf) = len;
        buf[sizeof(uint32_t)] = TESTOP_SEND_RND;
        sum = ibwtest_fill_random(buf + sizeof(uint32_t) + 1, size);
        buf[sizeof(uint32_t) + 1 + size] = sum;
        if (ibw_send(conn, buf, key, len)) {
-               DEBUG(0, ("varsize/ibw_send failed\n"));
+               DEBUG(DEBUG_ERR, ("varsize/ibw_send failed\n"));
                return -1;
        }
        tcx->nsent++;
@@ -234,31 +238,31 @@ int ibwtest_do_varsize_scenario_conn(struct ibwtest_ctx *tcx, struct ibw_conn *c
 int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
 {
        struct ibwtest_ctx      *tcx = NULL; /* userdata */
-       struct ibwtest_conn     *pconn = NULL; /* userdata */
+       struct ibwtest_conn     *tconn = NULL; /* userdata */
 
        if (ctx) {
                tcx = talloc_get_type(ctx->ctx_userdata, struct ibwtest_ctx);
 
                switch(ctx->state) {
                case IBWS_INIT:
-                       DEBUG(10, ("test IBWS_INIT\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWS_INIT\n"));
                        break;
                case IBWS_READY:
-                       DEBUG(10, ("test IBWS_READY\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWS_READY\n"));
                        break;
                case IBWS_CONNECT_REQUEST:
-                       DEBUG(10, ("test IBWS_CONNECT_REQUEST\n"));
-                       pconn = talloc_zero(conn, struct ibwtest_conn);
-                       if (ibw_accept(ctx, conn, pconn)) {
-                               DEBUG(0, ("error accepting the connect request\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWS_CONNECT_REQUEST\n"));
+                       tconn = talloc_zero(conn, struct ibwtest_conn);
+                       if (ibw_accept(ctx, conn, tconn)) {
+                               DEBUG(DEBUG_ERR, ("error accepting the connect request\n"));
                        }
                        break;
                case IBWS_STOPPED:
-                       DEBUG(10, ("test IBWS_STOPPED\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWS_STOPPED\n"));
                        tcx->kill_me = 1; /* main loop can exit */
                        break;
                case IBWS_ERROR:
-                       DEBUG(10, ("test IBWS_ERROR\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWS_ERROR\n"));
                        ibw_stop(tcx->ibwctx);
                        break;
                default:
@@ -268,24 +272,24 @@ int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
        }
 
        if (conn) {
-               pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
+               tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
                switch(conn->state) {
                case IBWC_INIT:
-                       DEBUG(10, ("test IBWC_INIT\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWC_INIT\n"));
                        break;
                case IBWC_CONNECTED:
                        if (gettimeofday(&tcx->start_time, NULL)) {
-                               DEBUG(0, ("gettimeofday error %d", errno));
+                               DEBUG(DEBUG_ERR, ("gettimeofday error %d", errno));
                                return -1;
                        }
                        ibwtest_send_id(conn);
                        break;
                case IBWC_DISCONNECTED:
-                       DEBUG(10, ("test IBWC_DISCONNECTED\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWC_DISCONNECTED\n"));
                        talloc_free(conn);
                        break;
                case IBWC_ERROR:
-                       DEBUG(10, ("test IBWC_ERROR\n"));
+                       DEBUG(DEBUG_DEBUG, ("test IBWC_ERROR %s\n", ibw_getLastError()));
                        break;
                default:
                        assert(0);
@@ -297,22 +301,22 @@ int ibwtest_connstate_handler(struct ibw_ctx *ctx, struct ibw_conn *conn)
 
 int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
 {
-       struct ibwtest_conn *pconn;
+       struct ibwtest_conn *tconn;
        enum testopcode op;
        struct ibwtest_ctx *tcx = talloc_get_type(conn->ctx->ctx_userdata, struct ibwtest_ctx);
        int     rc = 0;
 
        assert(conn!=NULL);
        assert(n>=sizeof(uint32_t)+1);
-       pconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
+       tconn = talloc_get_type(conn->conn_userdata, struct ibwtest_conn);
 
        op = (enum testopcode)((char *)buf)[sizeof(uint32_t)];
        if (op==TESTOP_SEND_ID) {
-               pconn->id = talloc_strdup(pconn, ((char *)buf)+sizeof(uint32_t)+1);
+               tconn->id = talloc_strdup(tconn, ((char *)buf)+sizeof(uint32_t)+1);
        }
        if (op==TESTOP_SEND_ID || op==TESTOP_SEND_TEXT) {
-               DEBUG(11, ("[%d]msg from %s: \"%s\"(%d)\n", op,
-                       pconn->id ? pconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
+               DEBUG(DEBUG_DEBUG, ("[%d]msg from %s: \"%s\"(%d)\n", op,
+                       tconn->id ? tconn->id : "NULL", ((char *)buf)+sizeof(uint32_t)+1, n));
        }
 
        if (tcx->is_server) {
@@ -320,30 +324,30 @@ int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
                        unsigned char sum;
                        sum = ibwtest_get_sum((unsigned char *)buf + sizeof(uint32_t) + 1,
                                n - sizeof(uint32_t) - 2);
-                       DEBUG(11, ("[%d]msg varsize %u/sum %u from %s\n",
+                       DEBUG(DEBUG_DEBUG, ("[%d]msg varsize %u/sum %u from %s\n",
                                op,
                                n - sizeof(uint32_t) - 2,
                                (uint32_t)sum,
-                               pconn->id ? pconn->id : "NULL"));
+                               tconn->id ? tconn->id : "NULL"));
                        if (sum!=((unsigned char *)buf)[n-1]) {
-                               DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n",
+                               DEBUG(DEBUG_ERR, ("ERROR: checksum mismatch %u!=%u\n",
                                        (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1]));
                                ibw_stop(tcx->ibwctx);
-                               return -3;
+                               goto error;
                        }
-               } else {
+               } else if (op!=TESTOP_SEND_ID) {
                        char *buf2;
                        void *key2;
 
                        /* bounce message regardless what it is */
                        if (ibw_alloc_send_buf(conn, (void **)&buf2, &key2, n)) {
                                fprintf(stderr, "ibw_alloc_send_buf error #2\n");
-                               return -1;
+                               goto error;
                        }
                        memcpy(buf2, buf, n);
                        if (ibw_send(conn, buf2, key2, n)) {
                                fprintf(stderr, "ibw_send error #2\n");
-                               return -2;
+                               goto error;
                        }
                        tcx->nsent++;
                }
@@ -368,12 +372,14 @@ int ibwtest_receive_handler(struct ibw_conn *conn, void *buf, int n)
                tcx->error = rc;
 
        return rc;
+error:
+       return -1;
 }
 
 void ibwtest_timeout_handler(struct event_context *ev, struct timed_event *te, 
-       struct timeval t, void *private)
+       struct timeval t, void *private_data)
 {
-       struct ibwtest_ctx *tcx = talloc_get_type(private, struct ibwtest_ctx);
+       struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx);
        int     rc;
 
        if (!tcx->is_server) {
@@ -398,14 +404,14 @@ static struct ibwtest_ctx *testctx = NULL;
 
 void ibwtest_sigint_handler(int sig)
 {
-       DEBUG(0, ("got SIGINT\n"));
+       DEBUG(DEBUG_ERR, ("got SIGINT\n"));
        if (testctx) {
                if (testctx->ibwctx->state==IBWS_READY ||
                        testctx->ibwctx->state==IBWS_CONNECT_REQUEST ||
                        testctx->ibwctx->state==IBWS_ERROR)
                {
                        if (testctx->stopping) {
-                               DEBUG(10, ("forcing exit...\n"));
+                               DEBUG(DEBUG_DEBUG, ("forcing exit...\n"));
                                testctx->kill_me = 1;
                        } else {
                                /* mostly expected case */
@@ -459,6 +465,19 @@ int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext,
        return 0;
 }
 
+static int ibwtest_get_address(const char *address, struct in_addr *addr)
+{
+       if (inet_pton(AF_INET, address, addr) <= 0) {
+               struct hostent *he = gethostbyname(address);
+               if (he == NULL || he->h_length > sizeof(*addr)) {
+                       DEBUG(DEBUG_ERR, ("invalid nework address '%s'\n", address));
+                       return -1;
+               }
+               memcpy(addr, he->h_addr, he->h_length);
+       }
+       return 0;
+}
+
 int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
 {
        int     i;
@@ -467,6 +486,7 @@ int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
        char    *tmp;
 
        tmp = talloc_strdup(tcx, optarg);
+       if (tmp == NULL) return -1;
        /* hack to reuse the above ibw_initattr parser */
        if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
                return -1;
@@ -476,7 +496,8 @@ int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
        for(i=0; i<tcx->naddrs; i++) {
                p = tcx->addrs + i;
                p->sin_family = AF_INET;
-               p->sin_addr.s_addr = inet_addr(attrs[i].name);
+               if (ibwtest_get_address(attrs[i].name, &p->sin_addr))
+                       return -1;
                p->sin_port = htons(atoi(attrs[i].value));
        }
 
@@ -491,12 +512,12 @@ int ibwtest_init_server(struct ibwtest_ctx *tcx)
        }
 
        if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) {
-               DEBUG(0, ("ERROR: ibw_bind failed\n"));
+               DEBUG(DEBUG_ERR, ("ERROR: ibw_bind failed\n"));
                return -1;
        }
        
        if (ibw_listen(tcx->ibwctx, 1)) {
-               DEBUG(0, ("ERROR: ibw_listen failed\n"));
+               DEBUG(DEBUG_ERR, ("ERROR: ibw_listen failed\n"));
                return -1;
        }
 
@@ -510,13 +531,14 @@ void ibwtest_usage(struct ibwtest_ctx *tcx, char *name)
        printf("\t%s -i <id> -o {name:value} -d {addr:port} -t nsec -s\n", name);
        printf("\t-i <id> is a free text, acting as a server id, max 23 chars [mandatory]\n");
        printf("\t-o name1:value1,name2:value2,... is a list of (name, value) pairs\n");
-       printf("\t-d addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
+       printf("\t-a addr1:port1,addr2:port2,... is a list of destination ip addresses\n");
        printf("\t-t nsec delta time between sends in nanosec [default %d]\n", tcx->nsec);
        printf("\t\t send message periodically and endless when nsec is non-zero\n");
        printf("\t-s server mode (you have to give exactly one -d address:port in this case)\n");
        printf("\t-n number of messages to send [default %d]\n", tcx->nmsg);
        printf("\t-l usec time to sleep in the main loop [default %d]\n", tcx->sleep_usec);
        printf("\t-v max variable msg size in bytes [default %d], 0=don't send var. size\n", tcx->maxsize);
+       printf("\t-d LogLevel [default %d]\n", LogLevel);       
        printf("Press ctrl+C to stop the program.\n");
 }
 
@@ -524,7 +546,7 @@ int main(int argc, char *argv[])
 {
        int     rc, op;
        int     result = 1;
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        struct ibwtest_ctx *tcx = NULL;
        float   usec;
 
@@ -532,24 +554,26 @@ int main(int argc, char *argv[])
        memset(tcx, 0, sizeof(struct ibwtest_ctx));
        tcx->nsec = 0;
        tcx->nmsg = 1000;
+       LogLevel = 0;
 
        /* here is the only case we can't avoid using global... */
        testctx = tcx;
        signal(SIGINT, ibwtest_sigint_handler);
        srand((unsigned)time(NULL));
 
-       while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:")) != -1) {
+       while ((op=getopt(argc, argv, "i:o:d:m:st:n:l:v:a:")) != -1) {
                switch (op) {
                case 'i':
                        tcx->id = talloc_strdup(tcx, optarg);
                        break;
                case 'o':
                        tcx->opts = talloc_strdup(tcx, optarg);
+                       if (tcx->opts) goto cleanup;
                        if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
                                &tcx->nattrs, op))
                                goto cleanup;
                        break;
-               case 'd':
+               case 'a':
                        if (ibwtest_getdests(tcx, op))
                                goto cleanup;
                        break;
@@ -568,6 +592,9 @@ int main(int argc, char *argv[])
                case 'v':
                        tcx->maxsize = (unsigned int)atoi(optarg);
                        break;
+               case 'd':
+                       LogLevel = atoi(optarg);
+                       break;
                default:
                        fprintf(stderr, "ERROR: unknown option -%c\n", (char)op);
                        ibwtest_usage(tcx, argv[0]);
@@ -612,7 +639,7 @@ int main(int argc, char *argv[])
 
        if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) {
                if (gettimeofday(&tcx->end_time, NULL)) {
-                       DEBUG(0, ("gettimeofday error %d\n", errno));
+                       DEBUG(DEBUG_ERR, ("gettimeofday error %d\n", errno));
                        goto cleanup;
                }
                usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 +
@@ -629,6 +656,6 @@ cleanup:
                talloc_free(tcx);
        if (ev)
                talloc_free(ev);
-       DEBUG(0, ("exited with code %d\n", result));
+       DEBUG(DEBUG_ERR, ("exited with code %d\n", result));
        return result;
 }