void debug_scan(char *msg, scan_t *scan) {
	debug_trace(msg,
			"id=%s off=%d prefix=%-3d header=%-3d gap=%-3d payload=%-3d post=%-3d "
				"nid=%s buf_len=%-3d wire_len=%-3d flags=%0x",
			id2str(scan->id), scan->offset, scan->hdr_prefix, scan->length,
			scan->hdr_gap, scan->hdr_payload, scan->hdr_postfix, id2str(
					scan->next_id), scan->buf_len, scan->wire_len,
			scan->scanner->sc_flags[scan->id]);
}
Exemplo n.º 2
0
int
pool_evrcmp(const Pool *pool, Id evr1id, Id evr2id, int mode)
{
  const char *evr1, *evr2;
  if (evr1id == evr2id)
    return 0;
  evr1 = id2str(pool, evr1id);
  evr2 = id2str(pool, evr2id);
  return evrcmp_str(pool, evr1, evr2, mode);
}
Exemplo n.º 3
0
static void
doquery(Pool *pool, Repo *repo, const char *query)
{
  Id id, type = 0;
  char qbuf[256];
  const char *qp;
  Dataiterator di;

  qp = strchr(query, ':');
  if (qp)
    {
      type = strn2id(pool, query, qp - query, 0);
      if (!type)
	exit(0);
      qp++;
    }
  else
    qp = query;
  snprintf(qbuf, sizeof(qbuf), "repository:repomd:%s", qp);
  id = str2id(pool, qbuf, 0);
  if (!id)
    exit(0);
  dataiterator_init(&di, pool, repo, SOLVID_META, id, 0, 0);
  dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
  while (dataiterator_step(&di))
    {
      if (type)
	{
	  dataiterator_setpos_parent(&di);
	  if (pool_lookup_id(pool, SOLVID_POS, REPOSITORY_REPOMD_TYPE) != type)
	    continue;
	}
      switch (di.key->type)
	{
	case REPOKEY_TYPE_ID:
	case REPOKEY_TYPE_CONSTANTID:
	  printf("%s\n", id2str(pool, di.kv.id));
	  break;
	case REPOKEY_TYPE_STR:
	  printf("%s\n", di.kv.str);
	  break;
	case REPOKEY_TYPE_NUM:
	  printf("%d\n", di.kv.num);
	  break;
	case REPOKEY_TYPE_SHA1:
	  printf("sha1:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
	  break;
	case REPOKEY_TYPE_SHA256:
	  printf("sha256:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
	  break;
	default:
	  break;
	}
    }
  dataiterator_free(&di);
}
Exemplo n.º 4
0
static void
set_sourcerpm(Repodata *data, Solvable *s, Id handle, char *sourcerpm)
{
  const char *p, *sevr, *sarch, *name, *evr;
  Pool *pool;

  p = strrchr(sourcerpm, '.');
  if (!p || strcmp(p, ".rpm") != 0)
    return;
  p--;
  while (p > sourcerpm && *p != '.')
    p--;
  if (*p != '.' || p == sourcerpm)
    return;
  sarch = p-- + 1;
  while (p > sourcerpm && *p != '-')
    p--;
  if (*p != '-' || p == sourcerpm)
    return;
  p--;
  while (p > sourcerpm && *p != '-')
    p--;
  if (*p != '-' || p == sourcerpm)
    return;
  sevr = p + 1;
  pool = s->repo->pool;
  if (!strcmp(sarch, "src.rpm"))
    repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
  else if (!strcmp(sarch, "nosrc.rpm"))
    repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
  else
    repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
  evr = id2str(pool, s->evr);
  if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
    repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
  else
    repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
  name = id2str(pool, s->name);
  if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
    repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
  else
    repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
}
Exemplo n.º 5
0
static unsigned int
adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
{
  Id id, name, marker;
  const char *n, *f, *k;
  const char **a;

  n = f = k = 0;
  marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
  for (a = atts; *a; a += 2)
    {
      if (!strcmp(*a, "name"))
	n = a[1];
      else if (!strcmp(*a, "flags"))
	f = a[1];
      else if (!strcmp(*a, "kind"))
	k = a[1];
      else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
	marker = SOLVABLE_PREREQMARKER;
    }
  if (!n)
    return olddeps;
  if (k && !strcmp(k, "package"))
    k = 0;
  if (k)
    {
      int l = strlen(k) + 1 + strlen(n) + 1;
      if (l > pd->acontent)
	{
	  pd->content = sat_realloc(pd->content, l + 256);
	  pd->acontent = l + 256;
	}
      sprintf(pd->content, "%s:%s", k, n);
      name = str2id(pool, pd->content, 1);
    }
  else
    name = str2id(pool, (char *)n, 1);
  if (f)
    {
      Id evr = makeevr_atts(pool, pd, atts);
      int flags;
      for (flags = 0; flags < 6; flags++)
	if (!strcmp(f, flagtab[flags]))
	  break;
      flags = flags < 6 ? flags + 1 : 0;
      id = rel2id(pool, name, evr, flags, 1);
    }
  else
    id = name;
#if 0
  fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
#endif
  return repo_addid_dep(pd->common.repo, olddeps, id, marker);
}
Exemplo n.º 6
0
/*
 * Class:     org_jnetpcap_packet_JHeaderScanner
 * Method:    bindNativeScanner
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_org_jnetpcap_packet_JHeaderScanner_bindNativeScanner
(JNIEnv *env, jobject obj, jint id) {

	if (id < 0 || id > MAX_ID_COUNT) {
		sprintf(str_buf, "invalid ID=%d (%s)", id, id2str(id));
		throwException(env, UNREGISTERED_SCANNER_EXCEPTION, str_buf);
		return;
	}

	if (native_protocols[id] == NULL) {
		
		sprintf(str_buf, "native scanner not registered under ID=%d (%s)", 
				id,
				id2str(id));
		throwException(env, UNREGISTERED_SCANNER_EXCEPTION,	str_buf);
		return;
	}

	setJMemoryPhysical(env, obj, toLong((void *)native_protocols[id]));
}
Exemplo n.º 7
0
int
pool_evrmatch(const Pool *pool, Id evrid, const char *epoch, const char *version, const char *release)
{
  const char *evr1;
  const char *s1;
  const char *r1;
  int r;

  evr1 = id2str(pool, evrid);
  for (s1 = evr1; *s1 >= '0' && *s1 <= '9'; s1++)
    ;
  if (s1 != evr1 && *s1 == ':')
    {
      if (epoch)
	{
	  r = vercmp(evr1, s1, epoch, epoch + strlen(epoch));
	  if (r)
	    return r;
	}
      evr1 = s1 + 1;
    }
  else if (epoch)
    {
      while (*epoch == '0')
	epoch++;
      if (*epoch)
	return -1;
    }
  for (s1 = evr1, r1 = 0; *s1; s1++)
    if (*s1 == '-')
      r1 = s1;
  if (version)
    {
      r = vercmp(evr1, r1 ? r1 : s1, version, version + strlen(version));
      if (r)
	return r;
    }
  if (release)
    {
      if (!r1)
	return -1;
      r = vercmp(r1 + 1, s1, release, release + strlen(release));
      if (r)
	return r;
    }
  return 0;
}
Exemplo n.º 8
0
int main()
{
  Pool *pool;
  Repo *installed;
  Solvable *s;
  Id p;
  int i;
  Queue todo, conflicts;
  void *state = 0;
 
  pool = pool_create();
  pool_setdebuglevel(pool, 1);
  installed = repo_create(pool, "@System");
  pool_set_installed(pool, installed);
  repo_add_rpmdb(installed, 0, 0, 0);
  queue_init(&todo);
  queue_init(&conflicts);
  FOR_REPO_SOLVABLES(installed, p, s)
    queue_push(&todo, p);
  pool_findfileconflicts(pool, &todo, 0, &conflicts, &iterate_handle, (void *)&state);
  queue_free(&todo);
  for (i = 0; i < conflicts.count; i += 5)
    printf("%s: %s[%s] %s[%s]\n", id2str(pool, conflicts.elements[i]), solvid2str(pool, conflicts.elements[i + 1]), id2str(pool, conflicts.elements[i + 2]), solvid2str(pool, conflicts.elements[i + 3]), id2str(pool, conflicts.elements[i + 4]));
  if (conflicts.count)
    {
      Queue job;
      queue_init(&job);
      pool_add_fileconflicts_deps(pool, &conflicts);
      pool_addfileprovides(pool);
      pool_createwhatprovides(pool);
      pool_setdebuglevel(pool, 0);
      Solver *solv = solver_create(pool);
      solv->fixsystem = 1;
#if 0
      solv->allowuninstall = 1;
#endif
      solver_solve(solv, &job);
      if (solv->problems.count)
        solver_printallsolutions(solv);
      else
        solver_printtransaction(solv);
      queue_free(&job);
      solver_free(solv);
    }
  queue_free(&conflicts);
  exit(0);
}
Exemplo n.º 9
0
void host_to_overview (host *h, var *ovdict) {
    char mid[16];
    char uuidstr[40];
    uuid2str (h->uuid, uuidstr);
    var *res = var_get_dict_forkey (ovdict, uuidstr);
    meter *m = h->first;
    while (m) {
        if (m->count >= SZ_EMPTY_VAL) {
            m = m->next;
            continue;
        }
        id2str (m->id, mid);
        
        if ((strncmp (mid, "top/", 4) == 0) ||
            (strncmp (mid, "df/", 3) == 0) ||
            (strncmp (mid, "proc/", 5) == 0) ||
            (strncmp (mid, "os/", 3) == 0) ||
            (strncmp (mid, "who/", 4) == 0)) {
            m = m->next;
            continue;
        }
        
        switch (m->id & MMASK_TYPE) {
            case MTYPE_INT:
                var_set_int_forkey (res, mid, meter_get_uint (m, 0));
                break;
            
            case MTYPE_FRAC:
                var_set_double_forkey (res, mid, meter_get_frac (m, 0));
                break;
            
            case MTYPE_STR:
                var_set_str_forkey (res, mid, m->d.str[0].str);
                break;
            
            default:
                break;
        }
        m = m->next;
    }
}
Exemplo n.º 10
0
/**
 * Scan packet buffer by dispatching to JBinding java objects
 */
void callJavaHeaderScanner(scan_t *scan) {

#ifdef DEBUG
	debug_enter("callJavaHeaderScanner");
#endif
	JNIEnv *env = scan->env;
	jobject jscanner = scan->scanner->sc_java_header_scanners[scan->id];

	if (jscanner == NULL) {
		sprintf(str_buf, "java header scanner not set for ID=%d (%s)",
				scan->id, id2str(scan->id));
#ifdef DEBUG
		debug_error("callJavaHeaderScanner()", str_buf);
#endif
		throwException(scan->env, NULL_PTR_EXCEPTION, str_buf);
		return;
	}

	env->CallVoidMethod(jscanner, scanHeaderMID, scan->scanner->sc_jscan);

#ifdef DEBUG
	debug_exit("callJavaHeaderScanner");
#endif
}
Exemplo n.º 11
0
/*
 * Class:     org_jnetpcap_packet_JPacket_State
 * Method:    toDebugStringJPacketState
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_jnetpcap_packet_JPacket_00024State_toDebugStringJPacketState
  (JNIEnv *env, jobject obj) {
	
	char buf[15 * 1024];
	buf[0] = '\0';
	
	packet_state_t *packet = (packet_state_t *)getJMemoryPhysical(env, obj);
	if (packet == NULL) {
		return NULL;
	}
	
	int fr = packet->pkt_frame_num;
	
	sprintf(buf, 
			"JPacket.State#%03d: sizeof(packet_state_t)=%d\n"
			"JPacket.State#%03d: sizeof(header_t)=%d and *%d=%d\n"
			"JPacket.State#%03d:   pkt_header_map=0x%X\n"
			"JPacket.State#%03d:        pkt_flags=0x%x\n"
			"JPacket.State#%03d: pkt_header_count=%d\n"
			"JPacket.State#%03d:      pkt_wirelen=%d\n",
			fr, (int) sizeof(packet_state_t),
			fr, (int) sizeof(header_t),
			(int) packet->pkt_header_count,
			(int) sizeof(header_t) * packet->pkt_header_count,
			fr, (int) packet->pkt_header_map,
			fr, (int) packet->pkt_flags,
			fr, (int) packet->pkt_header_count,
			fr, (int) packet->pkt_wirelen);
	
	char *p = buf;
	
	if (packet->pkt_header_count> 32) {
		sprintf(buf + strlen(buf), 
				"JPacket.State#%03d: TOO MANY HEADERS (more than 32)",
				fr);
		
		return env->NewStringUTF(buf);
	}

	p = buf + strlen(buf);

	sprintf(p,
			"JPacket.State#%03d   : "
			"[%10s(%2s/%4s) | %4s |"
			"%7s |"
			"%7s |"
			"%4s | "
			"%7s | "
			"%7s ]\n",
			fr,
			"Protocol",
			"ID",
			"Flag",
			"Start",
			"Prefix",
			"Header",
			"Gap",
			"Payload",
			"Postfix"
			);

	
	for (int i = 0; i < packet->pkt_header_count; i ++) {
		p = buf + strlen(buf);
		
		sprintf(p, 
				"JPacket.State#%03d[%d]: "
				"[%10s(%2d/%04X) | %5d | "
				"%6d | "
				"%6d | "
				"%3d | "
				"%7d | "
				"%7d ]\n",
				fr,	i,
				id2str(packet->pkt_headers[i].hdr_id),
				packet->pkt_headers[i].hdr_id,
				packet->pkt_headers[i].hdr_flags,
				packet->pkt_headers[i].hdr_offset,
				packet->pkt_headers[i].hdr_prefix,
				packet->pkt_headers[i].hdr_length,
				packet->pkt_headers[i].hdr_gap,
				packet->pkt_headers[i].hdr_payload,
				packet->pkt_headers[i].hdr_postfix
				);
		
	}
	
	return env->NewStringUTF(buf);
}
Exemplo n.º 12
0
/**
 * Scan packet buffer
 */
int scan(JNIEnv *env, jobject obj, jobject jpacket, scanner_t *scanner,
		packet_state_t *p_packet, int first_id, char *buf, int buf_len,
		uint32_t wirelen) {

	scan_t scan; // Our current in progress scan's state information
	scan_t *pscan = &scan;
	scan.env = env;
	scan.jscanner = obj;
	scan.jpacket = jpacket;
	scan.scanner = scanner;
	scan.packet = p_packet;
	scan.header = &p_packet->pkt_headers[0];
	scan.buf = buf;
	scan.buf_len = buf_len; // Changing buffer length, reduced by 'postfix'
	scan.mem_len = buf_len; // Constant in memory buffer length
	scan.wire_len = wirelen;
	scan.offset = 0;
	scan.length = 0;
	scan.id = first_id;
	scan.next_id = PAYLOAD_ID;
	scan.flags = 0;
	scan.stack_index = 0;

	scan.hdr_count = 0;
	scan.hdr_flags = 0;
	scan.hdr_prefix = 0;
	scan.hdr_gap = 0;
	scan.hdr_payload = 0;
	scan.hdr_postfix = 0;

	memset(scan.header, 0, sizeof(header_t));

	// Point jscan 
	setJMemoryPhysical(env, scanner->sc_jscan, toLong(&scan));

	// Local temp variables
	register uint64_t mask;

#ifdef DEBUG
	debug_enter("scan");
	debug_trace("processing packet", "#%d", p_packet->pkt_frame_num);
#endif

	/*
	 * Main scanner loop, 1st scans for builtin header types then
	 * reverts to calling on JBinding objects to provide the binding chain
	 */
	while (scan.id != END_OF_HEADERS) {
#ifdef DEBUG
		debug_trace("", "");
		debug_trace("processing header", id2str(scan.id));
		debug_scan("loop-top", &scan);
#endif

		/* A flag that keeps track of header recording. Set in record_header()*/
		scan.is_recorded = 0;

		/*
		 * If debugging is compiled in, we can also call on each protocols
		 * debug_* function to print out details about the protocol header
		 * structure. 
		 */
#ifdef DEBUG
		if (native_debug[scan.id]) {
			native_debug[scan.id](scan.buf + scan.offset);
		}
#endif

		/* 
		 * Scan of each protocol is done through a dispatch function table.
		 * Each protocol that has a protocol header scanner attached, a scanner
		 * designed specifically for that protocol. The protocol id is also the
		 * index into the table. There are 2 types of scanners both have exactly
		 * the same signature and thus both are set in this table. The first is
		 * the native scanner that only performs a direct scan of the header.
		 * The second scanner is a java header scanner. It is based on 
		 * JHeaderScanner class. A single dispatch method callJavaHeaderScanner
		 * uses the protocol ID to to dispatch to the appropriate java scanner.
		 * Uses a separate java specific table: sc_java_header_scanners[]. The
		 * java scanner is capable of calling the native scan method from java
		 * but also adds the ability to check all the attached JBinding[] for
		 * any additional registered bindings. Interesting fact is that if the
		 * java scanner doesn't have any bindings nor does it override the 
		 * default scan method to perform a scan in java and is also setup to 
		 * dispatch to native scanner, it is exactly same thing as if the 
		 * native scanner was dispatched directly from here, but round 
		 * about way through java land.
		 */
		if (scanner->sc_scan_table[scan.id] != NULL) {
			scanner->sc_scan_table[scan.id](&scan); // Dispatch to scanner
		}

#ifdef DEBUG
		debug_scan("loop-middle", &scan);
#endif

		if (scan.length == 0) {
#ifdef DEBUG
			debug_scan("loop-length==0", &scan);
#endif
			if (scan.id == PAYLOAD_ID) {
				if (scan.stack_index == 0) {
					scan.next_id = END_OF_HEADERS;
				} else {
					scan.stack_index --;
					scan.next_id = scan.stack[scan.stack_index].next_id;
					scan.offset = scan.stack[scan.stack_index].offset;
				}
			} else {
				scan.next_id = PAYLOAD_ID;
			}

		} else { // length != 0

#ifdef DEBUG
			debug_scan("loop-length > 0", &scan);
#endif

			/******************************************************
			 * ****************************************************
			 * * If override flag is set, then we reset the
			 * * discovered next protocol. If that is what the user 
			 * * wants then that is what he gets.
			 * ****************************************************
			 ******************************************************/
			if (scanner->sc_flags[scan.id] & FLAG_OVERRIDE_BINDING) {
#ifdef DEBUG
				debug_scan("TCP OVERRIDE", &scan);
#endif
				scan.next_id = PAYLOAD_ID;
			}

			/******************************************************
			 * ****************************************************
			 * * Now do HEURISTIC discovery scans if the appropriate
			 * * flags are set. Heuristics allow us to provide nxt
			 * * protocol binding, using discovery (an educated 
			 * * guess). 
			 * ****************************************************
			 ******************************************************/
			if (scanner->sc_flags[scan.id] & FLAG_HEURISTIC_BINDING) {

				/* 
				 * Save these critical properties, in case heuristic changes them
				 * for this current header, not the next one its supposed to
				 * check for.
				 */
				int saved_offset = scan.offset;
				int saved_length = scan.length;

				/* Advance offset to next header, so that heuristics can get a 
				 * peek. It will be restored at the end of heuristics block.
				 */
				scan.offset += scan.length + scan.hdr_gap;

				/*
				 * 2 types of heuristic bindings. Pre and post.
				 * Pre - heuristics are run before the direct discovery method
				 *       in scanner. Only after the pre-heuristic fail do we
				 *       utilize the directly discovered binding.
				 * 
				 * Post - heuristics are run after the direct discovery method
				 *        didn't produce a binding.
				 *
				 * ------------------------------------------------------------
				 * 
				 * In our case, since we have already ran the direct discovery
				 * in the header scanner, we save scan.next_id value, reset it,
				 * call the heuristic function, check its scan.next_id if it
				 * was set, if it was, then use that instead. Otherwise if it
				 * wasn't restore the original next_id and continue on normally.
				 */
				if (scanner->sc_flags[scan.id] & FLAG_HEURISTIC_PRE_BINDING) {
#ifdef DEBUG
					debug_scan("heurists_pre", &scan);
#endif

					int saved_next_id = scan.next_id;
					scan.next_id = PAYLOAD_ID;

					for (int i = 0; i < MAX_ID_COUNT; i++) {
						native_validate_func_t validate_func;
						validate_func
								= scanner->sc_heuristics_table[scan.id][i];

						if (validate_func == NULL) {
							break;
						}

						if ((scan.next_id = validate_func(&scan)) != INVALID) {
							break;
						}
					}

					if (scan.next_id == PAYLOAD_ID) {
						scan.next_id = saved_next_id;
					}

				} else if (scan.next_id == PAYLOAD_ID) {
#ifdef DEBUG
					debug_scan("heurists_post", &scan);
#endif
					for (int i = 0; i < MAX_ID_COUNT; i++) {
						native_validate_func_t validate_func;
						validate_func
								= scanner->sc_heuristics_table[scan.id][i];

						if (validate_func == NULL) {
							break;
						}

#ifdef DEBUG
						debug_trace("heurists_post", "[%d]", i);
#endif
						if ((scan.next_id = validate_func(&scan)) != INVALID) {

#ifdef DEBUG
							debug_scan("heurists_post::found", &scan);
#endif

							break;
						}
					}
				}

				/* Restore these 2 critical properties */
				scan.offset = saved_offset;
				scan.length = saved_length;
			}

			/******************************************************
			 * ****************************************************
			 * * Now record discovered information in structures
			 * ****************************************************
			 ******************************************************/
			record_header(&scan);

#ifdef DEBUG
			debug_header("header_t", scan.header - 1);
#endif
		} // End if len != 0

#ifdef DEBUG
		debug_scan("loop-bottom", &scan);
#endif

		scan.id = scan.next_id;
		scan.offset += scan.length + scan.hdr_gap;
		scan.length = 0;
		scan.next_id = PAYLOAD_ID;

		if (scan.offset >= scan.buf_len) {
			scan.id = END_OF_HEADERS;
		}

	} // End for loop

	/* record number of header entries found */
	//	scan.packet->pkt_header_count = count;

	process_flow_key(&scan);

#ifdef DEBUG
	debug_trace("loop-finished",
			"header_count=%d offset=%d header_map=0x%X",
			scan.packet->pkt_header_count, scan.offset,
			scan.packet->pkt_header_map);
	debug_exit("scan()");
#endif

	return scan.offset;
} // End scan()
Exemplo n.º 13
0
/**
 * Adjusts for a packet that has been truncated. Sets appropriate flags in the
 * header flags field, resets lengths of prefix, header, gap, payload and 
 * postfix appropriately to account for shortened packet.
 */
void adjustForTruncatedPacket(scan_t *scan) {
#ifdef DEBUG
	debug_enter("adjustForTruncatedPacket");
	debug_trace("packet", "%ld", scan->scanner->sc_cur_frame_num);
#endif

	/*
	 * Adjust for truncated packets. We check the end of the header record
	 * against the buf_len. If the end is past the buf_len, that means that we
	 * need to start trucating in the following order: 
	 * postfix, payload, gap, header, prefix
	 * 
	 * +-------------------------------------------+
	 * | prefix | header | gap | payload | postfix |
	 * +-------------------------------------------+
	 * 
	 */
	register int start = scan->offset + scan->hdr_prefix + scan->length
			+ scan->hdr_gap + scan->hdr_payload;

	register int end = start + scan->hdr_postfix;
	register int buf_len = scan->buf_len;

#ifdef DEBUG
	debug_scan((char *)id2str(scan->id), scan);
	debug_trace(id2str(scan->id), "offset=%d, pre=%d, len=%d, gap=%d, pay=%d, post=%d",
			scan->offset,
			scan->hdr_prefix,
			scan->length,
			scan->hdr_gap,
			scan->hdr_payload,
			scan->hdr_postfix);
	debug_trace(id2str(scan->id), "start=%d end=%d buf_len=%d", start, end, buf_len);
#endif

	if (end > buf_len) { // Check if postfix extends past the end of packet

		/*
		 * Because postfix is at the end, whenever the packet is truncated
		 * postfix is always truncated, unless it wasn't set
		 */
		if (scan->hdr_postfix > 0) {
			scan->hdr_flags |= HEADER_FLAG_PREFIX_TRUNCATED;
			scan->hdr_postfix = (start > scan->mem_len) ? 0 : scan->mem_len
					- start;
			scan->hdr_postfix = (scan->hdr_postfix < 0) ? 0 : scan->hdr_postfix;
#ifdef DEBUG
			debug_scan("adjust postfix", scan);
#endif
		}

		/* Position at payload and process */
		start -= scan->hdr_payload;
		end = start + scan->hdr_payload;

		if (end > buf_len) {
			scan->hdr_flags |= HEADER_FLAG_PAYLOAD_TRUNCATED;
			scan->hdr_payload = (start > buf_len) ? 0 : buf_len - start;
			scan->hdr_payload = (scan->hdr_payload < 0) ? 0 : scan->hdr_payload;

#ifdef DEBUG
			debug_scan("adjust payload", scan);
			debug_trace("adjust payload", "start=%d end=%d", start, end);
#endif

			/* Position at gap and process */
			start -= scan->hdr_gap;
			end = start + scan->hdr_gap;
			if (scan->hdr_gap > 0 && end > buf_len) {

				scan->hdr_flags |= HEADER_FLAG_GAP_TRUNCATED;
				scan->hdr_gap = (start > buf_len) ? 0 : buf_len - start;
				scan->hdr_gap = (scan->hdr_gap < 0) ? 0 : scan->hdr_gap;
#ifdef DEBUG
				debug_scan("adjust gap", scan);
#endif
			}

			/* Position at header and process */
			start -= scan->length;
			end = start + scan->length;

			if (end > buf_len) {
				scan->hdr_flags |= HEADER_FLAG_HEADER_TRUNCATED;
				scan->length = (start > buf_len) ? 0 : buf_len - start;
				scan->length = (scan->length < 0) ? 0 : scan->length;
#ifdef DEBUG
				debug_scan("adjust header", scan);
#endif

				/* Position at prefix and process */
				start -= scan->hdr_prefix;
				end = start + scan->hdr_prefix;

				if (0 && scan->hdr_prefix > 0 && end > buf_len) {
					scan->hdr_flags |= HEADER_FLAG_PREFIX_TRUNCATED;
					scan->hdr_prefix = (start > buf_len) ? 0 : buf_len - start;
					scan->hdr_prefix = (scan->hdr_prefix < 0) ? 0
							: scan->hdr_prefix;
#ifdef DEBUG
					debug_scan("adjust prefix", scan);
#endif

				}
			}
		}
	}

#ifdef DEBUG
	debug_exit("adjustForTruncatedPacket");
#endif
#undef DEBUG
}
Exemplo n.º 14
0
/* scanner specific debug_ trace functions */
void debug_header(char *msg, header_t *header) {
	debug_trace(msg,
			"id=%s prefix=%-3d header=%-3d gap=%-3d payload=%-3d post=%-3d",
			id2str(header->hdr_id), header->hdr_prefix, header->hdr_length,
			header->hdr_gap, header->hdr_payload, header->hdr_postfix);
}
Exemplo n.º 15
0
void
langdemo(Pool *pool)
{
  Id screenid, p, pp;

  screenid = str2id(pool, "3ddiag", 1);
  static const char *languages[] = {"es", "de"};
  pool_set_languages(pool, languages, 2);
  FOR_PROVIDES(p, pp, screenid)
    {
      unsigned int medianr;
      Id chktype = 0;
      char *loc = solvable_get_location(pool->solvables + p, &medianr);
      const char *chksum;
      chksum = solvable_lookup_checksum(pool->solvables + p, SOLVABLE_CHECKSUM, &chktype);
      printf("%s: %s\n%s[%d] %s:%s\n", solvable2str(pool, pool->solvables + p), solvable_lookup_str_poollang(pool->solvables + p, SOLVABLE_DESCRIPTION), loc, medianr, id2str(pool, chktype), chksum);
      printf("DE: %s\n", solvable_lookup_str_lang(pool->solvables + p, SOLVABLE_DESCRIPTION, "de", 1));
    }
Exemplo n.º 16
0
/** Write out a host's state as JSON data.
  * \param h The host object
  * \param into ioport to use
  */
int jsoncodec_encode_host (ioport *into, host *h) {
    char buffer[256];
    uint64_t pathbuffer[128];
    int paths = 0;
    uint64_t pathmask = 0;
    meter *m = h->first;
    int i;
    int first=1;
    int dobrk = 0;
    ioport_write (into, "{\n", 2);
    while (m) {
        pathmask = idhaspath (m->id);
        if (pathmask) {
            dobrk = 0;
            for (i=0; i<paths; ++i) {
                if (pathbuffer[i] == (m->id & pathmask)) {
                    dobrk = 1;
                    break;
                }
            }
            if (dobrk) {
                m = m->next;
                continue;
            }
            if (paths>127) return 1;
            pathbuffer[paths++] = (m->id & pathmask);
        }
        
        if (first) first=0;
        else ioport_write (into, ",\n", 2);
        ioport_write (into, "  \"", 3);

        if (pathmask) {
            id2str (m->id & pathmask, buffer);
            ioport_write (into, buffer, strlen(buffer));
            ioport_write (into, "\":", 2);
            if (m->count == SZ_EMPTY_VAL) {
                ioport_write (into, "null",4);
            }
            else if (m->count == SZ_EMPTY_ARRAY) {
                ioport_write (into, "[]",2);
            }
            else {
                if (m->count > 0) {
                    ioport_write (into, "[\n", 2);
                }
            
                for (i=0; (i==0)||(i<m->count); ++i) {
                    if (i) ioport_write (into, ",\n", 2);
                    jsoncodec_dump_pathval (m,i,into,(m->count ? 1 : 0));
                }
            
                if (m->count > 0) {
                    ioport_write (into, "\n  ]", 4);
                }
            }
        }
        else {
            id2str (m->id, buffer);
            ioport_write (into, buffer, strlen(buffer));
            ioport_write (into, "\":", 2);
            if (m->count == SZ_EMPTY_VAL) {
                ioport_write (into, "null", 4);
            }
            else if (m->count == SZ_EMPTY_ARRAY) {
                ioport_write (into, "[]", 2);
            }
            else {
                if (m->count > 0) {
                    ioport_write (into, "[", 1);
                }
                for (i=0; (i==0)||(i<m->count); ++i) {
                    jsoncodec_dump_val (m->id & MMASK_TYPE, m, i, into);
                    if ((i+1)<m->count) ioport_write (into, ",",1);
                }
                if (m->count > 0) {
                    ioport_write (into, "]", 1);
                }
            }
        }
        m=m->next;
    }
    ioport_write (into,"\n}\n",3);
    return 0;
}
Exemplo n.º 17
0
/** Inspect a host's metering data to determine its current status
  * and problems, then write it to disk.
  * \param host The host to inspect.
  */
void watchthread_handle_host (host *host) {
    int problemcount = 0;
    double totalbadness = 0.0;
    time_t tnow = time (NULL);
    meter *m = host->first;
    meterwatch *w;
    watchadjust *adj = NULL;
    watchtrigger maxtrigger = WATCH_NONE;
    char label[16];
    char uuidstr[40];
    
    pthread_rwlock_wrlock (&host->lock);

    /* We'll store the status information as a meter itself */
    meterid_t mid_status = makeid ("status",MTYPE_STR,0);
    meter *m_status = host_get_meter (host, mid_status);
    fstring ostatus = meter_get_str (m_status, 0);
    meter_setcount (m_status, 0);
    if (ostatus.str[0] == 0) strcpy (ostatus.str, "UNSET");
    
    /* If the data is stale, don't add to badness, just set the status. */
    if ((tnow - host->lastmodified) > 80) {
        if (strcmp (ostatus.str, "STALE") != 0) {
            uuid2str (host->uuid, uuidstr);
            log_info ("Status change host <%s> %s -> STALE", uuidstr, ostatus.str);
            tenant_set_notification (host->tenant, true, "STALE", host->uuid);
        }
        meter_set_str (m_status, 0, "STALE");
    }
    else {

        /* Figure out badness for each meter, and add to host */
        while (m) {
            m->badness = 0.0;
            int handled = 0;
            
            /* Get host-level adjustments in place */
            adj = adjustlist_find (&host->adjust, m->id);
            
            /* First go over the tenant-defined watchers */
            pthread_mutex_lock (&host->tenant->watch.mutex);
            w = host->tenant->watch.first;
            while (w) {
                if ((w->id & MMASK_NAME) == (m->id & MMASK_NAME)) {
                    m->badness += calculate_badness (m, w, adj, &maxtrigger);
                    handled = 1;
                }
                w = w->next;
            }
            pthread_mutex_unlock (&host->tenant->watch.mutex);
       
            /* If the tenant didn't have anything suitable, go over the
               global watchlist */
            if (! handled) {
                pthread_mutex_lock (&APP.watch.mutex);
                w = APP.watch.first;

                while (w) {
                    if ((w->id & MMASK_NAME) == (m->id & MMASK_NAME)) {
                        m->badness += calculate_badness (m, w, adj, 
                                                         &maxtrigger);
                        handled = 1;
                    }
                    w = w->next;
                }
                pthread_mutex_unlock (&APP.watch.mutex);
            }
      
            if (m->badness) problemcount++;
            totalbadness += m->badness;
            m = m->next;
        }
    
        /* Don't raise a CRIT alert on a WARN condition */
        switch (maxtrigger) {
            case WATCH_NONE:
                totalbadness = 0.0;
                break;
        
            case WATCH_WARN:
                if (host->badness > 50.0) totalbadness = 0.0;
                break;
        
            case WATCH_ALERT:
                if (host->badness > 90.0) totalbadness = 0.0;
                break;
        
            case WATCH_CRIT:
                if (host->badness > 150.0) totalbadness = 0.0;
                break;
        }
    
        host->badness += totalbadness;
    
        /* Put up the problems as a meter as well */
        meterid_t mid_problems = makeid ("problems",MTYPE_STR,0);
        meter *m_problems = host_get_meter (host, mid_problems);
    
        /* While we're looking at it, consider the current badness, if there
           are no problems, it should be going down. */
        if (! problemcount) {
            if (host->badness > 100.0) host->badness = host->badness/2.0;
            else if (host->badness > 1.0) host->badness = host->badness *0.75;
            else host->badness = 0.0;
            meter_set_empty_array (m_problems);
        }
        else {
            /* If we reached the top, current level may still be out of
               its league, so allow it to decay slowly */
            if (totalbadness == 0.0) host->badness *= 0.9;
        
            /* Fill in the problem array */
            int i=0;
            meter_setcount (m_problems, problemcount);
            m = host->first;
        
            while (m && (i<16)) {
                if (m->badness > 0.00) {
                    id2str (m->id, label);
                    meter_set_str (m_problems, i++, label);
                }
                m = m->next;
            }
        }
    
        meterid_t mid_badness = makeid ("badness",MTYPE_FRAC,0);
        meter *m_badness = host_get_meter (host, mid_badness);
        meter_setcount (m_badness, 0);
        meter_set_frac (m_badness, 0, host->badness);
    
        const char *nstatus = "UNSET";
    
        /* Convert badness to a status text */
        if (host->badness < 30.0) nstatus = "OK";
        else if (host->badness < 80.0) nstatus = "WARN";
        else if (host->badness < 120.0) nstatus = "ALERT";
        else nstatus = "CRIT";
    
        if (strcmp (nstatus, ostatus.str) != 0) {
            uuid2str (host->uuid, uuidstr);
            log_info ("Status change host <%s> %s -> %s", uuidstr, ostatus.str, nstatus);
            bool isproblem = (host->badness>= 80.0);
            tenant_set_notification (host->tenant, isproblem, nstatus, host->uuid);
        }
    
        meter_set_str (m_status, 0, nstatus);
    }
    
    /* Write to db */
    if (db_open (APP.writedb, host->tenant->uuid, NULL)) {
        db_save_record (APP.writedb, tnow, host);
        db_close (APP.writedb);
    }
    
    pthread_rwlock_unlock (&host->lock);
    
    /* for tallying the summaries we only need read access */

    if (! host->tenant) return;
    
    pthread_rwlock_rdlock (&host->lock);
    m = host->first;
    while (m) {
        summaryinfo_add_meterdata (&host->tenant->summ, m->id, &m->d);
        m = m->next;
    }
    pthread_rwlock_unlock (&host->lock);
}