import ipaddress


def get_ip_prefixes_from_config(config, services, ip_version):
    """Build a set of IP prefixes found in service configuration files.

    Arguments:
        config (obg): A configparser object which holds our configuration.
        services (list): A list of section names which are the name of the
        service checks.
        ip_version (int): IP protocol version

    Returns:
        A set of IP prefixes.

    """
    ip_prefixes = set()

    for service in services:
        ip_prefix = ipaddress.ip_network(config.get(service, 'ip_prefix'))
        if ip_prefix.version == ip_version:
            ip_prefixes.add(ip_prefix.with_prefixlen)

    return ip_prefixes