int cli_done = 0;
int TEST_CASE = -1;
+char* CERT_LOC = NULL;
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 {
} else {
- printf("wrong argument: %s\n", argv[2]);
+ CERT_LOC = argv[2];
- return -1;
+ ret = run_cli(argv[1]);
}
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);
extern int cli_done;
extern char* PREFERRED_CIPHERS;
-
+extern char* CERT_LOC;
extern int TEST_CASE;
extern SSL_CTX* ctx;
int auth;
char id[MAX_ID_LEN];
int chan_idx;
+ pthread_mutex_t lock;
+ struct SOCK_CONTEXT *next;
};
#include "socialize/core.h"
+
int make_socket_non_blocking (int sfd);
SSL_CTX *create_context();
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);
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);
#include "socialize/core.h"
+int init_all();
void sock_listen_and_serve(void* varg);
void fmt_logln(FILE *fp, char* fmt_out, ...);
+// TODO:
+// logger
#endif
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
}
+
+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
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);
}
int socialize(){
+ // TODO:
+ // fix prompt at the bottom
+
printf("start socializing!\n");
pthread_t rt;
struct SOCK_CONTEXT SOCK_CTX[MAX_CONN];
-
int make_socket_non_blocking (int sfd){
int flags, s;
}
-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;
}
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;
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;
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;
}
int calloc_chanctx(){
+
for(int i = 0; i < MAX_CONN; i++){
}
-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);
printf("write: client gone: %d\n", valwrite);
+ ;
+
return -2;
}
- return valwrite;
+ return valwrite;
}
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);
}
- SSL* sslfd = SOCK_CTX[sock_idx].ssl;
+ SSL* sslfd = sockctx->ssl;
uint8_t* rbuff_tmp = (uint8_t*)malloc(read_len * sizeof(uint8_t));
struct timespec rdeadline;
+ // TODO:
+ // simplify
+
while(valread < read_len){
clock_gettime(CLOCK_MONOTONIC_RAW, &rdeadline);
printf("time limit exceeded\n");
+
free(rbuff_tmp);
return -10;
printf("read: client gone: %d\n", valread);
+
free(rbuff_tmp);
return -2;
} 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));
int gencert(char* newcert, char* cname){
+ // TODO:
+ // gencert leak check
time_t exp_ca;
time(&exp_ca);
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;
}
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;
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");
}
- 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;
fmt_logln(LOGFP,"handle epoll add failed");
+
exit(EXIT_FAILURE);
}
-
}
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);
return;
}
- sock_communicate(chan_idx, sock_idx);
+ sock_communicate(chan_idx, cfd);
pthread_mutex_unlock(&G_MTX);
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");
hp.ctx_type = ISSOCK;
- hp.fd = SOCK_CTX[sock_idx].sockfd;
-
+ hp.fd = sockctx->sockfd;
+
ctx_read_packet(&hp);
if(hp.flag <= 0){
fmt_logln(LOGFP,"failed to read sock");
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
return;
fmt_logln(LOGFP,"not authenticate header: %s", hp.header);
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
return;
fmt_logln(LOGFP,"invalid signature");
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
free(hp.rbuff);
fmt_logln(LOGFP,"invalid id");
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
free(hp.rbuff);
fmt_logln(LOGFP, "failed to set sockctx");
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
free(hp.rbuff);
}
- 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);
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");
hp.ctx_type = ISSOCK;
- hp.fd = SOCK_CTX[sock_idx].sockfd;
+ hp.fd = sockctx->sockfd;
ctx_read_packet(&hp);
fmt_logln(LOGFP,"failed to read sock");
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
return;
fmt_logln(LOGFP,"not register header: %s", hp.header);
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
return;
fmt_logln(LOGFP,"failed to register: result: %d", result);
- free_sockctx(sock_idx, 1);
+
+
+ free_sockctx(cfd, 1);
return;
}
fmt_logln(LOGFP, "failed to send");
+
+
return;
}
- SOCK_CTX[sock_idx].chan_idx = result;
+ sockctx->chan_idx = result;
fmt_logln(LOGFP, "register success sent");
}
-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);
fmt_logln(LOGFP, "failed to communicate sock read");
+
return;
}
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;
memset(hp.wbuff, 0, MAX_BUFF);
- strncat(hp.wbuff, SOCK_CTX[sock_idx].id, idlen);
+ strncat(hp.wbuff, sockctx->id, idlen);
strcat(hp.wbuff,": ");
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){
continue;
}
+
}
+
fmt_logln(LOGFP, "sent to peer");
return;