Пример #1
0
/*---------------------------------------------------------------------------*/
static void
send_next_packet(void *ptr)
{
  struct rudolph1mh_conn *c = ptr;
  int len;
  if(c->nacks == 0) {
    len = format_data(c, c->chunk);
    mesh_send(&c->mesh, &c->partner);

    ctimer_set(&c->t, c->send_interval, send_next_packet, c);


    PRINTF("%d.%d: send_next_packet s_id %d, chunk %d\n",
        rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
        c->s_id, c->chunk);
    c->highest_chunk = c->chunk;
    if(len == RUDOLPH1MH_DATASIZE) { //Continue sending last packet
      c->chunk++;
    }
  }
  else {
    ctimer_set(&c->t, c->send_interval, send_next_packet, c);
  }
  c->nacks = 0;
}
Пример #2
0
/*---------------------------------------------------------------------------*/
void
rudolph1_send(struct rudolph1_conn *c, clock_time_t send_interval)
{
  c->version++;
  c->chunk = c->highest_chunk_heard = 0;
  /*  c->trickle_interval = TRICKLE_INTERVAL;*/
  format_data(c, 0);
  trickle_send(&c->trickle);
  c->chunk++;
  c->send_interval = send_interval;
  ctimer_set(&c->t, send_interval, send_next_packet, c);
}
Пример #3
0
// Main function
int main(int argc, char *argv[])
{

  // If there arguments passed to this program
  if (argc == 4)
     format_data(argv[1], argv[2], atoi(argv[3]));
  // Else print the usage
  else
     printf("Usage: DataExtract input_buffer_file_path pattern n_arg\n\r");

  return 0;
}
Пример #4
0
void adv_report_cb(struct adv_report *report)
{
	DBG("adv type %02x, addr type %02x", report->type, report->addr.type);
	DBG("address %s, data %s", format_address(report->addr.addr),
					format_data(report->data, report->len));

	memcpy(&peer_addr, &report->addr, sizeof(bdaddr_t));

	ll_scan_stop();
	ll_conn_create(SCAN_INTERVAL, SCAN_WINDOW, &peer_addr, 1);

}
Пример #5
0
/*---------------------------------------------------------------------------*/
static int
send_data(struct rudolph2_conn *c, clock_time_t interval)
{
  int len;

  len = format_data(c, c->snd_nxt);
  polite_send(&c->c, interval, POLITE_HEADER);
  PRINTF("%d.%d: send_data chunk %d, rcv_nxt %d\n",
	 rimeaddr_node_addr.u8[RIMEADDR_SIZE-2], rimeaddr_node_addr.u8[RIMEADDR_SIZE-1],
	 c->snd_nxt, c->rcv_nxt);

  return len;
}
Пример #6
0
  virtual int put (ACE_Message_Block *mblk, ACE_Time_Value *) 
  {
  
    for (ACE_Message_Block *temp = mblk;      temp != 0;   temp = temp->cont ())    
    {     
    	 if (temp->msg_type () != ACE_Message_Block::MB_STOP)
      {
      	format_data(temp);
      }
    }
    
    return put_next (mblk);
 } 
Пример #7
0
/*---------------------------------------------------------------------------*/
static void
recv_ipolite(struct ipolite_conn *ipolite, const rimeaddr_t *from)
{
  struct rudolph1_conn *c = (struct rudolph1_conn *)
    ((char *)ipolite - offsetof(struct rudolph1_conn, ipolite));
  struct rudolph1_datapacket *p = packetbuf_dataptr();

  PRINTF("%d.%d: Got ipolite type %d\n",
	 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	 p->h.type);

  c->nacks++;

  if(p->h.type == TYPE_NACK) {
    PRINTF("%d.%d: Got NACK for %d:%d (%d:%d)\n",
	   rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	   p->h.version, p->h.chunk,
	   c->version, c->chunk);
    if(p->h.version == c->version) {
      if(p->h.chunk < c->chunk) {
	/* Format and send a repair packet */
	PRINTF("%d.%d: sending repair for chunk %d\n",
	       rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	       p->h.chunk);
	format_data(c, p->h.chunk);
	ipolite_send(&c->ipolite, REPAIR_TIMEOUT, sizeof(struct rudolph1_hdr));
      }
    } else if(LT(p->h.version, c->version)) {
      format_data(c, 0);
      ipolite_send(&c->ipolite, c->send_interval / 2, sizeof(struct rudolph1_hdr));
    }
  } else if(p->h.type == TYPE_DATA) {
    /* This is a repair packet from someone else. */
    PRINTF("%d.%d: got repair for chunk %d\n",
	   rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	   p->h.chunk);
    handle_data(c, p);
  }
}
Пример #8
0
/*
 * deal with incorrect operation
 */
