PARCLog * parcLog_Create(const char *hostName, const char *applicationName, const char *processId, PARCLogReporter *reporter) { if (applicationName == NULL) { applicationName = _nilvalue; } if (hostName == NULL) { hostName = _nilvalue; } if (processId == NULL) { processId = _nilvalue; } PARCLog *result = parcObject_CreateInstance(PARCLog); if (result == NULL) { trapOutOfMemory("Creating an instance of PARCLog."); } result->hostName = parcMemory_StringDuplicate(hostName, strlen(hostName)); result->applicationName = parcMemory_StringDuplicate(applicationName, strlen(applicationName)); result->processId = parcMemory_StringDuplicate(processId, strlen(processId)); result->messageId = 0; result->level = PARCLogLevel_Off; result->reporter = parcLogReporter_Acquire(reporter); return result; }
/** * string: "facility=level" * Set the right thing in the logger */ static void _setLogLevel(int logLevelArray[MetisLoggerFacility_END], const char *string) { char *tofree = parcMemory_StringDuplicate(string, strlen(string)); char *p = tofree; char *facilityString = strsep(&p, "="); if (facilityString) { char *levelString = p; if (strcasecmp(facilityString, "all") == 0) { for (MetisLoggerFacility facility = 0; facility < MetisLoggerFacility_END; facility++) { _setLogLevelToLevel(logLevelArray, facility, levelString); } } else { MetisLoggerFacility facility; for (facility = 0; facility < MetisLoggerFacility_END; facility++) { if (strcasecmp(facilityString, metisLogger_FacilityString(facility)) == 0) { break; } } if (facility < MetisLoggerFacility_END) { _setLogLevelToLevel(logLevelArray, facility, levelString); } else { printf("Invalid facility string %s\n", facilityString); _usage(EXIT_FAILURE); } } } parcMemory_Deallocate((void **) &tofree); }
char * ccnxControlFacade_ToString(const CCNxTlvDictionary *contentDictionary) { char *string; char *jsonString = NULL; PARCJSON *json = ccnxControlFacade_GetJson(contentDictionary); if (json != NULL) { jsonString = parcJSON_ToString(json); } int failure = asprintf(&string, "CCNxControl { isCPI=%s, isNotification=%s, JSON=\"%s\"}", ccnxControlFacade_IsCPI(contentDictionary) ? "true" : "false", ccnxControlFacade_IsNotification(contentDictionary) ? "true" : "false", jsonString != NULL ? jsonString : "NULL"); if (jsonString) { parcMemory_Deallocate((void **) &jsonString); } assertTrue(failure > -1, "Error asprintf"); char *result = parcMemory_StringDuplicate(string, strlen(string)); free(string); return result; }
/** * @abstract create link name based on file descriptor * @discussion * * @param [in] type of connection * @param [in] fd file descriptor * @return allocated name, must be released with parcMemory_Deallocate() * * Example: * @code * { * * } * @endcode */ static const char * _createNameFromLinkData(const _connectionPair *linkData) { char nameBuffer[MAXPATHLEN]; const char *protocol = "udp"; // Get our local hostname and port char myHost[NI_MAXHOST], myPort[NI_MAXSERV]; int myResult = getnameinfo((struct sockaddr *) &linkData->myAddress, linkData->myAddressLength, myHost, NI_MAXHOST, myPort, NI_MAXSERV, NI_NUMERICSERV); // Get our peer's hostname and port char peerHost[NI_MAXHOST], peerPort[NI_MAXSERV]; int peerResult = getnameinfo((struct sockaddr *) &linkData->peerAddress, linkData->peerAddressLength, peerHost, NI_MAXHOST, peerPort, NI_MAXSERV, NI_NUMERICSERV); if ((peerResult == 0) && (myResult == 0)) { // point to point connection sprintf(nameBuffer, "%s://%s:%s<->%s:%s", protocol, myHost, myPort, peerHost, peerPort); } else if (myResult == 0) { // listener only sprintf(nameBuffer, "%s://%s:%s", protocol, myHost, myPort); } else { // some unknown possibility sprintf(nameBuffer, "%s://Unknown", protocol); } return parcMemory_StringDuplicate(nameBuffer, strlen(nameBuffer)); }
static const char * _strtoupper(const char *string) { char *upperCaseString = parcMemory_StringDuplicate(string, strlen(string)); for (char *i = upperCaseString; *i; i++) { *i = toupper(*i); } return upperCaseString; }
static TestData* commonSetup() { char *hexString = "30819F300D06092A864886F70D010101050003818D0030818902818100A826C09E01FF4970428213C96312B46050514FD5F87E670A4784C75D8B23CD073B1CBEF328E538584E442A769DF77299192BCF3603F50F14C5664994250E5C24DF47B86EA5C7CA99B3584E9A63BC5993569FF3612C71AD46A088CDC7346B9BE021D4CA1764CF5434F993E6120363C551E2979BDB3F0345B4994BCED9CB260EEB0203010001"; TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); data->hexString = parcMemory_StringDuplicate(hexString, strlen(hexString)); return data; }
static const char * _createNameFromLinkData(const _TemplateLinkData *linkData) { char nameBuffer[MAXPATHLEN]; const char *protocol = "template"; sprintf(nameBuffer, "%s://%s", protocol, linkData->linkIdentity); return parcMemory_StringDuplicate(nameBuffer, strlen(nameBuffer)); }
char * parcTime_TimevalAsString(struct timeval timeval) { char *string; int nwritten = asprintf(&string, "%ld.%06ld", timeval.tv_sec, (long) timeval.tv_usec); assertTrue(nwritten >= 0, "Error calling asprintf"); char *result = parcMemory_StringDuplicate(string, strlen(string)); free(string); return result; }
PARCLogEntry * parcLogEntry_Create(PARCLogLevel level, const char *hostName, const char *applicationName, const char *processName, const uint64_t messageId, const struct timeval timeStamp, PARCBuffer *payload) { PARCLogEntry *result = parcObject_CreateInstance(PARCLogEntry); if (result == NULL) { trapOutOfMemory("Creating an instance of PARCLogEntry."); } result->version = _parcLog_Version; result->timeStamp = timeStamp; result->hostName = parcMemory_StringDuplicate(hostName, strlen(hostName)); result->applicationName = parcMemory_StringDuplicate(applicationName, strlen(applicationName)); result->processName = parcMemory_StringDuplicate(processName, strlen(processName)); result->messageId = messageId; result->level = level; result->payload = parcBuffer_Acquire(payload); return result; }
MetisConfigurationFile * metisConfigurationFile_Create(MetisForwarder *metis, const char *filename) { assertNotNull(metis, "Parameter metis must be non-null"); assertNotNull(filename, "Parameter filename must be non-null"); MetisConfigurationFile *configFile = parcObject_CreateInstance(MetisConfigurationFile); if (configFile) { configFile->linesRead = 0; configFile->metis = metis; configFile->filename = parcMemory_StringDuplicate(filename, strlen(filename)); assertNotNull(configFile->filename, "Could not copy string '%s'", filename); // setup the control state for the command parser configFile->controlState = metisControlState_Create(configFile, _writeRead); // we do not register Help commands metisControlState_RegisterCommand(configFile->controlState, metisControlRoot_Create(configFile->controlState)); // open the file and make sure we can read it configFile->fh = fopen(configFile->filename, "r"); if (configFile->fh) { if (metisLogger_IsLoggable(metisForwarder_GetLogger(metis), MetisLoggerFacility_Config, PARCLogLevel_Debug)) { metisLogger_Log(metisForwarder_GetLogger(metis), MetisLoggerFacility_Config, PARCLogLevel_Debug, __func__, "Open config file %s", configFile->filename); } } else { if (metisLogger_IsLoggable(metisForwarder_GetLogger(metis), MetisLoggerFacility_Config, PARCLogLevel_Error)) { metisLogger_Log(metisForwarder_GetLogger(metis), MetisLoggerFacility_Config, PARCLogLevel_Error, __func__, "Could not open config file %s: (%d) %s", configFile->filename, errno, strerror(errno)); } // failure cleanup the object -- this nulls it so final return null be NULL metisConfigurationFile_Release(&configFile); } } return configFile; }
static _TemplateLinkData * _TemplateLinkData_Create() { _TemplateLinkData *linkData = parcMemory_AllocateAndClear(sizeof(_TemplateLinkData)); assertNotNull(linkData, "Could not create private data for new link"); // Any identity name can be used as long as it's guaranteed to be unique. // Typically, we use some identifying attribute of the link (such as its sockaddr info) // In this case we're using the current time. CCNxTimeStamp *timeStamp = ccnxTimeStamp_CreateFromCurrentUTCTime(); char *timeString = ccnxTimeStamp_ToString(timeStamp); ccnxTimeStamp_Release(&timeStamp); linkData->linkIdentity = parcMemory_StringDuplicate(timeString, strlen(timeString)); parcMemory_Deallocate(&timeString); linkData->queue = parcDeque_Create(); return linkData; }
AthenaTransportLinkModule * athenaTransportLinkModule_Create(const char *name, AthenaTransportLinkModule_Open *openMethod, AthenaTransportLinkModule_Poll *pollMethod) { AthenaTransportLinkModule *athenaTransportLinkModule = parcMemory_AllocateAndClear(sizeof(AthenaTransportLinkModule)); assertNotNull(athenaTransportLinkModule, "parcMemory_AllocateAndClear failed to create a new AthenaTransportLinkModule"); athenaTransportLinkModule->log = _parc_logger_create(name); athenaTransportLinkModule->name = parcMemory_StringDuplicate(name, strlen(name)); assertNotNull(athenaTransportLinkModule->name, "parcMemory_AllocateAndClear failed allocate athenaTransportLinkModule name"); athenaTransportLinkModule->instanceList = parcArrayList_Create((void (*)(void **))athenaTransportLink_Release); assertNotNull(athenaTransportLinkModule->instanceList, "athenaTransportLinkModule_TCP could not allocate instance list"); athenaTransportLinkModule->openMethod = openMethod; athenaTransportLinkModule->pollMethod = pollMethod; return athenaTransportLinkModule; }
/** * @abstract create link name based on linkData * @discussion * * @param [in] linkData * @param [in] listener flag * @return allocated name, must be released with parcMemory_Deallocate() * * Example: * @code * { * * } * @endcode */ static const char * _createNameFromLinkData(const _connectionPair *linkData, bool listener) { char nameBuffer[MAXPATHLEN]; const char *protocol = "eth"; // Get our local hostname and port char myMACString[ETHER_ADDR_LEN * 3]; char peerMACString[ETHER_ADDR_LEN * 3]; _addressToString(linkData->myAddress.ether_addr_octet, myMACString); if (!listener) { _addressToString(linkData->peerAddress.ether_addr_octet, peerMACString); sprintf(nameBuffer, "%s://%s<->%s", protocol, myMACString, peerMACString); } else { sprintf(nameBuffer, "%s://%s", protocol, myMACString); } return parcMemory_StringDuplicate(nameBuffer, strlen(nameBuffer)); }
AthenaTransportLink * athenaTransportLink_Create(const char *name, AthenaTransportLink_SendMethod *sendMethod, AthenaTransportLink_ReceiveMethod *receiveMethod, AthenaTransportLink_CloseMethod *closeMethod) { AthenaTransportLink *athenaTransportLink = parcObject_CreateAndClearInstance(AthenaTransportLink); if (athenaTransportLink != NULL) { athenaTransportLink->linkName = parcMemory_StringDuplicate(name, strlen(name)); athenaTransportLink->sendMethod = sendMethod; athenaTransportLink->receiveMethod = receiveMethod; athenaTransportLink->closeMethod = closeMethod; athenaTransportLink->log = _parc_logger_create(name); athenaTransportLink->linkEvents = AthenaTransportLinkEvent_None; athenaTransportLink->linkFlags = AthenaTransportLinkFlag_None; athenaTransportLink->eventFd = -1; } return athenaTransportLink; }
char * parcBuffer_ToHexString(const PARCBuffer *buffer) { if (buffer == NULL) { return parcMemory_StringDuplicate("null", 4); } size_t length = parcBuffer_Remaining(buffer); // Hopefully length is less than (2^(sizeof(size_t)*8) / 2) char *result = parcMemory_AllocateAndClear((length * 2) + 1); assertNotNull(result, "parcMemory_AllocateAndClear(%zu) returned NULL", (length * 2) + 1); for (size_t i = 0; i < length; i++) { unsigned char byte = parcBuffer_GetAtIndex(buffer, i); result[i * 2] = _toHexDigit(byte >> 4); result[i * 2 + 1] = _toHexDigit(byte); } result[length * 2] = 0; return result; }
bool metisConfigurationFile_Process(MetisConfigurationFile *configFile) { assertNotNull(configFile, "Parameter configFile must be non-null"); // default to a "true" return value and only set to false if we encounter an error. bool success = true; #define BUFFERLEN 2048 char buffer[BUFFERLEN]; configFile->linesRead = 0; // always clear errors and fseek to start of file in case we get called multiple times. clearerr(configFile->fh); rewind(configFile->fh); while (success && fgets(buffer, BUFFERLEN, configFile->fh) != NULL) { configFile->linesRead++; char *stripedBuffer = _trim(buffer); if (strlen(stripedBuffer) > 0) { if (stripedBuffer[0] != '#') { // not empty and not a comment // _parseArgs will modify the string char *copy = parcMemory_StringDuplicate(stripedBuffer, strlen(stripedBuffer)); PARCList *args = _parseArgs(copy); MetisCommandReturn result = metisControlState_DispatchCommand(configFile->controlState, args); // we ignore EXIT from the configuration file if (result == MetisCommandReturn_Failure) { if (metisLogger_IsLoggable(metisForwarder_GetLogger(configFile->metis), MetisLoggerFacility_Config, PARCLogLevel_Error)) { metisLogger_Log(metisForwarder_GetLogger(configFile->metis), MetisLoggerFacility_Config, PARCLogLevel_Error, __func__, "Error on input file %s line %d: %s", configFile->filename, configFile->linesRead, stripedBuffer); } success = false; } parcList_Release(&args); parcMemory_Deallocate((void **) ©); } } } if (ferror(configFile->fh)) { if (metisLogger_IsLoggable(metisForwarder_GetLogger(configFile->metis), MetisLoggerFacility_Config, PARCLogLevel_Error)) { metisLogger_Log(metisForwarder_GetLogger(configFile->metis), MetisLoggerFacility_Config, PARCLogLevel_Error, __func__, "Error on input file %s line %d: (%d) %s", configFile->filename, configFile->linesRead, errno, strerror(errno)); } success = false; } return success; }
void * keyNew(char *key) { return parcMemory_StringDuplicate(key, strlen(key)); }
void * valueNew(char *value) { return parcMemory_StringDuplicate(value, strlen(value)); }
int main(int argc, const char *argv[]) { header(); uint16_t port = PORT_NUMBER; uint16_t configurationPort = 2001; bool daemon = false; int capacity = -1; const char *configFileName = NULL; char *logfile = NULL; if (argc == 2 && strcasecmp(argv[1], "-h") == 0) { _usage(EXIT_SUCCESS); } int logLevelArray[MetisLoggerFacility_END]; for (int i = 0; i < MetisLoggerFacility_END; i++) { logLevelArray[i] = -1; } for (int i = 0; i < argc; i++) { if (argv[i][0] == '-') { if (strcmp(argv[i], "--config") == 0) { configFileName = argv[i + 1]; i++; } else if (strcmp(argv[i], "--port") == 0) { port = atoi(argv[i + 1]); i++; } else if (strcmp(argv[i], "--daemon") == 0) { daemon = true; } else if (strcmp(argv[i], "--capacity") == 0 || strcmp(argv[i], "-c") == 0) { capacity = atoi(argv[i + 1]); i++; } else if (strcmp(argv[i], "--log") == 0) { _setLogLevel(logLevelArray, argv[i + 1]); i++; } else if (strcmp(argv[i], "--log-file") == 0) { if (logfile) { // error cannot repeat fprintf(stderr, "Cannot specify --log-file more than once\n"); _usage(EXIT_FAILURE); } logfile = parcMemory_StringDuplicate(argv[i + 1], strlen(argv[i + 1])); i++; } else { _usage(EXIT_FAILURE); } } } // set restrictive umask, in case we create any files umask(027); if (daemon && (logfile == NULL)) { fprintf(stderr, "Must specify a logfile when running in daemon mode\n"); _usage(EXIT_FAILURE); } if (daemon) { // inside this call, parent will EXIT_SUCCESS and child will continue _daemonize(); } MetisLogger *logger = NULL; if (logfile) { logger = _createLogfile(logfile); parcMemory_Deallocate((void **) &logfile); } else { PARCLogReporter *stdoutReporter = parcLogReporterTextStdout_Create(); logger = metisLogger_Create(stdoutReporter, parcClock_Wallclock()); parcLogReporter_Release(&stdoutReporter); } for (int i = 0; i < MetisLoggerFacility_END; i++) { if (logLevelArray[i] > -1) { metisLogger_SetLogLevel(logger, i, logLevelArray[i]); } } // this will update the clock to the tick clock MetisForwarder *metis = metisForwarder_Create(logger); MetisConfiguration *configuration = metisForwarder_GetConfiguration(metis); if (capacity > -1) { metisConfiguration_SetObjectStoreSize(configuration, capacity); } metisConfiguration_StartCLI(configuration, configurationPort); if (configFileName) { metisForwarder_SetupFromConfigFile(metis, configFileName); } else { // NULL to not setup AF_UNIX metisForwarder_SetupAllListeners(metis, port, NULL); } MetisDispatcher *dispatcher = metisForwarder_GetDispatcher(metis); metisLogger_Log(logger, MetisLoggerFacility_Core, PARCLogLevel_Alert, "daemon", "metis running port %d configuration-port %d", port, configurationPort); metisDispatcher_Run(dispatcher); metisLogger_Log(logger, MetisLoggerFacility_Core, PARCLogLevel_Alert, "daemon", "metis exiting port %d", port); metisForwarder_Destroy(&metis); sleep(2); metisLogger_Release(&logger); return 0; }