Example #1
0
int main(int argc, char *argv[])
{
	char *conf_filename;
	int result;
	char *op_type;
	char *tracker_server;
	int arg_index;
	char *group_name;

	if (argc < 2)
	{
		usage(argv);
		return 1;
	}

	tracker_server = NULL;
	conf_filename = argv[1];
	arg_index = 2;

	if (arg_index >= argc)
	{
		op_type = "list";
	}
	else
	{
		int len;

		len = strlen(argv[arg_index]); 
		if (len >= 2 && strncmp(argv[arg_index], "-h", 2) == 0)
		{
			if (len == 2)
			{
				arg_index++;
				if (arg_index >= argc)
				{
					usage(argv);
					return 1;
				}

				tracker_server = argv[arg_index++];
			}
			else
			{
				tracker_server = argv[arg_index] + 2;
				arg_index++;
			}

			if (arg_index < argc)
			{
				op_type = argv[arg_index++];
			}
			else
			{
				op_type = "list";
			}
		}
		else
		{
			op_type = argv[arg_index++];
		}
	}

	log_init();
	g_log_context.log_level = LOG_DEBUG;
	ignore_signal_pipe();

	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}
	load_log_level_ex(conf_filename);

	if (tracker_server == NULL)
	{
		if (g_tracker_group.server_count > 1)
		{
			srand(time(NULL));
			rand();  //discard the first
			g_tracker_group.server_index = (int)( \
				(g_tracker_group.server_count * (double)rand()) \
				/ (double)RAND_MAX);
		}
	}
	else
	{
		int i;
		char ip_addr[IP_ADDRESS_SIZE];

		*ip_addr = '\0';
		if (getIpaddrByName(tracker_server, ip_addr, sizeof(ip_addr)) \
			 == INADDR_NONE)
		{
			printf("resolve ip address of tracker server: %s " \
				"fail!\n", tracker_server);
			return 2;
		}

		for (i=0; i<g_tracker_group.server_count; i++)
		{
			if (strcmp(g_tracker_group.servers[i].ip_addr, \
					ip_addr) == 0)
			{
				g_tracker_group.server_index = i;
				break;
			}
		}

		if (i == g_tracker_group.server_count)
		{
			printf("tracker server: %s not exists!\n", tracker_server);
			return 2;
		}
	}

	printf("server_count=%d, server_index=%d\n", g_tracker_group.server_count, g_tracker_group.server_index);

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}
	printf("\ntracker server is %s:%d\n\n", pTrackerServer->ip_addr, pTrackerServer->port);

	if (arg_index < argc)
	{
		group_name = argv[arg_index++];
	}
	else
	{
		group_name = NULL;
	}

	if (strcmp(op_type, "list") == 0)
	{
		if (group_name == NULL)
		{
			result = list_all_groups(NULL);
		}
		else
		{
			result = list_all_groups(group_name);
		}
	}
	else if (strcmp(op_type, "delete") == 0)
	{
		char *storage_id;
		if (arg_index >= argc)
		{
			usage(argv);
			return 1;
		}

		storage_id = argv[arg_index++];

		if ((result=tracker_delete_storage(&g_tracker_group, \
				group_name, storage_id)) == 0)
		{
			printf("delete storage server %s::%s success\n", \
				group_name, storage_id);
		}
		else
		{
			printf("delete storage server %s::%s fail, " \
				"error no: %d, error info: %s\n", \
				group_name, storage_id, \
				result, STRERROR(result));
		}
	}
	else if (strcmp(op_type, "set_trunk_server") == 0)
	{
		char *storage_id;
		char new_trunk_server_id[FDFS_STORAGE_ID_MAX_SIZE];

		if (group_name == NULL)
		{
			usage(argv);
			return 1;
		}
		if (arg_index >= argc)
		{
			storage_id = "";
		}
		else
		{
			storage_id = argv[arg_index++];
		}

		if ((result=tracker_set_trunk_server(&g_tracker_group, \
			group_name, storage_id, new_trunk_server_id)) == 0)
		{
			printf("set trunk server %s::%s success, " \
				"new trunk server: %s\n", group_name, \
				storage_id, new_trunk_server_id);
		}
		else
		{
			printf("set trunk server %s::%s fail, " \
				"error no: %d, error info: %s\n", \
				group_name, storage_id, \
				result, STRERROR(result));
		}
	}
	else
	{
		printf("Invalid command %s\n\n", op_type);
		usage(argv);
	}

	tracker_disconnect_server_ex(pTrackerServer, true);
	fdfs_client_destroy();
	return 0;
}
Example #2
0
int load_allow_hosts(IniContext *pIniContext, \
                     in_addr_t **allow_ip_addrs, int *allow_ip_count)
{
    int count;
    IniItem *pItem;
    IniItem *pItemStart;
    IniItem *pItemEnd;
    char *pItemValue;
    char *pStart;
    char *pEnd;
    char *p;
    char *pTail;
    int alloc_count;
    int nHeadLen;
    int i;
    in_addr_t addr;
    char hostname[256];

    if ((pItemStart=iniGetValuesEx(NULL, "allow_hosts", \
                                   pIniContext, &count)) == NULL)
    {
        *allow_ip_count = -1; /* -1 means match any ip address */
        *allow_ip_addrs = NULL;
        return 0;
    }

    pItemEnd = pItemStart + count;
    for (pItem=pItemStart; pItem<pItemEnd; pItem++)
    {
        if (strcmp(pItem->value, "*") == 0)
        {
            *allow_ip_count = -1; /* -1 means match any ip address*/
            *allow_ip_addrs = NULL;
            return 0;
        }
    }

    alloc_count = count;
    *allow_ip_count = 0;
    *allow_ip_addrs = (in_addr_t *)malloc(sizeof(in_addr_t) * alloc_count);
    if (*allow_ip_addrs == NULL)
    {
        logError("file: "__FILE__", line: %d, " \
                 "malloc %d bytes fail, errno: %d, error info: %s.", \
                 __LINE__, (int)sizeof(in_addr_t) * alloc_count, \
                 errno, STRERROR(errno));
        return errno != 0 ? errno : ENOMEM;
    }

    for (pItem=pItemStart; pItem<pItemEnd; pItem++)
    {
        if (*(pItem->value) == '\0')
        {
            continue;
        }

        pStart = strchr(pItem->value, '[');
        if (pStart == NULL)
        {
            addr = getIpaddrByName(pItem->value, NULL, 0);
            if (addr == INADDR_NONE)
            {
                logWarning("file: "__FILE__", line: %d, " \
                           "invalid host name: %s", \
                           __LINE__, pItem->value);
            }
            else
            {
                if (alloc_count < (*allow_ip_count) + 1)
                {
                    alloc_count = (*allow_ip_count) + \
                                  (pItemEnd - pItem);
                    *allow_ip_addrs = (in_addr_t *)realloc(
                                          *allow_ip_addrs,
                                          sizeof(in_addr_t)*alloc_count);
                    if (*allow_ip_addrs == NULL)
                    {
                        logError("file: "__FILE__", line: %d, "\
                                 "malloc %d bytes fail, " \
                                 "errno: %d, error info: %s", \
                                 __LINE__, (int)sizeof(in_addr_t)
                                 * alloc_count, \
                                 errno, STRERROR(errno));

                        return errno != 0 ? errno : ENOMEM;
                    }
                }

                (*allow_ip_addrs)[*allow_ip_count] = addr;
                (*allow_ip_count)++;
            }

            continue;
        }


        pEnd = strchr(pStart, ']');
        if (pEnd == NULL)
        {
            logWarning("file: "__FILE__", line: %d, " \
                       "invalid host name: %s, expect \"]\"", \
                       __LINE__, pItem->value);
            continue;
        }

        pItemValue = strdup(pItem->value);
        if (pItemValue == NULL)
        {
            logWarning("file: "__FILE__", line: %d, " \
                       "strdup fail, " \
                       "errno: %d, error info: %s.", \
                       __LINE__, errno, STRERROR(errno));
            continue;
        }

        nHeadLen = pStart - pItem->value;
        pStart = pItemValue + nHeadLen;
        pEnd = pItemValue + (pEnd - pItem->value);
        pTail = pEnd + 1;

        memcpy(hostname, pItem->value, nHeadLen);
        p = pStart + 1;  //skip [

        while (p <= pEnd)
        {
            char *pNumStart1;
            char *pNumStart2;
            int nStart;
            int nEnd;
            int nNumLen1;
            int nNumLen2;
            char end_ch1;
            char end_ch2;
            char szFormat[16];

            while (*p == ' ' || *p == '\t') //trim prior spaces
            {
                p++;
            }

            pNumStart1 = p;
            while (*p >='0' && *p <= '9')
            {
                p++;
            }

            nNumLen1 = p - pNumStart1;
            while (*p == ' ' || *p == '\t') //trim tail spaces
            {
                p++;
            }

            if (!(*p == ',' || *p == '-' || *p == ']'))
            {
                logWarning("file: "__FILE__", line: %d, " \
                           "invalid char \"%c\" in host name: %s",\
                           __LINE__, *p, pItem->value);
                break;
            }

            end_ch1 = *p;
            *(pNumStart1 + nNumLen1) = '\0';

            if (nNumLen1 == 0)
            {
                logWarning("file: "__FILE__", line: %d, " \
                           "invalid host name: %s, " \
                           "empty entry before \"%c\"", \
                           __LINE__, pItem->value, end_ch1);
                break;
            }

            nStart = atoi(pNumStart1);
            if (end_ch1 == '-')
            {
                p++;   //skip -

                /* trim prior spaces */
                while (*p == ' ' || *p == '\t')
                {
                    p++;
                }

                pNumStart2 = p;
                while (*p >='0' && *p <= '9')
                {
                    p++;
                }

                nNumLen2 = p - pNumStart2;
                /* trim tail spaces */
                while (*p == ' ' || *p == '\t')
                {
                    p++;
                }

                if (!(*p == ',' || *p == ']'))
                {
                    logWarning("file: "__FILE__", line: %d, " \
                               "invalid char \"%c\" in host name: %s",\
                               __LINE__, *p, pItem->value);
                    break;
                }

                end_ch2 = *p;
                *(pNumStart2 + nNumLen2) = '\0';

                if (nNumLen2 == 0)
                {
                    logWarning("file: "__FILE__", line: %d, " \
                               "invalid host name: %s, " \
                               "empty entry before \"%c\"", \
                               __LINE__, pItem->value, end_ch2);
                    break;
                }

                nEnd = atoi(pNumStart2);
            }
            else
            {
                nEnd = nStart;
            }

            if (alloc_count < *allow_ip_count+(nEnd - nStart + 1))
            {
                alloc_count = *allow_ip_count + (nEnd - nStart)
                              + (pItemEnd - pItem);
                *allow_ip_addrs = (in_addr_t *)realloc(
                                      *allow_ip_addrs,
                                      sizeof(in_addr_t)*alloc_count);
                if (*allow_ip_addrs == NULL)
                {
                    logError("file: "__FILE__", line: %d, "\
                             "malloc %d bytes fail, " \
                             "errno: %d, error info: %s.", \
                             __LINE__, \
                             (int)sizeof(in_addr_t) * \
                             alloc_count,\
                             errno, STRERROR(errno));

                    free(pItemValue);
                    return errno != 0 ? errno : ENOMEM;
                }
            }

            sprintf(szFormat, "%%0%dd%%s",  nNumLen1);
            for (i=nStart; i<=nEnd; i++)
            {
                sprintf(hostname + nHeadLen, szFormat, \
                        i, pTail);

                addr = getIpaddrByName(hostname, NULL, 0);
                if (addr == INADDR_NONE)
                {
                    logWarning("file: "__FILE__", line: %d, " \
                               "invalid host name: %s", \
                               __LINE__, hostname);
                }
                else
                {
                    (*allow_ip_addrs)[*allow_ip_count]=addr;
                    (*allow_ip_count)++;
                }

            }

            p++;
        }

        free(pItemValue);
    }

    if (*allow_ip_count == 0)
    {
        logWarning("file: "__FILE__", line: %d, " \
                   "allow ip count: 0", __LINE__);
    }

    if (*allow_ip_count > 0)
    {
        qsort(*allow_ip_addrs,  *allow_ip_count, sizeof(in_addr_t), \
              cmp_by_ip_addr_t);
    }

    /*
    printf("*allow_ip_count=%d\n", *allow_ip_count);
    for (i=0; i<*allow_ip_count; i++)
    {
    	struct in_addr address;
    	address.s_addr = (*allow_ip_addrs)[i];
    	printf("%s\n", inet_ntoa(address));
    }
    */

    return 0;
}
Example #3
0
int fdfs_load_storage_ids(char *content, const char *pStorageIdsFilename)
{
	char **lines;
	char *line;
	char *id;
	char *group_name;
	char *pHost;
	char *pIpAddr;
	char *pPort;
	FDFSStorageIdInfo *pStorageIdInfo;
	FDFSStorageIdInfo **ppStorageIdInfo;
	FDFSStorageIdInfo **ppStorageIdEnd;
	int alloc_bytes;
	int result;
	int line_count;
	int i;

	lines = split(content, '\n', 0, &line_count);
	if (lines == NULL)
	{
		return ENOMEM;
	}

	result = 0;
	do
	{
		g_storage_id_count = 0;
		for (i=0; i<line_count; i++)
		{
			trim(lines[i]);
			if (*lines[i] == '\0' || *lines[i] == '#')
			{
				continue;
			}
			g_storage_id_count++;
		}

		if (g_storage_id_count == 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"config file: %s, no storage id!", \
				__LINE__, pStorageIdsFilename);
			result = ENOENT;
			break;
		}

		alloc_bytes = sizeof(FDFSStorageIdInfo) * g_storage_id_count;
		g_storage_ids_by_ip = (FDFSStorageIdInfo *)malloc(alloc_bytes);
		if (g_storage_ids_by_ip == NULL)
		{
			result = errno != 0 ? errno : ENOMEM;
			logError("file: "__FILE__", line: %d, " \
				"malloc %d bytes fail, " \
				"errno: %d, error info: %s", __LINE__, \
				alloc_bytes, result, STRERROR(result));
			break;
		}
		memset(g_storage_ids_by_ip, 0, alloc_bytes);

		alloc_bytes = sizeof(FDFSStorageIdInfo *) * g_storage_id_count;
		g_storage_ids_by_id = (FDFSStorageIdInfo **)malloc(alloc_bytes);
		if (g_storage_ids_by_id == NULL)
		{
			result = errno != 0 ? errno : ENOMEM;
			logError("file: "__FILE__", line: %d, " \
				"malloc %d bytes fail, " \
				"errno: %d, error info: %s", __LINE__, \
				alloc_bytes, result, STRERROR(result));
			free(g_storage_ids_by_ip);
			break;
		}
		memset(g_storage_ids_by_id, 0, alloc_bytes);

		pStorageIdInfo = g_storage_ids_by_ip;
		for (i=0; i<line_count; i++)
		{
			line = lines[i];
			if (*line == '\0' || *line == '#')
			{
				continue;
			}

			id = line;
			group_name = line;
			while (!(*group_name == ' ' || *group_name == '\t' \
				|| *group_name == '\0'))
			{
				group_name++;
			}

			if (*group_name == '\0')
			{
				logError("file: "__FILE__", line: %d, " \
					"config file: %s, line no: %d, " \
					"content: %s, invalid format, " \
					"expect group name and ip address!", \
					__LINE__, pStorageIdsFilename, \
					i + 1, line);
				result = EINVAL;
				break;
			}

			*group_name = '\0';
			group_name++;  //skip space char
			while (*group_name == ' ' || *group_name == '\t')
			{
				group_name++;
			}
		
			pHost = group_name;
			while (!(*pHost == ' ' || *pHost == '\t' \
				|| *pHost == '\0'))
			{
				pHost++;
			}

			if (*pHost == '\0')
			{
				logError("file: "__FILE__", line: %d, " \
					"config file: %s, line no: %d, " \
					"content: %s, invalid format, " \
					"expect ip address!", __LINE__, \
					pStorageIdsFilename, i + 1, line);
				result = EINVAL;
				break;
			}

			*pHost = '\0';
			pHost++;  //skip space char
			while (*pHost == ' ' || *pHost == '\t')
			{
				pHost++;
			}

            pIpAddr = pHost;
            pPort = strchr(pHost, ':');
            if (pPort != NULL)
            {
                *pPort = '\0';
                pStorageIdInfo->port = atoi(pPort + 1);
            }
            else
            {
                pStorageIdInfo->port = 0;
            }
			if (getIpaddrByName(pIpAddr, pStorageIdInfo->ip_addr, \
				sizeof(pStorageIdInfo->ip_addr)) == INADDR_NONE)
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid host name: %s", __LINE__, pIpAddr);
				result = EINVAL;
				break;
			}

			if (!fdfs_is_server_id_valid(id))
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid server id: \"%s\", " \
					"which must be a none zero start " \
					"integer, such as 100001", __LINE__, id);
				result = EINVAL;
				break;
			}

			snprintf(pStorageIdInfo->id, \
				sizeof(pStorageIdInfo->id), "%s", id);
			snprintf(pStorageIdInfo->group_name, \
				sizeof(pStorageIdInfo->group_name), \
				"%s", group_name);
			pStorageIdInfo++;
		}
	} while (0);

	freeSplit(lines);
	if (result != 0)
	{
		return result;
	}

	logDebug("file: "__FILE__", line: %d, " \
		"g_storage_id_count: %d", __LINE__, g_storage_id_count);
	pStorageIdInfo = g_storage_ids_by_ip;
	for (i=0; i<g_storage_id_count; i++)
	{
        char szPortPart[16];
        if (pStorageIdInfo->port > 0)
        {
            sprintf(szPortPart, ":%d", pStorageIdInfo->port);
        }
        else
        {
            *szPortPart = '\0';
        }
		logDebug("%s  %s  %s%s", pStorageIdInfo->id,
			pStorageIdInfo->group_name,
			pStorageIdInfo->ip_addr, szPortPart);

		pStorageIdInfo++;
	}
	
	ppStorageIdEnd = g_storage_ids_by_id + g_storage_id_count;
	pStorageIdInfo = g_storage_ids_by_ip;
	for (ppStorageIdInfo=g_storage_ids_by_id; ppStorageIdInfo < \
		ppStorageIdEnd; ppStorageIdInfo++)
	{
		*ppStorageIdInfo = pStorageIdInfo++;
	}

	qsort(g_storage_ids_by_ip, g_storage_id_count, \
		sizeof(FDFSStorageIdInfo), fdfs_cmp_group_name_and_ip);
	qsort(g_storage_ids_by_id, g_storage_id_count, \
		sizeof(FDFSStorageIdInfo *), fdfs_cmp_server_id);

	return result;
}
Example #4
0
int get_url_content_ex(const char *url, const int url_len,
        const int connect_timeout, const int network_timeout,
        int *http_status, char **content, int *content_len, char *error_info)
{
	char domain_name[256];
	char ip_addr[IP_ADDRESS_SIZE];
	char out_buff[4096];
	int domain_len;
	int out_len;
	int alloc_size;
	int recv_bytes;
	int result;
	int sock;
	int port;
    bool bNeedAlloc;
	const char *pDomain;
	const char *pContent;
	const char *pURI;
	char *pPort;
	char *pSpace;

	*http_status = 0;
    if (*content == NULL)
    {
        bNeedAlloc = true;
        alloc_size = 64 * 1024;
    }
    else
    {
        bNeedAlloc = false;
        alloc_size = *content_len - 1;
    }
	*content_len = 0;

	if (url_len <= 7 || strncasecmp(url, "http://", 7) != 0)
	{
		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"invalid url.", __LINE__);
		return EINVAL;
	}

	pDomain = url + 7;
	pURI = strchr(pDomain, '/');
	if (pURI == NULL)
	{
		domain_len = url_len - 7;
		pURI = "/";
	}
	else
	{
		domain_len = pURI - pDomain;
	}

	if (domain_len >= sizeof(domain_name))
	{
		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"domain is too large, exceed %d.", \
			__LINE__, (int)sizeof(domain_name));
		return EINVAL;
	}

	memcpy(domain_name, pDomain, domain_len);
	*(domain_name + domain_len) = '\0';
	pPort = strchr(domain_name, ':');
	if (pPort == NULL)
	{
		port = 80;
	}
	else
	{
		*pPort = '\0';
		port = atoi(pPort + 1);
	}

	if (getIpaddrByName(domain_name, ip_addr, \
		sizeof(ip_addr)) == INADDR_NONE)
	{
		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"resolve domain \"%s\" fail.", \
			__LINE__, domain_name);
		return EINVAL;
	}

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock < 0)
	{
		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"socket create failed, errno: %d, " \
			"error info: %s", __LINE__, \
			errno, STRERROR(errno));
		return errno != 0 ? errno : EPERM;
	}

	if ((result=connectserverbyip_nb_auto(sock, ip_addr, port, \
			connect_timeout)) != 0)
	{
		close(sock);

		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"connect to %s:%d fail, errno: %d, " \
			"error info: %s", __LINE__, domain_name, \
			port, result, STRERROR(result));

		return result;
	}

	out_len = snprintf(out_buff, sizeof(out_buff), \
		"GET %s HTTP/1.0\r\n" \
		"Host: %s:%d\r\n" \
		"Connection: close\r\n" \
		"\r\n", pURI, domain_name, port);
	if ((result=tcpsenddata(sock, out_buff, out_len, network_timeout)) != 0)
	{
		close(sock);

		sprintf(error_info, "file: "__FILE__", line: %d, " \
			"send data to %s:%d fail, errno: %d, " \
			"error info: %s", __LINE__, domain_name, \
			port, result, STRERROR(result));

		return result;
	}

    if (bNeedAlloc)
    {
        *content = (char *)malloc(alloc_size + 1);
        if (*content == NULL)
        {
            close(sock);
            result = errno != 0 ? errno : ENOMEM;

            sprintf(error_info, "file: "__FILE__", line: %d, " \
                    "malloc %d bytes fail, errno: %d, " \
                    "error info: %s", __LINE__, alloc_size + 1, \
                    result, STRERROR(result));

            return result;
        }
    }

	do
	{
		recv_bytes = alloc_size - *content_len;
		if (recv_bytes <= 0)
		{
            if (bNeedAlloc)
            {
                alloc_size *= 2;
                *content = (char *)realloc(*content, alloc_size + 1);
                if (*content == NULL)
                {
                    *content_len = 0;
                    close(sock);
                    result = errno != 0 ? errno : ENOMEM;

                    sprintf(error_info, "file: "__FILE__", line: %d, " \
                            "realloc %d bytes fail, errno: %d, " \
                            "error info: %s", __LINE__, \
                            alloc_size + 1, \
                            result, STRERROR(result));

                    return result;
                }

                recv_bytes = alloc_size - *content_len;
            }
            else
            {
                    sprintf(error_info, "file: "__FILE__", line: %d, " \
                            "buffer size: %d is too small", \
                            __LINE__, alloc_size);
                    return ENOSPC;
            }
		}

		result = tcprecvdata_ex(sock, *content + *content_len, \
				recv_bytes, network_timeout, &recv_bytes);

		*content_len += recv_bytes;
	} while (result == 0);

    do
    {
        if (result == ENOTCONN)
        {
            result = 0;
        }
        else {
            sprintf(error_info, "file: "__FILE__", line: %d, " \
                    "recv data from %s:%d fail, errno: %d, " \
                    "error info: %s", __LINE__, domain_name, \
                    port, result, STRERROR(result));

            break;
        }

        *(*content + *content_len) = '\0';
        pContent = strstr(*content, "\r\n\r\n");
        if (pContent == NULL)
        {
            sprintf(error_info, "file: "__FILE__", line: %d, " \
                    "response data from %s:%d is invalid", \
                    __LINE__, domain_name, port);

            result = EINVAL;
            break;
        }

        pContent += 4;
        pSpace = strchr(*content, ' ');
        if (pSpace == NULL || pSpace >= pContent)
        {
            sprintf(error_info, "file: "__FILE__", line: %d, " \
                    "response data from %s:%d is invalid", \
                    __LINE__, domain_name, port);

            result = EINVAL;
            break;
        }

        *http_status = atoi(pSpace + 1);
        *content_len -= pContent - *content;
        memcpy(*content, pContent, *content_len);
        *(*content + *content_len) = '\0';
        *error_info = '\0';
    } while (0);

	close(sock);
    if (result != 0 && bNeedAlloc)
    {
        free(*content);
        *content = NULL;
        *content_len = 0;
    }

	return result;
}
Example #5
0
int load_allow_hosts(IniContext *pIniContext, \
		in_addr_t **allow_ip_addrs, int *allow_ip_count)
{
	int result;
	int count;
	IniItem *pItem;
	IniItem *pItemStart;
	IniItem *pItemEnd;
	char item_value[256];
	char *pStart;
	char *pEnd;
	int alloc_count;
	int nHeadLen;
	int nValueLen;
	int i;
	in_addr_t addr;
	char hostname[256];

	if ((pItemStart=iniGetValuesEx(NULL, "allow_hosts", \
		pIniContext, &count)) == NULL)
	{
		*allow_ip_count = -1; /* -1 means match any ip address */
		*allow_ip_addrs = NULL;
		return 0;
	}

	pItemEnd = pItemStart + count;
	for (pItem=pItemStart; pItem<pItemEnd; pItem++)
	{
		if (strcmp(pItem->value, "*") == 0)
		{
			*allow_ip_count = -1; /* -1 means match any ip address*/
			*allow_ip_addrs = NULL;
			return 0;
		}
	}

	alloc_count = count;
	*allow_ip_count = 0;
	*allow_ip_addrs = (in_addr_t *)malloc(sizeof(in_addr_t) * alloc_count);
	if (*allow_ip_addrs == NULL)
	{
		logError("file: "__FILE__", line: %d, " \
			"malloc %d bytes fail, errno: %d, error info: %s.", \
			__LINE__, (int)sizeof(in_addr_t) * alloc_count, \
			errno, STRERROR(errno));
		return errno != 0 ? errno : ENOMEM;
	}

	for (pItem=pItemStart; pItem<pItemEnd; pItem++)
	{
		if (*(pItem->value) == '\0')
		{
			continue;
		}

		pStart = strchr(pItem->value, '[');
		if (pStart == NULL)
		{
			if (strchr(pItem->value, '/') != NULL) //CIDR addresses
			{
				if ((result=parse_cidr_ips(pItem->value,
					allow_ip_addrs, &alloc_count,
					allow_ip_count, pItemEnd - pItem)) != 0)
				{
					return result;
				}
				continue;
			}

			addr = getIpaddrByName(pItem->value, NULL, 0);
			if (addr == INADDR_NONE)
			{
				logWarning("file: "__FILE__", line: %d, " \
					"invalid host name: %s", \
					__LINE__, pItem->value);
			}
			else
			{
				if ((result=check_realloc_allow_ips(allow_ip_addrs,
					&alloc_count, (*allow_ip_count) + (pItemEnd - pItem))) != 0)
				{
					return result;
				}

				(*allow_ip_addrs)[*allow_ip_count] = addr;
				(*allow_ip_count)++;
			}

			continue;
		}

		
		pEnd = strchr(pStart, ']');
		if (pEnd == NULL)
		{
			logError("file: "__FILE__", line: %d, " \
				"invalid host name: %s, expect \"]\"", \
				__LINE__, pItem->value);
			return EINVAL;
		}

		nValueLen = strlen(pItem->value);
		if (nValueLen >= (int)sizeof(item_value))
		{
			logError("file: "__FILE__", line: %d, " \
				"hostname too long, exceeds %d bytes", \
				__LINE__, (int)sizeof(item_value));
			return EINVAL;
		}
		memcpy(item_value, pItem->value, nValueLen + 1);
		nHeadLen = pStart - pItem->value;
		memcpy(hostname, pItem->value, nHeadLen);

		result = parse_range_hosts(pItem->value, item_value + nHeadLen,
				item_value + (pEnd - pItem->value),
				hostname, nHeadLen, allow_ip_addrs,
				&alloc_count, allow_ip_count, pItemEnd - pItem);
		if (result != 0)
		{
			return result;
		}
	}

	if (*allow_ip_count == 0)
	{
		logWarning("file: "__FILE__", line: %d, " \
			"allow ip count: 0", __LINE__);
	}

	if (*allow_ip_count > 0)
	{
		qsort(*allow_ip_addrs,  *allow_ip_count, sizeof(in_addr_t), \
			cmp_by_ip_addr_t);
	}

	logDebug("allow_ip_count=%d", *allow_ip_count);
	for (i=0; i<*allow_ip_count; i++)
	{
		struct in_addr address;
		address.s_addr = (*allow_ip_addrs)[i];
		logDebug("%d. %s", i + 1, inet_ntoa(address));
	}

	return 0;
}
Example #6
0
static int parse_range_hosts(const char *value, char *pStart, char *pEnd,
	char *hostname, const int nHeadLen, in_addr_t **allow_ip_addrs,
	int *alloc_count, int *allow_ip_count, const int remain_items)
{
	char *pTail;
	char *p;
	int result;
	int i;
	in_addr_t addr;

	pTail = pEnd + 1;
	p = pStart + 1;  //skip [
	while (p <= pEnd)
	{
		char *pNumStart1;
		char *pNumStart2;
		int nStart;
		int nEnd;
		int nNumLen1;
		int nNumLen2;
		char end_ch1;
		char end_ch2;
		char szFormat[16];

		while (*p == ' ' || *p == '\t') //trim prior spaces
		{
			p++;
		}

		pNumStart1 = p;
		while (*p >='0' && *p <= '9')
		{
			p++;
		}

		nNumLen1 = p - pNumStart1;
		while (*p == ' ' || *p == '\t') //trim tail spaces
		{
			p++;
		}

		if (!(*p == ',' || *p == '-' || *p == ']'))
		{
			logError("file: "__FILE__", line: %d, " \
				"invalid char \"%c\" in host name: %s",\
				__LINE__, *p, value);
			return EINVAL;
		}

		end_ch1 = *p;
		*(pNumStart1 + nNumLen1) = '\0';

		if (nNumLen1 == 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"invalid host name: %s, " \
				"empty entry before \"%c\"", \
				__LINE__, value, end_ch1);
			return EINVAL;
		}

		nStart = atoi(pNumStart1);
		if (end_ch1 == '-')
		{
			p++;   //skip -

			/* trim prior spaces */
			while (*p == ' ' || *p == '\t')
			{
				p++;
			}

			pNumStart2 = p;
			while (*p >='0' && *p <= '9')
			{
				p++;
			}

			nNumLen2 = p - pNumStart2;
			/* trim tail spaces */
			while (*p == ' ' || *p == '\t')
			{
				p++;
			}

			if (!(*p == ',' || *p == ']'))
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid char \"%c\" in host name: %s",\
					__LINE__, *p, value);
				return EINVAL;
			}

			end_ch2 = *p;
			*(pNumStart2 + nNumLen2) = '\0';

			if (nNumLen2 == 0)
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid host name: %s, " \
					"empty entry before \"%c\"", \
					__LINE__, value, end_ch2);
				return EINVAL;
			}

			nEnd = atoi(pNumStart2);
		}
		else
		{
			nEnd = nStart;
		}


		if ((result=check_realloc_allow_ips(allow_ip_addrs,
			alloc_count, (*allow_ip_count) + remain_items +
			(nEnd - nStart + 1))) != 0)
		{
			return result;
		}

		sprintf(szFormat, "%%0%dd%%s",  nNumLen1);
		for (i=nStart; i<=nEnd; i++)
		{
			sprintf(hostname + nHeadLen, szFormat, \
				i, pTail);

			addr = getIpaddrByName(hostname, NULL, 0);
			if (addr == INADDR_NONE)
			{
				logWarning("file: "__FILE__", line: %d, " \
					"invalid host name: %s", \
					__LINE__, hostname);
			}
			else
			{
				(*allow_ip_addrs)[*allow_ip_count] = addr;
				(*allow_ip_count)++;
			}

		}

		p++;
	}

	return 0;
}