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

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;
1.2.2.2 ! misho      11:        io_sockaddr_t sa;
1.2       misho      12: 
                     13:        ioTRACE(2);
                     14: 
1.2.2.1   misho      15:        cfg_loadAttribute(cfg, "mqttd", "port", &v, MQTT_PORT);
                     16:        port = strtol(AIT_GET_STR(&v), NULL, 0);
                     17:        AIT_FREE_VAL(&v);
                     18:        cfg_loadAttribute(cfg, "mqttd", "listen", &v, MQTT_HOST);
1.2       misho      19: 
1.2.2.1   misho      20:        host = gethostbyname(AIT_GET_STR(&v));
                     21:        AIT_FREE_VAL(&v);
1.2       misho      22:        if (!host) {
1.2.2.2 ! misho      23:                ioERROR(h_errno, "%s", hstrerror(h_errno));
1.2       misho      24:                return -1;
                     25:        }
                     26:        switch (host->h_addrtype) {
                     27:                case AF_INET:
1.2.2.2 ! misho      28:                        sa.sin.sin_len = sizeof(struct sockaddr_in);
        !            29:                        sa.sin.sin_family = AF_INET;
        !            30:                        sa.sin.sin_port = htons(port);
        !            31:                        memcpy(&sa.sin.sin_addr, host->h_addr, sizeof sa.sin.sin_addr);
1.2       misho      32:                        break;
                     33:                case AF_INET6:
1.2.2.2 ! misho      34:                        sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
        !            35:                        sa.sin6.sin6_family = AF_INET6;
        !            36:                        sa.sin6.sin6_port = htons(port);
        !            37:                        memcpy(&sa.sin6.sin6_addr, host->h_addr, sizeof sa.sin6.sin6_addr);
1.2       misho      38:                        break;
                     39:                default:
1.2.2.2 ! misho      40:                        ioERROR(host->h_addrtype, "unsupported socket type");
1.2       misho      41:                        return -1;
                     42:        }
                     43: 
1.2.2.2 ! misho      44:        s = socket(sa.ss.ss_family, SOCK_STREAM, 0);
1.2       misho      45:        if (s == -1) {
1.2.2.2 ! misho      46:                ioSYSERR(0);
1.2       misho      47:                return -1;
                     48:        }
                     49:        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1.2.2.2 ! misho      50:                ioSYSERR(0);
1.2       misho      51:                close(s);
                     52:                return -1;
                     53:        }
1.2.2.2 ! misho      54:        if (bind(s, (struct sockaddr*) &sa.ss, sa.ss.ss_len) == -1) {
        !            55:                ioSYSERR(0);
1.2       misho      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>