Пример #1
0
def proxydhcp():
    net4011 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    net4011.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    net4011.setsockopt(socket.IPPROTO_IP, IP_PKTINFO, 1)
    net4011.bind(('', 4011))
    cfg = cfm.ConfigManager(None)
    while True:
        ready = select.select([net4011], [], [], None)
        if not ready or not ready[0]:
            continue
        rq = bytearray(1024)
        rqv = memoryview(rq)
        nb, client = net4011.recvfrom_into(rq)
        if nb < 240:
            continue
        rp = bytearray(1024)
        rpv = memoryview(rp)
        try:
            optidx = rq.index(b'\x63\x82\x53\x63') + 4
        except ValueError:
            continue
        opts, disco = opts_to_dict(rq, optidx, 3)
        disco['uuid']
        node = None
        if disco.get('hwaddr', None) in macmap:
            node = macmap[disco['hwaddr']]
        elif disco.get('uuid', None) in uuidmap:
            node = uuidmap[disco['uuid']]
        if not node:
            continue
        hwlen = rq[2]
        myipn = myipbypeer.get(rqv[28:28 + hwlen].tobytes(), None)
        if not myipn:
            continue
        if opts.get(77, None) == b'iPXE':
            cfd = cfg.get_node_attributes(node, ('deployment.*'))
            profile = cfd.get(node, {}).get('deployment.pendingprofile',
                                            {}).get('value', None)
            if not profile:
                continue
            myip = socket.inet_ntoa(myipn)
            bootfile = 'http://{0}/confluent-public/os/{1}/boot.ipxe'.format(
                myip, profile).encode('utf8')
        elif disco['arch'] == 'uefi-x64':
            bootfile = b'confluent/x86_64/ipxe.efi'
        elif disco['arch'] == 'bios-x86':
            bootfile = b'confluent/x86_64/ipxe.kkpxe'
        rpv[:240] = rqv[:240].tobytes()
        rpv[0:1] = b'\x02'
        rpv[108:108 + len(bootfile)] = bootfile
        rpv[240:243] = b'\x35\x01\x05'
        rpv[243:249] = b'\x36\x04' + myipn
        rpv[20:24] = myipn
        rpv[249:268] = b'\x61\x11' + opts[97]
        rpv[268:280] = b'\x3c\x09PXEClient\xff'
        net4011.sendto(rpv[:281], client)
Пример #2
0
def inametonets(iname):
    addrs = netifaces.ifaddresses(iname)
    try:
        addrs = addrs[netifaces.AF_INET]
    except KeyError:
        return
    for addr in addrs:
        ip = struct.unpack('!I', socket.inet_aton(addr['addr']))[0]
        mask = struct.unpack('!I', socket.inet_aton(addr['netmask']))[0]
        net = ip & mask
        net = socket.inet_ntoa(struct.pack('!I', net))
        yield (net, mask_to_cidr(addr['netmask']), addr['addr'])
Пример #3
0
def int2ip(val):
    """Convert integer to IP string."""
    return socket.inet_ntoa(struct.pack('!L',val))
Пример #4
0
def ipfromint(numb):
    return socket.inet_ntoa(struct.pack('I', numb))
Пример #5
0
def int2ip(val):
    """Convert integer to IP string."""
    return socket.inet_ntoa(struct.pack('!L', val))