📄 ibwrapper_test.c
字号:
tconn->id ? tconn->id : "NULL")); if (sum!=((unsigned char *)buf)[n-1]) { DEBUG(0, ("ERROR: checksum mismatch %u!=%u\n", (uint32_t)sum, (uint32_t)((unsigned char *)buf)[n-1])); ibw_stop(tcx->ibwctx); goto error; } } 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"); goto error; } memcpy(buf2, buf, n); if (ibw_send(conn, buf2, key2, n)) { fprintf(stderr, "ibw_send error #2\n"); goto error; } tcx->nsent++; } } else { /* client: */ if (op==TESTOP_SEND_ID && tcx->maxsize) { /* send them in one blow */ rc = ibwtest_do_varsize_scenario_conn(tcx, conn); } if (tcx->nmsg) { char msg[26]; sprintf(msg, "hello world %d", tcx->nmsg--); rc = ibwtest_send_test_msg(tcx, conn, msg); if (tcx->nmsg==0) { ibw_stop(tcx->ibwctx); tcx->stopping = 1; } } } if (rc) 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_data){ struct ibwtest_ctx *tcx = talloc_get_type(private_data, struct ibwtest_ctx); int rc; if (!tcx->is_server) { struct ibw_conn *conn; char msg[50]; /* fill it with something variable... */ sprintf(msg, "hello world %d", tcx->cnt++); /* send something to everybody... */ for(conn=tcx->ibwctx->conn_list; conn!=NULL; conn=conn->next) { if (conn->state==IBWC_CONNECTED) { rc = ibwtest_send_test_msg(tcx, conn, msg); if (rc) tcx->error = rc; } } } /* else allow main loop run */}static struct ibwtest_ctx *testctx = NULL;void ibwtest_sigint_handler(int sig){ DEBUG(0, ("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")); testctx->kill_me = 1; } else { /* mostly expected case */ ibw_stop(testctx->ibwctx); testctx->stopping = 1; } } else testctx->kill_me = 1; }}int ibwtest_parse_attrs(struct ibwtest_ctx *tcx, char *optext, struct ibw_initattr **pattrs, int *nattrs, char op){ int i = 0, n = 1; int porcess_next = 1; char *p, *q; struct ibw_initattr *attrs = NULL; *pattrs = NULL; for(p = optext; *p!='\0'; p++) { if (*p==',') n++; } attrs = (struct ibw_initattr *)talloc_size(tcx, n * sizeof(struct ibw_initattr)); for(p = optext; *p!='\0'; p++) { if (porcess_next) { attrs[i].name = p; q = strchr(p, ':'); if (q==NULL) { fprintf(stderr, "-%c format error\n", op); return -1; } *q = '\0'; attrs[i].value = q + 1; porcess_next = 0; i++; p = q; /* ++ at end */ } if (*p==',') { *p = '\0'; /* ++ at end */ porcess_next = 1; } } *pattrs = attrs; *nattrs = n; 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(0, ("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; struct ibw_initattr *attrs = NULL; struct sockaddr_in *p; char *tmp; tmp = talloc_strdup(tcx, optarg); /* hack to reuse the above ibw_initattr parser */ if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op)) return -1; tcx->addrs = talloc_size(tcx, tcx->naddrs * sizeof(struct sockaddr_in)); for(i=0; i<tcx->naddrs; i++) { p = tcx->addrs + i; p->sin_family = AF_INET; if (ibwtest_get_address(attrs[i].name, &p->sin_addr)) return -1; p->sin_port = htons(atoi(attrs[i].value)); } return 0;}int ibwtest_init_server(struct ibwtest_ctx *tcx){ if (tcx->naddrs!=1) { fprintf(stderr, "incorrect number of addrs(%d!=1)\n", tcx->naddrs); return -1; } if (ibw_bind(tcx->ibwctx, &tcx->addrs[0])) { DEBUG(0, ("ERROR: ibw_bind failed\n")); return -1; } if (ibw_listen(tcx->ibwctx, 1)) { DEBUG(0, ("ERROR: ibw_listen failed\n")); return -1; } /* continued at IBWS_READY */ return 0;}void ibwtest_usage(struct ibwtest_ctx *tcx, char *name){ printf("Usage:\n"); 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-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");}int main(int argc, char *argv[]){ int rc, op; int result = 1; struct event_context *ev = NULL; struct ibwtest_ctx *tcx = NULL; float usec; tcx = talloc_zero(NULL, struct ibwtest_ctx); 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:a:")) != -1) { switch (op) { case 'i': tcx->id = talloc_strdup(tcx, optarg); break; case 'o': tcx->opts = talloc_strdup(tcx, optarg); if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs, &tcx->nattrs, op)) goto cleanup; break; case 'a': if (ibwtest_getdests(tcx, op)) goto cleanup; break; case 's': tcx->is_server = 1; break; case 't': tcx->nsec = (unsigned int)atoi(optarg); break; case 'n': tcx->nmsg = atoi(optarg); break; case 'l': tcx->sleep_usec = (unsigned int)atoi(optarg); break; 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]); goto cleanup; } } if (tcx->id==NULL) { ibwtest_usage(tcx, argv[0]); goto cleanup; } ev = event_context_init(NULL); assert(ev); tcx->ibwctx = ibw_init(tcx->attrs, tcx->nattrs, tcx, ibwtest_connstate_handler, ibwtest_receive_handler, ev ); if (!tcx->ibwctx) goto cleanup; if (tcx->is_server) rc = ibwtest_init_server(tcx); else rc = ibwtest_connect_everybody(tcx); if (rc) goto cleanup; while(!tcx->kill_me && !tcx->error) { if (tcx->nsec) { event_add_timed(ev, tcx, timeval_current_ofs(0, tcx->nsec), ibwtest_timeout_handler, tcx); } event_loop_once(ev); if (tcx->sleep_usec) usleep(tcx->sleep_usec); } if (!tcx->is_server && tcx->nsent!=0 && !tcx->error) { if (gettimeofday(&tcx->end_time, NULL)) { DEBUG(0, ("gettimeofday error %d\n", errno)); goto cleanup; } usec = (tcx->end_time.tv_sec - tcx->start_time.tv_sec) * 1000000 + (tcx->end_time.tv_usec - tcx->start_time.tv_usec); printf("usec: %f, nmsg: %d, usec/nmsg: %f\n", usec, tcx->nsent, usec/(float)tcx->nsent); } if (!tcx->error) result = 0; /* everything OK */cleanup: if (tcx) talloc_free(tcx); if (ev) talloc_free(ev); DEBUG(0, ("exited with code %d\n", result)); return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -