int main(void) { time_t now; idmef_time_t *ctime; idmef_alert_t *alert; idmef_message_t *idmef; assert(idmef_message_new(&idmef) == 0); assert(idmef_message_new_alert(idmef, &alert) == 0); ctime = idmef_alert_get_create_time(alert); assert(ctime != NULL); now = time(NULL); assert(now - idmef_time_get_sec(ctime) < MAX_LAG_SEC); exit(0); }
static void cast_int32(void) { idmef_value_t *value; assert(idmef_value_new_int32(&value, INT32_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_INT64); assert(idmef_value_get_int64(value) == INT32_MIN); idmef_value_destroy(value); assert(idmef_value_new_int32(&value, INT32_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_FLOAT); assert(idmef_value_get_float(value) == INT32_MIN); idmef_value_destroy(value); assert(idmef_value_new_int32(&value, INT32_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DOUBLE); assert(idmef_value_get_double(value) == INT32_MIN); idmef_value_destroy(value); assert(idmef_value_new_int32(&value, INT32_MIN) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA); assert((int) idmef_data_get_uint32(idmef_value_get_data(value)) == INT32_MIN); idmef_value_destroy(value); assert(idmef_value_new_int32(&value, INT32_MAX) == 0); assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) == 0); assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_TIME); assert(idmef_time_get_sec(idmef_value_get_time(value)) == INT32_MAX); idmef_value_destroy(value); }
static int do_btime_match(const idmef_criterion_value_t *cv, idmef_criterion_operator_t op, idmef_value_t *value) { time_t sec, wanted, matched; struct tm lt, *comp = cv->value; if ( idmef_value_get_type(value) != IDMEF_VALUE_TYPE_TIME ) return -1; sec = idmef_time_get_sec(idmef_value_get_time(value)); if ( ! gmtime_r(&sec, <) ) return prelude_error_from_errno(errno); /* * Apply mask */ if ( comp->tm_sec < 0 ) lt.tm_sec = -1; if ( comp->tm_min < 0 ) lt.tm_min = -1; if ( comp->tm_mon < 0 ) lt.tm_mon = -1; if ( comp->tm_hour < 0 ) lt.tm_hour = -1; if ( comp->tm_mday < 0 ) lt.tm_mday = -1; if ( comp->tm_year < 0 ) lt.tm_year = -1; if ( comp->tm_wday < 0 ) lt.tm_wday = -1; if ( comp->tm_yday < 0 ) lt.tm_yday = -1; wanted = timegm(comp); matched = timegm(<); if ( op & IDMEF_CRITERION_OPERATOR_EQUAL && matched == wanted ) return 1; if ( op & IDMEF_CRITERION_OPERATOR_LESSER && matched < wanted) return 1; if ( op & IDMEF_CRITERION_OPERATOR_GREATER && matched > wanted ) return 1; return 0; }
/** * idmef_time_to_ntpstamp: * @time: Pointer to an IDMEF time structure. * @out: Pointer to a #prelude_string_t output buffer. * * Translates @time to an user readable NTP timestamp string, * conforming to the IDMEF defined time format. * * Returns: number of bytes written on success, a negative value if an error occured. */ int idmef_time_to_ntpstamp(const idmef_time_t *time, prelude_string_t *out) { l_fp ts; struct timeval tv; unsigned ts_mask = TS_MASK; /* defaults to 20 bits (us) */ unsigned ts_roundbit = TS_ROUNDBIT; /* defaults to 20 bits (us) */ int ret; prelude_return_val_if_fail(time, prelude_error(PRELUDE_ERROR_ASSERTION)); prelude_return_val_if_fail(out, prelude_error(PRELUDE_ERROR_ASSERTION)); tv.tv_sec = idmef_time_get_sec(time); tv.tv_usec = idmef_time_get_usec(time); sTVTOTS(&tv, &ts); ts.l_ui += JAN_1970; /* make it time since 1900 */ ts.l_uf += ts_roundbit; ts.l_uf &= ts_mask; ret = prelude_string_sprintf(out, "0x%08lx.0x%08lx", (unsigned long) ts.l_ui, (unsigned long) ts.l_uf); return ret; }