int error_response(ServiceHandler h,char * errorinfo)
{
    char Buf[MAX_BUF_LEN] = "\0";
    int BufSize = MAX_BUF_LEN;
    mDataFormat data = (mDataFormat) malloc(sizeof(struct DataFormat));
    data->cmd = ERROR_RESP;
    data->value_num = 1;
    data->len_value1 = strlen(errorinfo);
    data->value1 = errorinfo;

    BufSize = format_data(Buf, data);
    send_data(h,Buf,BufSize);
    free(data);
    return 0;    
}
Пример #9
0
/*---------------------------------------------------------------------------*/
static void
send_next_packet(void *ptr)
{
  struct rudolph1_conn *c = ptr;
  int len;
  if(c->nacks == 0) {
    len = format_data(c, c->chunk);
    trickle_send(&c->trickle);
    if(len == RUDOLPH1_DATASIZE) {
      ctimer_set(&c->t, c->send_interval, send_next_packet, c);
    }
    PRINTF("%d.%d: send_next_packet chunk %d, next %d\n",
	   rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	   c->chunk, c->chunk + 1);

    c->highest_chunk_heard = c->chunk;
    c->chunk++;
    
  } else {
    ctimer_set(&c->t, c->send_interval, send_next_packet, c);
  }
  c->nacks = 0;
}
Пример #10
0
void parse_and_execute(char *dir, char *presetdir,
                       char *url, int ulen, buffer_t *buf)
{
    char *urlend = url + ulen;
    if(urlend == url)
        return; // Empty path, can't do anything
    if(ulen >= 11 && !strncmp(urlend - 11, "/index.json", 11)) {
        if(ulen == 11) {
            // It's root index, let's send short index
            STDASSERT(buffer_printf(buf, "{\"presets\": [\n"));
            DIR *dhandle = opendir(presetdir);
            if(dhandle) {
                struct dirent *ent;
                int first = TRUE;
                while((ent = readdir(dhandle))) {
                    if(ent->d_name[0] == '.') continue;
                    if(!first) {
                        STDASSERT(buffer_printf(buf, ",\n"));
                    } else {
                        first = FALSE;
                    }
                    STDASSERT(buffer_printf(buf, "\"%s\"", ent->d_name));
                }
                STDASSERT(closedir(dhandle));
            }
            STDASSERT(buffer_printf(buf, "\n], \"hosts\": [\n"));
            dhandle = opendir(dir);
            if(dhandle) {
                struct dirent *ent;
                int first = TRUE;
                while((ent = readdir(dhandle))) {
                    if(ent->d_name[0] == '.') continue;
                    if(!first) {
                        STDASSERT(buffer_printf(buf, ",\n"));
                    } else {
                        first = FALSE;
                    }
                    STDASSERT(buffer_printf(buf, "\"%s\"", ent->d_name));
                }
                STDASSERT(closedir(dhandle));
            }
            STDASSERT(buffer_printf(buf, "]}\n"));
        } else {
            // It's a directory, let's send info on all files
            int dirlen = strlen(dir);
            char fulldir[dirlen + urlend - url - 10];
            strcpy(fulldir, dir);
            if(fulldir[dirlen-1] == '/') {
                fulldir[dirlen-1] = 0;
            }
            assert(url[0] == '/');
            strncat(fulldir, url, urlend - url - 10);
            char *dirs[] = {fulldir, NULL};
            quickvisit_tree(buf, dirs, strlen(fulldir));
        }
    } else {
        char *query = memchr(url, '?', urlend - url);
        if(!query) {
            buffer_printf(buf, "{\"error\": \"no query\"}");
            return;
        }
        int dirlen = strlen(dir);
        char fullpath[dirlen + query - url + 1];
        strcpy(fullpath, dir);
        int fullen = dirlen;
        if(fullpath[dirlen-1] == '/') {
            fullpath[dirlen-1] = 0;
            fullen --;
        }
        assert(url[0] == '/');
        if(*(query-1) == '/') {
            char *dirs[] = {fullpath, NULL};
            traverse_tree(buf, dirs, strlen(fullpath));
            return;
        }
        strncpy(fullpath + fullen, url, query - url);
        fullen += query - url;
        if(fullen > 5 && !memcmp(fullpath+fullen-5, ".json", 5)) {
            fullen -= 5;
            fullpath[fullen] = 0;
        } else {
            fullpath[fullen] = 0;
        }
        char cf[16] = "AVERAGE";
        char sbuf[16];
        time_t start = 0;
        time_t end = 0;
        int step = 0;
        for(char *q = query+1; q < urlend;) {
            char *eq = memchr(q, '=', urlend - q);
            char *amp = memchr(q, '&', urlend - q);
            if(amp && amp < eq) {
                q = amp+1;
                continue;
            }
            if(!eq) break;
            if(!amp) amp = urlend;
            if(eq - q == 2 && !strncmp(q, "cf", 2)) {
                int len = amp - eq - 1;
                if(len > 15) len = 15;
                memcpy(cf, eq+1, len);
                cf[len] = 0;
            } if(eq - q == 5 && !strncmp(q, "start", 5)) {
                int len = amp - eq - 1;
                if(len > 15) len = 15;
                memcpy(sbuf, eq+1, len);
                sbuf[len] = 0;
                start = strtol(sbuf, NULL, 10);
            } if(eq - q == 3 && !strncmp(q, "end", 3)) {
                int len = amp - eq - 1;
                if(len > 15) len = 15;
                memcpy(sbuf, eq+1, len);
                sbuf[len] = 0;
                end = strtol(sbuf, NULL, 10);
            } if(eq - q == 4 && !strncmp(q, "step", 4)) {
                int len = amp - eq - 1;
                if(len > 15) len = 15;
                memcpy(sbuf, eq+1, len);
                sbuf[len] = 0;
                step = strtol(sbuf, NULL, 10);
            }
            q = amp+1;
        }
        format_data(buf, fullpath, cf, start, end, step);
    }
}
Пример #11
0
int main()
{
    char Buf[MAX_BUF_LEN] = "\0";
    int BufSize = MAX_BUF_LEN;
    char *str = "arming.hdb";
    char val1[MAX_BUF_LEN] = "\0";
    char val2[MAX_BUF_LEN] = "this is a teate ljfdkla  !";
    char val3[MAX_BUF_LEN]="\0";
    char val4[MAX_BUF_LEN]="\0";

    mDataFormat data = (mDataFormat) malloc(sizeof(struct DataFormat));
    data->cmd = SET_CMD;
    data->value_num = 2;
    val1[0]='2';
    data->len_value1 = strlen(val1);
    data->value1 = val1;
    data->len_value2 = strlen(val2);
    data->value2 = val2;
    int size = format_data(Buf, data);
    
    printf("format_data %s  size %d\n", Buf,size);

    mDataFormat rec = (mDataFormat) malloc(sizeof(struct DataFormat));
    rec->value1 = val3;
    rec->value2 = val4;
    parse_data(Buf, rec);
    printf("rec %d, %d, %s \n", rec->cmd, rec->value_num, rec->value1);
    assert(rec->cmd > 0);
    if (rec->cmd == SET_CMD)
    {
        printf("success! value1 %s\n",rec->value1);
    }
    else
    {
        printf("test error!\n");
    }
    // if(cmd == CLOSE_CMD && DataNum == 0)
    // {
    //     printf("Test Zero Data Result: Pass\n");
    // }
    // BufSize = MAX_BUF_LEN;
    // FormatData1(Buf,&BufSize,OPEN_CMD,str,strlen(str));
    // ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size);
    // if(cmd == OPEN_CMD && DataNum == 1
    //     && Data1Size == strlen(str)
    //     && strcmp(Data1,str) == 0)
    // {
    //     printf("%s\n",Data1);
    //     printf("Test One Data Result: Pass\n");
    // }
    // BufSize = MAX_BUF_LEN;
    // FormatData2(Buf,&BufSize,SET_CMD,str,strlen(str),str,strlen(str));
    // ParseData(Buf,MAX_BUF_LEN,&cmd,&DataNum,Data1,&Data1Size,Data2,&Data2Size);
    // if(cmd == SET_CMD && DataNum == 2
    //     && Data1Size == strlen(str) && Data2Size == strlen(str)
    //     && strcmp(Data1,str) == 0 && strcmp(Data2,str) == 0)
    // {
    //     printf("%s\n",Data1);
    //     printf("%s\n",Data2);
    //     printf("Test Two Data Result: Pass\n");
    // }
    return 0;
}
Пример #12
0
void csv_archive( csv_context_t* csv_buffer_list, buffer_t* url_str,
                  st_info_t* st_info )
{
    csv_buffer_t* csv_buffer;
    csv_row_t* csv_row;
    uint8_t* msh_data;
    size_t msh_length = 0;
    csv_buffer_t* final_csv = NULL;

    char time_string[32];

    // Step through the csv buffers list
    list_iterator_stop(csv_buffer_list);
    list_iterator_start(csv_buffer_list);
    while (list_iterator_hasnext(csv_buffer_list)) 
    {
        // Grab one buffer from the list
        csv_buffer = (csv_buffer_t*)list_iterator_next(csv_buffer_list);
        if (gDebug)
        {
          printf("%s[%d]: elapsed time %d <> %d TM_HOUR\n",
            csv_buffer->header->description, csv_buffer->header->channel,
            (int)(csv_buffer->end_time - csv_buffer->start_time), TM_HOUR);
        }
        while ((csv_buffer->end_time - csv_buffer->start_time) >= TM_HOUR) 
        {
            // Compress the csv data to FMash format
            fmash_csv_to_msh(csv_buffer, &msh_data, &msh_length);
            if (gDebug) {
                // Brag about our accomplishments
                fprintf(stdout, "compressed data size is %lu bytes\n", (unsigned long)msh_length);
                format_data(msh_data, msh_length, 0, 0);

                //* Verify the contents of the buffer are correct...
                if (!fmash_msh_to_csv( &final_csv, msh_data, msh_length )) {
                    fprintf(stdout, "FMash to CSV conversion failed\n");
                    goto queue_it;
                }
                if (!final_csv) {
                    fprintf(stdout, "final_csv not created\n");
                    goto queue_it;
                }
                if (!final_csv->header) {
                    fprintf(stdout, "final_csv->header not created\n");
                    goto queue_it;
                }
                if (!final_csv->list) {
                    fprintf(stdout, "final_csv->list not created\n");
                    goto queue_it;
                }
                fprintf(stdout, "=== FMASH VERIFICATION =================================\n");
                fprintf(stdout, "    file:         %s\n", csv_buffer->file_name);
                fprintf(stdout, "    channel:      %d\n", csv_buffer->header->channel);
                fprintf(stdout, "    description:  %s\n", csv_buffer->header->description);
                fprintf(stdout, "    lines:        %d\n", list_size(final_csv->list));
                list_iterator_stop(final_csv->list);
                list_iterator_start(final_csv->list);
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                    fprintf(stdout, "       | Timestamp            | Average   | High      | Low       |\n");
                    fprintf(stdout, "        ---------------------- ----------- ----------- ----------- \n");
                while (list_iterator_hasnext(final_csv->list))
                {
                    csv_row = (csv_row_t*)list_iterator_next(final_csv->list);
                    strftime(time_string, 31, "%Y/%m/%d %H:%M:%S", gmtime(&(csv_row->timestamp)));
                    printf("       | %s | % 9d | % 9d | % 9d |\n", time_string,
                           csv_row->average, csv_row->high, csv_row->low );
                }
                printf("\n");
                list_iterator_stop(final_csv->list);
                final_csv = csv_buffer_destroy(final_csv);
                fflush(stdout);
                // */
            }
 queue_it:
            // Add this as an opaque record
            if (msh_data && msh_length) {
                QueueOpaque(msh_data, msh_length, st_info->station,
                            st_info->network, st_info->csv_chan,
                            st_info->location, FALCON_IDSTRING);
                free(msh_data);
                msh_data = NULL;
            }
        }
        csv_buffer = NULL;
    }
    list_iterator_stop(csv_buffer_list);
}
Пример #13
0
static int sm_encrypt(const struct iso_sm_ctx *ctx, sc_card_t *card,
        const sc_apdu_t *apdu, sc_apdu_t **psm_apdu)
{
    struct sc_asn1_entry sm_capdu[4];
    u8 *p, *le = NULL, *sm_data = NULL, *fdata = NULL, *mac_data = NULL,
       *asn1 = NULL, *mac = NULL, *resp_data = NULL;
    size_t sm_data_len, fdata_len, mac_data_len, asn1_len, mac_len, le_len;
    int r, cse;
    sc_apdu_t *sm_apdu = NULL;

    if (!apdu || !ctx || !card || !card->reader || !psm_apdu) {
        r = SC_ERROR_INVALID_ARGUMENTS;
        goto err;
    }

    if ((apdu->cla & 0x0C) == 0x0C) {
        r = SC_ERROR_INVALID_ARGUMENTS;
        sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Given APDU is already protected with some secure messaging");
        goto err;
    }

    sc_copy_asn1_entry(c_sm_capdu, sm_capdu);

    sm_apdu = malloc(sizeof(sc_apdu_t));
    if (!sm_apdu) {
        r = SC_ERROR_OUT_OF_MEMORY;
        goto err;
    }
    sm_apdu->control = apdu->control;
    sm_apdu->flags = apdu->flags;
    sm_apdu->cla = apdu->cla|0x0C;
    sm_apdu->ins = apdu->ins;
    sm_apdu->p1 = apdu->p1;
    sm_apdu->p2 = apdu->p2;
    r = format_head(ctx, sm_apdu, &mac_data);
    if (r < 0) {
        sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format header of SM apdu");
        goto err;
    }
    mac_data_len = r;

    /* get le and data depending on the case of the insecure command */
    cse = apdu->cse;
    if ((apdu->le/ctx->block_length + 1)*ctx->block_length + 18 > 0xff+1)
        /* for encrypted APDUs we usually get authenticated status bytes (4B),
         * a MAC (11B) and a cryptogram with padding indicator (3B without
         * data).  The cryptogram is always padded to the block size. */
        cse |= SC_APDU_EXT;

    switch (cse) {
        case SC_APDU_CASE_1:
            break;
	case SC_APDU_CASE_2_SHORT:
            le_len = 1;
            r = format_le(apdu->le, sm_capdu + 1, &le, &le_len);
            if (r < 0) {
                sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format Le of SM apdu");
                goto err;
            }
            bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Protected Le (plain)", le, le_len);
            break;
	case SC_APDU_CASE_2_EXT:
            if (card->reader->active_protocol == SC_PROTO_T0) {
                /* T0 extended APDUs look just like short APDUs */
                le_len = 1;
                r = format_le(apdu->le, sm_capdu + 1, &le, &le_len);
                if (r < 0) {
                    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format Le of SM apdu");
                    goto err;
                }
            } else {
                /* in case of T1 always use 2 bytes for length */
                le_len = 2;
                r = format_le(apdu->le, sm_capdu + 1, &le, &le_len);
                if (r < 0) {
                    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format Le of SM apdu");
                    goto err;
                }
            }
            bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Protected Le (plain)", le, le_len);
            break;
        case SC_APDU_CASE_3_SHORT:
        case SC_APDU_CASE_3_EXT:
            r = format_data(card, ctx, apdu->data, apdu->datalen,
                    sm_capdu + 0, &fdata, &fdata_len);
            if (r < 0) {
                sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format data of SM apdu");
                goto err;
            }
            bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Padding-content indicator followed by cryptogram (plain)",
                    fdata, fdata_len);
            break;
        case SC_APDU_CASE_4_SHORT:
            /* in case of T0 no Le byte is added */
            if (card->reader->active_protocol != SC_PROTO_T0) {
                le_len = 1;
                r = format_le(apdu->le, sm_capdu + 1, &le, &le_len);
                if (r < 0) {
                    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format Le of SM apdu");
                    goto err;
                }
                bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Protected Le (plain)", le, le_len);
            }

            r = format_data(card, ctx, apdu->data, apdu->datalen,
                    sm_capdu + 0, &fdata, &fdata_len);
            if (r < 0) {
                sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format data of SM apdu");
                goto err;
            }
            bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Padding-content indicator followed by cryptogram (plain)",
                    fdata, fdata_len);
            break;
        case SC_APDU_CASE_4_EXT:
            if (card->reader->active_protocol == SC_PROTO_T0) {
                /* again a T0 extended case 4 APDU looks just
                 * like a short APDU, the additional data is
                 * transferred using ENVELOPE and GET RESPONSE */
            } else {
                /* only 2 bytes are use to specify the length of the
                 * expected data */
                le_len = 2;
                r = format_le(apdu->le, sm_capdu + 1, &le, &le_len);
                if (r < 0) {
                    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format Le of SM apdu");
                    goto err;
                }
                bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Protected Le (plain)", le, le_len);
            }

            r = format_data(card, ctx, apdu->data, apdu->datalen,
                    sm_capdu + 0, &fdata, &fdata_len);
            if (r < 0) {
                sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not format data of SM apdu");
                goto err;
            }
            bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Padding-content indicator followed by cryptogram (plain)",
                    fdata, fdata_len);
            break;
        default:
            sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Unhandled apdu case");
            r = SC_ERROR_INVALID_DATA;
            goto err;
    }


    r = sc_asn1_encode(card->ctx, sm_capdu, (u8 **) &asn1, &asn1_len);
    if (r < 0) {
        goto err;
    }
    if (asn1_len) {
        p = realloc(mac_data, mac_data_len + asn1_len);
        if (!p) {
            r = SC_ERROR_OUT_OF_MEMORY;
            goto err;
        }
        mac_data = p;
        /* Flawfinder: ignore */
        memcpy(mac_data + mac_data_len, asn1, asn1_len);
        mac_data_len += asn1_len;
        r = add_padding(ctx, mac_data, mac_data_len, &mac_data);
        if (r < 0) {
            goto err;
        }
        mac_data_len = r;
    }
    bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Data to authenticate", mac_data, mac_data_len);

    r = ctx->authenticate(card, ctx, mac_data, mac_data_len,
            &mac);
    if (r < 0) {
        sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not get authentication code");
        goto err;
    }
    mac_len = r;
    bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Cryptographic Checksum (plain)", mac, mac_len);


    /* format SM apdu */
    sc_format_asn1_entry(sm_capdu + 2, mac, &mac_len, SC_ASN1_PRESENT);
    r = sc_asn1_encode(card->ctx, sm_capdu, (u8 **) &sm_data, &sm_data_len);
    if (r < 0)
        goto err;
    sm_apdu->data = sm_data;
    sm_apdu->datalen = sm_data_len;
    sm_apdu->lc = sm_data_len;
    sm_apdu->le = 0;
    if (cse & SC_APDU_EXT) {
        sm_apdu->cse = SC_APDU_CASE_4_EXT;
#if OPENSC_NOT_BOGUS_ANYMORE
        sm_apdu->resplen = 0xffff+1;
#else
        sm_apdu->resplen = SC_MAX_EXT_APDU_BUFFER_SIZE;
#endif
    } else {
        sm_apdu->cse = SC_APDU_CASE_4_SHORT;
#if OPENSC_NOT_BOGUS_ANYMORE
        sm_apdu->resplen = 0xff+1;
#else
        sm_apdu->resplen = SC_MAX_APDU_BUFFER_SIZE;
#endif
    }
    resp_data = malloc(sm_apdu->resplen);
    if (!resp_data) {
        r = SC_ERROR_OUT_OF_MEMORY;
        goto err;
    }
    sm_apdu->resp = resp_data;
    bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "ASN.1 encoded encrypted APDU data", sm_apdu->data, sm_apdu->datalen);

    *psm_apdu = sm_apdu;

