File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / daemon.c
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Thu Dec 1 08:15:25 2011 UTC (12 years, 7 months ago) by misho
Branches: mqtt1_0
finish accept state mashine

    1: #include "global.h"
    2: 
    3: 
    4: static int
    5: startSession(int s, const char *csAddr)
    6: {
    7: 	return 0;
    8: }
    9: 
   10: /* ----------------------------------------------------------------------- */
   11: 
   12: int
   13: Run(int sock)
   14: {
   15: 	io_sockaddr_t sa;
   16: 	socklen_t sslen = sizeof sa.ss;
   17: 	int cli;
   18: 	char szAddr[STRSIZ] = { 0 };
   19: 
   20: 	FTRACE(1);
   21: 
   22: 	if (listen(sock, SOMAXCONN) == -1) {
   23: 		syslog(LOG_ERR, "Error:: listen(%d) #%d - %s\n", sock, errno, strerror(errno));
   24: 		return -1;
   25: 	}
   26: 
   27: 	while (!Kill) {
   28: 		if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
   29: 			syslog(LOG_ERR, "Error:: accept() #%d - %s", errno, strerror(errno));
   30: 			continue;
   31: 		} else
   32: 			VERB(1) {
   33: 				switch (sa.sa.sa_family) {
   34: 					case AF_INET:
   35: 						inet_ntop(AF_INET, &sa.sin.sin_addr, szAddr, sslen);
   36: 						snprintf(szAddr, sizeof szAddr, "%s:%d", 
   37: 								szAddr, ntohs(sa.sin.sin_port));
   38: 						break;
   39: 					case AF_INET6:
   40: 						inet_ntop(AF_INET6, &sa.sin6.sin6_addr, szAddr, sslen);
   41: 						snprintf(szAddr, sizeof szAddr, "%s:%d", 
   42: 								szAddr, ntohs(sa.sin6.sin6_port));
   43: 						break;
   44: 					case AF_LOCAL:
   45: 						strlcpy(szAddr, sa.sun.sun_path, sizeof szAddr);
   46: 						break;
   47: 					default:
   48: 						close(cli);
   49: 						syslog(LOG_ERR, "Error:: unsupported address type %d", 
   50: 								sa.sa.sa_family);
   51: 						continue;
   52: 				}
   53: 				syslog(LOG_DEBUG, "Connected client with socket=%d from %s", cli, szAddr);
   54: 			}
   55: 
   56: 		if (startSession(cli, szAddr)) {
   57: 			close(cli);
   58: 			VERB(1) syslog(LOG_DEBUG, "Terminated client with socket=%d", cli);
   59: 			continue;
   60: 		}
   61: 	}
   62: 
   63: 	return 0;
   64: }

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