import struct
import socket


def ip2long(*args):
    """
    .. function:: ip2long(ip) -> int

    Converts a decimal dotted quad IP string to long integer IP format.
    It can take either one column of IP strings or 4 columns each having one
    part of the IP address.

    Examples:

    >>> sql("select ip2long('123.123.123.123')")
    ip2long('123.123.123.123')
    --------------------------
    2071690107
    >>> sql("select ip2long(123,123,123,123)")
    ip2long(123,123,123,123)
    ------------------------
    2071690107

    """

    if len(args) == 1:
        try:
            return struct.unpack('!L', socket.inet_aton(args[0]))[0]
        except:
            return
    elif len(args) == 4:
        return struct.unpack('!L', socket.inet_aton('.'.join([str(x) for x in args])))[0]