err:
    free(fdata);
    free(asn1);
    free(mac_data);
    free(mac);
    free(le);
    if (r < 0) {
        free(resp_data);
        free(sm_apdu);
        free(sm_data);
    }

    return r;
}
Пример #14
0
int handle_one_request(ServiceHandler h, char *buf, int buf_size)
{

    debug;
    char Buf[MAX_BUF_LEN] = "\0";
    int BufSize = MAX_BUF_LEN;
    char Data1[MAX_BUF_LEN] = "\0";
    char Data2[MAX_BUF_LEN] = "\0";

    mDataFormat data = (mDataFormat) malloc(sizeof(struct DataFormat));
    data->value1 = Data1;
    data->value2 = Data2;

    memset(Data1, 0, MAX_BUF_LEN);
    memset(Data2, 0, MAX_BUF_LEN);
    data->value1 = Data1;
    data->value2 = Data2;

    /*if(receive_data(h,Buf,&BufSize) == 0)
      {
          fprintf(stderr,"Connection Error,%s:%d\n",__FILE__,__LINE__);
          return -1;            
      }*/
    if(BufSize == 0)
    {
        free(data);
        return -1;
    }
    int ret = parse_data(buf,data);

    // print the received data
    // printRec(data);

    if(ret == -1)
    {
        error_response(h,"Data Format Error!\n");
        printf("server parse_data error!\n");
        free(data);
        return -1;
    }
    if(data->cmd == OPEN_CMD)
    {
        debug;
        Database db = createNewDB(data->value1);
        debug_argv("open db:%p\n",db);
        match_sockfd_mdb(h, db);
        BufSize = MAX_BUF_LEN;
        data->value_num = 0;
        BufSize = format_data(Buf,data);
        assert(BufSize > 0);
        send_data(h,Buf,BufSize);
    }
    else if(data->cmd == CLOSE_CMD)
    {
        debug_argv("close db\n");
        Database db = NULL;
        get_mdb(h, &db);
        closeDB(db);
        BufSize = MAX_BUF_LEN;
        data->value_num = 0;
        BufSize = format_data(Buf,data);
        send_data(h,Buf,BufSize);
        detach_sockfd_mdb(h);
        service_stop(h);
        free(data);
        return 0;
    }
    else if(data->cmd == GET_CMD)
    {
        Database db = NULL;
        get_mdb(h, &db);

        //debug_argv("get db:%p\n",db);
        int key = *(int *)data->value1;
        Data value;

        char tem[MAX_BUF_LEN] = "\0";
        value.value = tem;
        ret = getValueByKey(db, key, &value);
       // printf("server getdata value => %s\n", value.value);
        if(ret == -1)
        {
            error_response(h,"The key NOT FOUND!\n");
            free(data);
            return -1;              
        }
        BufSize = MAX_BUF_LEN;
        data->value_num = 1;
        data->len_value1 = strlen(value.value);
        data->value1 = value.value;
        BufSize = format_data(Buf,data);
        send_data(h,Buf,BufSize);

    }
    else if(data->cmd == SET_CMD)
    {
        int key = *(int *)data->value1;
        Data value;
        value.value = data->value2;
        value.length = data->len_value2;
        Database db = NULL;
        get_mdb(h, &db);
        debug_argv("set: db:%p\n",db);
        ret = putKeyValue(db,key,&value);
        if (ret == -1)
        {
            error_response(h,"Save key & value error!\n");
            printf("set error : %d, %s\n", key, value.value);
            free(data);
            return -1;
        }
        BufSize = MAX_BUF_LEN;
        data->value_num = 0;
        BufSize = format_data(Buf,data);
        send_data(h,Buf,BufSize);    

    }
    else if(data->cmd == DEL_CMD)
    {
        int key = *(int *)data->value1;;           
        Database db = NULL;
        get_mdb(h, &db);
        debug_argv("delete: db:%p\n",db);
        ret = deleteValueByKey(db,key);
        if(ret == -1)
        {
            error_response(h,"The key NOT FOUND!\n");
            free(data);
            return -1;
        }            
        BufSize = MAX_BUF_LEN;
        data->value_num = 0;
        BufSize = format_data(Buf,data);
        send_data(h,Buf,BufSize);             
    }
    else
    {
        printf("Unknow Request!\n");
        error_response(h, "Unknow Request!\n");
    }                                   
    free(data);
    return 0;
}
Пример #15
0
int tftp_put(uint32_t ip, const char *filename, const void *buffer, int size)
{
	int len, send;
	int tries;
	int i;
	int block = 0, sent = 0;

	if(!microudp_arp_resolve(ip))
		return -1;

	microudp_set_callback(rx_callback);

	packet_data = microudp_get_tx_buffer();

	total_length = 0;
	transfer_finished = 0;
	tries = 5;
	while(1) {
		packet_data = microudp_get_tx_buffer();
		len = format_request(packet_data, TFTP_WRQ, filename);
		microudp_send(PORT_IN, PORT_OUT, len);
		for(i=0;i<2000000;i++) {
			last_ack = -1;
			microudp_service();
			if(last_ack == block)
				goto send_data;
			if(transfer_finished)
				goto fail;
		}
		tries--;
		if(tries == 0)
			goto fail;
	}

send_data:
	do {
		block++;
		send = sent+BLOCK_SIZE > size ? size-sent : BLOCK_SIZE;
		tries = 5;
		while(1) {
			packet_data = microudp_get_tx_buffer();
			len = format_data(packet_data, block, buffer, send);
			microudp_send(PORT_IN, data_port, len);
			for(i=0;i<12000000;i++) {
				microudp_service();
				if(transfer_finished)
					goto fail;
				if(last_ack == block)
					goto next;
			}
			if (!--tries)
				goto fail;
		}
next:
		sent += send;
		buffer += send;
	} while (send == BLOCK_SIZE);

	microudp_set_callback(NULL);

	return sent;

fail:
	microudp_set_callback(NULL);
	return -1;
}
Пример #16
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Write all new alarms to the diskloop.
 */
