<commit_before># Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Implements vlans, bridges, and iptables rules using linux utilities.
"""

import logging
import os
import signal

# TODO(ja): does the definition of network_path belong here?

from nova import db
from nova import flags
from nova import utils


FLAGS = flags.FLAGS
flags.DEFINE_string('dhcpbridge_flagfile',
                    '/etc/nova/nova-dhcpbridge.conf',
                    'location of flagfile for dhcpbridge')

flags.DEFINE_string('networks_path', utils.abspath('../networks'),
                    'Location to keep network config files')
flags.DEFINE_string('public_interface', 'vlan1',
                    'Interface for public IP addresses')
flags.DEFINE_string('bridge_dev', 'eth0',
                    'network device for bridges')
flags.DEFINE_string('routing_source_ip', utils.get_my_ip(),
                    'Public IP of network host')
flags.DEFINE_string('use_nova_chains', False,
                    'use the nova_ routing chains instead of default')

DEFAULT_PORTS = [("tcp", 80), ("tcp", 22), ("udp", 1194), ("tcp", 443)]

def init_host():
    """Basic networking setup goes here"""
    # NOTE(devcamcar): Cloud public DNAT entries, CloudPipe port
    # forwarding entries and a default DNAT entry.
    _confirm_rule("PREROUTING", "-t nat -s 0.0.0.0/0 "
             "-d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT "
             "--to-destination %s:%s" % (FLAGS.cc_host, FLAGS.cc_port))

    # NOTE(devcamcar): Cloud public SNAT entries and the default
    # SNAT rule for outbound traffic.
    _confirm_rule("POSTROUTING", "-t nat -s %s "
             "-j SNAT --to-source %s"
             % (FLAGS.private_range, FLAGS.routing_source_ip))

    _confirm_rule("POSTROUTING", "-t nat -s %s -j MASQUERADE" % FLAGS.private_range)
    _confirm_rule("POSTROUTING", "-t nat -s %(range)s -d %(range)s -j ACCEPT" % {'range': FLAGS.private_range})

def bind_floating_ip(floating_ip):
    """Bind ip to public interface"""
    _execute("sudo ip addr add %s dev %s" % (floating_ip,
                                             FLAGS.public_interface))


def unbind_floating_ip(floating_ip):
    """Unbind a public ip from public interface"""
    _execute("sudo ip addr del %s dev %s" % (floating_ip,
                                             FLAGS.public_interface))


def ensure_vlan_forward(public_ip, port, private_ip):
    """Sets up forwarding rules for vlan"""
    _confirm_rule("FORWARD", "-d %s -p udp --dport 1194 -j ACCEPT" % private_ip)
    _confirm_rule(
        "PREROUTING -t nat -d %s -p udp --dport %s -j DNAT --to %s:1194"
            % (public_ip, port, private_ip))


def ensure_floating_forward(floating_ip, fixed_ip):
    """Ensure floating ip forwarding rule"""
    _confirm_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
                           % (floating_ip, fixed_ip))
    _confirm_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"
                           % (fixed_ip, floating_ip))
    # TODO(joshua): Get these from the secgroup datastore entries
    _confirm_rule("FORWARD", "-d %s -p icmp -j ACCEPT"
                           % (fixed_ip))
    for (protocol, port) in DEFAULT_PORTS:
        _confirm_rule(
            "FORWARD -d %s -p %s --dport %s -j ACCEPT"
            % (fixed_ip, protocol, port))


def remove_floating_forward(floating_ip, fixed_ip):
    """Remove forwarding for floating ip"""
    _remove_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
                          % (floating_ip, fixed_ip))
    _remove_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"<commit_msg>fix a few missed calls to _confirm_rule and 80 char issues<commit_after># Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Implements vlans, bridges, and iptables rules using linux utilities.
"""

import logging
import os
import signal

# TODO(ja): does the definition of network_path belong here?

from nova import db
from nova import flags
from nova import utils


FLAGS = flags.FLAGS
flags.DEFINE_string('dhcpbridge_flagfile',
                    '/etc/nova/nova-dhcpbridge.conf',
                    'location of flagfile for dhcpbridge')

flags.DEFINE_string('networks_path', utils.abspath('../networks'),
                    'Location to keep network config files')
flags.DEFINE_string('public_interface', 'vlan1',
                    'Interface for public IP addresses')
flags.DEFINE_string('bridge_dev', 'eth0',
                    'network device for bridges')
flags.DEFINE_string('routing_source_ip', utils.get_my_ip(),
                    'Public IP of network host')
flags.DEFINE_string('use_nova_chains', False,
                    'use the nova_ routing chains instead of default')

DEFAULT_PORTS = [("tcp", 80), ("tcp", 22), ("udp", 1194), ("tcp", 443)]

def init_host():
    """Basic networking setup goes here"""
    # NOTE(devcamcar): Cloud public DNAT entries, CloudPipe port
    # forwarding entries and a default DNAT entry.
    _confirm_rule("PREROUTING", "-t nat -s 0.0.0.0/0 "
             "-d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT "
             "--to-destination %s:%s" % (FLAGS.cc_host, FLAGS.cc_port))

    # NOTE(devcamcar): Cloud public SNAT entries and the default
    # SNAT rule for outbound traffic.
    _confirm_rule("POSTROUTING", "-t nat -s %s "
             "-j SNAT --to-source %s"
             % (FLAGS.private_range, FLAGS.routing_source_ip))

    _confirm_rule("POSTROUTING", "-t nat -s %s -j MASQUERADE" %
                  FLAGS.private_range)
    _confirm_rule("POSTROUTING", "-t nat -s %(range)s -d %(range)s -j ACCEPT" %
                  {'range': FLAGS.private_range})

def bind_floating_ip(floating_ip):
    """Bind ip to public interface"""
    _execute("sudo ip addr add %s dev %s" % (floating_ip,
                                             FLAGS.public_interface))


def unbind_floating_ip(floating_ip):
    """Unbind a public ip from public interface"""
    _execute("sudo ip addr del %s dev %s" % (floating_ip,
                                             FLAGS.public_interface))


def ensure_vlan_forward(public_ip, port, private_ip):
    """Sets up forwarding rules for vlan"""
    _confirm_rule("FORWARD", "-d %s -p udp --dport 1194 -j ACCEPT" %
                  private_ip)
    _confirm_rule("PREROUTING",
                  "-t nat -d %s -p udp --dport %s -j DNAT --to %s:1194"
            % (public_ip, port, private_ip))


def ensure_floating_forward(floating_ip, fixed_ip):
    """Ensure floating ip forwarding rule"""
    _confirm_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
                           % (floating_ip, fixed_ip))
    _confirm_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"
                           % (fixed_ip, floating_ip))
    # TODO(joshua): Get these from the secgroup datastore entries
    _confirm_rule("FORWARD", "-d %s -p icmp -j ACCEPT"
                           % (fixed_ip))
    for (protocol, port) in DEFAULT_PORTS:
        _confirm_rule("FORWARD","-d %s -p %s --dport %s -j ACCEPT"
            % (fixed_ip, protocol, port))


def remove_floating_forward(floating_ip, fixed_ip):
    """Remove forwarding for floating ip"""
    _remove_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
                          % (floating_ip, fixed_ip))
    _remove_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"
{'fraction_non_alphanumeric': 0.11429882946981869, 'fraction_numerical': 0.011705301813174203, 'mean_word_length': 2.5952970297029703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3483494', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2646, 'n_words': 878}