#!/usr/bin/python2 # # Authors: # Jakub Hrozek # Jan Cholasta # # Copyright (C) 2011 Red Hat # see file 'COPYING' for use and warranty information # # 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 3 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, see . import os import sys from optparse import OptionParser from fnmatch import fnmatch, fnmatchcase try: from pylint import checkers from pylint.lint import PyLinter from pylint.checkers.typecheck import TypeChecker from astroid import Class, Instance, Module, InferenceError from pylint.reporters.text import TextReporter except ImportError: print >> sys.stderr, "To use {0}, please install pylint.".format(sys.argv[0]) sys.exit(32) # File names to ignore when searching for python source files IGNORE_FILES = ('.*', '*~', '*.in', '*.pyc', '*.pyo') IGNORE_PATHS = ( 'build', 'rpmbuild', 'dist', 'install/po/test_i18n.py', 'lite-server.py') class IPATypeChecker(TypeChecker): NAMESPACE_ATTRS = ['Command', 'Object', 'Method', 'Backend', 'Updater', 'Advice'] LOGGING_ATTRS = ['log', 'debug', 'info', 'warning', 'error', 'exception', 'critical'] # 'class or module': ['generated', 'properties'] ignore = { # Python standard library & 3rd party classes 'krbV.Principal': ['name'], 'socket._socketobject': ['sendall'], # should be 'subprocess.Popen' '.Popen': ['stdin', 'stdout', 'stderr', 'pid', 'returncode', 'poll', 'wait', 'communicate'], 'urlparse.ResultMixin': ['scheme', 'netloc', 'path', 'query', 'fragment', 'username', 'password', 'hostname', 'port'], 'urlparse.ParseResult': ['params'], 'pytest': ['fixture', 'raises', 'skip', 'yield_fixture', 'mark'], 'nose.tools': ['assert_equal', 'assert_raises'], 'datetime.tzinfo': ['houroffset', 'minoffset', 'utcoffset', 'dst'], # IPA classes 'ipalib.base.NameSpace': ['add', 'mod', 'del', 'show', 'find'], 'ipalib.cli.Collector': ['__options'], 'ipalib.config.Env': ['*'], 'ipalib.krb_utils.KRB5_CCache': LOGGING_ATTRS, 'ipalib.parameters.Param': ['cli_name', 'cli_short_name', 'label', 'default', 'doc', 'required', 'multivalue', 'primary_key', 'normalizer', 'default_from', 'autofill', 'query', 'attribute', 'include', 'exclude', 'flags', 'hint', 'alwaysask', 'sortorder', 'csv', 'option_group'], 'ipalib.parameters.Bool': ['truths', 'falsehoods'], 'ipalib.parameters.Data': ['minlength', 'maxlength', 'length', 'pattern', 'pattern_errmsg'], 'ipalib.parameters.Str': ['noextrawhitespace'], 'ipalib.parameters.Password': ['confirm'], 'ipalib.parameters.File': ['stdin_if_missing'], 'ipalib.plugins.dns.DNSRecord': ['validatedns', 'normalizedns'], 'ipalib.parameters.Enum': ['values'], 'ipalib.parameters.Number': ['minvalue', 'maxvalue'], 'ipalib.parameters.Decimal': ['precision', 'exponential', 'numberclass'], 'ipalib.parameters.DNSNameParam': ['only_absolute', 'only_relative'], 'ipalib.plugable.API': NAMESPACE_ATTRS + LOGGING_ATTRS, 'ipalib.plugable.Plugin': ['api', 'env'] + NAMESPACE_ATTRS + LOGGING_ATTRS, 'ipalib.session.AuthManager': LOGGING_ATTRS, 'ipalib.session.SessionAuthManager': LOGGING_ATTRS, 'ipalib.session.SessionManager': LOGGING_ATTRS, 'ipaserver.install.ldapupdate.LDAPUpdate': LOGGING_ATTRS, 'ipaserver.rpcserver.KerberosSession': ['api'] + LOGGING_ATTRS, 'ipatests.test_integration.base.IntegrationTest': [ 'domain', 'master', 'replicas', 'clients', 'ad_domains'] } def _related_classes(self, klass): yield klass for base in klass.ancestors(): yield base def _class_full_name(self, klass): return klass.root().name + '.' + klass.name def _find_ignored_attrs(self, owner): attrs = [] for klass in self._related_classes(owner): name = self._class_full_name(klass) if name in self.ignore: attrs += self.ignore[name] return attrs def visit_getattr(self, node): try: inferred = list(node.expr.infer()) except InferenceError: inferred = [] for owner in inferred: if isinstance(owner, Module): if node.attrname in self.ignore.get(owner.name, ()): return elif isinstance(owner, Class) or type(owner) is Instance: ignored = self._find_ignored_attrs(owner) for pattern in ignored: if fnmatchcase(node.attrname, pattern): return super(IPATypeChecker, self).visit_getattr(node) class IPALinter(PyLinter): ignore = (TypeChecker,) def __init__(self): super(IPALinter, self).__init__() self.missing = set() def register_checker(self, checker): if type(checker) in self.ignore: return super(IPALinter, self).register_checker(checker) def add_message(self, msg_id, line=None, node=None, args=None): if line is None and node is not None: line = node.fromlineno # Record missing packages if msg_id == 'F0401' and self.is_message_enabled(msg_id, line): self.missing.add(args) super(IPALinter, self).add_message(msg_id, line, node, args) def find_files(path, basepath): entries = os.listdir(path) # If this directory is a python package, look no further if '__init__.py' in entries: return [path] result = [] for filename in entries: filepath = os.path.join(path, filename) for pattern in IGNORE_FILES: if fnmatch(filename, pattern): filename = None break if filename is None: continue for pattern in IGNORE_PATHS: patpath = os.path.join(basepath, pattern).replace(os.sep, '/') if filepath == patpath: filename = None break if filename is None: continue if os.path.islink(filepath): continue # Recurse into subdirectories if os.path.isdir(filepath): result += find_files(filepath, basepath) continue # Add all *.py files if filename.endswith('.py'): result.append(filepath) continue # Add any other files beginning with a shebang and having # the word "python" on the first line file = open(filepath, 'r') line = file.readline(128) file.close() if line[:2] == '#!' and line.find('python') >= 0: result.append(filepath) return result def main(): optparser = OptionParser() optparser.add_option('--no-fail', help='report success even if errors were found', dest='fail', default=True, action='store_false') optparser.add_option('--enable-noerror', help='enable warnings and other non-error messages', dest='errors_only', default=True, action='store_false') options, args = optparser.parse_args() cwd = os.getcwd() if len(args) == 0: files = find_files(cwd, cwd) else: files = args for filename in files: dirname = os.path.dirname(filename) if dirname not in sys.path: sys.path.insert(0, dirname) linter = IPALinter() checkers.initialize(linter) linter.register_checker(IPATypeChecker(linter)) if options.errors_only: linter.disable_noerror_messages() linter.enable('F') linter.set_reporter(TextReporter()) linter.set_option('msg-template', '{path}:{line}: [{msg_id}({symbol}), {obj}] {msg})') linter.set_option('reports', False) linter.set_option('persistent', False) linter.check(files) if linter.msg_status != 0: print >> sys.stderr, """ =============================================================================== Errors were found during the static code check. """ if len(linter.missing) > 0: print >> sys.stderr, "There are some missing imports:" for mod in sorted(linter.missing): print >> sys.stderr, " " + mod print >> sys.stderr, """ Please make sure all of the required and optional (python-krbV, python-rhsm) python packages are installed. """ print >> sys.stderr, """\ If you are certain that any of the reported errors are false positives, please mark them in the source code according to the pylint documentation. =============================================================================== """ if options.fail: return linter.msg_status else: return 0 if __name__ == "__main__": sys.exit(main()) ef='#n181'>181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta name="robots" content="noindex">
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>ipcalc.c File Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.6 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="globals.html">Globals</a> &nbsp; </center>
<hr><h1>ipcalc.c File Reference</h1>provides utilities for manipulating IP addresses. 
<a href="#_details">More...</a>
<p>
<code>#include &lt;ctype.h&gt;</code><br>
<code>#include &lt;popt.h&gt;</code><br>
<code>#include &lt;stdio.h&gt;</code><br>
<code>#include &lt;stdlib.h&gt;</code><br>
<code>#include &lt;string.h&gt;</code><br>
<code>#include &lt;sys/socket.h&gt;</code><br>
<code>#include &lt;netinet/in.h&gt;</code><br>
<code>#include &lt;arpa/inet.h&gt;</code><br>
<code>#include &lt;netdb.h&gt;</code><br>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td colspan=2><br><h2>Defines</h2></td></tr>
<tr><td nowrap align=right valign=top><a name="a0" doxytag="ipcalc.c::IPBITS"></a>
#define&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a0">IPBITS</a>&nbsp;&nbsp;&nbsp;(sizeof(unsigned long int) * 8)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>the number of bits in an IP address.</em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top><a name="a1" doxytag="ipcalc.c::IPBYTES"></a>
#define&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a1">IPBYTES</a>&nbsp;&nbsp;&nbsp;(sizeof(unsigned long int))</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>the number of bytes in an IP address.</em></font><br><br></td></tr>
<tr><td colspan=2><br><h2>Functions</h2></td></tr>
<tr><td nowrap align=right valign=top>unsigned long int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a2">prefix2mask</a> (int prefix)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>creates a netmask from a specified number of bits.</em> <a href="#a2">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a3">mask2prefix</a> (unsigned long int mask)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>calculates the number of bits masked off by a netmask.</em> <a href="#a3">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>unsigned long int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a4">default_netmask</a> (unsigned long int addr)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>returns the default (canonical) netmask associated with specified IP address.</em> <a href="#a4">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>unsigned long int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a5">calc_broadcast</a> (unsigned long int addr, int prefix)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>calculate broadcast address given an IP address and a prefix length.</em> <a href="#a5">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>unsigned long int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a6">calc_network</a> (unsigned long int addr, int prefix)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>calculates the network address for a specified address and prefix.</em> <a href="#a6">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>const char*&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a7">get_hostname</a> (unsigned long int addr)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>returns the hostname associated with the specified IP address.</em> <a href="#a7">More...</a><em></em></font><br><br></td></tr>
<tr><td nowrap align=right valign=top>int&nbsp;</td><td valign=bottom><a class="el" href="ipcalc_c.html#a8">main</a> (int argc, const char **argv)</td></tr>
<tr><td>&nbsp;</td><td><font size=-1><em>wrapper program for ipcalc functions.</em> <a href="#a8">More...</a><em></em></font><br><br></td></tr>
</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
provides utilities for manipulating IP addresses.
<p>

