]> git.feebdaed.xyz Git - socialize.git/commitdiff
add: concurrent hashmap for socks
authorseantywork <seantywork@gmail.com>
Wed, 26 Feb 2025 01:18:28 +0000 (10:18 +0900)
committerseantywork <seantywork@gmail.com>
Wed, 26 Feb 2025 01:18:28 +0000 (10:18 +0900)
12 files changed:
cmd/cli/main.c
cmd/engine/main.c
include/socialize/cli/cli.h
include/socialize/core.h
include/socialize/ctl.h
include/socialize/sock/sock.h
include/socialize/utils.h
public/public/js/common.js
src/cli/cli.c
src/ctl.c
src/front/front.c
src/sock/sock.c

index 03afa37bc9afc712fb615bb1868996ad4ac9a003..0940f92b103798ca6e19bf7a9fc3f3ed8d675f56 100644 (file)
@@ -3,6 +3,7 @@
 
 int cli_done = 0;
 int TEST_CASE = -1;
+char* CERT_LOC = NULL;
 
 
 static void signal_handler(int sig){
@@ -15,21 +16,18 @@ static void signal_handler(int sig){
 int main(int argc, char **argv){
 
 
-    if(argc < 2){
-        printf("feed arguments\n");
-        printf("addr, or addr + number\n");
-
-        return -1;
-    }
-
 
     signal(SIGINT, signal_handler);
 
     int ret;
 
-    if(argc == 2){
+    if(argc != 3){
 
-        ret = run_cli(argv[1]);
+        printf("feed arguments\n");
+
+        printf("addr:port number | cert location\n");
+
+        return -1;
 
 
     } else {
@@ -48,9 +46,9 @@ int main(int argc, char **argv){
 
         } else {
 
-            printf("wrong argument: %s\n", argv[2]);
+            CERT_LOC = argv[2];
 
-            return -1;
+            ret = run_cli(argv[1]);
 
         }
 
index ab682b33403a92c53cd9abc1d41ca9606424ef33..3996a295de244bfd9f1f63626ee99cbc563a5c7e 100644 (file)
@@ -31,6 +31,17 @@ int main(){
 
     return -1;
   }
+
+  result = init_all();
+
+  if(result < 0){
+
+    printf("failed to init all\n");
+
+    return -1;
+  }
+
+
   pthread_create(&front_tid, NULL, (void*)front_listen_and_serve, NULL);
 
   sleepms(500);
index b8b8944466463e29f981f8b474a8446f72040803..2aac54610eb34d90efb41e7dfa943cea7ac9ec38 100644 (file)
@@ -44,7 +44,7 @@ void print_error_string(unsigned long err, const char* const label);
 extern int cli_done;
 extern char* PREFERRED_CIPHERS;
 
-
+extern char* CERT_LOC;
 extern int TEST_CASE;
 
 extern SSL_CTX* ctx;
index a26bf6683136659bb48a018b67eac7f50d82a134..a70e09c0652677dbc91182fec37cd0326a4a3f37 100644 (file)
@@ -209,6 +209,8 @@ struct SOCK_CONTEXT {
     int auth;
     char id[MAX_ID_LEN];
     int chan_idx;
+    pthread_mutex_t lock;
+    struct SOCK_CONTEXT *next;
 };
 
 
index 1696bfbd4bdac2aacd755dfd5173fce826b76973..a59d38796a5b32a4ef61935b97543389e93d1f7b 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "socialize/core.h"
 
+
 int make_socket_non_blocking (int sfd);
 
 SSL_CTX *create_context();
@@ -16,16 +17,12 @@ int extract_common_name(uint8_t* common_name, const char* cert);
 
 int idpw_verify(char* idpw, char* newid, uint8_t* newtoken);
 
-int update_chanctx_from_userinfo(char* id, char* pw);
-
-int update_chanctx_from_sockctx(int fd, char* id);
-
-
 
+int make_hash(int fd);
 
 int set_sockctx_by_fd(int fd);
 
-int get_sockctx_by_fd(int fd);
+struct SOCK_CONTEXT* get_sockctx_by_fd(int fd);
 
 int set_sockctx_id_by_fd(int fd, char* id);
 
@@ -45,16 +42,13 @@ int calloc_chanctx();
 
 int free_chanctx(int idx);
 
-int calloc_sockctx();
-
-int free_sockctx(int idx, int memfree);
+int calloc_sockctx(int fd);
 
+int free_sockctx(int fd, int memfree);
 
 
 
-int chanctx_write(int type, char* id, int write_len, uint8_t* wbuff);
 
-int chanctx_read(int type, char* id, int read_len, uint8_t* rbuff);
 
 int sockctx_write(int fd, int write_len, uint8_t* wbuff);
 
index e9519d6212035cd597d82c7baacf2469dab784e4..d136dc853587f7aa70351450e956659b59354d7f 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "socialize/core.h"
 
+int init_all();
 
 
 void sock_listen_and_serve(void* varg);
index 2f604795b985e41de156a635126dfd2abc624bc7..ce5b2bfa078ac56ef84f20c2e8234037ac69c2b2 100644 (file)
@@ -28,5 +28,7 @@ void sleepms(long ms);
 
 void fmt_logln(FILE *fp, char* fmt_out, ...);
 
+// TODO:
+//  logger
 
 #endif
index 35b3eb11c39d0f44102d5a8251841319fa60c23f..e26c5f9f2184bef1a6a186e878130dad3258c39a 100644 (file)
@@ -144,7 +144,13 @@ function signin(){
 
         if(msg.status == "success") {
 
-            alert("got data: \n" + msg.data)
+            alert("status: \n" + msg.data)
+
+            return
+
+        } else if (msg.status == "gencert"){
+
+            gencertDownload(msg.data)
 
             return
 
@@ -184,3 +190,22 @@ function sendMessage(cmd, msg){
 
 }
 
+
+function gencertDownload(data){
+
+    let filetype = "text/pem"
+    let filename = "cert.pem"
+
+    let blob = new Blob([data], { type: filetype });
+
+    let a = document.createElement('a');
+    a.download = filename;
+    a.href = URL.createObjectURL(blob);
+    a.dataset.downloadurl = [filetype, a.download, a.href].join(':');
+    a.style.display = "none";
+    document.body.appendChild(a);
+    a.click();
+    document.body.removeChild(a);
+    setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500);
+
+}
\ No newline at end of file
index d8eb30d927bd010fde7a1f44a66e6da1402fe346..ed6a0e002ee7bd4faea7d33b79a82b51855fa969 100644 (file)
@@ -333,12 +333,13 @@ int auth(){
 
         result = read_file_to_buffer(cert, MAX_PW_LEN, SUB1_CERT);
 
-    }
+    } else if(TEST_CASE == 2){
 
+        result = read_file_to_buffer(cert, MAX_PW_LEN, SUB2_CERT);
 
-    if(TEST_CASE == 2){
+    } else {
 
-        result = read_file_to_buffer(cert, MAX_PW_LEN, SUB2_CERT);
+        result = read_file_to_buffer(cert, MAX_PW_LEN, CERT_LOC);
 
     }
 
@@ -644,6 +645,9 @@ int join(){
 
 int socialize(){
 
+    // TODO:
+    //  fix prompt at the bottom
+
     printf("start socializing!\n");
 
     pthread_t rt;
index 817c6e43d333e9513d99ab12e39a4ea1842d5a8c..e8c62f0892be1f9c81e82dcca4a8f0b87dc2d7fd 100644 (file)
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -6,7 +6,6 @@ struct CHANNEL_CONTEXT CHAN_CTX[MAX_CONN];
 struct SOCK_CONTEXT SOCK_CTX[MAX_CONN];
 
 
-
 int make_socket_non_blocking (int sfd){
     int flags, s;
 
@@ -217,173 +216,81 @@ int idpw_verify(char* idpw, char *newid, uint8_t* newtoken){
 
 }
 
-int update_chanctx_from_userinfo(char* id, char* pw){
-
-    int ret_idx = -1;
-
-    int chan_idx = get_chanctx_by_id(id);
-
-    if(chan_idx < 0){
-
-
-        ret_idx = calloc_chanctx();
-
-        if(ret_idx < 0){
-
-            printf("id %s failed to calloc chanctx: max conn\n", id);
-
-            return -2;
-
-        }
-
-
-        strcpy(CHAN_CTX[ret_idx].id, id);
-        strcpy(CHAN_CTX[ret_idx].pw, pw);
-
-        chan_idx = ret_idx;
-
-        printf("id %s added to chanctx from userinfo\n", id);
-
-
-    } else {
-
-
-        strcpy(CHAN_CTX[chan_idx].pw, pw);
 
 
-        printf("id %s modified for existing chanctx from userinfo\n", id);
-
-
-    }
+int make_hash(int fd){
 
+    int hash = fd % MAX_CONN;
 
-
-    ret_idx = chan_idx;
-
-
-    return ret_idx;
-
+    return hash;
 }
 
 
-int update_chanctx_from_sockctx(int fd, char* id){
-
-
-    int ret_idx = -1;
+int set_sockctx_by_fd(int fd){
 
 
-    int sock_idx = get_sockctx_by_fd(fd);
+    int new_idx = calloc_sockctx(fd);
 
-    if(sock_idx < 0){
+    if(new_idx < 0){
 
-        printf("no sock fd associated with: %d\n",fd);
 
         return -1;
-    }
-
-    int chan_idx = get_chanctx_by_id(id);
-
-
-    if(chan_idx < 0){
-
-        ret_idx = calloc_chanctx();
-
-        if(ret_idx < 0){
-
-            printf("sock fd %d failed to calloc chanctx: max conn\n", fd);
-
-            return -2;
-
-        }
-
-        chan_idx = ret_idx;
-
-        CHAN_CTX[chan_idx].sockfd = SOCK_CTX[sock_idx].sockfd;
-        CHAN_CTX[chan_idx].ssl = SOCK_CTX[sock_idx].ssl;
-        CHAN_CTX[chan_idx].ctx = SOCK_CTX[sock_idx].ctx;
-
-        free_sockctx(sock_idx, 0);
-
-        strcpy(CHAN_CTX[chan_idx].id, id);
-
-        printf("id %s added to chanctx %d from sock\n", id, chan_idx);
-
-    } else {
-
-
-        CHAN_CTX[chan_idx].sockfd = SOCK_CTX[sock_idx].sockfd;
-        CHAN_CTX[chan_idx].ssl = SOCK_CTX[sock_idx].ssl;
-        CHAN_CTX[chan_idx].ctx = SOCK_CTX[sock_idx].ctx;
-
-        free_sockctx(sock_idx, 0);
-
-        printf("id %s modified for existing chanctx from sock\n", id);
-
 
     }
 
-    ret_idx = chan_idx;
-
-    return ret_idx;
-
+    return 0;
 }
 
 
 
 
+struct SOCK_CONTEXT* get_sockctx_by_fd(int fd){
 
+    int i = make_hash(fd);
 
-int set_sockctx_by_fd(int fd){
-
-
-    int new_idx = calloc_sockctx();
-
-    if(new_idx < 0){
-
-
-        return -1;
-
-    }
-
-    SOCK_CTX[new_idx].sockfd = fd;
-
-    return new_idx;
-}
-
+    //struct SOCK_CONTEXT* ctx = (struct SOCK_CONTEXT*)malloc(sizeof(struct SOCK_CONTEXT));
 
+    //memset(ctx, 0, sizeof(struct SOCK_CONTEXT));
 
+    struct SOCK_CONTEXT* ctx;
 
-int get_sockctx_by_fd(int fd){
+    pthread_mutex_lock(&SOCK_CTX[i].lock);
 
+    ctx = &SOCK_CTX[i];
 
-    for(int i = 0; i < MAX_CONN; i++){
+    while(ctx != NULL){
 
+        if(ctx->sockfd == fd){
 
-        if(SOCK_CTX[i].sockfd == fd){
+            pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
-            return i;
+            return ctx;
 
         }
 
+        ctx = ctx->next;
 
     }
 
+    pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
+    //free(ctx);
 
-    return -1;
+    return NULL;
 }
 
 
 int set_sockctx_id_by_fd(int fd, char* id){
 
-    int idx = get_sockctx_by_fd(fd);
+    struct SOCK_CONTEXT* ctx = get_sockctx_by_fd(fd);
 
-    if(idx < 0){
+    if(ctx == NULL){
 
         return -1;
     }
 
-    memcpy(SOCK_CTX[idx].id, id, MAX_ID_LEN);
+    memcpy(ctx->id, id, MAX_ID_LEN);
+
 
     return 0;
 }
@@ -391,15 +298,14 @@ int set_sockctx_id_by_fd(int fd, char* id){
 int get_sockctx_id_by_fd(int fd, char* id){
 
 
+    struct SOCK_CONTEXT* ctx = get_sockctx_by_fd(fd);
 
-    int idx = get_sockctx_by_fd(fd);
-
-    if(idx < 0){
+    if(ctx == NULL){
 
         return -1;
     }
 
-    memcpy(id, SOCK_CTX[idx].id, MAX_ID_LEN);
+    memcpy(id, ctx->id, MAX_ID_LEN);
 
     return 0;
 
@@ -474,14 +380,14 @@ int get_chanctx_by_id(char* id){
 int set_sockctx_chan_id_by_fd(int fd, int chan_id){
 
 
-    int idx = get_sockctx_by_fd(fd);
+    struct SOCK_CONTEXT* ctx = get_sockctx_by_fd(fd);
 
-    if(idx < 0){
+    if(ctx == NULL){
 
         return -1;
     }
 
-    SOCK_CTX[idx].chan_idx = chan_id;
+    ctx->chan_idx = chan_id;
 
     return 0;
 
@@ -493,14 +399,17 @@ int set_sockctx_chan_id_by_fd(int fd, int chan_id){
 int get_sockctx_chan_id_by_fd(int fd){
 
 
-    int idx = get_sockctx_by_fd(fd);
+    struct SOCK_CONTEXT* ctx = get_sockctx_by_fd(fd);
 
-    if(idx < 0){
+    if(ctx == NULL){
 
         return -1;
     }
 
-    return SOCK_CTX[idx].chan_idx;
+    int chan_idx = ctx->chan_idx;
+
+
+    return chan_idx;
 
 }
 
@@ -508,6 +417,7 @@ int get_sockctx_chan_id_by_fd(int fd){
 
 int calloc_chanctx(){
 
+
     for(int i = 0; i < MAX_CONN; i++){
 
 
@@ -578,229 +488,107 @@ int free_chanctx(int idx){
 }
 
 
-int calloc_sockctx(){
+int calloc_sockctx(int fd){
 
-    for(int i = 0; i < MAX_CONN; i++){
+    int i = make_hash(fd);
 
-        if(SOCK_CTX[i].allocated == 0){
+    struct SOCK_CONTEXT* ctx = NULL;
 
-            SOCK_CTX[i].ctx = NULL;
-            SOCK_CTX[i].ssl = NULL;
-            SOCK_CTX[i].sockfd = 0;
-            SOCK_CTX[i].chan_idx = -1;
+    pthread_mutex_lock(&SOCK_CTX[i].lock);
 
-            SOCK_CTX[i].allocated = 1;
+    ctx = &SOCK_CTX[i];
 
-            return i;
+    while(ctx != NULL){
 
-        }
-
-
-    }
+        if(ctx->allocated == 0){
 
+            ctx->ctx = NULL;
+            ctx->ssl = NULL;
+            ctx->sockfd = fd;
+            ctx->chan_idx = -1;
+            ctx->allocated = 1;
 
-    return -1;
-}
+            pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
-
-
-int free_sockctx(int idx, int memfree){
-
-
-    if (idx >= MAX_CONN){
-        return -10;
-    }
-
-    if(SOCK_CTX[idx].allocated != 1){
-        return -1;
-    }
-
-    if(memfree == 1){
-
-        SSL_shutdown(SOCK_CTX[idx].ssl);
-        SSL_free(SOCK_CTX[idx].ssl);
-        SSL_CTX_free(SOCK_CTX[idx].ctx);
-
-    } else {
-
-        SOCK_CTX[idx].ssl = NULL;
-        SOCK_CTX[idx].ctx = NULL;
-
-    }
-
-    SOCK_CTX[idx].sockfd = 0;
-    SOCK_CTX[idx].allocated = 0;
-
-
-    return 0;
-}
-
-
-int chanctx_write(int type, char* id, int write_len, uint8_t* wbuff){
-
-    if(type == ISSOCK){
-
-        int valwrite = 0;
-
-        int chan_idx = 0;
-
-        chan_idx = get_chanctx_by_id(id);
-
-        if(chan_idx < 0){
-
-            printf("write: no such id: %s\n", id);
-
-            return -1;
-        }
-
-
-        SSL* sslfd = CHAN_CTX[chan_idx].ssl;
-
-
-        valwrite = SSL_write(sslfd, wbuff, write_len);
-
-
-        if (valwrite <= 0){
-
-            printf("write: client gone: %d\n", valwrite);
-
-            return -2;
+            return i;
 
         }
 
-        return valwrite;
-
+        ctx = ctx->next;
 
     }
 
+    pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
-    printf("invalid chanctx write type: %d\n", type);
-
-    return -100;
-
-
+    return -1;
 }
 
-int chanctx_read(int type, char* id, int read_len, uint8_t* rbuff){
-
-    if(type == ISSOCK){
-
-        int valread = 0;
-
-        int chan_idx = 0;
-
-        int ms_until_deadline = 0;
 
-        chan_idx = get_chanctx_by_id(id);
 
-        if(chan_idx < 0){
+int free_sockctx(int fd, int memfree){
 
-            printf("read: no such id: %s\n", id);
 
-            return -1;
-        }
-
-
-        SSL* sslfd = CHAN_CTX[chan_idx].ssl;
-
-        uint8_t* rbuff_tmp = (uint8_t*)malloc(read_len * sizeof(uint8_t));
-
-        memset(rbuff_tmp, 0, read_len * sizeof(uint8_t));
-
-        int valread_tmp = 0;
-
-        struct timespec rnow;
-
-        clock_gettime(CLOCK_MONOTONIC_RAW, &rnow);
-
-        struct timespec rdeadline;
-
-        while(valread < read_len){
-
-            clock_gettime(CLOCK_MONOTONIC_RAW, &rdeadline);
-
-            ms_until_deadline = ((rdeadline.tv_sec - rnow.tv_sec) * 1000 + (rdeadline.tv_nsec - rnow.tv_nsec) / 1000000);
-
-            if(ms_until_deadline > HUB_TIMEOUT_MS){
-                
-                printf("time limit exceeded\n");
-
-                free(rbuff_tmp);
-
-                return -10;
-            }
-
-            valread_tmp = SSL_read(sslfd, (void*)rbuff_tmp, read_len);
-
-            if(valread_tmp <= 0){
-                
-                if(errno == EAGAIN){
-
-                    memset(rbuff_tmp, 0, read_len * sizeof(uint8_t));
-
-                    valread_tmp = 0;
-
-                    continue;
-                }
+    int i = make_hash(fd);
 
-                printf("read: client gone: %d\n", valread);
+    struct SOCK_CONTEXT* ctx = NULL;
 
-                free(rbuff_tmp);
+    pthread_mutex_lock(&SOCK_CTX[i].lock);
 
-                return -2;
-            }
+    ctx = &SOCK_CTX[i];
 
-            for(int i = 0 ; i < valread_tmp; i++){
+    while(ctx != NULL){
 
-                int idx = valread + i;
+        if(ctx->sockfd == fd){
 
-                rbuff[idx] = rbuff_tmp[i];
+            if(memfree == 1){
 
+                SSL_shutdown(ctx->ssl);
+                SSL_free(ctx->ssl);
+                SSL_CTX_free(ctx->ctx);
+        
+            } else {
+        
+                ctx->ssl = NULL;
+                ctx->ctx = NULL;
+        
             }
+        
+            ctx->sockfd = 0;
+            ctx->allocated = 0;
 
-            valread += valread_tmp;        
-
-            memset(rbuff_tmp, 0, read_len * sizeof(uint8_t));
-
-            valread_tmp = 0;
+            pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
+            return 0;
         }
 
-
-        free(rbuff_tmp);
-
-        return valread;
-
+        ctx = ctx->next;
 
     }
 
+    pthread_mutex_unlock(&SOCK_CTX[i].lock);
 
-    printf("invalid chanctx read type: %d\n", type);
-
-    return -100;
-
-
+    return -1;
 }
 
 
+
 int sockctx_write(int fd, int write_len, uint8_t* wbuff){
 
 
     int valwrite = 0;
 
-    int sock_idx = 0;
+    struct SOCK_CONTEXT* sockctx = NULL;
 
-    sock_idx = get_sockctx_by_fd(fd);
+    sockctx = get_sockctx_by_fd(fd);
 
-    if(sock_idx < 0){
+    if(sockctx == NULL){
 
         printf("write: no such fd: %d\n", fd);
 
         return -1;
     }
 
-    SSL* sslfd = SOCK_CTX[sock_idx].ssl;
-
+    SSL* sslfd = sockctx->ssl;
 
     valwrite = SSL_write(sslfd, (void*)wbuff, write_len);
 
@@ -808,12 +596,14 @@ int sockctx_write(int fd, int write_len, uint8_t* wbuff){
 
         printf("write: client gone: %d\n", valwrite);
 
+        ;
+
         return -2;
 
     }
 
-    return valwrite;
 
+    return valwrite;
 
 
 }
@@ -828,9 +618,9 @@ int sockctx_read(int fd, int read_len, uint8_t* rbuff){
 
     int ms_until_deadline = 0;
 
-    sock_idx = get_sockctx_by_fd(fd);
+    struct SOCK_CONTEXT* sockctx = get_sockctx_by_fd(fd);
 
-    if(sock_idx < 0){
+    if(sockctx == NULL){
 
         printf("read: no such fd: %d\n", fd);
 
@@ -838,7 +628,7 @@ int sockctx_read(int fd, int read_len, uint8_t* rbuff){
     }
 
 
-    SSL* sslfd = SOCK_CTX[sock_idx].ssl;
+    SSL* sslfd = sockctx->ssl;
 
     uint8_t* rbuff_tmp = (uint8_t*)malloc(read_len * sizeof(uint8_t));
 
@@ -852,6 +642,9 @@ int sockctx_read(int fd, int read_len, uint8_t* rbuff){
 
     struct timespec rdeadline;
 
+    // TODO:
+    //  simplify
+
     while(valread < read_len){
 
         clock_gettime(CLOCK_MONOTONIC_RAW, &rdeadline);
@@ -862,6 +655,7 @@ int sockctx_read(int fd, int read_len, uint8_t* rbuff){
             
             printf("time limit exceeded\n");
 
+
             free(rbuff_tmp);
 
             return -10;
@@ -882,6 +676,7 @@ int sockctx_read(int fd, int read_len, uint8_t* rbuff){
 
             printf("read: client gone: %d\n", valread);
 
+
             free(rbuff_tmp);
 
             return -2;
index 51cf7cd080b23cf1ac46f15fb22b73c15c09caa3..3d589b4b71906528f219433be1cfddc382bfde81 100644 (file)
@@ -560,7 +560,7 @@ void front_communicate(struct mg_connection* c, struct mg_ws_message *wm, char*
 
         } else {
 
-            cJSON_AddItemToObject(response, "status", cJSON_CreateString("success"));
+            cJSON_AddItemToObject(response, "status", cJSON_CreateString(WS_COMMAND_GENCERT));
             cJSON_AddItemToObject(response, "data", cJSON_CreateString(newcert));
             
             strcpy(ws_buff, cJSON_Print(response));
@@ -595,6 +595,8 @@ void front_communicate(struct mg_connection* c, struct mg_ws_message *wm, char*
 
 int gencert(char* newcert, char* cname){
 
+    // TODO:
+    //  gencert leak check
 
     time_t exp_ca;
     time(&exp_ca);
@@ -671,9 +673,33 @@ int gencert(char* newcert, char* cname){
         printf("Creating certificate failed...\n");
     }
 
+    BIO *x509_bio = BIO_new(BIO_s_mem());
 
-    fp = fopen("s.pem", "w");
-    PEM_write_X509(fp, x509_s);
-    fclose(fp);
+    if(!PEM_write_bio_X509(x509_bio, x509_s)){
+
+        BIO_free(x509_bio);
+
+        printf("failed to write cert data\n");
+
+        return -11;
+
+    }
+
+    unsigned char *certbuff;
+
+    int outlen = BIO_get_mem_data(x509_bio, &certbuff);
+
+    if (outlen < 1){
+        BIO_free(x509_bio);
+
+        printf("failed to get cert data\n");
+        return -12;
+    }
+
+    strncpy(newcert, certbuff, outlen);
+
+    BIO_free(x509_bio);
+
+    return 0;
 }
 
index 40158315db5711959f54d871db88663f262a34f7..5c591aa57b13651b894d8ee406a4670488c9614a 100644 (file)
@@ -14,6 +14,24 @@ char CA_PRIV[MAX_PW_LEN] = {0};
 
 char CA_PUB[MAX_PW_LEN] = {0};
 
+int init_all(){
+
+
+    for(int i = 0 ; i < MAX_CONN; i++){
+
+        pthread_mutex_init(&SOCK_CTX[i].lock, NULL);
+
+        SOCK_CTX[i].next = NULL;
+
+    }
+
+    // TODO:
+    //  init bucket
+
+    return 0;
+}
+
+
 void sock_listen_and_serve(void* varg){
 
     int result = 0;
@@ -284,11 +302,19 @@ void sock_handle_conn(){
             exit(EXIT_FAILURE);
         }
 
-        int sock_idx = set_sockctx_by_fd(infd);
+        int res = set_sockctx_by_fd(infd);
 
+        if(res < 0){
 
+            fmt_logln(LOGFP, "failed new conn sockctx");
 
-        if(sock_idx < 0){
+            exit(EXIT_FAILURE);
+
+        }
+
+        struct SOCK_CONTEXT* sockctx = get_sockctx_by_fd(infd);
+
+        if(sockctx == NULL){
 
             fmt_logln(LOGFP, "failed new conn sockctx");
 
@@ -296,10 +322,11 @@ void sock_handle_conn(){
 
         }
 
-        SOCK_CTX[sock_idx].ctx = ctx;
-        SOCK_CTX[sock_idx].ssl = ssl;
-        SOCK_CTX[sock_idx].auth = 0;
+        sockctx->ctx = ctx;
+        sockctx->ssl = ssl;
+        sockctx->auth = 0;
 
+        
 
         SOCK_EVENT.data.fd = infd;
         SOCK_EVENT.events = EPOLLIN | EPOLLET;
@@ -308,6 +335,7 @@ void sock_handle_conn(){
 
             fmt_logln(LOGFP,"handle epoll add failed");  
 
+            
 
             exit(EXIT_FAILURE);
 
@@ -318,7 +346,6 @@ void sock_handle_conn(){
         }
 
 
-
     }
 
 
@@ -332,25 +359,29 @@ void sock_handle_client(int cfd){
 
     pthread_mutex_lock(&G_MTX);
 
-    int sock_idx = get_sockctx_by_fd(cfd);
+    struct SOCK_CONTEXT* sockctx = get_sockctx_by_fd(cfd);
 
 
-    if(sock_idx < 0){
+    if(sockctx == NULL){
         
         pthread_mutex_unlock(&G_MTX);
 
         return;
     }
 
-    if(SOCK_CTX[sock_idx].auth == 0){
+
+    if(sockctx->auth == 0){
 
         sock_authenticate(cfd);
 
+        
+
         pthread_mutex_unlock(&G_MTX);
 
         return;
     }
 
+    
 
     int chan_idx = get_sockctx_chan_id_by_fd(cfd);
 
@@ -363,7 +394,7 @@ void sock_handle_client(int cfd){
         return;
     }
 
-    sock_communicate(chan_idx, sock_idx);
+    sock_communicate(chan_idx, cfd);
 
     pthread_mutex_unlock(&G_MTX);
 
@@ -383,11 +414,11 @@ void sock_authenticate(int cfd){
 
     uint8_t id[MAX_ID_LEN] = {0};
 
-    int sock_idx = get_sockctx_by_fd(cfd);
+    struct SOCK_CONTEXT* sockctx = get_sockctx_by_fd(cfd);
 
     fmt_logln(LOGFP,"not registered to sock ctx, auth"); 
 
-    if(sock_idx < 0){
+    if(sockctx == NULL){
 
         fmt_logln(LOGFP,"failed to get sock idx"); 
 
@@ -396,8 +427,8 @@ void sock_authenticate(int cfd){
 
 
     hp.ctx_type = ISSOCK;
-    hp.fd = SOCK_CTX[sock_idx].sockfd;
-    
+    hp.fd = sockctx->sockfd;
+
     ctx_read_packet(&hp);
 
     if(hp.flag <= 0){
@@ -405,7 +436,9 @@ void sock_authenticate(int cfd){
 
         fmt_logln(LOGFP,"failed to read sock"); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         return;
 
@@ -417,7 +450,9 @@ void sock_authenticate(int cfd){
 
         fmt_logln(LOGFP,"not authenticate header: %s", hp.header); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         return;
 
@@ -430,7 +465,9 @@ void sock_authenticate(int cfd){
 
         fmt_logln(LOGFP,"invalid signature"); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         free(hp.rbuff);
 
@@ -446,7 +483,9 @@ void sock_authenticate(int cfd){
 
         fmt_logln(LOGFP,"invalid id"); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         free(hp.rbuff);
 
@@ -465,7 +504,9 @@ void sock_authenticate(int cfd){
 
         fmt_logln(LOGFP, "failed to set sockctx");
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         free(hp.rbuff);
 
@@ -473,24 +514,10 @@ void sock_authenticate(int cfd){
 
     }
 
-    SOCK_CTX[sock_idx].auth = 1;
-
-    /*
-
-    int chan_idx = update_chanctx_from_sockctx(cfd, id);
-
-    if (chan_idx < 0){
-
-        fmt_logln(LOGFP, "failed to update chanctx");
-
-        free_sockctx(sock_idx, 1);
-
-        return;
-
-    }
+    sockctx->auth = 1;
 
+    
 
-    */
     uint64_t body_len = strlen("SUCCESS") + 1;
 
     memset(hp.header, 0, HUB_HEADER_BYTELEN);
@@ -542,11 +569,11 @@ void sock_register(int cfd){
 
     uint8_t id[MAX_ID_LEN] = {0};
 
-    int sock_idx = get_sockctx_by_fd(cfd);
+    struct SOCK_CONTEXT *sockctx = get_sockctx_by_fd(cfd);
 
     fmt_logln(LOGFP,"not registered to sock ctx, register"); 
 
-    if(sock_idx < 0){
+    if(sockctx == NULL){
 
         fmt_logln(LOGFP,"failed to get sock idx"); 
 
@@ -555,7 +582,7 @@ void sock_register(int cfd){
 
 
     hp.ctx_type = ISSOCK;
-    hp.fd = SOCK_CTX[sock_idx].sockfd;
+    hp.fd = sockctx->sockfd;
     
     ctx_read_packet(&hp);
 
@@ -564,7 +591,9 @@ void sock_register(int cfd){
 
         fmt_logln(LOGFP,"failed to read sock"); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         return;
 
@@ -591,7 +620,9 @@ void sock_register(int cfd){
 
         fmt_logln(LOGFP,"not register header: %s", hp.header); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         return;
 
@@ -602,7 +633,9 @@ void sock_register(int cfd){
 
         fmt_logln(LOGFP,"failed to register: result: %d", result); 
 
-        free_sockctx(sock_idx, 1);
+        
+
+        free_sockctx(cfd, 1);
 
         return;
     }
@@ -640,11 +673,13 @@ void sock_register(int cfd){
 
         fmt_logln(LOGFP, "failed to send");
 
+        
+
         return;
 
     }
 
-    SOCK_CTX[sock_idx].chan_idx = result;
+    sockctx->chan_idx = result;
 
     fmt_logln(LOGFP, "register success sent");
 
@@ -653,15 +688,27 @@ void sock_register(int cfd){
 }
 
 
-void sock_communicate(int chan_idx, int sock_idx){
+void sock_communicate(int chan_idx, int cfd){
 
     fmt_logln(LOGFP, "incoming sock communication ");
 
     struct HUB_PACKET hp;
 
+    hp.fd = -1;
+
+    struct SOCK_CONTEXT* sockctx = get_sockctx_by_fd(cfd);
+
+    if(sockctx == NULL){
+
+        fmt_logln(LOGFP, "failed to get sockctx");
+
+        return;
+
+    }
+
     hp.ctx_type = ISSOCK;
 
-    hp.fd = SOCK_CTX[sock_idx].sockfd;
+    hp.fd = sockctx->sockfd;
 
     ctx_read_packet(&hp);
 
@@ -669,6 +716,7 @@ void sock_communicate(int chan_idx, int sock_idx){
 
         fmt_logln(LOGFP, "failed to communicate sock read");
 
+    
         return;
 
     }
@@ -685,11 +733,13 @@ void sock_communicate(int chan_idx, int sock_idx){
 
     int counter = CHAN_CTX[chan_idx].fd_ptr;
 
-    int idlen = strlen(SOCK_CTX[sock_idx].id);
+    int idlen = strlen(sockctx->id);
 
     if(idlen + 2 + hp.body_len > MAX_BUFF){
 
-        fmt_logln(LOGFP, "total buf too long: id: %s", SOCK_CTX[sock_idx].id);
+        fmt_logln(LOGFP, "total buf too long: id: %s", sockctx->id);
+
+        
 
         return;
 
@@ -697,7 +747,7 @@ void sock_communicate(int chan_idx, int sock_idx){
 
     memset(hp.wbuff, 0, MAX_BUFF);
 
-    strncat(hp.wbuff, SOCK_CTX[sock_idx].id, idlen);
+    strncat(hp.wbuff, sockctx->id, idlen);
 
     strcat(hp.wbuff,": ");
 
@@ -707,20 +757,26 @@ void sock_communicate(int chan_idx, int sock_idx){
 
     free(hp.rbuff);
 
+    // TODO:
+    //  invalid if client gone
+    //  use reallocate
+
     for(int i = 0; i < counter; i++){
 
-        hp.fd = CHAN_CTX[chan_idx].fds[i];
+        int peerfd = CHAN_CTX[chan_idx].fds[i];
 
-        int peersock_idx = get_sockctx_by_fd(hp.fd);
+        struct SOCK_CONTEXT* peerctx = get_sockctx_by_fd(peerfd);
 
-        if(peersock_idx < 0){
+        if(peerctx == NULL){
 
-            fmt_logln(LOGFP, "failed to send to peer: no idx: %d", peersock_idx);
+            fmt_logln(LOGFP, "failed to send to peer: no peer for: %d", peerfd);
 
             continue;
 
         }
 
+        hp.fd = peerfd;
+
         ctx_write_packet(&hp);
 
         if(hp.flag <= 0){
@@ -729,8 +785,10 @@ void sock_communicate(int chan_idx, int sock_idx){
 
             continue;
         } 
+
     }
 
+    
     fmt_logln(LOGFP, "sent to peer");
 
     return;