File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / daemon.c
Revision 1.1.2.25: download - view: text, annotated - select for diffs - revision graph
Fri Dec 16 12:22:26 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
add comment

    1: #include "global.h"
    2: #include "rtlm.h"
    3: #include "utils.h"
    4: #include "mqttd.h"
    5: 
    6: 
    7: static inline struct tagSession *
    8: initSession(int sock, ait_val_t * __restrict v)
    9: {
   10: 	struct tagSession *sess = NULL;
   11: 	const char *str;
   12: 
   13: 	ioTRACE(5);
   14: 
   15: 	if (!v)
   16: 		return NULL;
   17: 
   18: 	sess = malloc(sizeof(struct tagSession));
   19: 	if (!sess) {
   20: 		ioDEBUG(3, "Error:: in malloc #%d - %s", errno, strerror(errno));
   21: 		io_freeVar(v);
   22: 		return NULL;
   23: 	} else
   24: 		memset(sess, 0, sizeof(struct tagSession));
   25: 
   26: 	str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry"));
   27: 	if (!str)
   28: 		sess->sess_retry = DEFAULT_RETRY;
   29: 	else
   30: 		sess->sess_retry = strtol(str, NULL, 0);
   31: 
   32: 	sess->sess_buf = mqtt_msgAlloc(0);
   33: 	if (!sess->sess_buf) {
   34: 		ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
   35: 		free(sess);
   36: 		io_freeVar(v);
   37: 		return NULL;
   38: 	}
   39: 
   40: 	sess->sess_sock = sock;
   41: 	strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
   42: 	io_freeVar(v);
   43: 	return sess;
   44: }
   45: 
   46: static void
   47: finiSession(struct tagSession *sess)
   48: {
   49: 	register int i;
   50: 	struct tagStore *store;
   51: 
   52: 	ioTRACE(5);
   53: 
   54: 	if (!sess)
   55: 		return;
   56: 
   57: 	for (i = 0; i < MQTT_QOS_RESERVED; i++)
   58: 		while ((store = SLIST_FIRST(&sess->sess_store[i]))) {
   59: 			SLIST_REMOVE_HEAD(&sess->sess_store[i], st_node);
   60: 			free(store);
   61: 		}
   62: 
   63: 	if (sess->sess_will.msg)
   64: 		free(sess->sess_will.msg);
   65: 	if (sess->sess_will.topic)
   66: 		free(sess->sess_will.topic);
   67: 
   68: 	if (sess->sess_sock > STDERR_FILENO)
   69: 		srv_Close(sess->sess_sock);
   70: 
   71: 	mqtt_msgFree(&sess->sess_buf, 42);
   72: 
   73: 	free(sess);
   74: }
   75: 
   76: static void
   77: stopSession(struct tagSession *sess)
   78: {
   79: 	mqtt_msg_t msg = { NULL, 0 };
   80: 	int ret;
   81: 
   82: 	pthread_mutex_lock(&mtx_sess);
   83: 	TAILQ_REMOVE(&Sessions, sess, sess_node);
   84: 	pthread_mutex_unlock(&mtx_sess);
   85: 
   86: 	ret = mqtt_msgDISCONNECT(&msg);
   87: 	if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1)
   88: 		ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
   89: 	else {
   90: 		ioDEBUG(5, "Sended %d bytes for disconnect", ret);
   91: 		free(msg.msg_base);
   92: 		msg.msg_len = 0;
   93: 	}
   94: 
   95: 	ioDEBUG(1, "Close socket=%d", sess->sess_sock);
   96: 	finiSession(sess);
   97: }
   98: 
   99: static void *
  100: thrSession(struct tagSession *sess)
  101: {
  102: 	pthread_cleanup_push((void(*)(void*)) stopSession, sess);
  103: 
  104: 	ioTRACE(2);
  105: 
  106: 	pthread_cleanup_pop(42);
  107: 	pthread_exit(NULL);
  108: }
  109: 
  110: static void *
  111: startSession(sched_task_t *task)
  112: {
  113: 	u_char basebuf[USHRT_MAX];
  114: 	mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
  115: 	mqtthdr_connflgs_t flg;
  116: 	mqtthdr_connack_t cack;
  117: 	struct tagSession *s, *sess = NULL;
  118: 	int ret;
  119: 	pthread_attr_t attr;
  120: 
  121: 	ioTRACE(4);
  122: 
  123: 	sess = initSession(TASK_FD(task), TASK_ARG(task));
  124: 	if (!sess) {
  125: 		io_freeVar(TASK_ARG(task));
  126: 		close(TASK_FD(task));
  127: 		return NULL;
  128: 	}
  129: 
  130: 	/* receive & decode packet */
  131: 	if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
  132: 		ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
  133: 		finiSession(sess);
  134: 		return NULL;
  135: 	}
  136: 	cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid, 
  137: 			sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, 
  138: 			&sess->sess_will.topic, &sess->sess_will.msg);
  139: 	ret = cack.retcode;
  140: 	flg.flags = cack.reserved;
  141: 	if (flg.reserved) {
  142: 		ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
  143: 		goto end;
  144: 	} else {
  145: 		sess->sess_clean = flg.clean_sess;
  146: 		sess->sess_will.qos = flg.will_qos;
  147: 		sess->sess_will.retain = flg.will_retain;
  148: 		sess->sess_will.flag = flg.will_flg;
  149: 	}
  150: 
  151: 	/* check online table for user */
  152: 	if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
  153: 		ioDEBUG(0, "Login:: DENIED for username %s and password %s", 
  154: 				sess->sess_user, sess->sess_pass);
  155: 		ret = MQTT_RETCODE_DENIED;
  156: 		goto end;
  157: 	} else {
  158: 		ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
  159: 		ret = MQTT_RETCODE_ACCEPTED;
  160: 	}
  161: 	if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
  162: 		ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
  163: 		TAILQ_FOREACH(s, &Sessions, sess_node)
  164: 			if (!strcmp(s->sess_cid, sess->sess_cid)) {
  165: 				/* found stale session & disconnect it! */
  166: 				stopSession(s);
  167: 				break;
  168: 			}
  169: 	}
  170: 	if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr, 
  171: 				sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg, 
  172: 				sess->sess_will.qos, sess->sess_will.retain) == -1) {
  173: 		ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
  174: 		ret = MQTT_RETCODE_DENIED;
  175: 		goto end;
  176: 	} else {
  177: 		ioDEBUG(0, "Session %s from %s and username %s is started", 
  178: 				sess->sess_cid, sess->sess_addr, sess->sess_user);
  179: 		ret = MQTT_RETCODE_ACCEPTED;
  180: 	}
  181: 
  182: 	ret = mqtt_msgCONNACK(&msg, ret);
  183: 	if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
  184: 		ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
  185: 		finiSession(sess);
  186: 		return NULL;
  187: 	} else {
  188: 		ioDEBUG(5, "Sended %d bytes", ret);
  189: 		free(msg.msg_base);
  190: 		msg.msg_len = 0;
  191: 	}
  192: 
  193: 	/* Start session thread OK ... */
  194: 	pthread_attr_init(&attr);
  195: 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  196: 
  197: 	pthread_mutex_lock(&mtx_sess);
  198: 	TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
  199: 	pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
  200: 	pthread_mutex_unlock(&mtx_sess);
  201: 
  202: 	call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid, 
  203: 			sess->sess_addr, sess->sess_user);
  204: 
  205: 	pthread_attr_destroy(&attr);
  206: 	return NULL;
  207: end:	/* close client connection */
  208: 	ret = mqtt_msgCONNACK(&msg, ret);
  209: 	if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
  210: 		ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
  211: 	} else {
  212: 		ioDEBUG(5, "Sended %d bytes", ret);
  213: 		free(msg.msg_base);
  214: 		msg.msg_len = 0;
  215: 	}
  216: 
  217: 	ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
  218: 	finiSession(sess);
  219: 	return NULL;
  220: }
  221: 
  222: /* ----------------------------------------------------------------------- */
  223: 
  224: static void *
  225: thrSched(void *arg __unused)
  226: {
  227: 	struct tagSession *sess;
  228: 
  229: 	ioTRACE(1);
  230: 
  231: 	schedRun(root, &Kill);
  232: 
  233: 	TAILQ_FOREACH(sess, &Sessions, sess_node)
  234: 		if (sess->sess_tid)
  235: 			pthread_cancel(sess->sess_tid);
  236: 	pthread_exit(NULL);
  237: }
  238: 
  239: int
  240: Run(int sock)
  241: {
  242: 	io_sockaddr_t sa;
  243: 	socklen_t sslen = sizeof sa.ss;
  244: 	int cli;
  245: 	pthread_t tid;
  246: 	ait_val_t *v;
  247: 	char str[STRSIZ];
  248: 
  249: 	ioTRACE(1);
  250: 
  251: 	if (pthread_create(&tid, NULL, thrSched, NULL)) {
  252: 		ioLOGERR(0);
  253: 		return -1;
  254: 	} else
  255: 		pthread_detach(tid);
  256: 	ioDEBUG(2, "Run scheduler management thread");
  257: 
  258: 	if (listen(sock, SOMAXCONN) == -1) {
  259: 		ioLOGERR(0);
  260: 		return -1;
  261: 	}
  262: 
  263: 	while (!Kill) {
  264: 		if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
  265: 			if (!Kill)
  266: 				continue;
  267: 			ioLOGERR(0);
  268: 			break;
  269: 		}
  270: 		v = io_allocVar();
  271: 		if (!v) {
  272: 			ioLIBERR(mqtt);
  273: 			break;
  274: 		} else {
  275: 			memset(str, 0, sizeof str);
  276: 			snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
  277: 			AIT_SET_STR(v, str);
  278: 		}
  279: 		ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
  280: 
  281: 		if (!schedRead(root, startSession, v, cli)) {
  282: 			io_freeVar(v);
  283: 			close(cli);
  284: 			ioDEBUG(1, "Terminated client with socket=%d", cli);
  285: 		}
  286: 	}
  287: 
  288: 	return 0;
  289: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>