<p>

<p>
 ipcalc provides utilities and a front-end command line interface for manipulating IP addresses, and calculating various aspects of an ip address/netmask/network address/prefix/etc.
<p>
Functionality can be accessed from other languages from the library interface, documented here. To use ipcalc from the shell, read the ipcalc(1) manual page.
<p>
When passing parameters to the various functions, take note of whether they take host byte order or network byte order. Most take host byte order, and return host byte order, but there are some exceptions.
<p>
<hr><h2>Function Documentation</h2>
<a name="a5" doxytag="ipcalc.c::calc_broadcast"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
unsigned long int calc_broadcast (
          </b></td>
          <td valign="bottom"><b>
unsigned long int <em>addr</em>, 
          </b></td>
        </tr>
        <tr>
          <td></td>
          <td><b>
int <em>prefix</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
calculate broadcast address given an IP address and a prefix length.
<p>

<p>
 <dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>addr</em>
&nbsp;</td><td>
an IP address in network byte order. </td></tr>
<tr><td valign=top><em>prefix</em>
&nbsp;</td><td>
a prefix length.
<p>
</td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
the calculated broadcast address for the network, in network byte order. </dl>    </td>
  </tr>
</table>
<a name="a6" doxytag="ipcalc.c::calc_network"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
unsigned long int calc_network (
          </b></td>
          <td valign="bottom"><b>
unsigned long int <em>addr</em>, 
          </b></td>
        </tr>
        <tr>
          <td></td>
          <td><b>
int <em>prefix</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
calculates the network address for a specified address and prefix.
<p>

<p>
 <dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>addr</em>
&nbsp;</td><td>
an IP address, in network byte order </td></tr>
<tr><td valign=top><em>prefix</em>
&nbsp;</td><td>
the network prefix </td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
the base address of the network that addr is associated with, in network byte order. </dl>    </td>
  </tr>
</table>
<a name="a4" doxytag="ipcalc.c::default_netmask"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
unsigned long int default_netmask (
          </b></td>
          <td valign="bottom"><b>
unsigned long int <em>addr</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
returns the default (canonical) netmask associated with specified IP address.
<p>

<p>
 When the Internet was originally set up, various ranges of IP addresses were segmented into three network classes: A, B, and C. This function will return a netmask that is associated with the IP address specified defining where it falls in the predefined classes.<dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>addr</em>
&nbsp;</td><td>
an IP address in network byte order. </td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
a netmask in network byte order. </dl>    </td>
  </tr>
</table>
<a name="a7" doxytag="ipcalc.c::get_hostname"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
const char * get_hostname (
          </b></td>
          <td valign="bottom"><b>
unsigned long int <em>addr</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
returns the hostname associated with the specified IP address.
<p>

<p>
 <dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>addr</em>
&nbsp;</td><td>
an IP address to find a hostname for, in network byte order
<p>
</td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
a hostname, or NULL if one cannot be determined. Hostname is stored in a static buffer that may disappear at any time, the caller should copy the data if it needs permanent storage. </dl>    </td>
  </tr>
</table>
<a name="a8" doxytag="ipcalc.c::main"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
main (
          </b></td>
          <td valign="bottom"><b>
int <em>argc</em>, 
          </b></td>
        </tr>
        <tr>
          <td></td>
          <td><b>
const char ** <em>argv</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
wrapper program for ipcalc functions.
<p>

<p>
 This is a wrapper program for the functions that the ipcalc library provides. It can be used from shell scripts or directly from the command line.
<p>
For more information, please see the ipcalc(1) man page.     </td>
  </tr>
</table>
<a name="a3" doxytag="ipcalc.c::mask2prefix"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
int mask2prefix (
          </b></td>
          <td valign="bottom"><b>
unsigned long int <em>mask</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
calculates the number of bits masked off by a netmask.
<p>

<p>
 This function calculates the significant bits in an IP address as specified by a netmask. See also <a class="el" href="ipcalc_c.html#a2">prefix2mask</a>.<dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>mask</em>
&nbsp;</td><td>
is the netmask, specified as an unsigned long integer in network byte order. </td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
the number of significant bits. </dl>    </td>
  </tr>
</table>
<a name="a2" doxytag="ipcalc.c::prefix2mask"></a><p>
<table width="100%" cellpadding="2" cellspacing="0" border="0">
  <tr>
    <td class="md">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td nowrap valign="top"><b> 
unsigned long int prefix2mask (
          </b></td>
          <td valign="bottom"><b>
int <em>bits</em>&nbsp;)
          </b></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
creates a netmask from a specified number of bits.
<p>

<p>
 This function converts a prefix length to a netmask. As CIDR (classless internet domain internet domain routing) has taken off, more an more IP addresses are being specified in the format address/prefix (i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0). If you need to see what netmask corresponds to the prefix part of the address, this is the function. See also <a class="el" href="ipcalc_c.html#a3">mask2prefix</a>.<dl compact><dt>
<b>Parameters: </b><dd>
<table border=0 cellspacing=2 cellpadding=0>
<tr><td valign=top><em>prefix</em>
&nbsp;</td><td>
is the number of bits to create a mask for. </td></tr>
</table>
</dl><dl compact><dt>
<b>Returns: </b><dd>
a network mask, in network byte order. </dl>    </td>
  </tr>
</table>
<hr><address><small>Generated at Mon Apr 30 16:39:02 2001 for initscripts by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.gif" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.6 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>,
 &copy;&nbsp;1997-2001</small></address>
</body>
</html>