Ejemplo n.º 1
0
Archivo: tftp.c Proyecto: olsner/lwip
static void
send_data(const ip_addr_t *addr, u16_t port)
{
  u16_t *payload;
  int ret;

  if (tftp_state.last_data != NULL) {
    pbuf_free(tftp_state.last_data);
  }

  tftp_state.last_data = init_packet(TFTP_DATA, tftp_state.blknum, TFTP_MAX_PAYLOAD_SIZE);
  if (tftp_state.last_data == NULL) {
    return;
  }

  payload = (u16_t *) tftp_state.last_data->payload;

  ret = tftp_state.ctx->read(tftp_state.handle, &payload[2], TFTP_MAX_PAYLOAD_SIZE);
  if (ret < 0) {
    send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Error occured while reading the file.");
    close_handle();
    return;
  }

  pbuf_realloc(tftp_state.last_data, (u16_t)(TFTP_HEADER_LENGTH + ret));
  resend_data(addr, port);
}
Ejemplo n.º 2
0
int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr)
{
	struct dhcpMessage packet;
	struct option_set *curr;
	unsigned char *lease_time;
	
	//Foxconn added start Bob Guo 11/15/2006
	FILE *fp;
	unsigned char *client_mac, *client_ip;
	char logBuffer[96];
	//Foxconn added end Bob Guo 11/15/2006
	
	u_int32_t lease_time_align = server_config.lease;
	struct in_addr addr;

	init_packet(&packet, oldpacket, DHCPACK);
	packet.yiaddr = yiaddr;
	
	if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
		memcpy(&lease_time_align, lease_time, 4);
		lease_time_align = ntohl(lease_time_align);
		if (lease_time_align > server_config.lease) 
			lease_time_align = server_config.lease;
		else if (lease_time_align < server_config.min_lease) 
			lease_time_align = server_config.lease;
	}
	
	add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
	
	curr = server_config.options;
	while (curr) {
		if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
			add_option_string(packet.options, curr->data);
		curr = curr->next;
	}

	add_bootp_options(&packet);

	addr.s_addr = packet.yiaddr;
	LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));

	if (send_packet(&packet, 0) < 0) 
		return -1;

	add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
	//Foxconn added start Bob Guo 11/15/2006
	client_mac = (unsigned char *)packet.chaddr;
	client_ip = (unsigned char *)&packet.yiaddr;
	sprintf(logBuffer, "[DHCP IP: (%d.%d.%d.%d)] to MAC address %02X:%02X:%02X:%02X:%02X:%02X,",
	                                          *client_ip, *(client_ip+1), *(client_ip+2), *(client_ip+3),
	                                          *client_mac, *(client_mac+1), *(client_mac+2), *(client_mac+3), *(client_mac+4), *(client_mac+5));
	if ((fp = fopen("/dev/aglog", "r+")) != NULL)
  {
        fwrite(logBuffer, sizeof(char), strlen(logBuffer)+1, fp);
        fclose(fp);
  }	
	//Foxconn added end Bob Guo 11/15/2006

	return 0;
}
Ejemplo n.º 3
0
static
void send_data()
{
  int data_size = 0;
  static unsigned temperature;

  temperature = 21;
  clear_buffer(outputBuffer);
  clear_buffer(payload_buf);
  generate_payload(payload_buf,temperature);

  if(init_buffer(COAP_DATA_BUFF_SIZE)){
    coap_packet_t* request =\
    (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));
    init_packet(request);
    coap_set_method(request, COAP_POST);
    request->tid = xact_id++;
    request->type = MESSAGE_TYPE_CON;
    coap_set_header_uri(request,service_uri);
    coap_set_option(request, Option_Type_Uri_Host,
      sizeof(char)*strlen(server_ip), (uint8_t*)server_ip);
    coap_set_option(request, Option_Type_Proxy_Uri,
    sizeof(char)*strlen(proxy_uri), (uint8_t*)proxy_uri);
    coap_set_payload(request,(uint8_t*)payload_buf,
    sizeof(char)*strlen(payload_buf));
    data_size = serialize_packet(request, (uint8_t*)outputBuffer);

    PRINTF("Now sending request to base station [");
    PRINTF(&client_conn->ripaddr);
    PRINTF("]:%u/%s\n",REMOTE_PORT,service_uri);
    uip_udp_packet_send(client_conn, outputBuffer, data_size);
    delete_buffer();
  }
}
Ejemplo n.º 4
0
static void send_ACK(GDHCPServer *dhcp_server,
		struct dhcp_packet *client_packet, uint32_t dest)
{
	struct dhcp_packet packet;
	uint32_t lease_time_sec;
	struct in_addr addr;

