printf("adding client info %s\n", domains[z].clients[f].info.noip.name);
int main(int argc, char **argv) {    struct addrinfo hints, *bind_address;    int ret, sockfd, max_fd;    struct domain domains[MAX_DOMAINS];    unsigned num_domains = 1, z, w, next_client_id = 1;    struct sigaction sa;    fd_set rfds;    (void)argc;    (void)argv;    /* create domain 0 */    memset(domains, 0, sizeof(domains[0]));    /* server socket stuff */    memset(&hints, 0, sizeof(hints));    hints.ai_family = PF_INET;    hints.ai_socktype = SOCK_STREAM;    ret = getaddrinfo_helper("172.30.3.1", 2000, &hints, &bind_address);    if (ret < 0) {        fprintf(stderr, "getaddrinfo_helper failed: %s\n",                strerror(errno));        exit(1);    }    sockfd = socket(PF_INET, SOCK_STREAM, 0);    if (sockfd < 0) {        fprintf(stderr, "failed to create socket: %s\n",                strerror(errno));        exit(1);    }    ret = bind(sockfd, bind_address->ai_addr,               bind_address->ai_addrlen);    if (ret < 0) {        fprintf(stderr, "failed to bind: %s\n",                strerror(errno));        exit(1);    }    ret = listen(sockfd, 4);    if (ret < 0) {        fprintf(stderr, "listen failed: %s\n",                strerror(errno));        exit(1);    }    /* signals */    memset(&sa, 0, sizeof(sa));    sa.sa_handler = exit_signal_handler;    sigaction(SIGHUP, &sa, NULL);    sigaction(SIGTERM, &sa, NULL);    sigaction(SIGINT, &sa, NULL);    sigaction(SIGQUIT, &sa, NULL);    sa.sa_handler = SIG_IGN;    sigaction(SIGUSR1, &sa, NULL);    sigaction(SIGUSR2, &sa, NULL);    /* main loop */    do {        if (verbose > 0)            printf("select()\n");        FD_ZERO(&rfds);        FD_SET(sockfd, &rfds);        max_fd = sockfd;        for (z = 0; z < num_domains; z++) {            for (w = 0; w < domains[z].num_clients; w++) {                FD_SET(domains[z].clients[w].sockfd, &rfds);                if (domains[z].clients[w].sockfd > max_fd)                    max_fd = domains[z].clients[w].sockfd;            }        }        ret = select(max_fd + 1, &rfds, NULL, NULL, NULL);        if (ret < 0 && errno != EINTR) {            fprintf(stderr, "select failed: %s\n", strerror(errno));            exit(1);        }        if (verbose > 0)            printf("after select() = %d\n", ret);        if (ret > 0) {            if (FD_ISSET(sockfd, &rfds)) {                struct sockaddr addr;                socklen_t addrlen = sizeof(addr);                ret = accept(sockfd, &addr, &addrlen);                if (ret < 0) {                    fprintf(stderr, "accept failed: %s\n", strerror(errno));                    exit(1);                }                if (domains[0].num_clients < MAX_CLIENTS) {                    struct client *client = &domains[0].clients[domains[0].num_clients++];                    memset(client, 0, sizeof(*client));                    client->id = next_client_id++;                    client->sockfd = ret;                    printf("new client: %u\n", client->id);                } else {                    /* sorry, domain 0 is full */                    close(sockfd);                }            }            for (z = 0; z < num_domains; z++) {                struct client *client = domains[z].clients;                for (w = 0; w < domains[z].num_clients; w++, client++) {                    if (FD_ISSET(client->sockfd, &rfds)) {                        unsigned char buffer[4096], buffer2[4096];                        ssize_t nbytes, i;                        struct packet_header *header = (struct packet_header*)buffer;                        nbytes = recv(client->sockfd, buffer, sizeof(buffer), 0);                        printf("packet: client=%u/%u len=%ld\n", z, w, (long)nbytes);                        for (i = 0; i < nbytes; i++) {                            printf("%02x ", buffer[i]);                            if (i % 16 == 15)                                printf("\n");                        }                        printf("\n\n");                        if (nbytes == 0) {                            printf("client disconnected\n");                            kill_client(&domains[z], w--);                            client--;                            continue;                        }                        if (nbytes < 16)                            continue;                        if (header->five != 0x05 || header->zero1 != 0x00 ||                            header->three != 0x03 || header->ten != 0x10) {                            printf("wrong packet, killing client\n");                            kill_client(&domains[z], w--);                            client--;                            continue;                        }                        switch (header->type) {                        case 0x0b:                            client->handshake = 1;                            respond(client, buffer,                                    packet_handshake_response,                                    sizeof(packet_handshake_response));                            break;                        case 0x00:                            if (buffer[22] == 0x02) {                                /* 00 00 02 00: client polls */                                size_t pos;                                unsigned f, num = 0;                                memcpy(buffer2, packet_poll, sizeof(packet_poll));                                pos = sizeof(packet_poll);                                printf("making poll packet pos=%u: %u\n", pos, domains[z].num_clients);                                for (f = 0; f < domains[z].num_clients; f++) {                                    if (domains[z].clients[f].info.noip.name[0] == 0)                                        continue;                                    printf("adding client info %s\n", domains[z].clients[f].info.noip.name);                                    memcpy(buffer2 + pos, &domains[z].clients[f].info.noip,                                           sizeof(domains[z].clients[f].info.noip));                                    num++;                                    pos += sizeof(client->info.noip);                                }                                printf("after client loop pos=%u\n", pos);                                buffer2[24] = (unsigned char)num;                                buffer2[32] = (unsigned char)num;                                memset(buffer2 + pos, 0, 4);                                pos += 4;                                buffer2[8] = pos & 0xff;                                buffer2[9] = (pos >> 8) & 0xff;                                buffer2[10] = 0;                                buffer2[11] = 0;                                buffer2[16] = (pos - 24) & 0xff;                                buffer2[17] = ((pos - 24) >> 8) & 0xff;                                buffer2[18] = 0;                                buffer2[19] = 0;                                respond(client, buffer, buffer2, pos);                            } else if (buffer[20] == 0x01 && buffer[22] == 0x01) {                                /* 01 00 01 00: poll chat */                                printf("poll_chat num=%u\n", client->num_chats);                                if (client->num_chats > 0) {                                    size_t pos;                                    memcpy(buffer2, packet_chat, sizeof(packet_chat));                                    pos = sizeof(packet_chat);                                    memcpy(buffer2 + pos, client->chats[0]->data,                                           client->chats[0]->size);                                    pos += client->chats[0]->size;                                    free(client->chats[0]);                                    client->num_chats--;                                    if (client->num_chats > 0)                                        memmove(client->chats, client->chats + 1,                                                sizeof(client->chats[0]) * client->num_chats);                                    memset(buffer2 + pos, 0, 5);                                    pos += 5;                                    buffer2[8] = pos & 0xff;                                    buffer2[9] = (pos >> 8) & 0xff;                                    buffer2[10] = 0;                                    buffer2[11] = 0;                                    buffer2[16] = (pos - 24) & 0xff;                                    buffer2[17] = ((pos - 24) >> 8) & 0xff;                                    buffer2[18] = 0;                                    buffer2[19] = 0;                                    respond(client, buffer, buffer2, pos);                                } else {                                    respond(client, buffer,                                            packet_ack2,                                            sizeof(packet_ack2));                                }                            } else if (buffer[20] == 0x01) {                                /* 01 00 00 00: client sends chat message */                                if (buffer[52] == 0x01)                                    enqueue_chat(&domains[z], buffer + 60,                                                 (size_t)nbytes - 60);                                respond(client, buffer,                                        packet_ack,                                        sizeof(packet_ack));                            } else {                                /* 00 00 10 00 or 00 00 00 00: client sends player position */                                memcpy(&client->info, buffer + 44, sizeof(client->info));                                respond(client, buffer,                                        packet_ack,                                        sizeof(packet_ack));                            }                            break;                        case 0x0e:                            respond(client, buffer,                                    packet_response2,                                    sizeof(packet_response2));                            break;                        }                    }                }            }        }    } while (!should_exit);    /* cleanup */    close(sockfd);    for (z = 0; z < num_domains; z++) {        struct client *client = domains[z].clients;        for (w = 0; w < domains[z].num_clients; w++, client++) {            close(client->sockfd);        }    }}