diff --git a/enbsim/cell_config.cpp b/enbsim/cell_config.cpp index 2bcd981..073d8f7 100644 --- a/enbsim/cell_config.cpp +++ b/enbsim/cell_config.cpp @@ -33,9 +33,8 @@ void copy_ecgi(ECGI_t *dest, ECGI_t *src) { dest->eUTRANcellIdentifier.size = src->eUTRANcellIdentifier.size; } -void cell_config_request(XRANCPDU *req, context_t *ctx) { +int cell_config_request(XRANCPDU *req, char *resp_buf, int resp_buf_size) { // TODO - asn_enc_rval_t er; XRANCPDU *resp; struct Cell cell; int ret; @@ -83,7 +82,13 @@ void cell_config_request(XRANCPDU *req, context_t *ctx) { xer_fprint(stdout, &asn_DEF_XRANCPDU, resp); - context_send(resp, ctx); + asn_enc_rval_t er = asn_encode_to_buffer(0, ATS_BER, &asn_DEF_XRANCPDU, resp, resp_buf, resp_buf_size); + if(er.encoded > resp_buf_size) { + fprintf(stderr, "Buffer of size %d is too small for %s, need %zu\n", + resp_buf_size, asn_DEF_XRANCPDU.name, er.encoded); + } ASN_STRUCT_FREE(asn_DEF_XRANCPDU, resp); + + return er.encoded; } diff --git a/enbsim/cell_config.h b/enbsim/cell_config.h index 09b5164..2d1a1bf 100644 --- a/enbsim/cell_config.h +++ b/enbsim/cell_config.h @@ -20,5 +20,5 @@ #include #include "context.h" -void cell_config_request(XRANCPDU *pdu, context_t *ctx); +int cell_config_request(XRANCPDU *req, char *resp_buf, int resp_buf_size); #endif diff --git a/enbsim/context.cpp b/enbsim/context.cpp index fc03fd0..373a4b7 100644 --- a/enbsim/context.cpp +++ b/enbsim/context.cpp @@ -32,29 +32,3 @@ void closecontext(context_t *context) { } } } - -/* -void context_timers_add(context_t *context) { - cell_config_timer_add(context); -} -*/ - -void context_send(XRANCPDU *pdu, context_t *context) { - char buffer[4096]; - int buf_size = 4096; - asn_enc_rval_t er; - - er = asn_encode_to_buffer(0, ATS_BER, &asn_DEF_XRANCPDU, pdu, buffer, buf_size); - if(er.encoded > buf_size) { - fprintf(stderr, "Buffer of size %d is too small for %s, need %zu\n", - buf_size, asn_DEF_XRANCPDU.name, er.encoded); - } - - struct evbuffer *tmp = evbuffer_new(); - evbuffer_add(tmp, buffer, er.encoded); - if (bufferevent_write_buffer(context->buf_ev, tmp)) { - printf("Error sending data to context on fd %d\n", context->fd); - closecontext(context); - } - evbuffer_free(tmp); -} diff --git a/enbsim/dispatch.cpp b/enbsim/dispatch.cpp index f78913e..718f7a4 100644 --- a/enbsim/dispatch.cpp +++ b/enbsim/dispatch.cpp @@ -18,26 +18,31 @@ #include "dispatch.h" #include "context.h" #include "cell_config.h" -//#include "handler.h" void dispatch(uint8_t *buffer, size_t buf_size, context_t *context) { - XRANCPDU *pdu = 0; + XRANCPDU *req_pdu = 0; asn_dec_rval_t rval; - rval = asn_decode(0, ATS_BER, &asn_DEF_XRANCPDU, (void **)&pdu, buffer, buf_size); + + int resp_buf_size = 4096; + char resp_buf[resp_buf_size]; + int nbytes = 0; + + rval = asn_decode(0, ATS_BER, &asn_DEF_XRANCPDU, (void **)&req_pdu, buffer, buf_size); switch (rval.code) { case RC_OK: break; case RC_WMORE: case RC_FAIL: default: - ASN_STRUCT_FREE(asn_DEF_XRANCPDU, pdu); + ASN_STRUCT_FREE(asn_DEF_XRANCPDU, req_pdu); return; } - xer_fprint(stdout, &asn_DEF_XRANCPDU, pdu); - switch (pdu->hdr.api_id) { + xer_fprint(stdout, &asn_DEF_XRANCPDU, req_pdu); + + switch (req_pdu->hdr.api_id) { case XRANC_API_ID_cellConfigRequest: - cell_config_request(pdu, context); + nbytes = cell_config_request(req_pdu, resp_buf, resp_buf_size); break; /* case XRANC_API_ID_uEAdmissionRequest: @@ -60,8 +65,18 @@ void dispatch(uint8_t *buffer, size_t buf_size, context_t *context) { break; */ default: - printf("Message %lu not handled\n", pdu->hdr.api_id); + printf("Message %lu not handled\n", req_pdu->hdr.api_id); } - ASN_STRUCT_FREE(asn_DEF_XRANCPDU, pdu); + ASN_STRUCT_FREE(asn_DEF_XRANCPDU, req_pdu); + + if (nbytes) { + struct evbuffer *tmp = evbuffer_new(); + evbuffer_add(tmp, resp_buf, nbytes); + if (bufferevent_write_buffer(context->buf_ev, tmp)) { + printf("Error sending data to context on fd %d\n", context->fd); + closecontext(context); + } + evbuffer_free(tmp); + } } diff --git a/enbsim/enbsim.cpp b/enbsim/enbsim.cpp index e7023ba..d0d420f 100644 --- a/enbsim/enbsim.cpp +++ b/enbsim/enbsim.cpp @@ -33,12 +33,29 @@ static void set_tcp_no_delay(evutil_socket_t fd) TCP_NODELAY, &one, sizeof one); } -static void timeoutcb(evutil_socket_t fd, short what, void *arg) -{ - struct event_base *base = (struct event_base *)arg; - printf("timeout\n"); +static int create_bind_socket(int enb_id) { + + char ip[18]; + int fd; + struct sockaddr_in localaddr; + + fd = socket(AF_INET, SOCK_STREAM, +#ifdef USE_SCTP + IPPROTO_SCTP +#else + IPPROTO_TCP +#endif + ); - event_base_loopexit(base, NULL); + + sprintf(ip, "127.0.0.%d", enb_id); + localaddr.sin_family = AF_INET; + localaddr.sin_addr.s_addr = inet_addr(ip); + localaddr.sin_port = 0; // Any local port will do + + bind(fd, (struct sockaddr *)&localaddr, sizeof(localaddr)); + + return fd; } static void readcb(struct bufferevent *bev, void *arg) @@ -61,14 +78,6 @@ static void readcb(struct bufferevent *bev, void *arg) } dispatch((uint8_t *)data, tbytes, ctx); - - struct evbuffer *output = bufferevent_get_output(bev); - - ++total_messages_read; - total_bytes_read += tbytes; - - /* Copy all the data from the input buffer to the output buffer. */ - evbuffer_add_buffer(output, input); } static void eventcb(struct bufferevent *bev, short events, void *ptr) @@ -85,85 +94,56 @@ int enbsim_main(int argc, char **argv, const Config& config) { //struct bufferevent **bevs; struct sockaddr_in sin; - struct event *evtimeout; - struct timeval timeout; int i; if (argc != 5) { - fprintf(stderr, "Usage: client