<commit_before>#!/usr/bin/python2 -O
# vim: fileencoding=utf-8
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2010-2016  <NAME> <<EMAIL>>
# Copyright (C) 2013-2016  <NAME>
#                              <<EMAIL>>
# Copyright (C) 2014-2016  <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

''' This module contains the NetVMMixin '''
import os
import re

import libvirt  # pylint: disable=import-error
import qubes
import qubes.events
import qubes.firewall
import qubes.exc


def _setter_mac(self, prop, value):
    ''' Helper for setting the MAC address '''
    # pylint: disable=unused-argument
    if not isinstance(value, basestring):
        raise ValueError('MAC address must be a string')
    value = value.lower()
    if re.match(r"^([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]$", value) is None:
        raise ValueError('Invalid MAC address value')
    return value


class NetVMMixin(qubes.events.Emitter):
    ''' Mixin containing network functionality '''
    mac = qubes.property('mac', type=str,
        default='00:16:3E:5E:6C:00',
        setter=_setter_mac,
        ls_width=17,
        doc='MAC address of the NIC emulated inside VM')

    # CORE2: swallowed uses_default_netvm
    netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
        default=(lambda self: self.app.default_fw_netvm if self.provides_network
            else self.app.default_netvm),
        ls_width=31,
        doc='''VM that provides network connection to this domain. When
            `None`, machine is disconnected. When absent, domain uses default
            NetVM.''')

    provides_network = qubes.property('provides_network', default=False,
        type=bool, setter=qubes.property.bool,
        doc='''If this domain can act as network provider (formerly known as
            NetVM or ProxyVM)''')

    firewall_conf = qubes.property('firewall_conf', type=str,
        default='firewall.xml')

    #
    # used in networked appvms or proxyvms (netvm is not None)
    #

    @qubes.tools.qvm_ls.column(width=15)
    @property
    def ip(self):
        '''IP address of this domain.'''
        if not self.is_networked():
            return None
        if self.netvm is not None:
            return self.netvm.get_ip_for_vm(self)  # pylint: disable=no-member
        else:
            return self.get_ip_for_vm(self)
<commit_msg>qubes/vm/net: allow setting custom IP<commit_after>#!/usr/bin/python2 -O
# vim: fileencoding=utf-8
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2010-2016  <NAME> <<EMAIL>>
# Copyright (C) 2013-2016  Marek Marczykowski-Górecki
#                              <<EMAIL>>
# Copyright (C) 2014-2016  <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

''' This module contains the NetVMMixin '''
import os
import re

import libvirt  # pylint: disable=import-error
import qubes
import qubes.events
import qubes.firewall
import qubes.exc


def _setter_mac(self, prop, value):
    ''' Helper for setting the MAC address '''
    # pylint: disable=unused-argument
    if not isinstance(value, basestring):
        raise ValueError('MAC address must be a string')
    value = value.lower()
    if re.match(r"^([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]$", value) is None:
        raise ValueError('Invalid MAC address value')
    return value


def _default_ip(self):
    if not self.is_networked():
        return None
    if self.netvm is not None:
        return self.netvm.get_ip_for_vm(self)  # pylint: disable=no-member
    else:
        return self.get_ip_for_vm(self)


def _setter_ip(self, prop, value):
    # pylint: disable=unused-argument
    if not isinstance(value, basestring):
        raise ValueError('IP address must be a string')
    value = value.lower()
    if re.match(r"^([0-9]{1,3}.){3}[0-9]{1,3}$", value) is None:
        raise ValueError('Invalid IP address value')
    return value


class NetVMMixin(qubes.events.Emitter):
    ''' Mixin containing network functionality '''
    mac = qubes.property('mac', type=str,
        default='00:16:3E:5E:6C:00',
        setter=_setter_mac,
        ls_width=17,
        doc='MAC address of the NIC emulated inside VM')

    ip = qubes.property('ip', type=str,
        default=_default_ip,
        setter=_setter_ip,
        ls_width=15,
        doc='IP address of this domain.')

    # CORE2: swallowed uses_default_netvm
    netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
        default=(lambda self: self.app.default_fw_netvm if self.provides_network
            else self.app.default_netvm),
        ls_width=31,
        doc='''VM that provides network connection to this domain. When
            `None`, machine is disconnected. When absent, domain uses default
            NetVM.''')

    provides_network = qubes.property('provides_network', default=False,
        type=bool, setter=qubes.property.bool,
        doc='''If this domain can act as network provider (formerly known as
            NetVM or ProxyVM)''')

    firewall_conf = qubes.property('firewall_conf', type=str,
        default='firewall.xml')

    #
    # used in networked appvms or proxyvms (netvm is not None)
    #


{'fraction_non_alphanumeric': 0.1065989847715736, 'fraction_numerical': 0.02107368097215813, 'mean_word_length': 3.1466836734693877, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4077164', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1982, 'n_words': 656}