void conn_clear_upd(struct conn *conn, int merge) { if (merge){ mavl_merge(conn->config, conn->config_upd); MAVLITER_DEFINE (it,conn->radios_upd); mavliter_foreach(&it){ struct mbag_item * ruitem = mavliter_get(&it); mavl_t radio_upd = ruitem->data; mbag_t radio = mbag_i_get_mbag(conn->radios,ruitem->iid,NULL); if (radio){ mavl_merge(radio,radio_upd); } } } mavl_del_all(conn->config_upd); mavl_del_all(conn->radios_upd); }
static void * wtpman_main(void *arg) { mavl_t r; int rc ; time_t timer; char sock_buf[SOCK_ADDR_BUFSIZE]; struct conn *conn; int last_state; struct wtpman *wtpman = (struct wtpman *) arg; wtpman->conn->seqnum = 0; conn = wtpman->conn; wtpman->conn->remote_cfg = cw_ktv_create(); /* We were invoked with an unencrypted packet, * so assume, it is a discovery request */ if (!wtpman->dtlsmode){ wtpman_run_discovery(arg); wtpman_remove(wtpman); return NULL; } /* reject connections to our multi- or broadcast sockets */ if (socklist[wtpman->socklistindex].type != SOCKLIST_UNICAST_SOCKET) { cw_dbg(DBG_DTLS,"Reject multi"); wtpman_remove(wtpman); return NULL; } conn->capwap_state = CAPWAP_STATE_DTLS_SETUP; /* establish dtls session */ if (!wtpman_dtls_setup(wtpman)) { wtpman_remove(wtpman); return NULL; } /*last_state = conn->capwap_state; conn->capwap_state = CAPWAP_STATE_JOIN; */ conn->capwap_prevstate = CAPWAP_STATE_DTLS_SETUP; conn->capwap_state = CAPWAP_STATE_JOIN; rc = 0; while (1){ int wait_join; int wait_change_state; if (!cw_run_state_machine(conn, &timer)){ cw_dbg(DBG_INFO,"WTP died"); wtpman_remove(wtpman); return NULL; }; /* switch (conn->capwap_transition){ case CW_TRANSITION(CAPWAP_STATE_DTLS_SETUP, CAPWAP_STATE_JOIN): { wait_join = cw_ktv_get_word(conn->global_cfg,"wait-join",CAPWAP_WAIT_JOIN); timer = cw_timer_start(wait_join); break; } case CW_TRANSITION(CAPWAP_STATE_JOIN, CAPWAP_STATE_JOIN): { char wtpname[CAPWAP_MAX_WTP_NAME_LEN]; cw_KTV_t * result; result = cw_ktv_get(conn->remote_cfg,"wtp-name",NULL); result->type->to_str(result,wtpname,CAPWAP_MAX_WTP_NAME_LEN); cw_dbg(DBG_INFO, "WTP joined: '%s', IP %s.", wtpname, sock_addr2str(&conn->addr,sock_buf) ); break; } case CW_TRANSITION(CAPWAP_STATE_JOIN,CAPWAP_STATE_TIMEOUT): { cw_dbg(DBG_MSG_ERR, "No join request from %s after %d seconds, WTP died.", sock_addr2str(&wtpman->conn->addr,sock_buf), wait_join); wtpman_remove(wtpman); return NULL; break; } case CW_TRANSITION(CAPWAP_STATE_JOIN, CAPWAP_STATE_CONFIGURE): { wait_change_state = cw_ktv_get_word(conn->global_cfg, "capwap-timers/change-state-pending-timer", CAPWAP_TIMER_CHANGE_STATE_PENDING_TIMER); break; } case CW_TRANSITION(CAPWAP_STATE_CONFIGURE,CAPWAP_STATE_TIMEOUT): { cw_dbg(DBG_MSG_ERR, "No Change State Event Request %s after %d seconds, WTP died.", sock_addr2str(&wtpman->conn->addr,sock_buf), wait_change_state); wtpman_remove(wtpman); return NULL; break; } } */ while (!cw_timer_timeout(timer)) { if (conn->update_cfg != NULL){ mavl_t tmp; tmp = conn->local_cfg; mavl_merge(conn->default_cfg, conn->local_cfg); mavl_merge(conn->default_cfg, conn->remote_cfg); conn->local_cfg=conn->update_cfg; cw_dbg(DBG_INFO, "Updating WTP %s",sock_addr2str(&conn->addr,sock_buf)); rc = cw_send_request(conn, CAPWAP_MSG_CONFIGURATION_UPDATE_REQUEST); conn->update_cfg=NULL; conn->local_cfg=tmp; } rc = cw_read_messages(wtpman->conn); if (rc < 0) { if (errno == EAGAIN) continue; } break; } if(rc<0){ conn->capwap_prevstate = conn->capwap_state; conn->capwap_state = CAPWAP_STATE_TIMEOUT; } } /* dtls is established, goto join state */ conn->capwap_state = CAPWAP_STATE_JOIN; if (!wtpman_join(wtpman)) { wtpman_remove(wtpman); return NULL; } cw_dbg(DBG_INFO, "WTP from %s has joined with session id: %s", sock_addr2str_p(&conn->addr,sock_buf), format_bin2hex(conn->session_id,16)); exit(0); /* // cw_dbg(DBG_INFO, "Creating data thread"); // pthread_t thread; // pthread_create(&thread, NULL, (void *) wtpman_run_data, (void *) wtpman); */ /* here the WTP has joined, now we assume an image data request or a configuration status request. Nothing else. */ rc = 0; while (!cw_timer_timeout(timer) && wtpman->conn->capwap_state == CAPWAP_STATE_CONFIGURE) { rc = cw_read_messages(wtpman->conn); if (rc < 0) { if (errno != EAGAIN) break; } } cw_dbg_ktv_dump(conn->remote_cfg,DBG_INFO,"-------------dump------------","DMP","---------end dump --------"); if (!cw_result_is_ok(rc)) { cw_dbg(DBG_INFO, "WTP Problem: %s", cw_strrc(rc)); wtpman_remove(wtpman); return NULL; } if (conn->capwap_state == CW_STATE_IMAGE_DATA) { wtpman_image_data(wtpman); return NULL; } conn->capwap_state = CAPWAP_STATE_RUN; /* // XXX testing ... // DBGX("Cofig to sql", ""); // props_to_sql(conn,conn->incomming,0); // radios_to_sql(conn); */ /*conn->msg_end=msg_end_handler;*/ /* The main run loop */ reset_echointerval_timer(wtpman); rc = 0; while (wtpman->conn->capwap_state == CAPWAP_STATE_RUN) { rc = cw_read_messages(wtpman->conn); if (rc < 0) { if (errno != EAGAIN) break; } /*// cw_dbg(DBG_X, "Time left: %d", //*/ /*cw_timer_timeleft(wtpman->echointerval_timer);*/ if (cw_timer_timeout(wtpman->echointerval_timer)) { cw_dbg(DBG_INFO, "Lost connection to WTP:%s", sock_addr2str_p(&conn->addr,sock_buf)); break; } /* // mavl_del_all(conn->outgoing); // conn_clear_upd(conn,1); // props_to_sql(conn,conn->incomming,0); // radios_to_sql(conn); */ r = db_get_update_tasks(conn, sock_addr2str(&conn->addr,sock_buf)); if (r) { /* // if (!conn->outgoing->count) // continue; */ cw_dbg(DBG_INFO, "Updating WTP %s",sock_addr2str(&conn->addr,sock_buf)); rc = cw_send_request(conn, CAPWAP_MSG_CONFIGURATION_UPDATE_REQUEST); /* // mavl_merge(conn->config, conn->outgoing); // mavl_destroy(conn->outgoing); // conn->outgoing = mbag_create(); // props_to_sql(conn,conn->incomming,0); // radios_to_sql(conn); // mavl_destroy(r); */ } r = db_get_radio_tasks(conn, sock_addr2str(&conn->addr,sock_buf)); if (r) { /* // if (!conn->radios_upd->count) // continue; */ cw_dbg(DBG_INFO, "Updating Radios for %s",sock_addr2str(&conn->addr,sock_buf)); rc = cw_send_request(conn, CAPWAP_MSG_CONFIGURATION_UPDATE_REQUEST); /* // conn_clear_upd(conn,1); // mavl_destroy(conn->radios_upd); // conn->radios_upd=mbag_i_create(); // radios_to_sql(conn); */ /* rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST); mavl_merge(conn->config, conn->outgoing); mavl_destroy(conn->outgoing); conn->outgoing = mbag_create(); config_to_sql(conn); radios_to_sql(conn); mavl_destroy(r); */ } } db_ping_wtp(sock_addr2str_p(&conn->addr,sock_buf), ""); wtpman_remove(wtpman); return NULL; }
static void wtpman_run(void *arg) { struct wtpman *wtpman = (struct wtpman *) arg; wtpman->conn->seqnum = 0; struct conn *conn = wtpman->conn; /* reject connections to our multi- or broadcast sockets */ /* if (socklist[wtpman->socklistindex].type != SOCKLIST_UNICAST_SOCKET) { cw_dbg(DBG_DTLS, "Dropping connection from %s to non-unicast socket.", CLIENT_IP); wtpman_remove(wtpman); return; } */ time_t timer = cw_timer_start(wtpman->conn->wait_dtls); /* establish dtls session */ /* if (!wtpman_establish_dtls(wtpman)) { wtpman_remove(wtpman); return; } */ /* dtls is established, goto join state */ if (!wtpman_join(wtpman, timer)) { wtpman_remove(wtpman); return; } conn->msg_start = msg_start_handler; cw_dbg(DBG_INFO, "WTP from %s has joined with session id: %s", sock_addr2str_p(&conn->addr), format_bin2hex(conn->session_id,16)); // cw_dbg(DBG_INFO, "Creating data thread"); // pthread_t thread; // pthread_create(&thread, NULL, (void *) wtpman_run_data, (void *) wtpman); /* here the WTP has joined, now we assume an image data request or an configuration status request. Nothing else. */ int rc = 0; while (!cw_timer_timeout(timer) && wtpman->conn->capwap_state == CW_STATE_CONFIGURE) { rc = cw_read_messages(wtpman->conn); if (rc < 0) { if (errno != EAGAIN) break; } } if (!cw_rcok(rc)) { cw_dbg(DBG_INFO, "WTP Problem: %s", cw_strrc(rc)); wtpman_remove(wtpman); return; } if (conn->capwap_state == CW_STATE_IMAGE_DATA) { wtpman_image_data(wtpman); return; } conn->capwap_state = CW_STATE_RUN; // XXX testing ... // DBGX("Cofig to sql", ""); props_to_sql(conn,conn->incomming,0); radios_to_sql(conn); conn->msg_end=msg_end_handler; /* The main run loop */ reset_echointerval_timer(wtpman); rc = 0; while (wtpman->conn->capwap_state == CW_STATE_RUN) { rc = cw_read_messages(wtpman->conn); if (rc < 0) { if (errno != EAGAIN) break; } // cw_dbg(DBG_X, "Time left: %d", // cw_timer_timeleft(wtpman->echointerval_timer)); if (cw_timer_timeout(wtpman->echointerval_timer)) { cw_dbg(DBG_INFO, "Lost connection to WTP:%s", sock_addr2str_p(&conn->addr)); break; } mavl_del_all(conn->outgoing); conn_clear_upd(conn,1); // props_to_sql(conn,conn->incomming,0); // radios_to_sql(conn); mavl_conststr_t r; r = db_get_update_tasks(conn, sock_addr2str(&conn->addr)); if (r) { if (!conn->outgoing->count) continue; cw_dbg(DBG_INFO, "Updating WTP %s",sock_addr2str(&conn->addr)); rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST); mavl_merge(conn->config, conn->outgoing); mavl_destroy(conn->outgoing); conn->outgoing = mbag_create(); props_to_sql(conn,conn->incomming,0); radios_to_sql(conn); mavl_destroy(r); } r = db_get_radio_tasks(conn, sock_addr2str(&conn->addr)); if (r) { if (!conn->radios_upd->count) continue; cw_dbg(DBG_INFO, "Updating Radios for %s",sock_addr2str(&conn->addr)); rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST); conn_clear_upd(conn,1); // mavl_destroy(conn->radios_upd); // conn->radios_upd=mbag_i_create(); radios_to_sql(conn); /* rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST); mavl_merge(conn->config, conn->outgoing); mavl_destroy(conn->outgoing); conn->outgoing = mbag_create(); config_to_sql(conn); radios_to_sql(conn); mavl_destroy(r); */ } } db_ping_wtp(sock_addr2str_p(&conn->addr), ""); wtpman_remove(wtpman); return; }