fixes due to the new naming;

v0.5.xx
Alexander Vdolainen 9 years ago
parent a61f1bbd83
commit 0dbff0dca7

@ -60,7 +60,6 @@ int __resolvehost(const char *hostname, char *buf, int buf_len,
struct hostent *hp = *rhp = NULL; struct hostent *hp = *rhp = NULL;
int herr = 0, hres = 0; int herr = 0, hres = 0;
hostbuf = malloc(sizeof(struct hostent)); hostbuf = malloc(sizeof(struct hostent));
if(!hostbuf) return NO_ADDRESS; if(!hostbuf) return NO_ADDRESS;
hres = gethostbyname_r(hostname, hostbuf, hres = gethostbyname_r(hostname, hostbuf,
@ -91,7 +90,7 @@ static int __conn_read(sxlink_t *co, void *buf, size_t buf_len)
do { do {
__try_again: __try_again:
if(co->flags & SNSX_CLOSED) { if(co->flags & SXMP_CLOSED) {
return -1; return -1;
} }
r = SSL_read(co->ssl, buf, (int)buf_len); r = SSL_read(co->ssl, buf, (int)buf_len);
@ -127,7 +126,7 @@ static int __conn_read(sxlink_t *co, void *buf, size_t buf_len)
default: default:
__close_conn: __close_conn:
ERR_free_strings(); ERR_free_strings();
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
fprintf(stderr, "[sxmplv2] (RD)Unknown error on %s (errno = %d)\n", co->uuid, errno); fprintf(stderr, "[sxmplv2] (RD)Unknown error on %s (errno = %d)\n", co->uuid, errno);
ERR_remove_state(0); ERR_remove_state(0);
return -1; return -1;
@ -175,7 +174,7 @@ static int __conn_write(sxlink_t *co, void *buf, size_t buf_len)
fd_set writeset; fd_set writeset;
__retry: __retry:
if(co->flags & SNSX_CLOSED) { if(co->flags & SXMP_CLOSED) {
return -1; return -1;
} }
r = SSL_write(co->ssl, buf, (int)buf_len); r = SSL_write(co->ssl, buf, (int)buf_len);
@ -197,7 +196,7 @@ static int __conn_write(sxlink_t *co, void *buf, size_t buf_len)
if(r < 0) { if(r < 0) {
/* set closed flag */ /* set closed flag */
ERR_free_strings(); ERR_free_strings();
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
fprintf(stderr, "[sxmplv2] (WR)Unknown error on %s (%d)\n", co->uuid, r); fprintf(stderr, "[sxmplv2] (WR)Unknown error on %s (%d)\n", co->uuid, r);
ERR_remove_state(0); ERR_remove_state(0);
return -1; return -1;
@ -214,11 +213,11 @@ int _sxmpl_writemsg(sxlink_t *co, sxmsg_t *msg)
int r; int r;
char *buf = NULL; char *buf = NULL;
if(!co || !msg) return SNE_FAILED; if(!co || !msg) return SXE_FAILED;
/* check message for validity */ /* check message for validity */
head = &msg->mhead; head = &msg->mhead;
if(head->payload_length && !msg->payload) return SNE_FAILED; if(head->payload_length && !msg->payload) return SXE_FAILED;
if(head->payload_length) { if(head->payload_length) {
buf = malloc(sizeof(sxmplv2_head_t) + head->payload_length); buf = malloc(sizeof(sxmplv2_head_t) + head->payload_length);
@ -232,12 +231,12 @@ int _sxmpl_writemsg(sxlink_t *co, sxmsg_t *msg)
rd = __conn_write(co, head, sizeof(sxmplv2_head_t)); rd = __conn_write(co, head, sizeof(sxmplv2_head_t));
else rd = __conn_write(co, buf, sizeof(sxmplv2_head_t) + head->payload_length); else rd = __conn_write(co, buf, sizeof(sxmplv2_head_t) + head->payload_length);
if(rd < 0) { if(rd < 0) {
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
r = SNE_ESSL; r = SXE_ESSL;
} }
pthread_mutex_unlock(&co->sslinout[1]); pthread_mutex_unlock(&co->sslinout[1]);
if(!(co->flags & SNSX_CLOSED)) r = SNE_SUCCESS; if(!(co->flags & SXMP_CLOSED)) r = SXE_SUCCESS;
if(buf) free(buf); if(buf) free(buf);
return r; return r;
} }
@ -456,12 +455,12 @@ static int __link_second_alloc(sxlink_t *co)
/* init list */ /* init list */
list_init_head(&co->write_pending); list_init_head(&co->write_pending);
return SNE_SUCCESS; return SXE_SUCCESS;
__fail: __fail:
idx_allocator_destroy(&co->idx_msg); idx_allocator_destroy(&co->idx_msg);
idx_allocator_destroy(&co->idx_ch); idx_allocator_destroy(&co->idx_ch);
return SNE_ENOMEM; return SXE_ENOMEM;
} }
static void __link_second_free(sxlink_t *co) static void __link_second_free(sxlink_t *co)
@ -503,12 +502,12 @@ static int __eval_syssexp(sxlink_t *co, sexp_t *sx)
if(sx->ty == SEXP_LIST) if(sx->ty == SEXP_LIST)
rpcf = sx->list->val; rpcf = sx->list->val;
else return SNE_BADPROTO; else return SXE_BADPROTO;
/* find an appropriate function */ /* find an appropriate function */
node = usrtc_lookup(rpc_list->rpc_tree, rpcf); node = usrtc_lookup(rpc_list->rpc_tree, rpcf);
if(!node) return SNE_ENORPC; if(!node) return SXE_ENORPC;
else rentry = (cx_rpc_t *)usrtc_node_getdata(node); else rentry = (cx_rpc_t *)usrtc_node_getdata(node);
/* call it */ /* call it */
@ -565,7 +564,7 @@ static void __link_destroy(sxlink_t *co)
pthread_mutex_destroy(&omsg->wait); pthread_mutex_destroy(&omsg->wait);
free(omsg); free(omsg);
} else { /* wake up */ } else { /* wake up */
omsg->mhead.opcode = SNE_LINKERROR; omsg->mhead.opcode = SXE_LINKERROR;
pthread_mutex_unlock(&omsg->wait); pthread_mutex_unlock(&omsg->wait);
} }
free(ppm); free(ppm);
@ -590,7 +589,7 @@ static void __link_destroy(sxlink_t *co)
msg = co->messages[i]; msg = co->messages[i];
if(!msg) continue; if(!msg) continue;
else head = &msg->mhead; else head = &msg->mhead;
head->opcode = SNE_LINKERROR; head->opcode = SXE_LINKERROR;
pthread_mutex_unlock(&msg->wait); pthread_mutex_unlock(&msg->wait);
pthread_mutex_destroy(&msg->wait); pthread_mutex_destroy(&msg->wait);
free(msg); free(msg);
@ -659,8 +658,8 @@ static void *__sxmpl_thread(void *b)
__wait_alive: __wait_alive:
/* flag test - FIXME: make it atomic (it will works atomically on x86, btw on others not) */ /* flag test - FIXME: make it atomic (it will works atomically on x86, btw on others not) */
if(!(co->flags & SNSX_ALIVE)) { if(!(co->flags & SXMP_ALIVE)) {
if(co->flags & SNSX_CLOSED) goto __finish; if(co->flags & SXMP_CLOSED) goto __finish;
else { else {
usleep(20); usleep(20);
goto __wait_alive; goto __wait_alive;
@ -680,14 +679,14 @@ static void *__sxmpl_thread(void *b)
*/ */
while(1) { while(1) {
__again: __again:
if(co->flags & SNSX_CLOSED) goto __finish; /* go away if required asap */ if(co->flags & SXMP_CLOSED) goto __finish; /* go away if required asap */
/* works with pending messages */ /* works with pending messages */
if(co->pending_messages && !(co->flags & SNSX_CLOSED)) { if(co->pending_messages && !(co->flags & SXMP_CLOSED)) {
pthread_mutex_lock(&co->write_pending_lock); pthread_mutex_lock(&co->write_pending_lock);
list_for_each_safe(&co->write_pending, iter, siter) { list_for_each_safe(&co->write_pending, iter, siter) {
ppm = container_of(iter, ppmsg_t, node); ppm = container_of(iter, ppmsg_t, node);
omsg = ppm->msg; omsg = ppm->msg;
if(_sxmpl_writemsg(co, omsg) != SNE_SUCCESS) { if(_sxmpl_writemsg(co, omsg) != SXE_SUCCESS) {
pthread_mutex_unlock(&co->write_pending_lock); pthread_mutex_unlock(&co->write_pending_lock);
goto __finish; /* write failed - finishing ... */ goto __finish; /* write failed - finishing ... */
} }
@ -711,7 +710,7 @@ static void *__sxmpl_thread(void *b)
e = pthread_mutex_timedlock(&(co->sslinout[0]), &wtick); e = pthread_mutex_timedlock(&(co->sslinout[0]), &wtick);
if(e == ETIMEDOUT) goto __again; if(e == ETIMEDOUT) goto __again;
} }
if(co->flags & SNSX_CLOSED) { if(co->flags & SXMP_CLOSED) {
pthread_mutex_unlock(&(co->sslinout[0])); pthread_mutex_unlock(&(co->sslinout[0]));
goto __finish; goto __finish;
} }
@ -728,7 +727,7 @@ static void *__sxmpl_thread(void *b)
} else printf("connread(head) µS: %ld\n", end.tv_usec - beg.tv_usec); } else printf("connread(head) µS: %ld\n", end.tv_usec - beg.tv_usec);
#endif #endif
if(co->flags & SNSX_CLOSED) { if(co->flags & SXMP_CLOSED) {
pthread_mutex_unlock(&(co->sslinout[0])); pthread_mutex_unlock(&(co->sslinout[0]));
goto __finish; /* go away if required asap */ goto __finish; /* go away if required asap */
} }
@ -738,7 +737,7 @@ static void *__sxmpl_thread(void *b)
#endif #endif
if(rd < 0) { if(rd < 0) {
__sslproto_error: __sslproto_error:
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
pthread_mutex_unlock(&(co->sslinout[0])); pthread_mutex_unlock(&(co->sslinout[0]));
goto __finish; goto __finish;
} else { } else {
@ -763,7 +762,7 @@ static void *__sxmpl_thread(void *b)
mid = mhead->msgid; mid = mhead->msgid;
/* if we're need to do something */ /* if we're need to do something */
if(mhead->msgid >= 1024) { if(mhead->msgid >= 1024) {
mhead->opcode = SNE_INVALINDEX; mhead->opcode = SXE_INVALINDEX;
goto __return_error; goto __return_error;
} else { } else {
// pthread_mutex_lock(&co->idx_msg_lock); // pthread_mutex_lock(&co->idx_msg_lock);
@ -771,11 +770,11 @@ static void *__sxmpl_thread(void *b)
//thread_mutex_unlock(&co->idx_msg_lock); //thread_mutex_unlock(&co->idx_msg_lock);
} }
if(!msg) { if(!msg) {
if(mhead->attr & SXMSG_OPEN) mhead->opcode = SNE_BADPROTO; if(mhead->attr & SXMSG_OPEN) mhead->opcode = SXE_BADPROTO;
else { else {
if((mhead->attr & SXMSG_PROTO) || (mhead->attr & SXMSG_LINK)) if((mhead->attr & SXMSG_PROTO) || (mhead->attr & SXMSG_LINK))
mhead->opcode = SNE_BADPROTO; mhead->opcode = SXE_BADPROTO;
else mhead->opcode = SNE_NOSUCHMSG; else mhead->opcode = SXE_NOSUCHMSG;
} }
} }
__return_error: __return_error:
@ -824,7 +823,7 @@ static void *__sxmpl_thread(void *b)
if(mhead->attr & SXMSG_CLOSED) goto __finish; /* close the link */ if(mhead->attr & SXMSG_CLOSED) goto __finish; /* close the link */
if(mhead->attr & SXMSG_PULSE) { /* it's a link pulse messages */ if(mhead->attr & SXMSG_PULSE) { /* it's a link pulse messages */
/* TODO: syncronization and so on */ /* TODO: syncronization and so on */
if(mhead->opcode == SNE_RAPIDMSG) { /* custom pulse */ if(mhead->opcode == SXE_RAPIDMSG) { /* custom pulse */
sx = parse_sexp(bbuf, mhead->payload_length); sx = parse_sexp(bbuf, mhead->payload_length);
if(sx && co->ssys->on_pulse) co->ssys->on_pulse(co, sx); if(sx && co->ssys->on_pulse) co->ssys->on_pulse(co, sx);
if(sx) destroy_sexp(sx); if(sx) destroy_sexp(sx);
@ -834,7 +833,7 @@ static void *__sxmpl_thread(void *b)
if((mhead->attr & SXMSG_OPEN) && (mhead->attr & SXMSG_REPLYREQ)) { /* dialog initiation */ if((mhead->attr & SXMSG_OPEN) && (mhead->attr & SXMSG_REPLYREQ)) { /* dialog initiation */
channel = co->channels[mhead->reserve]; channel = co->channels[mhead->reserve];
if(!channel) { /* ok, we'are failed */ if(!channel) { /* ok, we'are failed */
mhead->opcode = SNE_NOSUCHCHAN; mhead->opcode = SXE_NOSUCHCHAN;
__ret_regerr: __ret_regerr:
mhead->payload_length = 0; mhead->payload_length = 0;
mhead->attr &= ~SXMSG_REPLYREQ; mhead->attr &= ~SXMSG_REPLYREQ;
@ -849,11 +848,11 @@ static void *__sxmpl_thread(void *b)
/* if message is busy - fails */ /* if message is busy - fails */
mid = mhead->msgid; mid = mhead->msgid;
msg = co->messages[mid]; msg = co->messages[mid];
if(msg) { mhead->opcode = SNE_EBUSY; goto __ret_regerr; } if(msg) { mhead->opcode = SXE_EBUSY; goto __ret_regerr; }
/* now we will take a deal */ /* now we will take a deal */
if(!(msg = malloc(sizeof(sxmsg_t)))) { if(!(msg = malloc(sizeof(sxmsg_t)))) {
mhead->opcode = SNE_ENOMEM; goto __ret_regerr; mhead->opcode = SXE_ENOMEM; goto __ret_regerr;
} else { } else {
/* set mutex and channel */ /* set mutex and channel */
pthread_mutex_init(&msg->wait, NULL); pthread_mutex_init(&msg->wait, NULL);
@ -896,7 +895,7 @@ static void *__sxmpl_thread(void *b)
if(mhead->payload_length) { if(mhead->payload_length) {
msg->payload = malloc(mhead->payload_length); msg->payload = malloc(mhead->payload_length);
if(msg->payload) memcpy(msg->payload, bbuf, mhead->payload_length); if(msg->payload) memcpy(msg->payload, bbuf, mhead->payload_length);
else msg->mhead.opcode = SNE_ENOMEM; else msg->mhead.opcode = SXE_ENOMEM;
} }
pthread_mutex_unlock(&msg->wait); /* wake up thread waiting for */ pthread_mutex_unlock(&msg->wait); /* wake up thread waiting for */
@ -920,7 +919,7 @@ static void *__sxmpl_thread(void *b)
free(msg); free(msg);
/* we must reply */ /* we must reply */
mhead->opcode = SNE_ETIMEDOUT; mhead->opcode = SXE_ETIMEDOUT;
goto __ret_regerr; goto __ret_regerr;
} else { } else {
memcpy(&msg->mhead, mhead, sizeof(sxmplv2_head_t)); memcpy(&msg->mhead, mhead, sizeof(sxmplv2_head_t));
@ -928,7 +927,7 @@ static void *__sxmpl_thread(void *b)
msg->payload = malloc(mhead->payload_length); msg->payload = malloc(mhead->payload_length);
if(msg->payload) memcpy(msg->payload, bbuf, mhead->payload_length); if(msg->payload) memcpy(msg->payload, bbuf, mhead->payload_length);
else { else {
mhead->opcode = msg->mhead.opcode = SNE_ENOMEM; /* we will return it to waitee */ mhead->opcode = msg->mhead.opcode = SXE_ENOMEM; /* we will return it to waitee */
msg->mhead.attr &= ~SXMSG_REPLYREQ; /* doesn't need to reply */ msg->mhead.attr &= ~SXMSG_REPLYREQ; /* doesn't need to reply */
/* reply here now */ /* reply here now */
mhead->payload_length = 0; mhead->payload_length = 0;
@ -943,14 +942,14 @@ static void *__sxmpl_thread(void *b)
} }
pthread_mutex_unlock(&msg->wait); /* wake up thread waiting for */ pthread_mutex_unlock(&msg->wait); /* wake up thread waiting for */
} }
} else { mhead->opcode = SNE_BADPROTO; goto __ret_regerr; } } else { mhead->opcode = SXE_BADPROTO; goto __ret_regerr; }
} }
} }
} }
__finish: __finish:
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
__link_destroy(co); __link_destroy(co);
__sxmpl_bundle_destroy(b); /* destroy bundle */ __sxmpl_bundle_destroy(b); /* destroy bundle */
return NULL; return NULL;
@ -966,10 +965,10 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
sxmplv2_bundle_t *bundle; sxmplv2_bundle_t *bundle;
sexp_t *sx; sexp_t *sx;
size_t rd; size_t rd;
int r = SNE_FAILED, i; int r = SXE_FAILED, i;
if(!co) { if(!co) {
errno = SNE_ENOMEM; errno = SXE_ENOMEM;
return NULL; return NULL;
} }
@ -980,7 +979,7 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
if(!ssys->ctx) { if(!ssys->ctx) {
/* init SSL certificates and context */ /* init SSL certificates and context */
ssys->ctx = SSL_CTX_new(TLSv1_2_server_method()); ssys->ctx = SSL_CTX_new(TLSv1_2_server_method());
if(!ssys->ctx) { r = SNE_ENOMEM; goto __fail; } if(!ssys->ctx) { r = SXE_ENOMEM; goto __fail; }
else { else {
/* set verify context */ /* set verify context */
SSL_CTX_set_verify(ssys->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, SSL_CTX_set_verify(ssys->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
@ -998,18 +997,18 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
/* set the local certificate from CertFile */ /* set the local certificate from CertFile */
if(SSL_CTX_use_certificate_file(ssys->ctx, ssys->certpem, if(SSL_CTX_use_certificate_file(ssys->ctx, ssys->certpem,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* set the private key from KeyFile (may be the same as CertFile) */ /* set the private key from KeyFile (may be the same as CertFile) */
if(SSL_CTX_use_PrivateKey_file(ssys->ctx, ssys->certkey, if(SSL_CTX_use_PrivateKey_file(ssys->ctx, ssys->certkey,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ssys->ctx)) { if (!SSL_CTX_check_private_key(ssys->ctx)) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
@ -1017,23 +1016,23 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
/* now we will create an SSL connection */ /* now we will create an SSL connection */
co->ssl = SSL_new(ssys->ctx); co->ssl = SSL_new(ssys->ctx);
if(!co->ssl) { r = SNE_ENOMEM; goto __fail; } if(!co->ssl) { r = SXE_ENOMEM; goto __fail; }
else SSL_set_fd(co->ssl, sck); /* attach connected socket */ else SSL_set_fd(co->ssl, sck); /* attach connected socket */
/* set the context to verify ssl connection */ /* set the context to verify ssl connection */
SSL_set_ex_data(co->ssl, ex_ssldata_index, (void *)co); SSL_set_ex_data(co->ssl, ex_ssldata_index, (void *)co);
SSL_set_accept_state(co->ssl); SSL_set_accept_state(co->ssl);
if(SSL_accept(co->ssl) == -1) { r = SNE_EPERM; goto __fail; } /* leak here ? */ if(SSL_accept(co->ssl) == -1) { r = SXE_EPERM; goto __fail; } /* leak here ? */
// SSL_do_handshake(co->ssl); // SSL_do_handshake(co->ssl);
/* ok, now we are able to allocate and so on */ /* ok, now we are able to allocate and so on */
/* set connection to the batch mode */ /* set connection to the batch mode */
co->flags |= SNSX_BATCHMODE; co->flags |= SXMP_BATCHMODE;
/* allocate our first buffer */ /* allocate our first buffer */
buf = mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); buf = mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(buf == MAP_FAILED) { r = SNE_ENOMEM; goto __fail2; } if(buf == MAP_FAILED) { r = SXE_ENOMEM; goto __fail2; }
/* allocate first message */ /* allocate first message */
if(!(msg = malloc(sizeof(sxmsg_t)))) { r = SNE_ENOMEM; goto __fail2; } if(!(msg = malloc(sizeof(sxmsg_t)))) { r = SXE_ENOMEM; goto __fail2; }
else { else {
memset(msg, 0, sizeof(sxmsg_t)); memset(msg, 0, sizeof(sxmsg_t));
co->messages[0] = msg; co->messages[0] = msg;
@ -1041,17 +1040,17 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
bbuf = (char *)buf; bbuf = (char *)buf;
bbuf += sizeof(sxmplv2_head_t); bbuf += sizeof(sxmplv2_head_t);
while(co->flags & SNSX_BATCHMODE) { while(co->flags & SXMP_BATCHMODE) {
rd = __conn_read(co, buf, sizeof(sxmplv2_head_t)); rd = __conn_read(co, buf, sizeof(sxmplv2_head_t));
if(rd == sizeof(sxmplv2_head_t)) { if(rd == sizeof(sxmplv2_head_t)) {
head = (sxmplv2_head_t *)buf; head = (sxmplv2_head_t *)buf;
/* check for returns */ /* check for returns */
if(head->opcode != SNE_SUCCESS) { r = head->opcode; goto __fail3; } if(head->opcode != SXE_SUCCESS) { r = head->opcode; goto __fail3; }
else { /* opcode is fine */ else { /* opcode is fine */
/* if we're ready for messaging mode, turn off batch mode */ /* if we're ready for messaging mode, turn off batch mode */
if(co->flags & SNSX_MESSAGINGMODE) { if(co->flags & SXMP_MESSAGINGMODE) {
co->flags &= ~SNSX_BATCHMODE; co->flags &= ~SXMP_BATCHMODE;
break; break;
} }
} }
@ -1059,8 +1058,8 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
if(!head->payload_length) continue; /* pass the following check up */ if(!head->payload_length) continue; /* pass the following check up */
rd = __conn_read(co, bbuf, head->payload_length); rd = __conn_read(co, bbuf, head->payload_length);
if(rd == -1) { r = SNE_LINKERROR; goto __fail3; } if(rd == -1) { r = SXE_LINKERROR; goto __fail3; }
if(rd != head->payload_length) { r = SNE_LINKERROR; goto __fail3; } if(rd != head->payload_length) { r = SXE_LINKERROR; goto __fail3; }
bbuf[rd] = '\0'; bbuf[rd] = '\0';
sx = parse_sexp(bbuf, rd); sx = parse_sexp(bbuf, rd);
if(!sx) goto __fail3; if(!sx) goto __fail3;
@ -1072,33 +1071,33 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
r = __eval_syssexp(co, sx); r = __eval_syssexp(co, sx);
memcpy(head, &msg->mhead, sizeof(sxmplv2_head_t)); memcpy(head, &msg->mhead, sizeof(sxmplv2_head_t));
head->opcode = r; head->opcode = r;
if(r != SNE_SUCCESS) { /* we finish */ if(r != SXE_SUCCESS) { /* we finish */
head->payload_length = 0; head->payload_length = 0;
__conn_write(co, head, sizeof(sxmplv2_head_t)); __conn_write(co, head, sizeof(sxmplv2_head_t));
destroy_sexp(sx); destroy_sexp(sx);
goto __fail3; goto __fail3;
} }
rd = __conn_write(co, buf, sizeof(sxmplv2_head_t) + msg->mhead.payload_length); rd = __conn_write(co, buf, sizeof(sxmplv2_head_t) + msg->mhead.payload_length);
if(rd == -1) { r = SNE_LINKERROR; goto __fail3; } if(rd == -1) { r = SXE_LINKERROR; goto __fail3; }
if(rd != sizeof(sxmplv2_head_t) + msg->mhead.payload_length) { if(rd != sizeof(sxmplv2_head_t) + msg->mhead.payload_length) {
destroy_sexp(sx); destroy_sexp(sx);
goto __fail3; goto __fail3;
} }
destroy_sexp(sx); destroy_sexp(sx);
} else { r = SNE_LINKERROR; goto __fail3; } } else { r = SXE_LINKERROR; goto __fail3; }
} }
/* if we're there - negotiation is done, going to init messaging mode */ /* if we're there - negotiation is done, going to init messaging mode */
r = __link_second_alloc(co); r = __link_second_alloc(co);
if(r != SNE_SUCCESS) goto __fail3; if(r != SXE_SUCCESS) goto __fail3;
/* free message */ /* free message */
co->messages[0] = NULL; co->messages[0] = NULL;
free(msg); free(msg);
/* and now we're need to create a thread poll */ /* and now we're need to create a thread poll */
if(!(bundle = malloc(sizeof(sxmplv2_bundle_t)))) { r = SNE_ENOMEM; goto __fail4; } if(!(bundle = malloc(sizeof(sxmplv2_bundle_t)))) { r = SXE_ENOMEM; goto __fail4; }
else { else {
bundle->buf = buf; bundle->buf = buf;
bundle->conn = co; bundle->conn = co;
@ -1116,9 +1115,9 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
} }
/* all is done, connection now ready */ /* all is done, connection now ready */
co->flags |= SNSX_ALIVE; co->flags |= SXMP_ALIVE;
r = SNE_SUCCESS; r = SXE_SUCCESS;
errno = r; errno = r;
/* free context for this thread */ /* free context for this thread */
@ -1127,7 +1126,7 @@ sxlink_t *sxlink_master_accept(conn_sys_t *ssys, int sck, struct in_addr *addr)
return co; return co;
__fail5: __fail5:
r = SNE_ENOMEM; r = SXE_ENOMEM;
/* bundles will be freed by the threads when SSL_read will fails. */ /* bundles will be freed by the threads when SSL_read will fails. */
__fail4: __fail4:
__link_second_free(co); __link_second_free(co);
@ -1161,7 +1160,7 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
sxlink_t *co = __link_minimal_alloc(NULL); sxlink_t *co = __link_minimal_alloc(NULL);
struct hostent *host_; struct hostent *host_;
struct sockaddr_in addr; struct sockaddr_in addr;
int r = SNE_SUCCESS, sck; int r = SXE_SUCCESS, sck;
#ifdef WIN32 #ifdef WIN32
WSADATA wsaData; WSADATA wsaData;
#endif #endif
@ -1174,9 +1173,9 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
size_t rd, wr; size_t rd, wr;
int i; int i;
r = SNE_IGNORED; r = SXE_IGNORED;
if(!host || !SSL_cert) goto __fail; if(!host || !SSL_cert) goto __fail;
if(!co) { r = SNE_ENOMEM; goto __fail; } if(!co) { r = SXE_ENOMEM; goto __fail; }
#ifdef WIN32 #ifdef WIN32
WSAStartup(MAKEWORD(2, 2), &wsaData); WSAStartup(MAKEWORD(2, 2), &wsaData);
@ -1189,7 +1188,7 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
if(!ssys->ctx) { if(!ssys->ctx) {
/* init SSL certificates and context */ /* init SSL certificates and context */
ssys->ctx = SSL_CTX_new(TLSv1_2_client_method()); ssys->ctx = SSL_CTX_new(TLSv1_2_client_method());
if(!ssys->ctx) { r = SNE_ENOMEM; goto __fail; } if(!ssys->ctx) { r = SXE_ENOMEM; goto __fail; }
else { else {
/* set verify context */ /* set verify context */
SSL_CTX_set_verify(ssys->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, SSL_CTX_set_verify(ssys->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
@ -1203,36 +1202,36 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
/* set the local certificate from CertFile */ /* set the local certificate from CertFile */
if(SSL_CTX_use_certificate_file(ssys->ctx, SSL_cert, if(SSL_CTX_use_certificate_file(ssys->ctx, SSL_cert,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* set the private key from KeyFile (may be the same as CertFile) */ /* set the private key from KeyFile (may be the same as CertFile) */
if(SSL_CTX_use_PrivateKey_file(ssys->ctx, SSL_cert, if(SSL_CTX_use_PrivateKey_file(ssys->ctx, SSL_cert,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ssys->ctx)) { if (!SSL_CTX_check_private_key(ssys->ctx)) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
} else { } else {
/* set the local certificate from CertFile */ /* set the local certificate from CertFile */
if(SSL_CTX_use_certificate_file(ssys->ctx, SSL_cert, if(SSL_CTX_use_certificate_file(ssys->ctx, SSL_cert,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* set the private key from KeyFile (may be the same as CertFile) */ /* set the private key from KeyFile (may be the same as CertFile) */
if(SSL_CTX_use_PrivateKey_file(ssys->ctx, SSL_cert, if(SSL_CTX_use_PrivateKey_file(ssys->ctx, SSL_cert,
SSL_FILETYPE_PEM)<=0) { SSL_FILETYPE_PEM)<=0) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ssys->ctx)) { if (!SSL_CTX_check_private_key(ssys->ctx)) {
r = SNE_ESSL; r = SXE_ESSL;
goto __fail; goto __fail;
} }
} }
@ -1241,13 +1240,13 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
#ifdef WIN32 #ifdef WIN32
host_ = gethostbyname(host); host_ = gethostbyname(host);
if(!host_) { if(!host_) {
r = SNE_FAILED; r = SXE_FAILED;
goto __fail; goto __fail;
} }
#else #else
r = __resolvehost(host, hostbuf, 2048, &host_); r = __resolvehost(host, hostbuf, 2048, &host_);
if(r) { if(r) {
r = SNE_FAILED; r = SXE_FAILED;
goto __fail; goto __fail;
} }
#endif #endif
@ -1263,7 +1262,7 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
r = connect(sck, (struct sockaddr*)&addr, sizeof(addr)); r = connect(sck, (struct sockaddr*)&addr, sizeof(addr));
if(r) { if(r) {
close(sck); close(sck);
r = SNE_FAILED; /* couldn't connect to the desired host */ r = SXE_FAILED; /* couldn't connect to the desired host */
goto __fail; goto __fail;
} }
@ -1272,19 +1271,19 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
SSL_set_fd(co->ssl, sck); /* attach connected socket */ SSL_set_fd(co->ssl, sck); /* attach connected socket */
SSL_set_connect_state(co->ssl); SSL_set_connect_state(co->ssl);
if(SSL_connect(co->ssl) == -1) { if(SSL_connect(co->ssl) == -1) {
r = SNE_EPERM; r = SXE_EPERM;
/* shutdown connection */ /* shutdown connection */
goto __fail; goto __fail;
} /* if success we're ready to use established SSL channel */ } /* if success we're ready to use established SSL channel */
/* set connection to the batch mode */ /* set connection to the batch mode */
co->flags |= SNSX_BATCHMODE; co->flags |= SXMP_BATCHMODE;
/* allocate our first buffer */ /* allocate our first buffer */
buf = mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); buf = mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(buf == MAP_FAILED) { r = SNE_ENOMEM; goto __fail2; } if(buf == MAP_FAILED) { r = SXE_ENOMEM; goto __fail2; }
/* allocate first message */ /* allocate first message */
if(!(msg = malloc(sizeof(sxmsg_t)))) { r = SNE_ENOMEM; goto __fail2; } if(!(msg = malloc(sizeof(sxmsg_t)))) { r = SXE_ENOMEM; goto __fail2; }
else { else {
memset(msg, 0, sizeof(sxmsg_t)); memset(msg, 0, sizeof(sxmsg_t));
co->messages[0] = msg; co->messages[0] = msg;
@ -1295,25 +1294,25 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
sexp_t *sx; sexp_t *sx;
size_t ln; size_t ln;
while(co->flags & SNSX_BATCHMODE) { while(co->flags & SXMP_BATCHMODE) {
/* form a message -- credentials */ /* form a message -- credentials */
ln = snprintf(bbuf, 65535 - sizeof(sxmplv2_head_t), "(auth-set-credentials \"%s\" \"%s\")", ln = snprintf(bbuf, 65535 - sizeof(sxmplv2_head_t), "(auth-set-credentials \"%s\" \"%s\")",
login ? login : "nil", passwd ? passwd : "nil"); login ? login : "nil", passwd ? passwd : "nil");
head->opcode = SNE_SUCCESS; head->opcode = SXE_SUCCESS;
head->payload_length = ln; head->payload_length = ln;
wr = __conn_write(co, buf, ln + sizeof(sxmplv2_head_t)); wr = __conn_write(co, buf, ln + sizeof(sxmplv2_head_t));
if(wr < 0) goto __fail2; if(wr < 0) goto __fail2;
rd = __conn_read(co, head, sizeof(sxmplv2_head_t)); rd = __conn_read(co, head, sizeof(sxmplv2_head_t));
if(rd < 0) goto __fail2; if(rd < 0) goto __fail2;
if(head->opcode != SNE_SUCCESS) { if(head->opcode != SXE_SUCCESS) {
r = head->opcode; r = head->opcode;
goto __fail2; goto __fail2;
} }
/* ok, get available channels */ /* ok, get available channels */
head->opcode = SNE_SUCCESS; head->opcode = SXE_SUCCESS;
ln = snprintf(bbuf, 65535 - sizeof(sxmplv2_head_t), "(get-channels-list)"); ln = snprintf(bbuf, 65535 - sizeof(sxmplv2_head_t), "(get-channels-list)");
head->payload_length = ln; head->payload_length = ln;
wr = __conn_write(co, buf, ln + sizeof(sxmplv2_head_t)); wr = __conn_write(co, buf, ln + sizeof(sxmplv2_head_t));
@ -1321,7 +1320,7 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
rd = __conn_read(co, head, sizeof(sxmplv2_head_t)); rd = __conn_read(co, head, sizeof(sxmplv2_head_t));
if(rd < 0) goto __fail2; if(rd < 0) goto __fail2;
if(head->opcode != SNE_SUCCESS) goto __fail2; if(head->opcode != SXE_SUCCESS) goto __fail2;
if(!head->payload_length) goto __fail2; if(!head->payload_length) goto __fail2;
rd = __conn_read(co, bbuf, head->payload_length); rd = __conn_read(co, bbuf, head->payload_length);
if(rd < 0) goto __fail2; if(rd < 0) goto __fail2;
@ -1329,9 +1328,9 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
/* perform a parsing of the desired message */ /* perform a parsing of the desired message */
bbuf[rd] = '\0'; bbuf[rd] = '\0';
sx = parse_sexp(bbuf, rd); sx = parse_sexp(bbuf, rd);
if(!sx) { r = SNE_BADPROTO; goto __fail2; } if(!sx) { r = SXE_BADPROTO; goto __fail2; }
r = __eval_syssexp(co, sx); r = __eval_syssexp(co, sx);
if(!r) r = SNE_SUCCESS; if(!r) r = SXE_SUCCESS;
destroy_sexp(sx); destroy_sexp(sx);
/* write back */ /* write back */
@ -1339,20 +1338,20 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
head->payload_length = 0; head->payload_length = 0;
wr = __conn_write(co, head, sizeof(sxmplv2_head_t)); wr = __conn_write(co, head, sizeof(sxmplv2_head_t));
if(wr < 0) { if(wr < 0) {
r = SNE_LINKERROR; goto __fail2;} r = SXE_LINKERROR; goto __fail2;}
if(r != SNE_SUCCESS) { r = SNE_LINKERROR; goto __fail2;} if(r != SXE_SUCCESS) { r = SXE_LINKERROR; goto __fail2;}
} }
/* if we're there - negotiation is done, going to init messaging mode */ /* if we're there - negotiation is done, going to init messaging mode */
r = __link_second_alloc(co); r = __link_second_alloc(co);
if(r != SNE_SUCCESS) goto __fail3; if(r != SXE_SUCCESS) goto __fail3;
/* free message */ /* free message */
co->messages[0] = NULL; co->messages[0] = NULL;
free(msg); free(msg);
/* and now we're need to create a thread poll */ /* and now we're need to create a thread poll */
if(!(bundle = malloc(sizeof(sxmplv2_bundle_t)))) { r = SNE_ENOMEM; goto __fail4; } if(!(bundle = malloc(sizeof(sxmplv2_bundle_t)))) { r = SXE_ENOMEM; goto __fail4; }
else { else {
bundle->buf = buf; bundle->buf = buf;
bundle->conn = co; bundle->conn = co;
@ -1369,12 +1368,12 @@ sxlink_t *sxlink_connect(conn_sys_t *ssys, const char *host,
} }
/* all is done, connection now ready */ /* all is done, connection now ready */
co->flags |= SNSX_ALIVE; co->flags |= SXMP_ALIVE;
return co; return co;
__fail5: __fail5:
r = SNE_ENOMEM; r = SXE_ENOMEM;
/* bundles will be freed by the threads when SSL_read will fails. */ /* bundles will be freed by the threads when SSL_read will fails. */
__fail4: __fail4:
__link_second_free(co); __link_second_free(co);
@ -1409,10 +1408,10 @@ int sxlink_close(sxlink_t *co)
pthread_mutex_unlock(&(co->sslinout[1])); pthread_mutex_unlock(&(co->sslinout[1]));
/* we will not wait anything */ /* we will not wait anything */
co->flags |= SNSX_CLOSED; co->flags |= SXMP_CLOSED;
/* TODO: wait until all threads will finish */ /* TODO: wait until all threads will finish */
usleep(20000); usleep(20000);
return SNE_SUCCESS; return SXE_SUCCESS;
} }

Loading…
Cancel
Save