bakaid commented on a change in pull request #674: Minificpp 1007 - ECU C2 
integration.
URL: https://github.com/apache/nifi-minifi-cpp/pull/674#discussion_r348451432
 
 

 ##########
 File path: nanofi/src/coap/c2agent.c
 ##########
 @@ -0,0 +1,481 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "coap/c2structs.h"
+#include "coap/c2agent.h"
+#include "coap/c2protocol.h"
+#include "coap/coapprotocol.h"
+
+#include "nanofi/coap_message.h"
+
+#include <time.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sys/socket.h>
+#include <sys/utsname.h>
+#include <sys/errno.h>
+
+#include "utlist.h"
+#include <uuid/uuid.h>
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define MAC
+#include <sys/sysctl.h>
+#else
+#include <sys/sysinfo.h>
+#endif
+
+uint64_t get_physical_memory_size() {
+#ifdef MAC
+    uint64_t memsize = 0;
+    size_t len = sizeof(memsize);
+    if (sysctlbyname("hw.memsize", &memsize, &len, NULL, 0) == 0) {
+        return memsize;
+    }
+#else
+    struct sysinfo sys_info;
+    if (sysinfo(&sys_info) == 0) {
+        return sys_info.totalram;
+    }
+#endif
+    return 0;
+}
+
+uint16_t get_num_vcores() {
+#ifdef MAC
+    uint16_t num_cpus = 0;
+    size_t len = sizeof(num_cpus);
+    if (sysctlbyname("hw.logicalcpu", &num_cpus, &len, NULL, 0) == 0) {
+        return num_cpus;
+    }
+    return 0;
+#else
+    long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+    if (ncpus < 0) {
+        return 0;
+    }
+    return (uint16_t)ncpus;
+#endif
+}
+
+char * get_ipaddr_from_hostname(const char * hostname) {
+    struct addrinfo hints, *infoptr;
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_UNSPEC;
+
+    int result = getaddrinfo(hostname, NULL, &hints, &infoptr);
+    if (result) {
+        printf("error getting addr info getaddrinfo: %s\n", 
gai_strerror(result));
+        return NULL;
+    }
+
+    size_t len = 45;
+    char * host = (char *)malloc((len+1) * sizeof(char));
+    struct addrinfo *p;
+    for (p = infoptr; p != NULL; p = p->ai_next) {
+        if (getnameinfo(p->ai_addr, p->ai_addrlen, host, len, NULL, 0, 
NI_NUMERICHOST) == 0) {
+            freeaddrinfo(infoptr);
+            return host;
+        }
+    }
+
+    freeaddrinfo(infoptr);
+    free(host);
+    return NULL;
+}
+
+int get_machine_architecture(char * buff) {
 
 Review comment:
   This is unsafe for two reasons:
    - we don't know the size of utsname.machine
    - we don't check nor document the requirement for the size of buff
   
   According to the documentation:
   ``` 
   The length of the arrays in a struct utsname is unspecified (see
   NOTES); the fields are terminated by a null byte ('\0').
   ```
   This means that we shouldn't just blindly strcpy the contents to our buffer.
   Two methods to make this function safe would be to:
    - make it return a char*, NULL on failure and a properly sized allocated 
buffer to which the string is copied on success
    - make it receive a buffer size as well, return an error code if it is too 
small to hold the string and copy it to the buffer otherwise (and also document 
a reasonable size for the supplied buffer).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to