Example #1
0
static void
handle_query_response (const Foo__LookupResult *result,
                       void *closure_data)
{
  if (result == NULL)
    printf ("Error processing request.\n");
  else if (result->person == NULL)
    printf ("Not found.\n");
  else
    {
      Foo__Person *person = result->person;
      unsigned i;
      printf ("%s\n"
              " %u\n", person->name, person->id);
      if (person->email)
        printf (" %s\n", person->email);
      for (i = 0; i < person->n_phone; i++)
        {
          const ProtobufCEnumValue *ev;
          ev = protobuf_c_enum_descriptor_get_value (&foo__person__phone_type__descriptor, person->phone[i]->type);
          printf (" %s %s\n",
                  ev ? ev->name : "???",
                  person->phone[i]->number);
        }
    }

  * (protobuf_c_boolean *) closure_data = 1;
}
Example #2
0
void KineticLogger_LogStatus(KineticProto_Status* status)
{
    if (LogLevel < 0) {
        return;
    }

    ProtobufCMessage* protoMessage = (ProtobufCMessage*)status;
    KineticProto_Status_StatusCode code = status->code;

    if (code == KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS) {
        printf("Operation completed successfully\n");
    }
    else if (code == KINETIC_PROTO_STATUS_STATUS_CODE_INVALID_STATUS_CODE) {
        printf("Operation was aborted!\n");
    }
    else {
        // Output status code short name
        const ProtobufCMessageDescriptor* protoMessageDescriptor = protoMessage->descriptor;
        const ProtobufCFieldDescriptor* statusCodeDescriptor =
            protobuf_c_message_descriptor_get_field_by_name(protoMessageDescriptor, "code");
        const ProtobufCEnumDescriptor* statusCodeEnumDescriptor =
            (ProtobufCEnumDescriptor*)statusCodeDescriptor->descriptor;
        const ProtobufCEnumValue* eStatusCodeVal =
            protobuf_c_enum_descriptor_get_value(statusCodeEnumDescriptor, code);
        KineticLogger_LogPrintf("Operation completed but failed w/error: %s=%d(%s)\n",
                                statusCodeDescriptor->name, code, eStatusCodeVal->name);

        // Output status message, if supplied
        if (status->statusMessage) {
            const ProtobufCFieldDescriptor* statusMsgFieldDescriptor =
                protobuf_c_message_descriptor_get_field_by_name(protoMessageDescriptor, "statusMessage");
            const ProtobufCMessageDescriptor* statusMsgDescriptor =
                (ProtobufCMessageDescriptor*)statusMsgFieldDescriptor->descriptor;

            KineticLogger_LogPrintf("  %s: '%s'", statusMsgDescriptor->name, status->statusMessage);
        }

        // Output detailed message, if supplied
        if (status->has_detailedMessage) {
            char tmp[8], msg[256];
            const ProtobufCFieldDescriptor* statusDetailedMsgFieldDescriptor =
                protobuf_c_message_descriptor_get_field_by_name(
                    protoMessageDescriptor, "detailedMessage");
            const ProtobufCMessageDescriptor* statusDetailedMsgDescriptor =
                (ProtobufCMessageDescriptor*)
                statusDetailedMsgFieldDescriptor->descriptor;

            sprintf(msg, "  %s: ", statusDetailedMsgDescriptor->name);
            for (size_t i = 0; i < status->detailedMessage.len; i++) {
                sprintf(tmp, "%02hhX", status->detailedMessage.data[i]);
                strcat(msg, tmp);
            }
            KineticLogger_LogPrintf("  %s", msg);
        }
    }
}
Example #3
0
static void
required_field_print(const ProtobufCFieldDescriptor *field,
					 const void *member,
					 FILE *out, int tabs)
{
  switch (field->type)
	{
	case PROTOBUF_C_TYPE_SFIXED32:
	case PROTOBUF_C_TYPE_SINT32:
	case PROTOBUF_C_TYPE_INT32:
	  fprintf(out, "%d", *(const int32_t *)member); break;
	case PROTOBUF_C_TYPE_FIXED32:
	case PROTOBUF_C_TYPE_UINT32:
	  fprintf(out, "%u", *(const uint32_t *)member); break;
	case PROTOBUF_C_TYPE_SFIXED64:
	case PROTOBUF_C_TYPE_SINT64:
	case PROTOBUF_C_TYPE_INT64:
	  fprintf(out, "%"PRId64, *(const int64_t *)member); break;
	case PROTOBUF_C_TYPE_FIXED64:
	case PROTOBUF_C_TYPE_UINT64:
	  fprintf(out, "%"PRIu64, *(const uint64_t *)member); break;
	case PROTOBUF_C_TYPE_FLOAT:
	  fprintf(out, "%f", *(const float *)member); break;
	case PROTOBUF_C_TYPE_DOUBLE:
	  fprintf(out, "%lf", *(const double *)member); break;
	case PROTOBUF_C_TYPE_BOOL:
	  fprintf(out, "%s", *(const protobuf_c_boolean *)member
							? "true" : "false");
	  break;
	case PROTOBUF_C_TYPE_STRING:
	  fprintf(out, "[%zu] \"%s\"", strlen(*(char * const *)member),
			  *(char * const *)member);
	  break;
	case PROTOBUF_C_TYPE_BYTES:
	  {
		const ProtobufCBinaryData * bd = ((const ProtobufCBinaryData*) member);
		fprintf(out, "[%zu] ", bd->len);
		int i;
		for (i = 0; i < bd->len; i++)
			fprintf(out, "%02x ", bd->data[i]);

		int printable = 1;
		for (i = 0; i < bd->len; i++) {
			if (!isprint(bd->data[i])) {
				printable = 0;
				break;
			}
		}

		if (printable && bd->len > 0) {
			fprintf(out, "(\"");
			for (i = 0; i < bd->len; i++)
				fprintf(out, "%c", bd->data[i]);
			fprintf(out, "\")");
		}
		break;
	  }
	//case PROTOBUF_C_TYPE_GROUP:          // NOT SUPPORTED
	case PROTOBUF_C_TYPE_MESSAGE:
	  fprintf(out, "\n");
		message_body_print(*(ProtobufCMessage * const *)member, out, tabs);
		break;
	case PROTOBUF_C_TYPE_ENUM:
	{
		int32_t value = *(const int32_t *) member;
		const ProtobufCEnumValue *evalue = protobuf_c_enum_descriptor_get_value(field->descriptor, value);
		fprintf(out, "%d (%s)", evalue->value, evalue->name);
		break;
	}
	}
}
Example #4
0
static void
print_yaml(dns_dtdata_t *dt) {
	Dnstap__Dnstap *frame = dt->frame;
	Dnstap__Message *m = frame->message;
	const ProtobufCEnumValue *ftype, *mtype;
	static isc_boolean_t first = ISC_TRUE;

	ftype = protobuf_c_enum_descriptor_get_value(
				     &dnstap__dnstap__type__descriptor,
				     frame->type);
	if (ftype == NULL)
		return;

	if (!first)
		printf("---\n");
	else
		first = ISC_FALSE;

	printf("type: %s\n", ftype->name);

	if (frame->has_identity)
		printf("identity: %.*s\n", (int) frame->identity.len,
		       frame->identity.data);

	if (frame->has_version)
		printf("version: %.*s\n", (int) frame->version.len,
		       frame->version.data);

	if (frame->type != DNSTAP__DNSTAP__TYPE__MESSAGE)
		return;

	printf("message:\n");

	mtype = protobuf_c_enum_descriptor_get_value(
				     &dnstap__message__type__descriptor,
				     m->type);
	if (mtype == NULL)
		return;

	printf("  type: %s\n", mtype->name);

	if (!isc_time_isepoch(&dt->qtime)) {
		char buf[100];
		isc_time_formatISO8601(&dt->qtime, buf, sizeof(buf));
		printf("  query_time: !!timestamp %s\n", buf);
	}

	if (!isc_time_isepoch(&dt->rtime)) {
		char buf[100];
		isc_time_formatISO8601(&dt->rtime, buf, sizeof(buf));
		printf("  response_time: !!timestamp %s\n", buf);
	}

	if (dt->msgdata.base != NULL) {
		printf("  message_size: %zdb\n", (size_t) dt->msgdata.length);
	} else
		printf("  message_size: 0b\n");

	if (m->has_socket_family) {
		const ProtobufCEnumValue *type =
			protobuf_c_enum_descriptor_get_value(
				&dnstap__socket_family__descriptor,
				m->socket_family);
		if (type != NULL)
			printf("  socket_family: %s\n", type->name);
	}

	printf("  socket_protocol: %s\n", dt->tcp ? "TCP" : "UDP");

	if (m->has_query_address) {
		ProtobufCBinaryData *ip = &m->query_address;
		char buf[100];

		(void)inet_ntop(ip->len == 4 ? AF_INET : AF_INET6,
				ip->data, buf, sizeof(buf));
		printf("  query_address: %s\n", buf);
	}

	if (m->has_response_address) {
		ProtobufCBinaryData *ip = &m->response_address;
		char buf[100];

		(void)inet_ntop(ip->len == 4 ? AF_INET : AF_INET6,
				ip->data, buf, sizeof(buf));
		printf("  response_address: %s\n", buf);
	}

	if (m->has_query_port)
		printf("  query_port: %u\n", m->query_port);

	if (m->has_response_port)
		printf("  response_port: %u\n", m->response_port);

	if (m->has_query_zone) {
		isc_result_t result;
		dns_fixedname_t fn;
		dns_name_t *name;
		isc_buffer_t b;
		dns_decompress_t dctx;

		dns_fixedname_init(&fn);
		name = dns_fixedname_name(&fn);

		isc_buffer_init(&b, m->query_zone.data, m->query_zone.len);
		isc_buffer_add(&b, m->query_zone.len);

		dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
		result = dns_name_fromwire(name, &b, &dctx, 0, NULL);
		if (result == ISC_R_SUCCESS) {
			printf("  query_zone: ");
			dns_name_print(name, stdout);
			printf("\n");
		}
	}

	if (dt->msg != NULL) {
		printf("  %s:\n", ((dt->type & DNS_DTTYPE_QUERY) != 0)
				     ? "query_message_data"
				     : "response_message_data");

		print_packet(dt, &dns_master_style_yaml);

		printf("  %s: |\n", ((dt->type & DNS_DTTYPE_QUERY) != 0)
				     ? "query_message"
				     : "response_message");
		print_packet(dt, &dns_master_style_indent);
	}
};
Example #5
0
void KineticLogger_LogProtobuf(const KineticProto* proto)
{
    if (LogLevel < 0) {
        return;
    }

    LOG_PROTO_INIT();
    char tmpBuf[1024];

    if (proto == NULL) {
        return;
    }

    LOG("Kinetic Protobuf:");
    if (proto->command) {
        LOG_PROTO_LEVEL_START("command");

        if (proto->command->header) {
            LOG_PROTO_LEVEL_START("header");
            {
                if (proto->command->header->has_clusterVersion) {
                    LOGF("%sclusterVersion: %lld", _indent,
                         proto->command->header->clusterVersion);
                }
                if (proto->command->header->has_identity) {
                    LOGF("%sidentity: %lld", _indent,
                         proto->command->header->identity);
                }
                if (proto->command->header->has_connectionID) {
                    LOGF("%sconnectionID: %lld", _indent,
                         proto->command->header->connectionID);
                }
                if (proto->command->header->has_sequence) {
                    LOGF("%ssequence: %lld", _indent,
                         proto->command->header->sequence);
                }
                if (proto->command->header->has_ackSequence) {
                    LOGF("%sackSequence: %lld", _indent,
                         proto->command->header->ackSequence);
                }
                if (proto->command->header->has_messageType) {
                    const ProtobufCEnumValue* eVal = protobuf_c_enum_descriptor_get_value(
                                                         &KineticProto_message_type__descriptor,
                                                         proto->command->header->messageType);
                    LOGF("%smessageType: %s", _indent, eVal->name);
                }
                if (proto->command->header->has_timeout) {
                    LOGF("%stimeout: %lld", _indent,
                         proto->command->header->ackSequence);
                }
                if (proto->command->header->has_earlyExit) {
                    LOGF("%searlyExit: %s", _indent,
                         proto->command->header->earlyExit ? _str_true : _str_false);
                }
                if (proto->command->header->has_backgroundScan) {
                    LOGF("%sbackgroundScan: %s", _indent,
                         proto->command->header->backgroundScan ? _str_true : _str_false);
                }
            }
            LOG_PROTO_LEVEL_END();
        }

        if (proto->command->body) {
            LOG_PROTO_LEVEL_START("body");
            {
                if (proto->command->body->keyValue) {
                    LOG_PROTO_LEVEL_START("keyValue");
                    {
                        if (proto->command->body->keyValue->has_key) {
                            BYTES_TO_CSTRING(tmpBuf,
                                             proto->command->body->keyValue->key, 0,
                                             proto->command->body->keyValue->key.len);
                            LOGF("%skey: '%s'", _indent, tmpBuf);
                        }
                        if (proto->command->body->keyValue->has_newVersion) {
                            BYTES_TO_CSTRING(tmpBuf,
                                             proto->command->body->keyValue->newVersion,
                                             0, proto->command->body->keyValue->newVersion.len);
                            LOGF("%snewVersion: '%s'", _indent, tmpBuf);
                        }
                        if (proto->command->body->keyValue->has_dbVersion) {
                            BYTES_TO_CSTRING(tmpBuf,
                                             proto->command->body->keyValue->dbVersion,
                                             0, proto->command->body->keyValue->dbVersion.len);
                            LOGF("%sdbVersion: '%s'", _indent, tmpBuf);
                        }
                        if (proto->command->body->keyValue->has_tag) {
                            BYTES_TO_CSTRING(tmpBuf,
                                             proto->command->body->keyValue->tag,
                                             0, proto->command->body->keyValue->tag.len);
                            LOGF("%stag: '%s'", _indent, tmpBuf);
                        }
                        if (proto->command->body->keyValue->has_force) {
                            LOGF("%sforce: %s", _indent,
                                 proto->command->body->keyValue->force ? _str_true : _str_false);
                        }
                        if (proto->command->body->keyValue->has_algorithm) {
                            const ProtobufCEnumValue* eVal = protobuf_c_enum_descriptor_get_value(
                                                                 &KineticProto_algorithm__descriptor,
                                                                 proto->command->body->keyValue->algorithm);
                            LOGF("%salgorithm: %s", _indent, eVal->name);
                        }
                        if (proto->command->body->keyValue->has_metadataOnly) {
                            LOGF("%smetadataOnly: %s", _indent,
                                 proto->command->body->keyValue->metadataOnly ? _str_true : _str_false);
                        }
                        if (proto->command->body->keyValue->has_synchronization) {
                            const ProtobufCEnumValue* eVal = protobuf_c_enum_descriptor_get_value(
                                                                 &KineticProto_synchronization__descriptor,
                                                                 proto->command->body->keyValue->synchronization);
                            LOGF("%ssynchronization: %s", _indent, eVal->name);
                        }
                    }
                    LOG_PROTO_LEVEL_END();
                }
            }

            LOG_PROTO_LEVEL_END();
        }

        if (proto->command->status) {
            LOG_PROTO_LEVEL_START("status");
            {
                if (proto->command->status->has_code) {
                    const ProtobufCEnumValue* eVal = protobuf_c_enum_descriptor_get_value(
                                                         &KineticProto_status_status_code__descriptor,
                                                         proto->command->status->code);
                    LOGF("%scode: %s", _indent, eVal->name);
                }

                if (proto->command->status->statusMessage) {
                    LOGF("%sstatusMessage: '%s'", _indent, proto->command->status->statusMessage);
                }
                if (proto->command->status->has_detailedMessage) {
                    BYTES_TO_CSTRING(tmpBuf,
                                     proto->command->status->detailedMessage,
                                     0, proto->command->status->detailedMessage.len);
                    LOGF("%detailedMessage: '%s'", _indent, tmpBuf);
                }
            }
            LOG_PROTO_LEVEL_END();
        }
        LOG_PROTO_LEVEL_END();
    }

    if (proto->has_hmac) {
        BYTES_TO_CSTRING(tmpBuf,
                         proto->hmac,
                         0, proto->hmac->key.len);
        LOGF("%shmac: '%s'", _indent, tmpBuf);
    }
}
Example #6
0
static void LogUnboxed(int log_level,
                void const * const fieldData,
                ProtobufCFieldDescriptor const * const fieldDesc,
                size_t const i,
                char* log_indent)
{
    switch (fieldDesc->type) {
    case PROTOBUF_C_TYPE_INT32:
    case PROTOBUF_C_TYPE_SINT32:
    case PROTOBUF_C_TYPE_SFIXED32:
        {
            int32_t const * value = (int32_t const *)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %ld", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_INT64:
    case PROTOBUF_C_TYPE_SINT64:
    case PROTOBUF_C_TYPE_SFIXED64:
        {
            int64_t* value = (int64_t*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %lld", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_UINT32:
    case PROTOBUF_C_TYPE_FIXED32:
        {
            uint32_t* value = (uint32_t*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %lu", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_UINT64:
    case PROTOBUF_C_TYPE_FIXED64:
        {
            uint64_t* value = (uint64_t*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %llu", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_FLOAT:
        {
            float* value = (float*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %f", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_DOUBLE:
        {
            double* value = (double*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %f", log_indent, fieldDesc->name, value[i]);
        }
        break;

    case PROTOBUF_C_TYPE_BOOL:
        {
            protobuf_c_boolean* value = (protobuf_c_boolean*)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %s", log_indent, fieldDesc->name, BOOL_TO_STRING(value[i]));
        }
        break;

    case PROTOBUF_C_TYPE_STRING:
        {
            char** strings = (char**)fieldData;
            KineticLogger_LogPrintf(log_level, "%s%s: %s", log_indent, fieldDesc->name, strings[i]);
        }
        break;

    case PROTOBUF_C_TYPE_BYTES:
        {
            ProtobufCBinaryData* value = (ProtobufCBinaryData*)fieldData;
            log_proto_level_start(fieldDesc->name);
            KineticLogger_LogByteArray(log_level, log_indent,
                                       (ByteArray){.data = value[i].data,
                                                   .len = value[i].len});
            log_proto_level_end();
        }
        break;

    case PROTOBUF_C_TYPE_ENUM:
        {
            int * value = (int*)fieldData;
            ProtobufCEnumDescriptor const * enumDesc = fieldDesc->descriptor;
            ProtobufCEnumValue const * enumVal = protobuf_c_enum_descriptor_get_value(enumDesc, value[i]);
            KineticLogger_LogPrintf(log_level, "%s%s: %s", log_indent, fieldDesc->name, enumVal->name);
        }
        break;

    case PROTOBUF_C_TYPE_MESSAGE:  // nested message
        {
            ProtobufCMessage** msg = (ProtobufCMessage**)fieldData;
            if (msg[i] != NULL)
            {
                log_proto_level_start(fieldDesc->name);
                log_protobuf_message(log_level, msg[i], log_indent);
                log_proto_level_end();
            }
        } break;

    default:
        KineticLogger_LogPrintf(log_level, "Invalid message field type!: %d", fieldDesc->type);
        KINETIC_ASSERT(false); // should never get here!
        break;
    };