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