Annotation of mqtt/src/utils.c, revision 1.2.2.1

1.2       misho       1: #include "global.h"
                      2: 
                      3: 
                      4: int
1.2.2.1 ! misho       5: srv_Socket(cfg_root_t * __restrict cfg)
1.2       misho       6: {
                      7:        int s = -1, n = 1;
                      8:        struct hostent *host;
1.2.2.1 ! misho       9:        ait_val_t v;
1.2       misho      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: 
1.2.2.1 ! misho      17:        cfg_loadAttribute(cfg, "mqttd", "port", &v, MQTT_PORT);
        !            18:        port = strtol(AIT_GET_STR(&v), NULL, 0);
        !            19:        AIT_FREE_VAL(&v);
        !            20:        cfg_loadAttribute(cfg, "mqttd", "listen", &v, MQTT_HOST);
1.2       misho      21: 
1.2.2.1 ! misho      22:        host = gethostbyname(AIT_GET_STR(&v));
        !            23:        AIT_FREE_VAL(&v);
1.2       misho      24:        if (!host) {
                     25:                printf("Error:: resolver #%d - %s\n", h_errno, hstrerror(h_errno));
                     26:                return -1;
                     27:        }
                     28:        switch (host->h_addrtype) {
                     29:                case AF_INET:
                     30:                        sin->sin_len = sizeof(struct sockaddr_in);
                     31:                        sin->sin_family = AF_INET;
                     32:                        sin->sin_port = htons(port);
                     33:                        memcpy(&sin->sin_addr, host->h_addr, sizeof sin->sin_addr);
                     34:                        break;
                     35:                case AF_INET6:
                     36:                        sin6->sin6_len = sizeof(struct sockaddr_in6);
                     37:                        sin6->sin6_family = AF_INET6;
                     38:                        sin6->sin6_port = htons(port);
                     39:                        memcpy(&sin6->sin6_addr, host->h_addr, sizeof sin6->sin6_addr);
                     40:                        break;
                     41:                default:
                     42:                        printf("Error:: unsupported socket type %d\n", host->h_addrtype);
                     43:                        return -1;
                     44:        }
                     45: 
                     46:        s = socket(ss.ss_family, SOCK_STREAM, 0);
                     47:        if (s == -1) {
                     48:                printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
                     49:                return -1;
                     50:        }
                     51:        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
                     52:                printf("Error:: setsockopt(SO_REUSEADDR) #%d - %s\n", errno, strerror(errno));
                     53:                close(s);
                     54:                return -1;
                     55:        }
                     56:        if (bind(s, (struct sockaddr*) &ss, ss.ss_len) == -1) {
                     57:                printf("Error:: bind() #%d - %s\n", errno, strerror(errno));
                     58:                close(s);
                     59:                return -1;
                     60:        }
                     61: 
                     62:        return s;
                     63: }
                     64: 
                     65: int
                     66: srv_Close(int s)
                     67: {
                     68:        ioTRACE(2);
                     69: 
                     70:        if (s > STDERR_FILENO)
                     71:                shutdown(s, SHUT_RDWR);
                     72: 
                     73:        return close(s);
                     74: }

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