Exemple #1
0
int 
getnameinfo (const struct sockaddr *sa, socklen_t salen, 
             char *host, socklen_t hostlen, 
             char *serv, socklen_t servlen, 
             unsigned int flags)
{
    int port;
    char *s;
    struct servent *se;

    if ((flags & ~NI_MASK) != 0) {
        return EAI_BADFLAGS;
    }
    switch (sa->sa_family) {
    case PF_INET:
#ifdef CYGPKG_NET_INET6
    case PF_INET6:
#endif
        if (host != (char *)NULL) {
            s = _inet_ntop((struct sockaddr *)sa, host, hostlen);
            if (!s) {
                return EAI_FAIL;
            }
        }
        if (serv != (char *)NULL) {
            port = _inet_port((struct sockaddr *)sa);
            if (!port) {
                return EAI_FAIL;
            }
            se = (struct servent *)NULL;
            if ((flags & NI_NUMERICSERV) == 0) {
                if ((flags & NI_DGRAM) == 0) {
                    se = getservbyport(port, "tcp");
                }
                if (se == (struct servent *)NULL) {
                    se = getservbyport(port, "ucp");
                }
            }
            if (se != (struct servent *)NULL) {
                sprintf(serv, "%s/%s", se->s_name, se->s_proto);
            } else {
                sprintf(serv, "%d", port);
            }
        }
        break;
    default:
        return EAI_FAMILY;
    }
    return EAI_NONE;
}
Exemple #2
0
int 
getnameinfo (const struct sockaddr *sa, socklen_t salen, 
             char *host, socklen_t hostlen, 
             char *serv, socklen_t servlen, 
             unsigned int flags)
{
    int port;
    char *s;
    struct servent *se;
    int error;
    int numeric = (flags & NI_NUMERICHOST);

    if ((flags & ~NI_MASK) != 0) {
        return EAI_BADFLAGS;
    }
    switch (sa->sa_family) {
    case PF_INET:
#ifdef CYGPKG_NET_INET6
    case PF_INET6:
#endif
        if (host != (char *)NULL) {
            if ( !numeric) {
                error = EAI_NONAME;
#ifdef CYGPKG_NS_DNS_BUILD
                error = -cyg_dns_getnameinfo(sa, host,hostlen);
#endif
                if ((error == EAI_NONAME) && (flags & NI_NAMEREQD)) {
                    return EAI_NONAME;
                }
                // If lookup failed, try it as numeric address
                numeric = !(error == EAI_NONE);
            }
            if (numeric) {
                s = _inet_ntop((struct sockaddr *)sa, host, hostlen);
                if (!s) {
                    return EAI_FAIL;
                }
            }
            if (!numeric && flags & NI_NOFQDN) {
                s = index(host, '.');
                if (s) {
                    *s = '\0';
                }
            }
        }
    
        if (serv != (char *)NULL) {
            port = _inet_port((struct sockaddr *)sa);
            if (!port) {
                return EAI_FAIL;
            }
            se = (struct servent *)NULL;
            if ((flags & NI_NUMERICSERV) == 0) {
                if ((flags & NI_DGRAM) == 0) {
                    se = getservbyport(htons(port), "tcp");
                }
                if (se == (struct servent *)NULL) {
                    se = getservbyport(htons(port), "udp");
                }
            }
            if (se != (struct servent *)NULL) {
                diag_snprintf(serv,servlen, "%s/%s", se->s_name, se->s_proto);
            } else {
                diag_snprintf(serv,servlen, "%d", port);
            }
        }
        break;
    default:
        return EAI_FAMILY;
    }
    return EAI_NONE;
}
Exemple #3
0
int 
img_demo_thread(CYG_ADDRWORD data)
{
    int err, s, client, client_len;
    struct sockaddr client_addr;
    struct sockaddr_in my_addr;
    struct addrinfo *ai, *addrs, hints;
    char buf[256], addr_buf[256];
    int one = 1;
    fd_set in_fds, src_fds;
    int num, len;
    struct timeval tv;

    INIT_PER_THREAD_DATA();

    printf("Image demo here\n");
#ifdef CYGPKG_FS_RAM
    // Stage files from JFFS2 image into RAM
    err = mount("", "/ramfs", "ramfs");        
    if (err >= 0) {
        do_copy_all("/", "/ramfs");
    } else {
        pexit("Can't mount RAMfs\n");
    }
    chdir("/ramfs");
#endif
    if(GrOpen() < 0) {
        fprintf(stderr, "Couldn't connect to Nano-X server\n");
        exit(1);
    }
    // Set up as a generic server, listening on TCP/7734
#if 0
    bzero(&hints, sizeof(hints));
    hints.ai_family = PF_UNSPEC;b
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
        diag_printf("can't getaddrinfo(): %s\n", gai_strerror(err));
        pexit("getaddrinfo");
    }
    s = socket(ai->ai_family, ai->ai_socktype, 0);
    if (s < 0) {
        pexit("stream socket");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEADDR");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEPORT");
    }
    if(bind(s, ai->ai_addr, ai->ai_addr->sa_len) < 0) {
        pexit("bind error");
    }
    listen(s, SOMAXCONN);