	init_packet(dhcp_server, &packet, client_packet, DHCPACK);
	packet.yiaddr = htonl(dest);

	lease_time_sec = dhcp_server->lease_seconds;

	dhcp_add_option_uint32(&packet, DHCP_LEASE_TIME, lease_time_sec);

	add_server_options(dhcp_server, &packet);

	addr.s_addr = htonl(dest);

	debug(dhcp_server, "Sending ACK to %s", inet_ntoa(addr));

	send_packet_to_client(dhcp_server, &packet);

	add_lease(dhcp_server, 0, packet.chaddr, packet.yiaddr);

	if (dhcp_server->event_fn)
		dhcp_server->event_fn(ether_ntoa((void*)packet.chaddr),
				      inet_ntoa(addr),
				      dhcp_server->fn_data);
}
Ejemplo n.º 5
0
void init_multicast_packet(struct multicast_packet *mp, 
		enum packet_flags flags, uint8_t hops, const rimeaddr_t *originator,
		const rimeaddr_t *sender, uint8_t seqno, uint8_t num_ids) {

	init_packet((struct packet*)mp, MULTICAST|flags, hops, originator, sender, seqno);
	mp->num_ids = num_ids;
}
Ejemplo n.º 6
0
/**
 * Streams a message instead of getting the entire msg as a byte array
 * and then sending the bytes accross.
 */
void stream_msg(ScripterMsg* msg) {
	init_packet();
	// send header
	put_slip_char(get_msg_id_most_repr(msg->id));
	put_slip_char(get_msg_id_least_repr(msg->id));
	put_slip_char(msg->param_count);
		
	// send parameters
	MsgParam* param = msg->parameters;
	while(param != NULL) {
		// size is 3 bytes, so we first get
		// rid of most representative byte
		if (param->type == PT_BYTE_STREAM) {
			int size = (*(param->data.data_stream.fp_get_size))(&param->data.data_stream);
			put_slip_char(get_size_most_repr(size));
			put_slip_char(get_size_middle_repr(size));
			put_slip_char(get_size_least_repr(size));
			char *c = NULL;
			while( (c =(*(param->data.data_stream.fp_get_char))(&param->data.data_stream)) != NULL ) {
				put_slip_char(*c);
			}
		} else {
			put_slip_char(get_size_most_repr(param->data.data_blob.size));
			put_slip_char(get_size_middle_repr(param->data.data_blob.size));
			put_slip_char(get_size_least_repr(param->data.data_blob.size));
			int i;
			for(i=0; i<(int)param->data.data_blob.size; i++) {
				put_slip_char(*(param->data.data_blob.data+i));
			}
		}
		param = param->next_param;
	}
	finish_packet();	
}
static void
send_data(void)
{
  char buf[MAX_PAYLOAD_LEN];

  if (init_buffer(COAP_DATA_BUFF_SIZE)) {
    int data_size = 0;
    int service_id = random_rand() % NUMBER_OF_URLS;
    coap_packet_t* request = (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t));
    init_packet(request);

    coap_set_method(request, COAP_GET);
    request->tid = xact_id++;
    request->type = MESSAGE_TYPE_CON;
    coap_set_header_uri(request, service_urls[service_id]);

    data_size = serialize_packet(request, buf);

    PRINTF("Client sending request to:[");
    PRINT6ADDR(&client_conn->ripaddr);
    PRINTF("]:%u/%s\n", (uint16_t)REMOTE_PORT, service_urls[service_id]);
    uip_udp_packet_send(client_conn, buf, data_size);

    delete_buffer();
  }
}
void collect(socket_fd client_socket){
  packet_t anything;
  unsigned int usleep_time = (unsigned int) 1000000 / samples_per_second;
  struct sockaddr_in* sock = alloc(struct sockaddr_in, 1);
  char buffer[sizeof(packet_t)];
 
  sock->sin_family = AF_INET;
  sock->sin_port = htons(sink_dport);
  sock->sin_addr.s_addr = inet_addr(sink_dip);

  while(npackets > 0){
    memset(&anything, 0, sizeof(anything));
    init_packet(&anything);

    write_data(anything.accumulated_time, anything.in_time, anything.id, "me", anything.samples);
    
    swap_packet_byte_order(&anything);

    memcpy(buffer, &anything, sizeof(packet_t)); 

    if(sendto(client_socket, buffer, sizeof(packet_t), 0,(const struct sockaddr *) sock, sizeof(struct sockaddr_in)) == -1){
      log(E, "Could not send packet\n");
    }

    npackets--;

    usleep(usleep_time);
  }

  printf("Simulation has ended.\n");
}
Ejemplo n.º 9
0
/**
 * Function called from within Lua to wipe the camscripter graphics area clean.
 * Just forward on across the serial port.
 */