void alarm_archive( alarm_context_t* alarm_list, buffer_t* url_str,
                    st_info_t* st_info )
{
    time_t   start_time = 0;
    time_t   end_time   = 0;
    uint8_t  buf_byte   = 0;
    uint16_t buf_word   = 0;
    uint32_t buf_dword  = 0L;
    //uint64_t buf_qword  = 0LL;

    uint16_t  version_type = 0x8000 | FALCON_VERSION;
    uint16_t  alarm_count  = 0;
    char*     retmsg     = NULL;
    buffer_t* alarm_data = NULL;
    alarm_line_t* alarm  = NULL;

    alarm_data = buffer_init();

    if (!alarm_data) {
        if (gDebug)
            fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
        else
            syslog(LOG_ERR, "falcon: unable to allocate space for alarm data\n");
        return;
    }

    // Print each line in the filtered list
    list_iterator_stop(alarm_list);
    list_iterator_start(alarm_list);
    while (list_iterator_hasnext(alarm_list)) 
    {
        alarm = (alarm_line_t*)list_iterator_next(alarm_list);
        if (alarm->sent)
            continue;

        if (!start_time)
          start_time = alarm->timestamp;
        if (!end_time)
          end_time = alarm->timestamp;

        if (gDebug)
            fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n", 
                    __FILE__, __LINE__, __DATE__, alarm->text);

        if ((retmsg = q330LogMsg(alarm->text, st_info->station,
                                 st_info->network, "LOG",
                                 st_info->location)) != NULL)
        { // error trying to log the message
            if (gDebug)
                fprintf(stderr, "falcon: failed to write alarms to log: %s\n", retmsg);
            else
                syslog(LOG_ERR, "falcon: failed to write alarms to log: %s\n", retmsg);
            exit(1);
        }

        if (gDebug)
        {
            fprintf(stdout, "Alarm '%s':\n", alarm->description);
            fprintf(stdout, "    Channel    : %02x\n", (alarm->channel));
            fprintf(stdout, "    Timestamp  : %li\n", (long)(alarm->timestamp));
            fprintf(stdout, "    Event Code : 0x%02x\n", (alarm->event));
            fprintf(stdout, "    Sent       : %s\n", alarm->sent ? "Yes" : "No");
            fprintf(stdout, "    Hash       : 0x%08lx\n", (unsigned long)(alarm->hash));
            fprintf(stdout, "    Text       : %s\n", alarm->text);
        }

        if (start_time > alarm->timestamp)
            start_time = alarm->timestamp;
        if (end_time < alarm->timestamp)
            end_time = alarm->timestamp;

        if (!alarm->description[0])
        {
            if (gDebug)
                fprintf(stderr, "falcon: description code not found\n");
            else
                syslog(LOG_ERR, "falcon: description code not found\n");
            continue;
        }

        // There must be at least enough space for one more alarm.
        // If there isn't, queue this buffer's contents, and reset it.
        // This should prevent us from ever fragmenting alarm data
        // across opaque blockettes.
        if (alarm_count && (alarm_data->length > 400))
        {
            buffer_seek(alarm_data, 2);
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));

            if (gDebug) {
                fprintf(stdout, "falcon: [MID] alarms were found\n");
                fprintf(stdout, "[MID] raw buffer:\n");
                format_data(alarm_data->content, alarm_data->length, 0, 0);
            }

            QueueOpaque(alarm_data->content, (int)alarm_data->length,
                        st_info->station, st_info->network,
                        st_info->alarm_chan, st_info->location,
                        FALCON_IDSTRING);
            alarm_data  = buffer_destroy(alarm_data);
            alarm_count = 0;
            start_time  = alarm->timestamp;
            end_time    = alarm->timestamp;
            alarm_data  = buffer_init();
            if (!alarm_data) {
                if (gDebug)
                    fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
                else
                    syslog(LOG_ERR, "falcon: unable to allocate space for alarm data\n");
                return;
            }
        } // If we've built up a records worth of alarm data

        if (!alarm_count)
        {
            // Write alarm header info
            if (gDebug)
            {
                fprintf(stderr, "falcon: Writing alarm header info.\n");
            }
            buf_word = htons(version_type);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
            // Reserve space for elements that will be assigned just 
            // prior to queueing data
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        }

        // Add an alarm
        buf_word = htons(alarm->channel);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        buf_dword = htonl(alarm->timestamp);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buffer_write(alarm_data, &(alarm->event), sizeof(alarm->event));
        buf_byte = (uint8_t)strlen(alarm->description);
        buffer_write(alarm_data, &buf_byte, sizeof(buf_byte));
        buffer_write(alarm_data, (uint8_t*)(alarm->description), (size_t)buf_byte);
        buffer_terminate(alarm_data);

        alarm_count++;
        alarm->sent = 1;
    }
    list_iterator_stop(alarm_list);

    if (alarm_count && alarm_data && alarm_data->content && alarm_data->length)
    {
        buffer_seek(alarm_data, 2);
        buf_dword = htonl(start_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_dword = htonl(end_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_word = htons(alarm_count);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));

        if (gDebug) {
            fprintf(stderr, "falcon: [END] alarms were found\n");
            fprintf(stderr, "[END] raw buffer:\n");
            format_data(alarm_data->content, alarm_data->length, 0, 0);
        }

        QueueOpaque(alarm_data->content, (int)alarm_data->length,
                    st_info->station, st_info->network,
                    st_info->alarm_chan, st_info->location,
                    FALCON_IDSTRING);
    }
    alarm_data = buffer_destroy(alarm_data);

    // Make sure we limit the accumulation of alarm messages
    while (list_size(alarm_list) > MAX_CONTEXT_ALARMS)
    {
        alarm = list_fetch(alarm_list);
        alarm = alarm_line_destroy(alarm);
    }
}
Пример #17
0
/*---------------------------------------------------------------------------*/
static void
recv_mesh(struct mesh_conn *mesh, const rimeaddr_t *from, uint8_t hops)
{
  struct rudolph1mh_conn *c = (struct rudolph1mh_conn *) ((char *) mesh
      - offsetof(struct rudolph1mh_conn, mesh));
  struct rudolph1mh_datapacket *p = packetbuf_dataptr();

  PRINTF("%d.%d: Got mesh type %d from %d.%d\n",
      rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
      p->h.type, from->u8[0], from->u8[1]);

  if(!rimeaddr_cmp(&c->partner, from)){
    if(!rimeaddr_cmp(&c->partner, &rimeaddr_null)){
      rimeaddr_t lfrom;
      PRINTF("%d.%d: Unexpected packet from %d.%d\n",
            rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
            from->u8[0], from->u8[1]);
      rimeaddr_copy(&lfrom, from);
      send_busy(c, &lfrom);
      return;
    } else {
      rimeaddr_copy(&c->partner, from);
    }
  }


  if(p->h.type == TYPE_ACK){
    if(p->h.s_id == c->s_id && p->h.chunk == c->highest_chunk){
      ctimer_stop(&c->t);
      rimeaddr_copy(&c->partner, &rimeaddr_null);
      if(c->cb->read_done){
        c->cb->read_done(c);
      }
      PRINTF("GOT ACK\n");
    } else {
      PRINTF("Unexpected ACK sid %i,%i C %i,%i\n", p->h.s_id, c->s_id, p->h.chunk, c->highest_chunk);
    }



  } else if(p->h.type == TYPE_NACK) {
    c->nacks++;
    PRINTF("%d.%d: Got NACK for %d:%d (%d:%d)\n",
        rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
        p->h.s_id, p->h.chunk,
        c->s_id, c->chunk);
    if(p->h.s_id == c->s_id) {
      if(p->h.chunk < c->chunk) {
        /* Format and send a repair packet */
        PRINTF("%d.%d: sending repair for chunk %d\n",
            rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
            p->h.chunk);
        format_data(c, p->h.chunk);
        mesh_send(&c->mesh, &c->partner);
      }
    }
    else if(LT(p->h.s_id, c->s_id)) {
      format_data(c, 0);
      mesh_send(&c->mesh, &c->partner);
    }
  }
  else if(p->h.type == TYPE_DATA ) {
    /* This is a repair packet from someone else. */
    PRINTF("%d.%d: got chunk %d\n",
        rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
        p->h.chunk);
    handle_data(c, p);
  } else if(p->h.type == TYPE_FIN ) {
    /* This is a repair packet from someone else. */
    PRINTF("%d.%d: got last chunk %d\n",
        rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
        p->h.chunk);
    if(!rimeaddr_cmp(&c->partner, &rimeaddr_null)){
      handle_data(c, p);
    }{
      rimeaddr_copy(&c->partner, from);
    }
    send_ack(c);
    rimeaddr_copy(&c->partner, &rimeaddr_null);

  } else if(p->h.type == TYPE_BUSY){
    PRINTF("%d.%d: %d.%d is busy\n",
        rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
        from->u8[0], from->u8[1]);
    //Wait
  }
}