<commit_before>/* tiny -- tiny sender
 *
 * (c) 2010 <NAME> <<EMAIL>>
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "../coap.h"

coap_pdu_t *
make_pdu( unsigned int value ) {
  coap_pdu_t *pdu;
  unsigned char enc;
  static char buf[20];
  int len;

  if ( ! ( pdu = coap_new_pdu() ) )
    return NULL;

  pdu->hdr->type = COAP_MESSAGE_NON;
  pdu->hdr->code = COAP_REQUEST_POST;
  
  enc = COAP_PSEUDOFP_ENCODE(value);
  coap_add_data( pdu, 1, &enc);

  len = snprintf(buf, 20, "%u", COAP_PSEUDOFP_DECODE(enc));
  if ( len > 0 ) {
    coap_add_data( pdu, len, buf );
  }

  return pdu;
}

void 
usage( const char *program ) {
  const char *p;

  p = strrchr( program, '/' );
  if ( p )
    program = ++p;

  fprintf( stderr, "%s -- tiny fake sensor\n"
	   "(c) 2010 <NAME> <<EMAIL>>\n\n"
	   "usage: %s [group address]\n"
	   "\n\nSends some fake sensor values to specified multicast group\n",
	   program, program );
}

int 
main(int argc, char **argv) {
  coap_context_t  *ctx;
  struct timeval tv;
  coap_pdu_t  *pdu;
  struct sockaddr_in6 dst;
  int hops = 16;

  if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) {
    usage( argv[0] );
    exit( 1 );
  }

  ctx = coap_new_context();
  if ( !ctx )
    return -1;

  memset(&dst, 0, sizeof(struct sockaddr_in6 ));
  dst.sin6_family = AF_INET6;
  inet_pton( AF_INET6, argc > 1 ? argv[1] : "::1", &dst.sin6_addr );
  dst.sin6_port = htons( COAP_DEFAULT_PORT );

  if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) {
    /* set socket options for multicast */ 

    if ( setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
		     (char *)&hops, sizeof(hops) ) < 0 )
      perror("setsockopt: IPV6_MULTICAST_HOPS");

  }

  while ( 1 ) {
    
    if (! (pdu = make_pdu( rand() & 0xfff ) ) )
      return -1;

    coap_send( ctx, &dst, pdu );

    tv.tv_sec = 5; tv.tv_usec = 0;

    select( 0, 0, 0, 0, &tv );
    
  }

  coap_free_context( ctx );

  return 0;
}
<commit_msg>explicitly set transaction id<commit_after>/* tiny -- tiny sender
 *
 * (c) 2010 <NAME> <<EMAIL>>
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "../coap.h"

static coap_tid_t id;

coap_pdu_t *
make_pdu( unsigned int value ) {
  coap_pdu_t *pdu;
  unsigned char enc;
  static unsigned char buf[20];
  int len;

  if ( ! ( pdu = coap_new_pdu() ) )
    return NULL;

  pdu->hdr->type = COAP_MESSAGE_NON;
  pdu->hdr->code = COAP_REQUEST_POST;
  pdu->hdr->id = htons(id++);

  enc = COAP_PSEUDOFP_ENCODE(value);
  coap_add_data( pdu, 1, &enc);

  len = sprintf((char *)buf, "%u", COAP_PSEUDOFP_DECODE(enc));
  if ( len > 0 ) {
    coap_add_data( pdu, len, buf );
  }

  return pdu;
}

void 
usage( const char *program ) {
  const char *p;

  p = strrchr( program, '/' );
  if ( p )
    program = ++p;

  fprintf( stderr, "%s -- tiny fake sensor\n"
	   "(c) 2010 <NAME> <<EMAIL>>\n\n"
	   "usage: %s [group address]\n"
	   "\n\nSends some fake sensor values to specified multicast group\n",
	   program, program );
}

int 
main(int argc, char **argv) {
  coap_context_t  *ctx;
  struct timeval tv;
  coap_pdu_t  *pdu;
  struct sockaddr_in6 dst;
  int hops = 16;

  if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) {
    usage( argv[0] );
    exit( 1 );
  }

  ctx = coap_new_context();
  if ( !ctx )
    return -1;
  id = rand() & ((1 << (8 * sizeof(coap_tid_t))) - 1);

  memset(&dst, 0, sizeof(struct sockaddr_in6 ));
  dst.sin6_family = AF_INET6;
  inet_pton( AF_INET6, argc > 1 ? argv[1] : "::1", &dst.sin6_addr );
  dst.sin6_port = htons( COAP_DEFAULT_PORT );

  if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) {
    /* set socket options for multicast */ 

    if ( setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
		     (char *)&hops, sizeof(hops) ) < 0 )
      perror("setsockopt: IPV6_MULTICAST_HOPS");

  }

  while ( 1 ) {
    
    if (! (pdu = make_pdu( rand() & 0xfff ) ) )
      return -1;

    coap_send( ctx, &dst, pdu );

    tv.tv_sec = 5; tv.tv_usec = 0;

    select( 0, 0, 0, 0, &tv );
    
  }

  coap_free_context( ctx );

  return 0;
}

{'fraction_non_alphanumeric': 0.168348623853211, 'fraction_numerical': 0.0231651376146789, 'mean_word_length': 2.928828828828829, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5237606', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1849, 'n_words': 435}