/** * \brief Function to send DISCONNECT packet and close the connection. * \param conn The connection to close. */ umqtt_ret broker_disconnect(struct broker_conn *conn) { LOG_DEBUG_FN("fn: broker_disconnect"); umqtt_ret ret = UMQTT_SUCCESS; if (conn->state) { /* disconnect from active session */ struct mqtt_packet *pkt = construct_default_packet(DISCONNECT, 0, 0); if (!pkt) { return UMQTT_DISCONNECT_ERROR; } ret = conn->send_method(conn, pkt); if (ret) { free_packet(pkt); return UMQTT_PACKET_ERROR; } free_packet(pkt); } if (conn->disconnect_method(conn)) { return UMQTT_DISCONNECT_ERROR; } conn->state = UMQTT_DISCONNECTED; return UMQTT_SUCCESS; }
/** * \brief Function to send SUBSCRIBE packet to broker. * \param conn The connection to SUBSCRIBE on. * \param topic The topic for which the message should be published. * \param topic_len The length of the topic. */ umqtt_ret broker_subscribe(struct broker_conn *conn, const char *topic, size_t topic_len) { LOG_DEBUG_FN("fn: broker_subscribe"); umqtt_ret ret = UMQTT_SUCCESS; /* send subscribe packet */ struct mqtt_packet *pkt = construct_default_packet(SUBSCRIBE, 0, 0); if (!pkt) { LOG_ERROR("Creating subscribe packet"); return UMQTT_PACKET_ERROR; } set_un_subscribe_payload(pkt, topic, topic_len, UMQTT_DEFAULT_QOS); finalise_packet(pkt); /* register subscription */ conn->subs[conn->sub_count] = pkt; ret = conn->send_method(conn, pkt); if (ret) { LOG_ERROR("Broker connection failed"); free_packet(pkt); return ret; } /* get response */ struct mqtt_packet *pkt_resp; if (init_packet(&pkt_resp)) { LOG_ERROR("Allocating memory"); return UMQTT_MEM_ERROR; } uint8_t count = 0; do { ret = conn->receive_method(conn, pkt_resp); if (ret) { LOG_ERROR("Subscribe Response Packet Failed"); free_packet(pkt_resp); return ret; } } while (pkt_resp->fixed->generic.type != SUBACK && count++ < MAX_RESP_PKT_RETRIES); if (pkt_resp->fixed->generic.type != SUBACK) { ret = UMQTT_SUBSCRIBE_ERROR; } free_packet(pkt_resp); return ret; }
/* * Queue a packet for reliable transmission. */ void queue_packet_reliable(PACKET *p, int priority) { assert(p->len < 510); THREAD_DATA::net_t *n = get_thread_data()->net; if (priority == SP_DEFERRED) { n->queues->d_prio->push_back(p); } else if (priority == SP_HIGH || priority == SP_NORMAL) { uint32_t ack_id; PACKET *p2; /* packet with space for reliable header */ p2 = allocate_packet(p->len + 6); ack_id = n->rel_o->next_ack_id++; /* add reliable header and free old (headerless) packet */ build_packet(p2->data, "AACZ", 0x00, 0x03, ack_id, p->data, p->len); free_packet(p); /* store a copy for retransmission until its ack is received */ RPACKET *rp = allocate_rpacket(p2->len, get_ticks_ms(), ack_id); memcpy(rp->data, p2->data, p2->len); n->rel_o->queue->push_back(rp); /* send p2 */ queue_packet(p2, priority); } else { assert(0); } }
void remove_response_record(struct subnet_record *subrec, struct response_record *rrec) { if (rrec->prev) rrec->prev->next = rrec->next; if (rrec->next) rrec->next->prev = rrec->prev; if (subrec->responselist == rrec) subrec->responselist = rrec->next; if(rrec->userdata) { if(rrec->userdata->free_fn) { (*rrec->userdata->free_fn)(rrec->userdata); } else { ZERO_STRUCTP(rrec->userdata); SAFE_FREE(rrec->userdata); } } /* Ensure we can delete. */ rrec->packet->locked = False; free_packet(rrec->packet); ZERO_STRUCTP(rrec); SAFE_FREE(rrec); num_response_packets--; /* count of total number of packets still around */ }
/*---------------------------------------------------------------------------*/ static void tx_done(int status, struct rdc_buf_list *q, struct neighbor_queue *n) { mac_callback_t sent; struct qbuf_metadata *metadata; void *cptr; uint8_t ntx; metadata = (struct qbuf_metadata *)q->ptr; sent = metadata->sent; cptr = metadata->cptr; ntx = n->transmissions; switch(status) { case MAC_TX_OK: PRINTF("csma: rexmit ok %d\n", n->transmissions); break; case MAC_TX_COLLISION: case MAC_TX_NOACK: PRINTF("csma: drop with status %d after %d transmissions, %d collisions\n", status, n->transmissions, n->collisions); break; default: PRINTF("csma: rexmit failed %d: %d\n", n->transmissions, status); break; } free_packet(n, q, status); mac_call_sent_callback(sent, cptr, status, ntx); }
struct ccnl_interest_s* ccnl_interest_remove(struct ccnl_relay_s *ccnl, struct ccnl_interest_s *i) { struct ccnl_interest_s *i2; /* if (!i) return NULL; */ DEBUGMSG_CORE(TRACE, "ccnl_interest_remove %p\n", (void *) i); /* #ifdef USE_NFN if (!(i->flags & CCNL_PIT_COREPROPAGATES)) return i->next; #endif */ while (i->pending) { struct ccnl_pendint_s *tmp = i->pending->next; \ ccnl_free(i->pending); i->pending = tmp; } i2 = i->next; DBL_LINKED_LIST_REMOVE(ccnl->pit, i); free_packet(i->pkt); ccnl_free(i); return i2; }
/**************************************************************************** tdb traversal fn to find a matching 137 packet **************************************************************************/ static int traverse_match(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state) { struct unexpected_key key; struct packet_struct *p; if (kbuf.dsize != sizeof(key)) { return 0; } memcpy(&key, kbuf.dptr, sizeof(key)); if (key.packet_type != match_type) return 0; p = parse_packet(dbuf.dptr, dbuf.dsize, match_type); if ((match_type == NMB_PACKET && p->packet.nmb.header.name_trn_id == match_id) || (match_type == DGRAM_PACKET && match_mailslot_name(p, match_name))) { matched_packet = p; return -1; } free_packet(p); return 0; }
void *write_thread(void *_ts) { struct ts *ts = _ts; struct packet *packet; mode_t umask_val = umask(0); dir_perm = (0777 & ~umask_val) | (S_IWUSR | S_IXUSR); set_thread_name("tsdump-write"); while ((packet = queue_get(ts->packet_queue))) { if (!packet->data_len) continue; p_dbg1(" - Got packet %d, size: %u, file_time:%lu packet_time:%lu depth:%d\n", packet->num, packet->data_len, ALIGN_DOWN(packet->ts.tv_sec, ts->rotate_secs), packet->ts.tv_sec, ts->packet_queue->items); handle_files(ts, packet); if (ts->output_fd > -1) { p_dbg2(" - Writing into fd:%d size:%d file:%s\n", ts->output_fd, packet->data_len, ts->output_filename); ssize_t written = write(ts->output_fd, packet->data, packet->data_len); if (written != packet->data_len) { p_err("Can not write data (fd:%d written %zd of %d file:%s)", ts->output_fd, written, packet->data_len, ts->output_filename); } } free_packet(packet); } close_output_file(ts, NO_UNLINK); return NULL; }
void queue_packet_large(PACKET *p, int priority) { // chunk packets need to be sent in succession if (priority == SP_DEFERRED) priority = SP_NORMAL; // break the packet down into chunks int offset = 0; while (offset < p->len) { // also must fit into a cluster packet int chunk_size = MIN(p->len - offset, 240); // min size must be < guard clauses in queue_packet and queue_packet_reliable PACKET *q = allocate_packet(chunk_size + 2); build_packet(q->data, "AAZ", 0x00, (offset + chunk_size < p->len) ? 0x08 : 0x09, &p->data[offset], chunk_size ); queue_packet_reliable(q, priority); offset += chunk_size; } free_packet(p); }
void main(void) { volatile packet_t *p; volatile uint8_t t=20; uint8_t chan; char c; gpio_data(0); gpio_pad_dir_set( 1ULL << LED ); /* read from the data register instead of the pad */ /* this is needed because the led clamps the voltage low */ gpio_data_sel( 1ULL << LED); /* trim the reference osc. to 24MHz */ trim_xtal(); uart_init(INC, MOD, SAMP); vreg_init(); maca_init(); /* sets up tx_on, should be a board specific item */ *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2)); gpio_pad_dir_set( 1ULL << 44 ); set_power(0x0f); /* 0dbm */ chan = 0; set_channel(chan); /* channel 11 */ *MACA_MACPANID = 0xaaaa; *MACA_MAC16ADDR = 0x1111; *MACA_TXACKDELAY = 68; /* 68 puts the tx ack at about the correct spot */ set_prm_mode(AUTOACK); print_welcome("rftest-rx"); while(1) { /* call check_maca() periodically --- this works around */ /* a few lockup conditions */ check_maca(); if((p = rx_packet())) { /* print and free the packet */ printf("autoack-rx --- "); print_packet(p); free_packet(p); } if(uart1_can_get()) { c = uart1_getc(); if(c == 'z') t++; if(c == 'x') t--; *MACA_TXACKDELAY = t; printf("tx ack delay: %d\n\r", t); } } }
static void test_parse_packet_ether_ipv4_succeeds() { buffer *ipv4_buffer = setup_dummy_ether_ipv4_packet( ); assert_int_equal( parse_packet( ipv4_buffer ), true ); free_packet( ipv4_buffer ); }
/** * @brief local callback to handle incoming content chunks * * @note Gets called from CCNL thread context * * @returns 1 if chunk is handled and no further processing should happen * @returns 0 otherwise **/ int ccnlriot_consumer(struct ccnl_relay_s *relay, struct ccnl_face_s *from, struct ccnl_pkt_s *pkt) { (void) from; (void) relay; if (dow_state == DOW_STATE_STOPPED) { LOG_DEBUG("ccnl_helper: we're in stopped, do nothing\n"); free_packet(pkt); return 1; } uint32_t cont_id = (uint32_t) strtol((char*)pkt->pfx->comp[2], NULL, 16); if (cont_id > dow_highest_id) { dow_highest_id = cont_id; } LOG_DEBUG("%" PRIu32 " ccnl_helper: local consumer for prefix: %s\n", xtimer_now().ticks32, ccnl_prefix_to_path_detailed(_prefix_str, pkt->pfx, 1, 0, 0)); memset(_prefix_str, 0, CCNLRIOT_PFX_LEN); #if DOW_DEPUTY /* XXX: might be unnecessary du to mutex now */ /* if we're currently transferring our cache to the new deputy, we do not touch the content store */ if (dow_state == DOW_STATE_HANDOVER) { LOG_DEBUG("ccnl_helper: we're in handover state, cannot touch content store right now\n"); free_packet(pkt); return 1; } #endif /* check if prefix is for ALL and contains an ACK */ if ((ccnl_prefix_cmp(ccnl_helper_all_pfx, NULL, pkt->pfx, CMP_MATCH) >= 1) && (strncmp((char*) pkt->content, CCNLRIOT_CONT_ACK, strlen(CCNLRIOT_CONT_ACK)) == 0)) { dow_content_t *cc = (dow_content_t*) pkt->content; LOG_DEBUG("ccnl_helper: content number is %i\n", cc->num); if (cc->num >= 0) { _remove_pit(relay, cc->num); } LOG_DEBUG("ccnl_helper: received ACK, flag the content\n"); msg_t m = { .type = DOW_MSG_RECEIVED_ACK }; msg_try_send(&m, dow_pid); free_packet(pkt); return 1; }
void test_parse_ether_fails_if_version_is_no_ipv4() { buffer *ip_version = setup_dummy_ether_ipv4_packet( ); ( ( ipv4_header_t * ) ( ( char * ) ( ip_version->data ) + sizeof( ether_header_t ) ) )->version = 6; assert_int_equal( parse_packet( ip_version ), false ); free_packet( ip_version ); }
static void test_parse_packet_fails_if_arp_hw_type_is_no_ethernet_type() { buffer *arp_hw_type = setup_dummy_ether_arp_packet( ); ( ( arp_header_t * ) ( ( char * ) ( arp_hw_type->data ) + sizeof( ether_header_t ) ) )->ar_hrd = ARPHRD_ETHER; assert_int_equal( parse_packet( arp_hw_type ), false ); free_packet( arp_hw_type ); }
static void test_parse_packet_fails_if_packet_size_is_short_ethernet_size() { buffer *arp_short_ethernet_size = setup_dummy_ether_arp_packet( ); arp_short_ethernet_size->length = sizeof( ether_header_t ) - ETH_ADDRLEN; assert_int_equal( parse_packet( arp_short_ethernet_size ), false ); free_packet( arp_short_ethernet_size ); }
void routing_send_signal( int to, int sig ) { read_packet *rp = make_packet( sig, FPM_DEV, to, NULL, NULL ); if( write( fpm, rp, sizeof( read_packet ) ) < 0 ) { fprintf(stderr, "%s: Write error - %s\n", __func__, strerror( errno ) ); } free_packet(rp); }
static void empty_pkt_queue(packet_list_t *l) { while (l->begin() != l->end()) { free_packet(*l->begin()); l->erase(l->begin()); } }
static void resolve_path_replied( void *user_data, dlist_element *hops ) { assert( user_data != NULL ); resolve_path_replied_params *param = user_data; routing_switch *routing_switch = param->routing_switch; buffer *original_packet = param->original_packet; if ( hops == NULL ) { warn( "No available path found." ); free_buffer( original_packet ); xfree( param ); return; } original_packet->user_data = NULL; if ( !parse_packet( original_packet ) ) { warn( "Received unsupported packet" ); free_packet( original_packet ); free_hop_list( hops ); xfree( param ); return; } // count elements uint32_t hop_count = count_hops( hops ); // send flow entry from tail switch for ( dlist_element *e = get_last_element( hops ); e != NULL; e = e->prev, hop_count-- ) { uint16_t idle_timer = ( uint16_t ) ( routing_switch->idle_timeout + hop_count ); modify_flow_entry( e->data, original_packet, idle_timer ); } // for(;;) // send packet out for tail switch dlist_element *e = get_last_element( hops ); pathresolver_hop *last_hop = e->data; output_packet_from_last_switch( last_hop, original_packet ); // free them free_hop_list( hops ); free_packet( original_packet ); xfree( param ); }
int main(int argc, char **argv) { umqtt_ret ret = UMQTT_SUCCESS; /* initialise packet */ struct mqtt_packet *pkt = NULL; if (argv[1]) { pkt = construct_packet_headers(get_string_type(argv[1])); } if (!pkt) { log_std(LOG_ERROR, "Constructing headers: Packet creation failed"); ret = UMQTT_ERROR; goto free; } else { switch (pkt->fixed->generic.type) { case CONNECT: ret = build_connect_pkt_getopts(argc, argv, pkt); break; case PUBLISH: ret = build_publish_pkt_getopts(argc, argv, pkt); break; case SUBSCRIBE: case UNSUBSCRIBE: ret = build_un_subscribe_pkt_getopts(argc, argv, pkt); break; default: ret = build_generic_pkt_getopts(argc, argv, pkt); break; } } if (ret) { log_std(LOG_ERROR, "Building packet: Packet creation failed"); goto free; } /* Cleanup packet */ finalise_packet(pkt); /* print */ print_packet_detailed_info(pkt); print_packet_hex_debug(pkt); print_packet_raw_debug(pkt); free: if (pkt) { free_packet(pkt); } return ret; }
void FeBaseStream::clear_packet_queue() { sf::Lock l( m_packetq_mutex ); while ( !m_packetq.empty() ) { AVPacket *p = m_packetq.front(); m_packetq.pop(); free_packet( p ); } }
/** * \brief Function to test the encoding and decoding of the remaining packet * length. * \return sucess or failurer of tests. */ int test_enc_dec_remaining_pkt_len() { int ret = 0, i, j; unsigned int lengths[8] = { 0, 127, 128, 16383, 16384, 2097151, 2097152, 268435455 }; struct mqtt_packet *pkt = '\0'; log_std(LOG_INFO, "Testing remaining packet length encoding/decoding and length estimation:"); init_packet(&pkt); init_packet_fixed_header(pkt, CONNECT); for (i = 0; i < 8; i++) { encode_remaining_len(pkt, lengths[i]); log_std(LOG_INFO, "Length: %d\t\tEncoded: %02X %02X %02X %02X\t", lengths[i], pkt->fixed->remain_len[0], pkt->fixed->remain_len[1], pkt->fixed->remain_len[2], pkt->fixed->remain_len[3]); int len_dec = decode_remaining_len(pkt); log_std(LOG_INFO, "Decoded:%d\t\t", len_dec); if (len_dec != lengths[i]) { log_std(LOG_INFO, "\tDecode FAILED\t"); ret = 1; } int bytes = required_remaining_len_bytes(lengths[i]); for (j = 1; j <= 4 && (pkt->fixed->remain_len[j-1] & 0x80); j++); log_std(LOG_INFO, "Estimated %d bytes %d ", bytes, j); if (bytes == j) { log_std(LOG_INFO, "Successfully"); } else { log_std(LOG_INFO, "Unsuccessfully"); } } free_packet(pkt); if (ret) { log_std(LOG_INFO, "Remaining length encoding/decoding failed"); } else { log_std(LOG_INFO, "Remaining length encoding/decoding successful"); } return ret; }
void free_free_queue(nic_t *nic) { packet_t *pkt, *pkt_next; pthread_mutex_lock(&nic->free_packet_queue_mutex); pkt = nic->free_packet_queue; while (pkt) { pkt_next = pkt->next; free_packet(pkt); pkt = pkt_next; } nic->free_packet_queue = NULL; pthread_mutex_unlock(&nic->free_packet_queue_mutex); }
void release_kbnode( KBNODE n ) { KBNODE n2; while( n ) { n2 = n->next; if( !is_cloned_kbnode(n) ) { free_packet( n->pkt ); xfree( n->pkt ); } free_node( n ); n = n2; } }
void free_all_packets(void) { volatile int i; safe_irq_disable(MACA); free_head = 0; for(i=0; i<NUM_PACKETS; i++) { free_packet((volatile packet_t *)&(packet_pool[i])); } rx_head = 0; rx_end = 0; tx_head = 0; tx_end = 0; irq_restore(); if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } return; }
VOID OnUnload(IN PDRIVER_OBJECT DriverObject) { UNICODE_STRING linkname; free_packet(); RtlInitUnicodeString(&linkname, L"\\??\\ip_fw"); IoDeleteSymbolicLink(&linkname); IoDeleteDevice(g_devcontrol); pfhook_free(); module_ipfw->modevent(g_driver_object, MOD_UNLOAD, NULL); iflist_free(); log_free(); }
// this routine handles returning responses from the virtual terminals to the // driver for sorting and queueing. The variable 's' represents the mapped // response while 't' represents the raw response. void routing_return( int target, char *s, char *t ) { read_packet *rp = NULL; if (s == NULL) return; rp = make_packet( DATA, FPM_DEV, target, s, t ); printf("%s: write combined mapped+raw response (%d bytes)\n", __func__, rp->size); if( write( fpm, rp, sizeof(read_packet) + rp->size ) < 0 ) { printf("%s: Write error - %s\n", __func__, strerror( errno ) ); } free_packet(rp); printf("%s: write to fpm\n", __func__ ); }
void free_tx_head(void) { volatile packet_t *p; safe_irq_disable(MACA); BOUND_CHECK(tx_head); p = tx_head; tx_head = tx_head->left; if(tx_head == 0) { tx_end = 0; } free_packet(p); // print_packets("free tx head"); irq_restore(); if(bit_is_set(*NIPEND, INT_NUM_MACA)) { *INTFRC = (1 << INT_NUM_MACA); } return; }
void ping_timer_cb(evutil_socket_t fd, short event, void *args) { struct node *node; struct node_list *node_list; struct node *local_node; struct daemon *daemon; struct packet *pkt; node = (struct node *)args; daemon = node->daemon; node_list = daemon->node_list; local_node = daemon->local_node; if(node->meta_conn_state == NODE_MFD_DISCONNECTED) { return; } if(node->ping >= node_list->pingtimeout) { log_error("Error: %s node %d ping reach timeout count", __func__, node->id); node_del_data_event(node); node_del_meta_event(node); return; } pkt = alloc_packet0(); pkt->type = P_META_PING; pkt->node_from = local_node->id; packet_set_node_to(node->id, pkt); node->ping++; if(node_put_meta_packet(node, pkt) < 0){ log_error("Error: %s node %d meta queue push failed.\n", __func__, node->id); free_packet(pkt); node_del_data_event(node); node_del_meta_event(node); return; } timer_add_tb(daemon->timer_base, node->ping_timer); }
void send_response(response_t *res, enum cmd_t cmd) { packet_t* packet = new_packet(T_RESPONSE, cmd); if (packet == NULL) return; packet->body_len = 2 * sizeof(int) + res->data_len; packet->body = malloc(packet->body_len); if (packet->body) { memcpy(packet->body, (char *)res, 2 * sizeof(int)); if (res->data_len > 0 && res->data != NULL) memcpy(packet->body + 2 * sizeof(int), res->data, res->data_len); packet->checksum = calc_checksum(packet); size_t size = sizeof(packet_t) - sizeof(char *) + packet->body_len; transmit(STDOUT, (char *)packet, size, NULL); } free_packet(packet); }
int node_disconnect(struct node *node) { int i, ret; struct device *dev; struct packet *pkt, *notify; struct daemon *daemon = node->daemon; log_error("%s: disconnect node %d\n", __func__, node->id); pkt = alloc_packet0(); if (!pkt) { log_error("Error: %s alloc packet failed", __func__); return -ENOMEM; } pkt->type = P_KMOD_DISCONN; pkt->dev_id = MAX_DEVICES; pkt->node_from = node->id; pkt->kmod_from = INVALID_ID; //FIXME 是否导入了不一致? pkt->node_to = daemon->local_node->id; for (i = 0; i < daemon->dev_list->dev_num; i++) { dev = daemon->dev_list->devs[i]; if (!device_want_recv(dev)) continue; notify = packet_clone(pkt); if (!notify) { log_error("alloc disconnect notify packet failed."); ret = -ENOMEM; goto out; } notify->kmod_to = (1 << dev->id); ret = dev_put_meta_packet(dev, notify); if (ret < 0) { log_error("Error: put meta packet failed."); goto out; } } out: free_packet(pkt); return ret; }