Annotation of mqtt/src/utils.c, revision 1.2
1.2 ! misho 1: #include "global.h"
! 2:
! 3:
! 4: int
! 5: srv_Socket(sl_config * __restrict cfg)
! 6: {
! 7: int s = -1, n = 1;
! 8: struct hostent *host;
! 9: char szStr[STRSIZ];
! 10: u_short port;
! 11: struct sockaddr_storage ss;
! 12: struct sockaddr_in *sin = (struct sockaddr_in*) &ss;
! 13: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &ss;
! 14:
! 15: ioTRACE(2);
! 16:
! 17: cfg_LoadAttribute(cfg, CFG("mqttd"), CFG("port"), CFG(szStr), sizeof szStr, MQTT_PORT);
! 18: port = strtol(szStr, NULL, 0);
! 19: cfg_LoadAttribute(cfg, CFG("mqttd"), CFG("listen"), CFG(szStr), sizeof szStr, MQTT_HOST);
! 20:
! 21: host = gethostbyname(szStr);
! 22: if (!host) {
! 23: printf("Error:: resolver #%d - %s\n", h_errno, hstrerror(h_errno));
! 24: return -1;
! 25: }
! 26: switch (host->h_addrtype) {
! 27: case AF_INET:
! 28: sin->sin_len = sizeof(struct sockaddr_in);
! 29: sin->sin_family = AF_INET;
! 30: sin->sin_port = htons(port);
! 31: memcpy(&sin->sin_addr, host->h_addr, sizeof sin->sin_addr);
! 32: break;
! 33: case AF_INET6:
! 34: sin6->sin6_len = sizeof(struct sockaddr_in6);
! 35: sin6->sin6_family = AF_INET6;
! 36: sin6->sin6_port = htons(port);
! 37: memcpy(&sin6->sin6_addr, host->h_addr, sizeof sin6->sin6_addr);
! 38: break;
! 39: default:
! 40: printf("Error:: unsupported socket type %d\n", host->h_addrtype);
! 41: return -1;
! 42: }
! 43:
! 44: s = socket(ss.ss_family, SOCK_STREAM, 0);
! 45: if (s == -1) {
! 46: printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
! 47: return -1;
! 48: }
! 49: if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
! 50: printf("Error:: setsockopt(SO_REUSEADDR) #%d - %s\n", errno, strerror(errno));
! 51: close(s);
! 52: return -1;
! 53: }
! 54: if (bind(s, (struct sockaddr*) &ss, ss.ss_len) == -1) {
! 55: printf("Error:: bind() #%d - %s\n", errno, strerror(errno));
! 56: close(s);
! 57: return -1;
! 58: }
! 59:
! 60: return s;
! 61: }
! 62:
! 63: int
! 64: srv_Close(int s)
! 65: {
! 66: ioTRACE(2);
! 67:
! 68: if (s > STDERR_FILENO)
! 69: shutdown(s, SHUT_RDWR);
! 70:
! 71: return close(s);
! 72: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>