#else
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
        pexit("stream socket");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEADDR");
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
        pexit("setsockopt SO_REUSEPORT");
    }
    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_len = sizeof(my_addr);
    my_addr.sin_port = htons(7734);
    my_addr.sin_addr.s_addr = INADDR_ANY;
    if(bind(s, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
        pexit("bind error");
    }
    listen(s, SOMAXCONN);
#endif
    while (true) {
        // Wait for a connection
        FD_ZERO(&src_fds);
        FD_SET(s, &src_fds);
        tv.tv_sec = 15;
        tv.tv_usec = 0;
        num = select(s+1, &src_fds, 0, 0, &tv);
        if (num > 0) {
            client_len = sizeof(client_addr);
            if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
                pexit("accept");
            }
            client_len = sizeof(client_addr);
            getpeername(client, &client_addr, &client_len);
            _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
            diag_printf("connection from %s(%d)\n", addr_buf, ntohs(_inet_port(&client_addr)));
            fdprintf(client, "Hello %s(%d)\n", addr_buf, ntohs(_inet_port(&client_addr)));
            while (true) {
                fdprintf(client, "// Ready\n");
                tv.tv_sec = 5;
                tv.tv_usec = 0;
                FD_ZERO(&in_fds);
                FD_SET(client, &in_fds);
                num = select(client+1, &in_fds, 0, 0, &tv);
                if (num > 0) {
                    len = read(client, buf, sizeof(buf)-1);
                    if (len <= 0) {
                        diag_printf("Client read error: %s\n", strerror(errno));
                        break;
                    }
                    buf[len-1] = '\0';
                    do_cmd(client, buf);
                } else if (num == 0) {
                    fdprintf(client, "<IDLE> show_all\n");
                    do_show_all(client, "show_all .");
                } else {
                    perror("select");
                }
            }
            close(client);
            diag_printf("Connection with %s closed\n", addr_buf);
        } else if (num == 0) {
            // No connection within 15 seconds
            do_show_all(0, "show_all .");
        } else {
            diag_printf("select returned: %d\n", num);
            pexit("bad select");
        }
    }
}
static void
server_test(struct bootp *bp)
{
    int sock_indx, i, s, socks[MAXSOCK], client, client_len;
    struct sockaddr client_addr;
    char buf[256], addr_buf[256];
    char host_addr_buf[256], host_port_buf[32];
    int one = 1;
    fd_set in_fds, src_fds;
    int num, len;
    struct timeval tv;
    struct addrinfo *ai, *addrs, hints;
    int err, last_sock;

    bzero(&hints, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
        diag_printf("can't getaddrinfo(): %s\n", gai_strerror(err));
        pexit("getaddrinfo failed");
    }
    sock_indx = 0;  last_sock = -1;
    ai = addrs;
    while (ai) {
        _inet_ntop(ai->ai_addr, addr_buf, sizeof(addr_buf));
        diag_printf("Family: %d, Socket: %d, Addr: %s\n", ai->ai_family, ai->ai_socktype, addr_buf);
        s = socket(ai->ai_family, ai->ai_socktype, 0);
        if (s < 0) {
            pexit("stream socket");
        }
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
            pexit("setsockopt SO_REUSEADDR");
        }
        if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
            pexit("setsockopt SO_REUSEPORT");
        }
        if(bind(s, ai->ai_addr, ai->ai_addr->sa_len) < 0) {
            pexit("bind error");
        }
        listen(s, SOMAXCONN);
        socks[sock_indx++] = s;
        if (sock_indx >= MAXSOCK) {
            pexit("Too many address types");
        }
        ai = ai->ai_next;
        if (s > last_sock) last_sock = s;
    }
    while (true) {
        // Wait for some activity on one of the ports
        FD_ZERO(&src_fds);
        for (s = 0;  s < sock_indx;  s++) {
            FD_SET(socks[s], &src_fds);
        }
        num = select(last_sock+1, &src_fds, 0, 0, 0);
        if (num > 0) {
            // There are 'num' sockets ready to connect
            for (i = 0;  i < sock_indx; i++) {
                s = socks[i];
                if (FD_ISSET(s, &src_fds)) {
                    client_len = sizeof(client_addr);
                    if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
                        pexit("accept");
                    }
                    client_len = sizeof(client_addr);
                    getpeername(client, &client_addr, &client_len);
                    if (getnameinfo (&client_addr, client_len, 
                                     host_addr_buf, sizeof(host_addr_buf),
                                     host_port_buf, sizeof(host_port_buf), 
                                     NI_NUMERICHOST) == EAI_NONE) {
                        diag_printf("connection from %s(%s)\n", host_addr_buf, host_port_buf);
                        diag_sprintf(buf, "Hello %s(%s)\n", host_addr_buf, host_port_buf);
                    } else {
                        _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
                        diag_printf("connection from %s(%d)\n", addr_buf, _inet_port(&client_addr));
                        diag_sprintf(buf, "Hello %s(%d)\n", addr_buf, _inet_port(&client_addr));
                    }
                    write(client, buf, strlen(buf));
                    tv.tv_sec = 5;
                    tv.tv_usec = 0;
                    FD_ZERO(&in_fds);
                    FD_SET(client, &in_fds);
                    num = select(client+1, &in_fds, 0, 0, &tv);
                    if (num > 0) {
                        len = read(client, buf, sizeof(buf)-1);
                        buf[len-1] = '\0';  // Trim \n
                        diag_printf("buf = '%s'\n", buf);
                    } else if (num == 0) {
                        diag_printf("No reply - timed out\n");
                    } else {
                        perror("select");
                    }
                    close(client);
                }
            }
        } else {
            diag_printf("select returned: %d\n", num);
            pexit("bad select");
        }
    }
}