#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void)
{
	FILE *ifp = NULL;
    struct hostent *he = NULL;
	//struct in_addr *inp = NULL;

    int i, j;
    int retval;
    int dflag;
    char *addr = NULL;

    for (i = 0; i < r_ss_num; i++) {
        dflag = 0;
        for (ii = start_ptr; strcmp(ii->m_pszServName, END_OF_SHM); ii++) {
            if (ii->m_pszDomainName != NULL && strcmp(ii->m_pszDomainName, "")) {
                if (!strcmp(ii->m_pszDomainName, (r_ss + i)->du.domain)) {
                    memcpy((char *)&(ii->ss), (char *)(r_ss + i), sizeof(struct svr_status));
                    dflag = 1;
                }
				else if (!strcmp(ii->m_pszDomainName, (r_ss + i)->du.hostname)) {
                    memcpy((char *)&(ii->ss), (char *)(r_ss + i), sizeof(struct svr_status));
                    dflag = 1;
                }
            }
        }

        // Check If IP 
		if (dflag == 0)
		{
	        //if (((retval = inet_aton((r_ss + i)->du.domain, inp)) != 0) && dflag == 0) {
			if (((retval = inet_addr((r_ss + i)->du.domain)) != INADDR_NONE) && dflag == 0) {
			    for (ii = start_ptr; strcmp(ii->m_pszServName, END_OF_SHM); ii++) {
				    if (!strcmp(ii->m_szIpString, (r_ss + i)->du.domain)) {
					    strncpy(ii->m_pszDomainName, (r_ss + i)->du.domain, MAX_DOMAIN_NAME);
						memcpy((char *)&(ii->ss), (char *)(r_ss + i), sizeof(struct svr_status));
	                    dflag = 1;
		            }
			    }
	        }
		}

        // Check If Domain
		if (dflag == 0)
		{
			addr = ((r_ss + i)->du.hostname[0] == '\0') ? (r_ss + i)->du.domain : (r_ss + i)->du.hostname;
	        if (addr != NULL)
		        he = gethostbyname(addr);
			if (!he)
	            continue;
		}

        if (dflag == 0)
		{
            for (ii = start_ptr; strcmp(ii->m_pszServName, END_OF_SHM); ii++)
            {
                for (j = 0; he->h_addr_list[j]; j++)
                {
                    if (!strcmp(ii->m_szIpString, inet_ntoa(*(struct in_addr *)he->h_addr_list[j])))
                    {
						snprintf(logString, 1024, "[+++++] ServName : %s , IPADDR : %s , DomainName : %s", ii->m_pszServName, ii->m_szIpString, addr);
						WriteSHLog(logString, shLogFile);
                        strncpy(ii->m_pszDomainName, addr, MAX_DOMAIN_NAME);
                        memcpy((char *)&(ii->ss), (char *)(r_ss + i), sizeof(struct svr_status));
                    }
                }
            }
        }
    }
	fclose(shLogFile);
    munmap((void *)start_ptr, shm_size);
}

int get_sizeof_shm()
{
	struct stat stat;

	int memsize;
	int filed;

	/***        Posix Share Memory        ***/
	filed = Shm_open(SHM_NAME, O_RDONLY, FILE_MODE);
	Fstat(filed, &stat);
	Close(filed);
	/* get size of mapping memory */
	memsize = stat.st_size;
	return memsize;
}

servinfo_t *get_shm_start_pointer()
{
	struct stat stat;
	servinfo_t *start_ptr;
	int fd;

	fd = Shm_open(SHM_NAME, O_RDWR, FILE_MODE);
	Fstat(fd, &stat);
	start_ptr = Mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	Close(fd);
	return start_ptr;
}

void view_header_packet(struct common_message_header *cheader)
{
    /******************************************************************************/
    /**************************** View receive packet(header) *********************/
    /******************************************************************************/
    fprintf(stderr, "Transaction ID -> [ %s ]\n", cheader->szTransactionID);
    fprintf(stderr, "Mode           -> [ 0x%04X ]\n", ntohl(cheader->ulMode));
    fprintf(stderr, "Issuer         -> [ %s ]\n", cheader->szIssuer);
    fprintf(stderr, "Service ID     -> [ %s ]\n", cheader->szServiceID);
    fprintf(stderr, "Result         -> [ 0x%04X ]\n", ntohl(cheader->ulResult));
    fprintf(stderr, "Body Length    -> [ %lu ]\n\n", (unsigned long)ntohl(cheader->ulBodyLength));
    fflush(stderr);
}

void view_serv_status_packet(struct svr_status *recv_serv)
{
    /******************************************************************************/
    /**************************** View receive packet(body) ***********************/
    /******************************************************************************/
    fprintf(stderr, "Time stamp     -> [ %ld ]\n", (long)recv_serv->stamp);
    fprintf(stderr, "Domain         -> [ %s ]\n", recv_serv->du.domain);
    fprintf(stderr, "Total          -> [ %llu ]\n", (recv_serv->du.total));
    fprintf(stderr, "Free           -> [ %llu ]\n", (recv_serv->du.free));
    fprintf(stderr, "Manual y/n     -> [ %d ]\n", (recv_serv->du.manual_disable));
    fprintf(stderr, "upload level   -> [ %d ]\n", (recv_serv->du.upload_level));
    fprintf(stderr, "disk backlog   -> [ %d ]\n", (recv_serv->du.disk_backlog));
    fprintf(stderr, "disk percent   -> [ %d ]\n", (recv_serv->du.disk_percent));
    fprintf(stderr, "conn establish -> [ %d ]\n", (recv_serv->du.connection_established));
    fprintf(stderr, "conn total     -> [ %d ]\n", (recv_serv->du.connection_total));
    fprintf(stderr, "traffic ip     -> [ %lu ]\n", (recv_serv->du.traffic_ip));
    fprintf(stderr, "traffic ib     -> [ %lu ]\n", (recv_serv->du.traffic_ib));
    fprintf(stderr, "traffic op     -> [ %lu ]\n", (recv_serv->du.traffic_op));
    fprintf(stderr, "traffic ob     -> [ %lu ]\n", (recv_serv->du.traffic_ob));
    fprintf(stderr, "Hostname       -> [ %s ]\n\n", recv_serv->du.hostname);
    fflush(stderr);
}
