File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / utils.c
Revision 1.2.2.3: download - view: text, annotated - select for diffs - revision graph
Tue Apr 24 08:06:09 2012 UTC (12 years, 2 months ago) by misho
Branches: mqtt1_1
add new Pubs structure
fix error messages
optimize srv_socket

    1: #include "global.h"
    2: 
    3: 
    4: int
    5: srv_Socket(cfg_root_t * __restrict cfg)
    6: {
    7: 	int s = -1, n = 1;
    8: 	ait_val_t v;
    9: 	u_short port;
   10: 	io_sockaddr_t sa;
   11: 
   12: 	ioTRACE(2);
   13: 
   14: 	cfg_loadAttribute(cfg, "mqttd", "port", &v, MQTT_PORT);
   15: 	port = strtol(AIT_GET_STR(&v), NULL, 0);
   16: 	AIT_FREE_VAL(&v);
   17: 	cfg_loadAttribute(cfg, "mqttd", "listen", &v, MQTT_HOST);
   18: 	if (!io_gethostbyname(AIT_GET_STR(&v), port, &sa)) {
   19: 		ioLIBERR(io);
   20: 		AIT_FREE_VAL(&v);
   21: 		return -1;
   22: 	} else
   23: 		AIT_FREE_VAL(&v);
   24: 
   25: 	s = socket(sa.ss.ss_family, SOCK_STREAM, 0);
   26: 	if (s == -1) {
   27: 		ioSYSERR(0);
   28: 		return -1;
   29: 	}
   30: 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
   31: 		ioSYSERR(0);
   32: 		close(s);
   33: 		return -1;
   34: 	}
   35: 	if (bind(s, &sa.sa, sa.sa.sa_len) == -1) {
   36: 		ioSYSERR(0);
   37: 		close(s);
   38: 		return -1;
   39: 	}
   40: 
   41: 	return s;
   42: }
   43: 
   44: int
   45: srv_Close(int s)
   46: {
   47: 	ioTRACE(2);
   48: 
   49: 	if (s > STDERR_FILENO)
   50: 		shutdown(s, SHUT_RDWR);
   51: 
   52: 	return close(s);
   53: }

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