int clear_graphics(lua_State *_l) {
    init_packet();
    put_slip_char(get_msg_id_most_repr(CLEAR_GRAPHICS_MSG_ID));
    put_slip_char(get_msg_id_least_repr(CLEAR_GRAPHICS_MSG_ID));
    put_slip_char(0); // number of params
    finish_packet();
    return 0;
}
Ejemplo n.º 10
0
/*
 * Write a pubkey-enc packet for the public key PK to OUT.
 */
int
write_pubkey_enc (ctrl_t ctrl,
                  PKT_public_key *pk, int throw_keyid, DEK *dek, iobuf_t out)
{
  PACKET pkt;
  PKT_pubkey_enc *enc;
  int rc;
  gcry_mpi_t frame;

  print_pubkey_algo_note ( pk->pubkey_algo );
  enc = xmalloc_clear ( sizeof *enc );
  enc->pubkey_algo = pk->pubkey_algo;
  keyid_from_pk( pk, enc->keyid );
  enc->throw_keyid = throw_keyid;

  /* Okay, what's going on: We have the session key somewhere in
   * the structure DEK and want to encode this session key in an
   * integer value of n bits. pubkey_nbits gives us the number of
   * bits we have to use.  We then encode the session key in some
   * way and we get it back in the big intger value FRAME.  Then
   * we use FRAME, the public key PK->PKEY and the algorithm
   * number PK->PUBKEY_ALGO and pass it to pubkey_encrypt which
   * returns the encrypted value in the array ENC->DATA.  This
   * array has a size which depends on the used algorithm (e.g. 2
   * for Elgamal).  We don't need frame anymore because we have
   * everything now in enc->data which is the passed to
   * build_packet().  */
  frame = encode_session_key (pk->pubkey_algo, dek,
                              pubkey_nbits (pk->pubkey_algo, pk->pkey));
  rc = pk_encrypt (pk->pubkey_algo, enc->data, frame, pk, pk->pkey);
  gcry_mpi_release (frame);
  if (rc)
    log_error ("pubkey_encrypt failed: %s\n", gpg_strerror (rc) );
  else
    {
      if ( opt.verbose )
        {
          char *ustr = get_user_id_string_native (ctrl, enc->keyid);
          log_info (_("%s/%s.%s encrypted for: \"%s\"\n"),
                    openpgp_pk_algo_name (enc->pubkey_algo),
                    openpgp_cipher_algo_name (dek->algo),
                    dek->use_aead? openpgp_aead_algo_name (dek->use_aead)
                    /**/         : "CFB",
                    ustr );
          xfree (ustr);
        }
      /* And write it. */
      init_packet (&pkt);
      pkt.pkttype = PKT_PUBKEY_ENC;
      pkt.pkt.pubkey_enc = enc;
      rc = build_packet (out, &pkt);
      if (rc)
        log_error ("build_packet(pubkey_enc) failed: %s\n",
                   gpg_strerror (rc));
    }
  free_pubkey_enc(enc);
  return rc;
}
Ejemplo n.º 11
0
int sendNAK(struct dhcpMessage *oldpacket)
{
        struct dhcpMessage packet;

        init_packet(&packet, oldpacket, DHCPNAK);
        
        DEBUG(LOG_INFO, "sending NAK");
        return send_packet(&packet, 1);
}
Ejemplo n.º 12
0
/* open a link */
void init_ethmac()
{
    /* initialize the packet rx buffer */
    init_packet();

    /* open ethernet port and wait for connection requests
       keep trying forever */
    while (!open_link());
}
Ejemplo n.º 13
0
int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket)
{
	struct dhcpMessage packet;

	init_packet(&packet, oldpacket, DHCPNAK);

	DEBUG("Sending NAK");
	return send_packet(&packet, 1);
}
Ejemplo n.º 14
0
/* NOINLINE: limit stack usage in caller */
static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
{
	struct dhcp_packet packet;

	init_packet(&packet, oldpacket, DHCPNAK);

	log1("Sending NAK");
	send_packet(&packet, /*force_bcast:*/ 1);
}
Ejemplo n.º 15
0
/****************
 * Make a hash value from the public key certificate
 */
