Beispiel #1
0
static void run_ikev2(void)
{
	print_number("COUNT", count);
	print_chunk("Ni", ni, 0);
	print_chunk("Nr", nr, 0);
	print_symkey("g^ir", g_ir, 0);
	print_symkey("g^ir (new)", g_ir_new, 0);
	print_chunk("SPIi", spi_i, 0);
	print_chunk("SPIr", spi_r, 0);

	if (hasher == NULL) {
		print_line(hasher_name);
		return;
	}

	/* SKEYSEED = prf(Ni | Nr, g^ir) */
	PK11SymKey *skeyseed =
		ikev2_ike_sa_skeyseed(hasher, ni, nr, g_ir);
	print_symkey("SKEYSEED", skeyseed, 0);

	/* prf+(SKEYSEED, Ni | Nr | SPIi | SPIr) */
	PK11SymKey *dkm =
		ikev2_ike_sa_keymat(hasher, skeyseed,
				    ni, nr, spi_i, spi_r, dkm_length / 8);
	print_symkey("DKM", dkm, dkm_length / 8);

	/* prf+(SK_d, Ni | Nr) */
	PK11SymKey *SK_d = key_from_symkey_bytes(dkm, 0, hasher->hash_digest_len);
	PK11SymKey *child_sa_dkm =
		ikev2_child_sa_keymat(hasher, SK_d, NULL, ni, nr, child_sa_dkm_length / 8);
	print_symkey("DKM(Child SA)", child_sa_dkm, child_sa_dkm_length / 8);

	/* prf+(SK_d, g^ir (new) | Ni | Nr) */
	PK11SymKey *child_sa_dkm_dh =
		ikev2_child_sa_keymat(hasher, SK_d, g_ir_new, ni, nr,
				      child_sa_dkm_length / 8);
	print_symkey("DKM(Child SA D-H)", child_sa_dkm_dh, child_sa_dkm_length / 8);

	/* prf(SK_d (old), g^ir (new) | Ni | Nr) */
	PK11SymKey *skeyseed_rekey =
		ikev2_ike_sa_rekey_skeyseed(hasher, SK_d, g_ir_new, ni, nr);
	print_symkey("SKEYSEED(Rekey)", skeyseed_rekey, 0);

	free_any_symkey("skeyseed", &skeyseed);
	free_any_symkey("dkm", &dkm);
	free_any_symkey("SK_d", &SK_d);
	free_any_symkey("child_sa_dkm", &child_sa_dkm);
	free_any_symkey("child_sa_dkm_dh", &child_sa_dkm_dh);
	free_any_symkey("skeyseed_rekey", &skeyseed_rekey);
}
void init_hasChunks(char* has_chunk_file_name) {
    FILE* has_chunk_file = fopen(has_chunk_file_name,"r");

    int chunk_num = 0;
    char read_buffer[CHUNK_FILE_LINE_BUFFER_SIZE];
    while (fgets(read_buffer, CHUNK_FILE_LINE_BUFFER_SIZE, has_chunk_file)) {
        chunk_num++;
    }
    
    /* set the global variable*/
    has_chunks_number = chunk_num;
    has_chunks = malloc(sizeof(struct chunk_s) * chunk_num);

    memset(read_buffer, 0, CHUNK_FILE_LINE_BUFFER_SIZE);
    fseek(has_chunk_file, 0, SEEK_SET);

    int count = 0;
    char hash_buffer[SHA1_HASH_SIZE*2];
    while (fgets(read_buffer, CHUNK_FILE_LINE_BUFFER_SIZE, has_chunk_file)) {
        sscanf(read_buffer,"%d %s", &(has_chunks[count].id), hash_buffer);
        hex2binary(hash_buffer, SHA1_HASH_SIZE * 2, has_chunks[count].hash_binary);
        has_chunks[count].data = NULL;
        count++;
    } 
    fclose(has_chunk_file);

    printf("********CHUNKS I HAVE**********\n");
    int i = 0;
    for(i = 0; i < chunk_num; i++){
        print_chunk(&has_chunks[i]);
    }
    printf("*******END CHUNKS I HAVE********\n");
}
Beispiel #3
0
void
print_chunks (chunk_info_t *first)
{
  chunk_info_t *chunk;
  DL_FOREACH (first, chunk) 
    {
      print_chunk (chunk);
    }
Beispiel #4
0
static void msg_run(void)
{
	print_number("Len", len);
	/* byte aligned */
	passert(len == (len & -4));
	/* when len==0, msg may contain one byte :-/ */
	passert((len == 0 && msg.len <= 1)
		|| (len == msg.len * BITS_PER_BYTE));
	print_chunk("Msg", msg, 0);
	struct hash_context *hash = hash_alg->hash_ops->init(hash_alg, "sha", DBG_CRYPT);
	/* See above, use LEN, not MSG.LEN */
	hash_alg->hash_ops->digest_bytes(hash, "msg", msg.ptr, len / BITS_PER_BYTE);
	chunk_t bytes = alloc_chunk(l, "bytes");
	hash_alg->hash_ops->final_bytes(&hash, bytes.ptr, bytes.len);
	print_chunk("MD", bytes, 0);
	freeanychunk(bytes);
}
Beispiel #5
0
static void monte_run(void)
{
	print_chunk("Seed", seed, 0);
	chunk_t MDi_3 = alloc_chunk(seed.len, "MDi_3");
	chunk_t MDi_2 = alloc_chunk(seed.len, "MDi_2");
	chunk_t MDi_1 = alloc_chunk(seed.len, "MDi_1");
	chunk_t Mi = alloc_chunk(3 * seed.len, "Mi");
	for (int j = 0; j < 100; j++) {
		//MD[0] = MD[1] = MD[2] = Seed
		memcpy(MDi_3.ptr, seed.ptr, seed.len);
		memcpy(MDi_2.ptr, seed.ptr, seed.len);
		memcpy(MDi_1.ptr, seed.ptr, seed.len);
		for (int i = 3; i < 1003; i++) {
			// shuffle
			chunk_t tmp = MDi_3;
			MDi_3 = MDi_2;
			MDi_2 = MDi_1;
			MDi_1 = seed;
			seed = tmp;
			// M[i] = MD[i-3] || MD[i-2] || MD[i-1];
			memcpy(Mi.ptr + seed.len * 0, MDi_3.ptr, seed.len);
			memcpy(Mi.ptr + seed.len * 1, MDi_2.ptr, seed.len);
			memcpy(Mi.ptr + seed.len * 2, MDi_1.ptr, seed.len);
			// MDi = SHA(Mi);
			struct hash_context *hash = hash_alg->hash_ops->init(hash_alg,
									     "sha", DBG_CRYPT);
			hash_alg->hash_ops->digest_bytes(hash, "msg", Mi.ptr, Mi.len);
			hash_alg->hash_ops->final_bytes(&hash, seed.ptr, seed.len);
			// printf("%d ", i);
			// print_chunk("MDi", seed, 0);
		}
		print_line("");
		print_number("COUNT", j);
		// MDj = Seed = MD1002;
		// OUTPUT: MDj; (aka seed)
		print_chunk("MD", seed, 0);
	}
	freeanychunk(MDi_3);
	freeanychunk(MDi_2);
	freeanychunk(MDi_1);
	freeanychunk(Mi);
	print_line("");
	exit(0);
}
Beispiel #6
0
static void gcm_run_test(void)
{
	print_number("Count", NULL, count);
	print_symkey("Key", NULL, key, 0);
	print_chunk("IV", NULL, iv, 0);
	print_chunk("CT", NULL, ct, 0);
	print_chunk("AAD", NULL, aad, 0);
	print_chunk("Tag", NULL, tag, 0);
	const struct encrypt_desc *gcm_alg = lookup_by_taglen();
	if (gcm_alg == NULL) {
		fprintf(stderr, "taglen %lu not supported\n",
			taglen);
		return;
	}
	PK11SymKey *gcm_key = encrypt_key_from_symkey_bytes("GCM key", gcm_alg,
							    0, sizeof_symkey(key),
							    key);

	chunk_t text_and_tag = clone_chunk_chunk(ct, tag, "text-and-tag");

	bool result = gcm_alg->encrypt_ops
		->do_aead(gcm_alg,
			  salt.ptr, salt.len,
			  iv.ptr, iv.len,
			  aad.ptr, aad.len,
			  text_and_tag.ptr,
			  ct.len, tag.len,
			  gcm_key,
			  FALSE/*encrypt*/);
	if (result) {
		/* plain text */
		chunk_t pt = {
			.ptr = text_and_tag.ptr,
			.len = ct.len,
		};
		print_chunk("PT", NULL, pt, 0);
	} else {
		print_line("FAIL");
	}
	release_symkey(__func__, "GCM-key", &gcm_key);
	freeanychunk(text_and_tag);
}
Beispiel #7
0
static void debug(void)
{
  CHUNK* cur = heap;

  printf("heap = %lx\n", (uintptr_t)heap);
  while (cur < heap_ptr) {
    print_chunk(cur);
    cur++;
  }
  printf("heap+heap_ptr\n");
  printf("stack = %lu chunk(s)\n", stack_size());
}
Beispiel #8
0
static void
dbg_found_read_dep_error (check_ctx_t *ctx, int *dst, int *dst2, int idx)
{
    char               *res = RTmalloc (1024);
    lts_type_t          ltstype = GBgetLTStype (ctx->parent);
    int                 typeno = lts_type_get_state_typeno (ltstype, idx);
    print_chunk (ctx->parent, res, 1024, typeno, dst[idx]);
    Print1 (lerror, "Found missing read dependency in group %d (diff slot: %d -- %s != %s).\\"
                    "Identifying culprit read slot...",
             ctx->group, idx, str_slot(ctx, dst2, idx), res);
    RTfree (res);
}
Beispiel #9
0
static void run_psk(void)
{
	print_number("COUNT", count);
	print_chunk("CKY_I", cky_i, 0);
	print_chunk("CKY_R", cky_r, 0);
	print_chunk("Ni", ni, 0);
	print_chunk("Nr", nr, 0);
	print_symkey("g^xy", g_xy, 0);
	print_chunk("pre-shared-key", psk, 0);

	if (prf == NULL) {
		print_line(prf->key);
		return;
	}

	PK11SymKey *skeyid = ikev1_pre_shared_key_skeyid(prf->prf, psk,
							 ni, nr);
	print_symkey("SKEYID", skeyid, 0);
	ikev1_skeyid_alphabet(skeyid);
	release_symkey(__func__, "skeyid", &skeyid);
}
Beispiel #10
0
static void run_sig(void)
{
	print_number("COUNT", count);
	print_chunk("CKY_I", cky_i, 0);
	print_chunk("CKY_R", cky_r, 0);
	print_chunk("Ni", ni, 0);
	print_chunk("Nr", nr, 0);
	print_symkey("g^xy", g_xy, 0);

	if (hasher == NULL) {
		print_line(hasher_name);
		return;
	}

	PK11SymKey *skeyid =
		ikev1_signature_skeyid(hasher,
				       ni, nr,
				       g_xy);
	print_symkey("SKEYID", skeyid, 0);
	ikev1_skeyid_alphabet(skeyid);
	free_any_symkey("skeyid", &skeyid);
}
Beispiel #11
0
void print_memory_chunks()
{
  memchunk_t * _mcp;
  unsigned long long i  = 0;

  // Traverse whole memory chunk list and print it's information
  for (_mcp = _memchunks_head; _mcp;  _mcp = _mcp->next, i++)
  {
    printf("Faund chunk:\n");
    print_chunk(_mcp);
  }

  printf("Total chunk count: %llu\n", i);
}
Beispiel #12
0
static char *
str_slot (check_ctx_t *ctx, int *s, int i)
{
    model_t         model = ctx->parent;
    lts_type_t      ltstype = GBgetLTStype (model);
    char           *name = lts_type_get_state_name (ltstype, i);
    char           *type = lts_type_get_state_type (ltstype, i);
    int             typeno = lts_type_get_state_typeno (ltstype, i);
    int             max = 4096;
    char           *res = RTmalloc (max);
    int             l = snprintf (res, max, "%s : %s = ", name, type);
    if (s != NULL) {
        print_chunk (model, res+l, max-l, typeno, s[i]);
    } else {
        res[l-2] = '\0';
    }
    return res;
}
Beispiel #13
0
static inline void
print_ti (check_ctx_t *ctx, transition_info_t *ti)
{
    if (ti == NULL || ti->labels == NULL) return;
    model_t         model = ctx->parent;
    lts_type_t      ltstype = GBgetLTStype (model);
    int             Max = 4096;
    char           *tmp = RTmalloc (Max);
    int             l;
    for (int i = 0; i < ctx->L; i++) {
        char           *name = lts_type_get_edge_label_name (ltstype, i);
        char           *type = lts_type_get_edge_label_type (ltstype, i);
        int             typeno = lts_type_get_edge_label_typeno (ltstype, i);

        char           *res = tmp;
        l  = snprintf (res, Max, " --> %s : %s = ", name, type);
        l += print_chunk (model, res+l, Max-l, typeno, ti->labels[i]);
        Printf (lerror, "%s\n", res);
    }
    RTfree (tmp);
}
Beispiel #14
0
void* malloc(size_t size)
{
  memchunk_t * _mcp, * _nmcp;
  size_t flen, nsize;

  // First step:
  // We need to find first free memory chunk of bigger or equal size.
  // Becouse we have extra overhead, so we're looking for (size + overhead) chunk. 
  
  flen = size + sizeof(memchunk_t);

#ifdef MALLOC_DEBUG_EXTRA
  printf("\n--------------------------------------------------------------------------------\n");
  printf("Allocating %llu bytes chunk (actualy requested %llu)", (unsigned long long)flen, (unsigned long long)size);
#endif

  for (_mcp = _memchunks_head;
        !(_mcp && (_mcp->flags == 0) && (_mcp->size >= flen)); 
          _mcp = _mcp->next
#ifdef MALLOC_DEBUG_EXTRA
          , printf(".")
#endif
        ); 

#ifdef MALLOC_DEBUG_EXTRA
  printf("\n");
#endif

  // if there is no free chunks...
  if (!_mcp)
  {
#ifdef MALLOC_DEBUG_EXTRA
    printf("We have no free memory chunks. Sorry..\n");
#endif
    return NULL;
  }

#ifdef MALLOC_DEBUG_EXTRA
  printf("Found %llu bytes sized free chunk:\n", (unsigned long long)_mcp->size);
  print_chunk(_mcp);
#endif

  if (_mcp->size > flen)
  {
    // We found bigger shunk and we need to split it in two peaces
    // where first be allocated and second is free
    // found freechunk : [|size_t s|next|prev|..... data ....|]
    
#ifdef MALLOC_DEBUG_EXTRA
    printf("\t It is bigger (need %llu, found %llu)\n\t Splitting:\n", (unsigned long long)flen, (unsigned long long)_mcp->size); 
#endif

    nsize = _mcp->size - flen; // TODO: maybe it will be nice if we check new chunk size and if it smaller or equal than sizeof(memchunk_t), then do not split founded chunk in two chunks

#ifdef MALLOC_DEBUG_EXTRA
    printf("\t New free chunk size is %llu bytes\n", (unsigned long long)nsize);
#endif

    _nmcp = (void*)_mcp + flen; 
    _nmcp->size = nsize; 
    _nmcp->flags = 0;
    _nmcp->next = _mcp->next;
    _nmcp->prev = _mcp;

#ifdef MALLOC_DEBUG_EXTRA
    printf("_nmcp:\n");
    print_chunk(_nmcp);
#endif

    _mcp->next = _nmcp;
    _mcp->flags |= MEM_CHUNK_IN_USE;
    _mcp->size = flen;

#ifdef MALLOC_DEBUG_EXTRA
    printf("returning chunk:\n");
    print_chunk(_mcp);
    printf("EOM\n\n");
#endif
  }

  return (void*)_mcp+sizeof(memchunk_t);
}
Beispiel #15
0
static void print_sys_chunk_array(struct btrfs_super_block *sb)
{
	struct extent_buffer *buf;
	struct btrfs_disk_key *disk_key;
	struct btrfs_chunk *chunk;
	u8 *array_ptr;
	unsigned long sb_array_offset;
	u32 num_stripes;
	u32 array_size;
	u32 len = 0;
	u32 cur_offset;
	struct btrfs_key key;
	int item;

	buf = malloc(sizeof(*buf) + sizeof(*sb));
	if (!buf) {
		fprintf(stderr, "%s\n", strerror(ENOMEM));
		exit(1);
	}
	write_extent_buffer(buf, sb, 0, sizeof(*sb));
	array_size = btrfs_super_sys_array_size(sb);

	array_ptr = sb->sys_chunk_array;
	sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
	cur_offset = 0;
	item = 0;

	while (cur_offset < array_size) {
		disk_key = (struct btrfs_disk_key *)array_ptr;
		len = sizeof(*disk_key);
		if (cur_offset + len > array_size)
			goto out_short_read;

		btrfs_disk_key_to_cpu(&key, disk_key);

		array_ptr += len;
		sb_array_offset += len;
		cur_offset += len;

		printf("\titem %d ", item);
		btrfs_print_key(disk_key);
		putchar('\n');

		if (key.type == BTRFS_CHUNK_ITEM_KEY) {
			chunk = (struct btrfs_chunk *)sb_array_offset;
			/*
			 * At least one btrfs_chunk with one stripe must be
			 * present, exact stripe count check comes afterwards
			 */
			len = btrfs_chunk_item_size(1);
			if (cur_offset + len > array_size)
				goto out_short_read;

			print_chunk(buf, chunk);
			num_stripes = btrfs_chunk_num_stripes(buf, chunk);
			if (!num_stripes) {
				printk(
	    "ERROR: invalid number of stripes %u in sys_array at offset %u\n",
					num_stripes, cur_offset);
				break;
			}
			len = btrfs_chunk_item_size(num_stripes);
			if (cur_offset + len > array_size)
				goto out_short_read;
		} else {
			printk(
		"ERROR: unexpected item type %u in sys_array at offset %u\n",
				(u32)key.type, cur_offset);
 			break;
		}
		array_ptr += len;
		sb_array_offset += len;
		cur_offset += len;

		item++;
	}

	free(buf);
	return;

out_short_read:
	printk("ERROR: sys_array too short to read %u bytes at offset %u\n",
			len, cur_offset);
	free(buf);
}
Beispiel #16
0
int main (int argc, char **argv) {
    if ((argc < 3) && (argc != 2 ||
        (strcmp(argv[1], "stats") != 0 && 
         strcmp(argv[1], "check") != 0 &&
         strcmp(argv[1], "index") != 0)))
    {
        usage(argv[0]);
    }

    char *shc_hosts = getenv("SHC_HOSTS");
    if (!shc_hosts) {
        fprintf(stderr, "SHC_HOSTS environment variable not found!\n");
        exit(-1);
    }
    char *secret = getenv("SHC_SECRET");

    if (parse_nodes_string(shc_hosts) != 0) {
        fprintf(stderr, "Can't parse the nodes string : %s!\n", shc_hosts);
        exit(-1);
    }

    shardcache_client_t *client = shardcache_client_create(nodes, num_nodes, secret);

    int rc = 0;
    int is_boolean = 0;

    FILE *output_file = stdout;

    char *cmd = argv[1];
    if (strcasecmp(cmd, "getf") == 0) {
        int fd = shardcache_client_getf(client, argv[2], strlen(argv[2]));
        if (fd >= 0) {
            if (argc > 3) {
                output_file = fopen(argv[3], "w");
                if (!output_file) {
                    fprintf(stderr, "Can't open file %s for writing : %s\n",
                            argv[3], strerror(errno));
                    exit(-1);
                }
            }
            char buf[1024];
            int rb = 0;
            while ((rb = read(fd, &buf, sizeof(buf))) > 0)
               fwrite(&buf, 1, rb, output_file);
            close(fd);
        } else {
            fprintf(stderr, "Can't fetch data using shardcache_client_getf()\n");
            exit(-1);
        }
    } else if (strcasecmp(cmd, "get") == 0) {
        void *out = NULL;
        size_t len = shardcache_client_get(client, argv[2], strlen(argv[2]), &out); 
        if (len && out) {
            if (argc > 3) {
                output_file = fopen(argv[3], "w");
                if (!output_file) {
                    fprintf(stderr, "Can't open file %s for writing : %s\n",
                            argv[3], strerror(errno));
                    exit(-1);
                }
            }
            print_chunk(NULL, NULL, 0, out, len, 0, output_file);
        }
    } else if (strcasecmp(cmd, "offset") == 0) {
        if (argc < 5)
            usage(argv[0]);
        int offset = strtol(argv[3], NULL, 10);
        int size = strtol(argv[4], NULL, 10);
        char out[size];
        size_t len = shardcache_client_offset(client, argv[2], strlen(argv[2]), offset, out, size); 

        if (argc > 5) {
            output_file = fopen(argv[5], "w");
            if (!output_file) {
                fprintf(stderr, "Can't open file %s for writing : %s\n",
                        argv[5], strerror(errno));
                exit(-1);
            }
        }

        if (len) {
            print_chunk(NULL, NULL, 0, out, len, 0, output_file);
        }
    } else if (strcasecmp(cmd, "geta") == 0 || strcasecmp(cmd, "get_async") == 0) {
        if (argc > 3) {
            output_file = fopen(argv[3], "w");
            if (!output_file) {
                fprintf(stderr, "Can't open file %s for writing : %s\n",
                        argv[3], strerror(errno));
                exit(-1);
            }
        }
        rc = shardcache_client_get_async(client, argv[2], strlen(argv[2]), print_chunk, output_file); 
    } else if (strcasecmp(cmd, "get_multi") == 0) {
        char **keys = &argv[2];
        int num_keys = argc - 2;

        char *outdir = NULL;
        if (argc > 3) {
            if (strncmp(argv[argc-1], "-o", 2) == 0) {
                char *dir = argv[argc-1] + 2;
                if (*dir) {
                    outdir = dir;
                    num_keys--;
                }
            } else if (strncmp(argv[argc-2], "-o", 2) == 0) {
                outdir = argv[argc-1];
                num_keys -= 2;
            }
            if (outdir) {
                int olen = strlen(outdir) -1;
                while (olen >= 0 && outdir[olen] == '/')
                    outdir[olen--] = 0;
            }
        }

        shc_multi_item_t *items[num_keys+1];
        int i;
        for (i = 0; i < num_keys; i++) {
            items[i] = shc_multi_item_create(keys[i], strlen(keys[i]), NULL, 0);
        }
        items[num_keys] = NULL;

        rc = shardcache_client_get_multi(client, items);

        for (i = 0; i < num_keys; i++) {
            size_t klen = items[i]->klen;
            char *keystr = malloc(klen+1);
            memcpy(keystr, items[i]->key, klen);
            keystr[klen] = 0;
            FILE *out = NULL;
            char *outname = NULL;
            if (outdir) {
                int len = strlen(outdir) + klen + 2;
                outname = malloc(len);
                snprintf(outname, len, "%s/%s", outdir, keystr);
                out = fopen(outname, "w");
                if (!out) {
                    fprintf(stderr, "Can't open file %s for writing : %s\n",
                            outname, strerror(errno));
                    exit(-1);
                }
            }
            if (out)
                printf("Saving key %s to file %s\n", keystr, outname);
            else
                printf("Value for key: %s\n", keystr);

            print_chunk(NULL, NULL, 0, items[i]->data, items[i]->dlen, 0, out);
            printf("\n");
            shc_multi_item_destroy(items[i]);
            if (out)
                fclose(out);
            if (outname)
                free(outname);
        }
    } else if (strcasecmp(cmd, "set") == 0 ||
               strcasecmp(cmd, "add") == 0)
    {
        FILE *infile = NULL;
        uint32_t expire = 0;
        char *inpath = NULL;
        while (argc > 3) {
            if (strncmp(argv[argc-1], "-i", 2) == 0) {
                char *dir = argv[argc-1] + 2;
                if (*dir) {
                    inpath = dir;
                    argc--;
                }
            } else if (strncmp(argv[argc-1], "-e", 2) == 0) {
                char *expire_str = argv[argc-1] + 2;
                if (*expire_str) {
                    expire = strtol(expire_str, NULL, 10);
                    argc--;
                }
            } else if (strncmp(argv[argc-2], "-i", 2) == 0) {
                inpath = argv[argc-1];
                argc -= 2;
            } else if (strncmp(argv[argc-2], "-e", 2) == 0) {
                expire = strtol(argv[argc-1], NULL, 10);
                argc -= 2;
            } else {
                fprintf(stderr, "Unknown option %s\n", argv[3]);
                exit(-1);
            }
        }

        if (inpath) {
            infile = fopen(inpath, "r");
            if (!infile) {
                fprintf(stderr, "Can't open file for %s reading: %s\n",
                        inpath, strerror(errno));
                exit(-1);
            }
        }
        char *in = NULL;
        size_t s = 0;
        char buf[1024];
        int rb = fread(buf, 1, 1024, infile ? infile : stdin);
        while (rb > 0) {
            in = realloc(in, s+rb);
            memcpy(in + s, buf, rb);
            s += rb;
            rb = fread(buf, 1, 1024, infile ? infile : stdin);
        }

        if (strcasecmp(cmd, "set") == 0) {
            rc = shardcache_client_set(client, argv[2], strlen(argv[2]), in, s, expire);
        } else {
            rc = shardcache_client_add(client, argv[2], strlen(argv[2]), in, s, expire);
            if (rc == 1)
                printf("Already exists!\n");
        }
        if (infile)
            fclose(infile);
    } else if (strcasecmp(cmd, "del") == 0 || strcasecmp(cmd, "delete") == 0) {
        rc = shardcache_client_del(client, argv[2], strlen(argv[2]));
    } else if (strcasecmp(cmd, "evict") == 0) {
        rc = shardcache_client_evict(client, argv[2], strlen(argv[2]));
    } else if (strcasecmp(cmd, "exists") == 0) {
        rc = shardcache_client_exists(client, argv[2], strlen(argv[2]));
        is_boolean = 1;
    } else if (strcasecmp(cmd, "touch") == 0) {
        rc = shardcache_client_touch(client, argv[2], strlen(argv[2]));
    } else if (strcasecmp(cmd, "stats") == 0) {
        int found = 0;
        char *selected_node = NULL;
        if (argc > 2)
            selected_node = argv[2];

        char *stats = NULL;
        size_t len;
        int i;
        for (i = 0; i < num_nodes; i++) {
            char *label = shardcache_node_get_label(nodes[i]);
            char *address = shardcache_node_get_address(nodes[i]); 
            if (selected_node && strcmp(label, selected_node) != 0)
                continue;
            found++;
            printf("* Stats for node: %s (%s)\n\n", label, address);
            int rc = shardcache_client_stats(client, label, &stats, &len); 
            if (rc == 0)
                printf("%s\n", stats);
            else
                printf("Error querying node: %s (%s)\n", label, address);
            if (stats)
                free(stats);
            printf("\n");
        }
        if (found == 0 && selected_node)
            fprintf(stderr, "Error: Unknown node %s\n", selected_node);
    } else if (strcasecmp(cmd, "check") == 0) {
        int found = 0;
        char *selected_node = NULL;
        if (argc > 2)
            selected_node = argv[2];

        int i;
        for (i = 0; i < num_nodes; i++) {
            char *label = shardcache_node_get_label(nodes[i]);
            if (selected_node && strcmp(label, selected_node) != 0)
                continue;
            found++;
            int rc = shardcache_client_check(client, label);
            if (rc == 0)
                printf("%s OK\n", label);
            else
                printf("%s NOT OK\n", label);
        }
        if (found == 0 && selected_node)
            fprintf(stderr, "Error: Unknown node %s\n", selected_node);
    } else if (strcasecmp(cmd, "index") == 0) {
        int found = 0;
        char *selected_node = NULL;
        if (argc > 2)
            selected_node = argv[2];

        int i;
        for (i = 0; i < num_nodes; i++) {
            char *label = shardcache_node_get_label(nodes[i]);
            char *address = shardcache_node_get_address(nodes[i]); 
            if (selected_node && strcmp(label, selected_node) != 0)
                continue;
            found++;
            printf("* Index for node: %s (%s)\n\n", label, address);
            shardcache_storage_index_t *index = shardcache_client_index(client, label);
            if (index) {
                int n;
                for (n = 0; n < index->size; n ++) {
                    shardcache_storage_index_item_t *item = &index->items[n];
                    char keystr[item->klen+1];
                    snprintf(keystr, sizeof(keystr), "%s", (char *)item->key);
                    printf("%s => %u\n", keystr, (uint32_t)item->vlen);
                }
                shardcache_free_index(index);
            } else {
                printf("%s NOT OK\n", label);
            }
            printf("\n");
        }
        if (found == 0 && selected_node)
            fprintf(stderr, "Error: Unknown node %s\n", selected_node);
    } else {
        usage(argv[0]);
    }

    if (output_file)
        fclose(output_file);

    if (strncasecmp(cmd, "get", 3) != 0 &&
        strncasecmp(cmd, "offset", 6) != 0)
    {
        if (is_boolean) {
            if (rc == 0) {
                printf("NO\n");
            } else if (rc == 1) {
                printf("YES\n");
                rc = 0;
            } else {
                printf("ERR\n");
            }
        } else {
            if (rc == 0)
                printf("OK\n");
            else
                printf("ERR\n");
        }
    }

    if (shardcache_client_errno(client) != SHARDCACHE_CLIENT_OK) {
        fprintf(stderr, "errno: %d, errstr: %s\n",
                shardcache_client_errno(client), shardcache_client_errstr(client));
    }

    shardcache_client_destroy(client);
    exit(rc);
}
Beispiel #17
0
void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb)
{
	struct btrfs_item *item;
	struct btrfs_disk_key disk_key;
	u32 i;
	u32 nr;

	nr = btrfs_header_nritems(eb);

	printf("leaf %llu items %d free space %d generation %llu owner %llu\n",
		(unsigned long long)btrfs_header_bytenr(eb), nr,
		btrfs_leaf_free_space(root, eb),
		(unsigned long long)btrfs_header_generation(eb),
		(unsigned long long)btrfs_header_owner(eb));
	print_uuids(eb);
	fflush(stdout);

	for (i = 0; i < nr; i++) {
		u32 item_size;
		void *ptr;
		u64 objectid;
		u32 type;
		u64 offset;
		char flags_str[256];
		char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
		u8 uuid[BTRFS_UUID_SIZE];

		item = btrfs_item_nr(i);
		item_size = btrfs_item_size(eb, item);
		/* Untyped extraction of slot from btrfs_item_ptr */
		ptr = btrfs_item_ptr(eb, i, void*);

		btrfs_item_key(eb, &disk_key, i);
		objectid = btrfs_disk_key_objectid(&disk_key);
		type = btrfs_disk_key_type(&disk_key);
		offset = btrfs_disk_key_offset(&disk_key);

		printf("\titem %d ", i);
		btrfs_print_key(&disk_key);
		printf(" itemoff %d itemsize %d\n",
			btrfs_item_offset(eb, item),
			btrfs_item_size(eb, item));

		if (type == 0 && objectid == BTRFS_FREE_SPACE_OBJECTID)
			print_free_space_header(eb, i);

		switch (type) {
		case BTRFS_INODE_ITEM_KEY:
			print_inode_item(eb, ptr);
			break;
		case BTRFS_INODE_REF_KEY:
			print_inode_ref_item(eb, item_size, ptr);
			break;
		case BTRFS_INODE_EXTREF_KEY:
			print_inode_extref_item(eb, item_size, ptr);
			break;
		case BTRFS_DIR_ITEM_KEY:
		case BTRFS_DIR_INDEX_KEY:
		case BTRFS_XATTR_ITEM_KEY:
			print_dir_item(eb, item_size, ptr);
			break;
		case BTRFS_DIR_LOG_INDEX_KEY:
		case BTRFS_DIR_LOG_ITEM_KEY: {
			struct btrfs_dir_log_item *dlog;

			dlog = btrfs_item_ptr(eb, i, struct btrfs_dir_log_item);
			printf("\t\tdir log end %Lu\n",
			       (unsigned long long)btrfs_dir_log_end(eb, dlog));
			break;
			}
		case BTRFS_ORPHAN_ITEM_KEY:
			printf("\t\torphan item\n");
			break;
		case BTRFS_ROOT_ITEM_KEY:
			print_root(eb, i);
			break;
		case BTRFS_ROOT_REF_KEY:
			print_root_ref(eb, i, "ref");
			break;
		case BTRFS_ROOT_BACKREF_KEY:
			print_root_ref(eb, i, "backref");
			break;
		case BTRFS_EXTENT_ITEM_KEY:
			print_extent_item(eb, i, 0);
			break;
		case BTRFS_METADATA_ITEM_KEY:
			print_extent_item(eb, i, 1);
			break;
		case BTRFS_TREE_BLOCK_REF_KEY:
			printf("\t\ttree block backref\n");
			break;
		case BTRFS_SHARED_BLOCK_REF_KEY:
			printf("\t\tshared block backref\n");
			break;
		case BTRFS_EXTENT_DATA_REF_KEY: {
			struct btrfs_extent_data_ref *dref;

			dref = btrfs_item_ptr(eb, i, struct btrfs_extent_data_ref);
			printf("\t\textent data backref root %llu "
			       "objectid %llu offset %llu count %u\n",
			       (unsigned long long)btrfs_extent_data_ref_root(eb, dref),
			       (unsigned long long)btrfs_extent_data_ref_objectid(eb, dref),
			       (unsigned long long)btrfs_extent_data_ref_offset(eb, dref),
			       btrfs_extent_data_ref_count(eb, dref));
			break;
			}
		case BTRFS_SHARED_DATA_REF_KEY: {
			struct btrfs_shared_data_ref *sref;
			sref = btrfs_item_ptr(eb, i, struct btrfs_shared_data_ref);
			printf("\t\tshared data backref count %u\n",
			       btrfs_shared_data_ref_count(eb, sref));
			break;
			}
		case BTRFS_EXTENT_REF_V0_KEY:
#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
			print_extent_ref_v0(eb, i);
#else
			BUG();
#endif
			break;
		case BTRFS_CSUM_ITEM_KEY:
			printf("\t\tcsum item\n");
			break;
		case BTRFS_EXTENT_CSUM_KEY:
			printf("\t\textent csum item\n");
			break;
		case BTRFS_EXTENT_DATA_KEY:
			print_file_extent_item(eb, item, i, ptr);
			break;
		case BTRFS_BLOCK_GROUP_ITEM_KEY: {
			struct btrfs_block_group_item bg_item;

			read_extent_buffer(eb, &bg_item, (unsigned long)ptr,
					   sizeof(bg_item));
			memset(flags_str, 0, sizeof(flags_str));
			bg_flags_to_str(btrfs_block_group_flags(&bg_item),
					flags_str);
			printf("\t\tblock group used %llu chunk_objectid %llu flags %s\n",
			       (unsigned long long)btrfs_block_group_used(&bg_item),
			       (unsigned long long)btrfs_block_group_chunk_objectid(&bg_item),
			       flags_str);
			break;
			}
		case BTRFS_FREE_SPACE_INFO_KEY: {
			struct btrfs_free_space_info *free_info;

			free_info = btrfs_item_ptr(eb, i, struct btrfs_free_space_info);
			printf("\t\tfree space info extent count %u flags %u\n",
			       (unsigned)btrfs_free_space_extent_count(eb, free_info),
			       (unsigned)btrfs_free_space_flags(eb, free_info));
			break;
			}
		case BTRFS_FREE_SPACE_EXTENT_KEY:
			printf("\t\tfree space extent\n");
			break;
		case BTRFS_FREE_SPACE_BITMAP_KEY:
			printf("\t\tfree space bitmap\n");
			break;
		case BTRFS_CHUNK_ITEM_KEY:
			print_chunk(eb, ptr);
			break;
		case BTRFS_DEV_ITEM_KEY:
			print_dev_item(eb, ptr);
			break;
		case BTRFS_DEV_EXTENT_KEY: {
			struct btrfs_dev_extent *dev_extent;

			dev_extent = btrfs_item_ptr(eb, i,
						    struct btrfs_dev_extent);
			read_extent_buffer(eb, uuid,
				(unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
				BTRFS_UUID_SIZE);
			uuid_unparse(uuid, uuid_str);
			printf("\t\tdev extent chunk_tree %llu\n"
			       "\t\tchunk_objectid %llu chunk_offset %llu "
			       "length %llu\n"
			       "\t\tchunk_tree_uuid %s\n",
			       (unsigned long long)
			       btrfs_dev_extent_chunk_tree(eb, dev_extent),
			       (unsigned long long)
			       btrfs_dev_extent_chunk_objectid(eb, dev_extent),
			       (unsigned long long)
			       btrfs_dev_extent_chunk_offset(eb, dev_extent),
			       (unsigned long long)
			       btrfs_dev_extent_length(eb, dev_extent),
			       uuid_str);
			break;
			}
		case BTRFS_QGROUP_STATUS_KEY: {
			struct btrfs_qgroup_status_item *qg_status;

			qg_status = btrfs_item_ptr(eb, i,
					struct btrfs_qgroup_status_item);
			memset(flags_str, 0, sizeof(flags_str));
			qgroup_flags_to_str(btrfs_qgroup_status_flags(eb, qg_status),
					flags_str);
			printf("\t\tversion %llu generation %llu flags %s "
				"scan %lld\n",
				(unsigned long long)
				btrfs_qgroup_status_version(eb, qg_status),
				(unsigned long long)
				btrfs_qgroup_status_generation(eb, qg_status),
				flags_str,
				(unsigned long long)
				btrfs_qgroup_status_rescan(eb, qg_status));
			break;
			}
		case BTRFS_QGROUP_RELATION_KEY:
			break;
		case BTRFS_QGROUP_INFO_KEY: {
			struct btrfs_qgroup_info_item *qg_info;

			qg_info = btrfs_item_ptr(eb, i,
						 struct btrfs_qgroup_info_item);
			printf("\t\tgeneration %llu\n"
			     "\t\treferenced %llu referenced_compressed %llu\n"
			     "\t\texclusive %llu exclusive_compressed %llu\n",
			       (unsigned long long)
			       btrfs_qgroup_info_generation(eb, qg_info),
			       (unsigned long long)
			       btrfs_qgroup_info_referenced(eb, qg_info),
			       (unsigned long long)
			       btrfs_qgroup_info_referenced_compressed(eb,
								       qg_info),
			       (unsigned long long)
			       btrfs_qgroup_info_exclusive(eb, qg_info),
			       (unsigned long long)
			       btrfs_qgroup_info_exclusive_compressed(eb,
								      qg_info));
			break;
			}
		case BTRFS_QGROUP_LIMIT_KEY: {
			struct btrfs_qgroup_limit_item *qg_limit;

			qg_limit = btrfs_item_ptr(eb, i,
					 struct btrfs_qgroup_limit_item);
			printf("\t\tflags %llx\n"
			     "\t\tmax_referenced %lld max_exclusive %lld\n"
			     "\t\trsv_referenced %lld rsv_exclusive %lld\n",
			       (unsigned long long)
			       btrfs_qgroup_limit_flags(eb, qg_limit),
			       (long long)
			       btrfs_qgroup_limit_max_referenced(eb, qg_limit),
			       (long long)
			       btrfs_qgroup_limit_max_exclusive(eb, qg_limit),
			       (long long)
			       btrfs_qgroup_limit_rsv_referenced(eb, qg_limit),
			       (long long)
			       btrfs_qgroup_limit_rsv_exclusive(eb, qg_limit));
			break;
			}
		case BTRFS_UUID_KEY_SUBVOL:
		case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
			print_uuid_item(eb, btrfs_item_ptr_offset(eb, i),
					btrfs_item_size_nr(eb, i));
			break;
		case BTRFS_STRING_ITEM_KEY: {
			const char *str = eb->data + btrfs_item_ptr_offset(eb, i);

			printf("\t\titem data %.*s\n", item_size, str);
			break;
			}
		case BTRFS_PERSISTENT_ITEM_KEY:
			printf("\t\tpersistent item objectid ");
			print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
			printf(" offset %llu\n", (unsigned long long)offset);
			switch (objectid) {
			case BTRFS_DEV_STATS_OBJECTID:
				print_dev_stats(eb, ptr, item_size);
				break;
			default:
				printf("\t\tunknown persistent item objectid %llu\n",
						objectid);
			}
			break;
		case BTRFS_TEMPORARY_ITEM_KEY:
			printf("\t\ttemporary item objectid ");
			print_objectid(stdout, objectid, BTRFS_TEMPORARY_ITEM_KEY);
			printf(" offset %llu\n", (unsigned long long)offset);
			switch (objectid) {
			case BTRFS_BALANCE_OBJECTID:
				print_balance_item(eb, ptr);
				break;
			default:
				printf("\t\tunknown temporary item objectid %llu\n",
						objectid);
			}
			break;
		};
		fflush(stdout);
	}
}