PracticeDev/study_clang/zmq/zeromq-curve/server.c

183 lines
4.7 KiB
C

/*
* =====================================================================================
*
* Filename: send.c
*
* Description:
*
* Version: 1.0
* Created: 09/28/2018 07:14:44 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <zmq.h>
#include <string.h>
#include <pthread.h>
#define fb_debug(fmt, arg...) \
do{\
printf("%s %d : ", __FILE__, __LINE__); \
printf(fmt, ##arg); \
printf("\n"); \
}while(0)
#define fb_assert(x, info) \
do{\
if(!(x)) { \
fb_debug(info); \
return -1;\
} \
}while(0)
struct ser_opt {
void *zmq_ctx;
const char *publickey;
const char *ip;
int port;
};
void *publickey_server(void *arg) {
pthread_detach(pthread_self());
if(!arg) {
fb_debug("no arg when thread start");
return NULL;
}
struct ser_opt *server = (struct ser_opt*)arg;
void *sock = zmq_socket(server -> zmq_ctx, ZMQ_DEALER);
char szaddr[64] = {0};
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", server -> ip, server -> port);
zmq_bind(sock, szaddr);
char szmsg[1024] = {0};
while(1) {
bzero(szmsg, sizeof(szmsg));
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
if (strcmp("request publickey", szmsg) == 0) {
snprintf(szmsg, sizeof(szmsg), "%s", server -> publickey);
} else {
snprintf(szmsg, sizeof(szmsg), "%s", "no such service");
}
zmq_send(sock, szmsg, strlen(szmsg), 0);
}
return NULL;
}
int start_publickey_server(struct ser_opt *server) {
pthread_t pt;
pthread_create(&pt, NULL, publickey_server, server);
return 0;
}
int with_curve(const char *ip, int port) {
fb_debug("test tZMQ with curve");
void * ctx = zmq_ctx_new();
fb_assert(ctx, "create zmq context failed");
void *sock = zmq_socket(ctx, ZMQ_DEALER);
fb_assert(sock, "create zmq socket failed");
char szsecertkey[128] = {0};
char szpublickey[128] = {0};
zmq_curve_keypair(szpublickey, szsecertkey);
fb_debug("szsecertkey = [%s]", szsecertkey);
fb_debug("szpublickey = [%s]", szpublickey);
struct ser_opt server;
server.publickey = szpublickey;
server.ip = ip;
server.port = port + 1;
server.zmq_ctx = ctx;
start_publickey_server(&server);
int option = 1;
zmq_setsockopt(sock, ZMQ_CURVE_SERVER, &option, sizeof(option));
zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szsecertkey, strlen(szsecertkey));
fb_debug("start secert comunication");
char szaddr[128] = {0};
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
zmq_bind(sock, szaddr);
char szmsg[1024] = {0};
while(1) {
bzero(szmsg, sizeof(szmsg));
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
fb_debug("recv msg : [%s]", szmsg);
if(strcasecmp("bye", szmsg) == 0) {
fb_debug("send msg : [%s]", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
break;
}
strcat(szmsg, ", too");
fb_debug("send msg : [%s]", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
}
zmq_unbind(sock, szaddr);
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
return 0;
}
int without_curve(const char *ip, int port) {
fb_debug("test tZMQ without curve");
void * ctx = zmq_ctx_new();
fb_assert(ctx, "create zmq context failed");
void *sock = zmq_socket(ctx, ZMQ_DEALER);
fb_assert(sock, "create zmq socket failed");
char szaddr[128] = {0};
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
zmq_bind(sock, szaddr);
char szmsg[1024] = {0};
while(1) {
bzero(szmsg, sizeof(szmsg));
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
fb_debug("recv psg : [%s]", szmsg);
if(strcasecmp("bye", szmsg) == 0) {
fb_debug("send msg : [%s]", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
break;
}
strcat(szmsg, ", too");
fb_debug("send msg : [%s]", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
}
zmq_unbind(sock, szaddr);
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
return 0;
}
int main(int argc, char * argv[]) {
const char *ip = "127.0.0.1";
int port = 5678;
int is_withcurve = 0;
int i = 0;
for(i = 0; i < argc; i++) {
if(strcmp("-s", argv[i]) == 0) {
is_withcurve = 1;
}
}
if(is_withcurve) {
with_curve(ip, port);
} else {
without_curve(ip, port);
}
return 0;
}