void
hash_public_key( MD_HANDLE md, PKT_public_key *pk )
{
    PACKET pkt;
    int rc = 0;
    int ctb;
    ulong pktlen;
    int c;
    IOBUF a = iobuf_temp();
  #if 0
    FILE *fp = fopen("dump.pk", "a");
    int i=0;

    fprintf(fp, "\nHashing PK (v%d):\n", pk->version);
  #endif

    /* build the packet */
    init_packet(&pkt);
    pkt.pkttype = PKT_PUBLIC_KEY;
    pkt.pkt.public_key = pk;
    if( (rc = build_packet( a, &pkt )) )
	log_fatal("build public_key for hashing failed: %s\n", g10_errstr(rc));

    if( !(pk->version == 3 && pk->pubkey_algo == 16) ) {
	/* skip the constructed header but don't do this for our very old
	 * v3 ElG keys */
	ctb = iobuf_get_noeof(a);
	pktlen = 0;
	if( (ctb & 0x40) ) {
	    c = iobuf_get_noeof(a);
	    if( c < 192 )
		pktlen = c;
	    else if( c < 224 ) {
		pktlen = (c - 192) * 256;
		c = iobuf_get_noeof(a);
		pktlen += c + 192;
	    }
	    else if( c == 255 ) {
		pktlen	= iobuf_get_noeof(a) << 24;
		pktlen |= iobuf_get_noeof(a) << 16;
		pktlen |= iobuf_get_noeof(a) << 8;
		pktlen |= iobuf_get_noeof(a);
	    }
	}
	else {
	    int lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
	    for( ; lenbytes; lenbytes-- ) {
		pktlen <<= 8;
		pktlen |= iobuf_get_noeof(a);
	    }
	}
	/* hash a header */
	md_putc( md, 0x99 );
	pktlen &= 0xffff; /* can't handle longer packets */
	md_putc( md, pktlen >> 8 );
	md_putc( md, pktlen & 0xff );
    }
Ejemplo n.º 16
0
Archivo: server.c Proyecto: rzr/connman
static void send_inform(GDHCPServer *dhcp_server,
				struct dhcp_packet *client_packet)
{
	struct dhcp_packet packet;

	init_packet(dhcp_server, &packet, client_packet, DHCPACK);
	add_server_options(dhcp_server, &packet);
	send_packet_to_client(dhcp_server, &packet);
}
Ejemplo n.º 17
0
int parse_gesture(const unsigned char *raw) {

    Packet *p = init_packet(raw);

    printf("%s\n", get_gesture(p));
    destroy_packet(p);

    return 0;

}
Ejemplo n.º 18
0
int init_pongpacket(struct mt_packet *packet, unsigned char *srcmac, unsigned char *dstmac) {
	init_packet(packet, MT_PTYPE_PONG, srcmac, dstmac, 0, 0);

	/* Zero out sessionkey & counter */
	bzero(packet->data + 14, 4);

	/* Remove data counter field from header */
	packet->size -= 4;
	return packet->size;
}
Ejemplo n.º 19
0
static void
write_header( cipher_filter_context_t *cfx, IOBUF a )
{
    PACKET pkt;
    PKT_encrypted ed;
    byte temp[18];
    unsigned blocksize;
    unsigned nprefix;

    blocksize = cipher_get_blocksize( cfx->dek->algo );
    if( blocksize < 8 || blocksize > 16 )
        log_fatal("unsupported blocksize %u\n", blocksize );

    memset( &ed, 0, sizeof ed );
    ed.len = cfx->datalen;
    ed.extralen = blocksize+2;
    ed.new_ctb = !ed.len && !RFC1991;
    if( cfx->dek->use_mdc ) {
        ed.mdc_method = DIGEST_ALGO_SHA1;
        cfx->mdc_hash = md_open( DIGEST_ALGO_SHA1, 0 );
        if ( DBG_HASHING )
            md_start_debug( cfx->mdc_hash, "creatmdc" );
    }

    {
        char buf[20];

        sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
        write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
    }

    init_packet( &pkt );
    pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
    pkt.pkt.encrypted = &ed;
    if( build_packet( a, &pkt ))
        log_bug("build_packet(ENCR_DATA) failed\n");
    nprefix = blocksize;
    randomize_buffer( temp, nprefix, 1 );
    temp[nprefix] = temp[nprefix-2];
    temp[nprefix+1] = temp[nprefix-1];
    print_cipher_algo_note( cfx->dek->algo );
    cfx->cipher_hd = cipher_open( cfx->dek->algo,
                                  cfx->dek->use_mdc? CIPHER_MODE_CFB
                                  : CIPHER_MODE_AUTO_CFB, 1 );
    /*   log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
    cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
    cipher_setiv( cfx->cipher_hd, NULL, 0 );
    /*  log_hexdump( "prefix", temp, nprefix+2 ); */
    if( cfx->mdc_hash ) /* hash the "IV" */
        md_write( cfx->mdc_hash, temp, nprefix+2 );
    cipher_encrypt( cfx->cipher_hd, temp, temp, nprefix+2);
    cipher_sync( cfx->cipher_hd );
    iobuf_write(a, temp, nprefix+2);
    cfx->header=1;
}
Ejemplo n.º 20
0
int sendNAK(struct dhcpMessage *oldpacket)
{
	struct dhcpMessage packet;

	init_packet(&packet, oldpacket, DHCPNAK);
	
	LOG(LOG_INFO, "broadcasting NAK to %02x:%02x:%02x:%02x:%02x:%02x",
	    packet.chaddr[0], packet.chaddr[1], packet.chaddr[2], 
	    packet.chaddr[3], packet.chaddr[4], packet.chaddr[5]);
	return send_packet(&packet, 1);
}
Ejemplo n.º 21
0
/**
 * Sends the picture currently in the pixbuf over serial
 */
int send_picture_msg(lua_State *_l) {
	uint32_t x, y;
  	uint32_t size_x, size_y;

    // only send if we're in debug mode
    if (debug_on) {
        init_packet();
        // First the header
        // first the ID we have to put it in two bytes
        // big endian
        put_slip_char(get_msg_id_most_repr(PPM_MSG_ID));
        put_slip_char(get_msg_id_least_repr(PPM_MSG_ID));
        
        // now the param count. 1 bytes
        put_slip_char(1);
        
        cc3_pixbuf_rewind(); // just in case
        uint8_t *row = cc3_malloc_rows(1);
        
        // now parameters
        size_x = cc3_g_pixbuf_frame.width;
        size_y = cc3_g_pixbuf_frame.height;
        char* head = malloc(20);
        sprintf(head,"P6\n%d %d\n255\n",size_x,size_y );
        uint32_t size = strlen(head)+size_x*size_y*3;
        
        // size is 3 bytes, so we first get
        // rid of most representative byte
        put_slip_char(get_size_most_repr(size));
        put_slip_char(get_size_middle_repr(size));
        put_slip_char(get_size_least_repr(size));
        put_slip_string(head);
        free(head);
        
		bool led_on = false;
        for (y = 0; y < size_y; y++) {
            cc3_pixbuf_read_rows(row, 1);
        	cc3_led_set_state(0, led_on=!led_on);
            for (x = 0; x < size_x * 3U; x++) {
                /* Sleep to prevent byte-loss. Seems a bit extreme, but 
                   I saw problems with even 1000 and 500 as intervals. */
                if (x % 100 == 0) {
                    cc3_timer_wait_ms (5);
                }
                put_slip_char(row[x]);
            }
        }
        cc3_led_set_state(0, false);
        free(row);
        finish_packet();
    }    
    return 0;
}
Ejemplo n.º 22
0
int parse_packet(const unsigned char *raw) {

    Packet *p = init_packet(raw);

    printf("x=%f\ty=%f\tz=%f\tval=%f\n", p->acc_x, p->acc_y, p->acc_z, p->w);

    printf("gesture=%s\narm=%s\n", get_gesture(p), get_arm(p));
    destroy_packet(p);

    return 0;

}
Ejemplo n.º 23
0
int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr)
{
	struct dhcpMessage packet;
	struct option_set *curr;
	unsigned char *lease_time;
	u_int32_t lease_time_align = server_config.lease;
	struct in_addr addr;

	init_packet(&packet, oldpacket, DHCPACK);
	packet.yiaddr = yiaddr;
	
	if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
		memcpy(&lease_time_align, lease_time, 4);
		lease_time_align = ntohl(lease_time_align);
		if (lease_time_align > server_config.lease) 
			lease_time_align = server_config.lease;
		else if (lease_time_align < server_config.min_lease) 
			lease_time_align = server_config.lease;
	}
	
	add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
	
	curr = server_config.options;
	while (curr) {
		if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
			add_option_string(packet.options, curr->data);
		curr = curr->next;
	}

	add_bootp_options(&packet);

	addr.s_addr = packet.yiaddr;
	// LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));
	

	if(get_option(oldpacket, DHCP_SERVER_ID)){	// by honor
		LOG(LOG_INFO, "broadcasting ACK of %s to %02x:%02x:%02x:%02x:%02x:%02x",  inet_ntoa(addr),
		    packet.chaddr[0], packet.chaddr[1], packet.chaddr[2], 
		    packet.chaddr[3], packet.chaddr[4], packet.chaddr[5]);
		if (send_packet(&packet, 1) < 0) return -1;
	}
	else{
		LOG(LOG_INFO, "sending ACK of %s to %02x:%02x:%02x:%02x:%02x:%02x", inet_ntoa(addr),
		    packet.chaddr[0], packet.chaddr[1], packet.chaddr[2], 
		    packet.chaddr[3], packet.chaddr[4], packet.chaddr[5]);
		if (send_packet(&packet, 0) < 0) return -1;
	}

	add_lease(packet.chaddr, packet.yiaddr, lease_time_align);

	return 0;
}
Ejemplo n.º 24
0
/**
 * \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;
}
Ejemplo n.º 25
0
Archivo: umidi.c Proyecto: bluhm/sys
static usbd_status
open_out_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *))
{
	if (jack->opened)
		return USBD_IN_USE;

	jack->arg = arg;
	jack->u.out.intr = intr;
	init_packet(&jack->packet);
	jack->opened = 1;
	jack->endpoint->num_open++;
	
	return USBD_NORMAL_COMPLETION;
}
Ejemplo n.º 26
0
Archivo: server.c Proyecto: rzr/connman
static void send_NAK(GDHCPServer *dhcp_server,
			struct dhcp_packet *client_packet)
{
	struct dhcp_packet packet;

	init_packet(dhcp_server, &packet, client_packet, DHCPNAK);

	debug(dhcp_server, "Sending NAK");

	dhcp_send_raw_packet(&packet,
			dhcp_server->server_nip, SERVER_PORT,
			INADDR_BROADCAST, CLIENT_PORT, MAC_BCAST_ADDR,
			dhcp_server->ifindex);
}
Ejemplo n.º 27
0
/**
 * \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;
}
Ejemplo n.º 28
0
Archivo: test.c Proyecto: uranix/yantp
int main() {
    struct packet p;
    init_packet(&p);
    print_packet(&p);
    tag_client_send(&p);
    print_packet(&p);
    tag_server_recv(&p);
    print_packet(&p);
    tag_server_send(&p);
    print_packet(&p);
    tag_client_recv(&p);
    print_packet(&p);
    return 0;
}
Ejemplo n.º 29
0
int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr)
{
	struct dhcpMessage packet;
	struct option_set *curr;
	char *lease_time;
	u_int32_t lease_time_align = server_config.lease;
	struct in_addr addr;
	char *hostname;
	char hostname_copy[64];
	
	init_packet(&packet, oldpacket, DHCPACK);
	packet.yiaddr = yiaddr;
	
	if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
		memcpy(&lease_time_align, lease_time, 4);
		lease_time_align = ntohl(lease_time_align);
		if (lease_time_align > server_config.lease) 
			lease_time_align = server_config.lease;
		else if (lease_time_align < server_config.min_lease) 
			lease_time_align = server_config.lease;
	}
	
	add_simple_option(packet.options, DHCP_LEASE_TIME, lease_time_align);
	
	curr = server_config.options;
	while (curr) {
		if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
			add_option_string(packet.options, curr->data);
		curr = curr->next;
	}

	add_bootp_options(&packet);

	addr.s_addr = packet.yiaddr;
	LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));

	if (send_packet(&packet, 0) < 0) 
		return -1;

	hostname = get_option(oldpacket, DHCP_HOST_NAME);
	memset(hostname_copy, 0, sizeof(hostname_copy));
	if (hostname) {
	    strncpy(hostname_copy, hostname, hostname[-1]);
	}
			
	add_lease(packet.chaddr, packet.yiaddr, lease_time_align, hostname_copy);

	return 0;
}
Ejemplo n.º 30
0
void send_ack(pcap_t *p, struct pktbuf *inpkt, struct client_conf *client) {
    struct pktbuf *pkt;

    pkt = malloc(sizeof(struct pktbuf));
    init_packet(pkt, inpkt, client);

    pkt->dhcp_packet->xid = inpkt->dhcp_packet->xid;
    build_options(pkt, DHCPACK, client);

    fprintf(stderr, "\n\nWriting DHCPACK to socket: %d bytes\n\n", pkt->pktlen);

    send_packet(p, pkt);

    free(pkt);
}