Add VES stndDefined PM and subscription for O-DU.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / utils / sys_utils.c
index fc5c942..9b49995 100644 (file)
@@ -46,6 +46,8 @@ static char b64_encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                     'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                     '4', '5', '6', '7', '8', '9', '+', '/'};
 
+static char b64_decoding_table[256] = {0};
+
 static int b64_mod_table[] = {0, 2, 1};
 
 bool dir_exists(const char *path) {
@@ -66,7 +68,7 @@ void file_touch(const char *fname, const char *content) {
 
     FILE *f = fopen(fname, "w");
     if(f == 0) {
-        log_error("fopen failed");
+        log_error("fopen failed\n");
         return;
     }
 
@@ -86,12 +88,13 @@ char *file_read_content(const char *fname) {
         fseek(f, 0, SEEK_END);
         length = ftell(f);
         fseek(f, 0, SEEK_SET);
-        buffer = (char*)malloc(sizeof(char) * length);
+        buffer = (char*)malloc(sizeof(char) * (length + 1));
         if(buffer) {
             fread(buffer, 1, length, f);
         }
         fclose(f);
     }
+    buffer[length] = 0;
 
     return buffer;
 }
@@ -138,7 +141,7 @@ long int get_microseconds_since_epoch(void) {
     long int useconds;
 
     gettimeofday(&tv, 0);
-    useconds = t*1000 + tv.tv_usec; //add the microseconds to the seconds
+    useconds = t*1000000 + tv.tv_usec; //add the microseconds to the seconds
 
     return useconds;
 }
@@ -258,15 +261,15 @@ bool check_port_open(const char *host, uint16_t port) {
     }
 
     freeaddrinfo(res);
-    if(returnStatus == 0) {
-        close(simpleSocket);
+    close(simpleSocket);
+    if(returnStatus == 0) {    
         return true;
     }
     
     return false;
 }
 
-char *b64_encode(const unsigned char *data, size_t input_length) {
+char *b64_encode(const uint8_t *data, size_t input_length) {
     assert(data);
     assert(input_length);
 
@@ -299,6 +302,60 @@ char *b64_encode(const unsigned char *data, size_t input_length) {
     return encoded_data;
 }
 
+uint8_t *b64_decode(const char *data, size_t input_length, size_t *output_length) {
+    assert(data);
+    assert(input_length);
+    assert(output_length);
+
+    int i, j;
+
+    //one time compute decoding table
+    if(b64_decoding_table['A'] == 0) {
+        for(i = 0; i < 64; i++) {
+            b64_decoding_table[(unsigned char)b64_encoding_table[i]] = i;
+        }
+    }
+
+    if(input_length % 4 != 0) {
+        return 0;
+    }
+
+    *output_length = input_length / 4 * 3;
+    if(data[input_length - 1] == '=') {
+        (*output_length )--;
+    }
+    if(data[input_length - 2] == '=') {
+        (*output_length )--;
+    }
+
+    uint8_t *decoded_data = (uint8_t*)malloc(*output_length + 1);
+    if(decoded_data == 0) {
+        return 0;
+    }
+
+    for(i = 0, j = 0; i < input_length;) {
+        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+        uint32_t triple = ( sextet_a << 3 * 6 ) + ( sextet_b << 2 * 6 ) + ( sextet_c << 1 * 6 ) + ( sextet_d << 0 * 6 );
+
+        if(j < *output_length) {
+            decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
+        }
+
+        if(j < *output_length) {
+            decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
+        }
+
+        if(j < *output_length) {
+            decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
+        }
+    }
+
+    return decoded_data;
+}
+
 char *str_replace(const char *orig, const char *rep, const char *with) {
     assert(orig);
     assert(rep);
@@ -367,7 +424,7 @@ char *read_key(const char *filename) {
 
     fp = fopen(filename, "r");
     if(fp == 0) {
-        log_error("could not open file %s", filename);
+        log_error("could not open file %s\n", filename);
         return 0;
     }
 
@@ -383,7 +440,7 @@ char *read_key(const char *filename) {
             if(key_string) {
                 key_string = (char *)realloc(key_string, strlen(key_string) + read + 1);
                 if(key_string == 0) {
-                    log_error("bad allocation");
+                    log_error("bad allocation\n");
                     free(line);
                     return 0;
                 }
@@ -393,7 +450,7 @@ char *read_key(const char *filename) {
             else {
                 key_string = strdup(line);
                 if(key_string == 0) {
-                    log_error("bad allocation");
+                    log_error("bad allocation\n");
                     free(line);
                     return 0;
                 }
@@ -412,3 +469,19 @@ char *read_key(const char *filename) {
 
     return key_string;
 }
+
+void vsftp_daemon_init(void) {
+    system("/usr/sbin/vsftpd &");
+}
+
+void vsftp_daemon_deinit(void) {
+    system("killall -9 vsftpd");
+}
+
+void sftp_daemon_init(void) {
+    system("/usr/sbin/sshd -D &");
+}
+
+void sftp_daemon_deinit(void) {
+    system("killall -9 sshd");
+}