3 BQ@sdZddlZddlZddlZyDejejejdZejd8[ddZddZ ddZ d9d d Z Wnej k ry.ddl Z d dZd dZ ddZ d:dd Z Wn\ek rddlZejdZGdddejZddZddZ ddZ d;dd Z YnXYnXdZdZdZdZdZdZdZdZdZGdddZGdddeZGd d!d!eZGd"d#d#eZ Gd$d%d%eZ!Gd&d'd'eZ"dZ#dZ$dZ%dZ&dZ'd(Z(d)Z)d*Z*dZ+d+Z,d,Z-d-Z.d.Z/d/Z0d0Z1d1Z2dZ3Gd2d3d3Z4Gd4d5d5Z5d6d7Z6dS) GPLv2+; See copying for details. NcCs|j|dS)N)bind) descriptoraddrr/usr/lib/python3.6/netlink.py_nl_bindsrcCs|jS)N) getsockname)rrrr_nl_getsocknamesr cCs|j|dS)N)send)rmsgrrr_nl_sendsr @cCs |j|S)N)recvfrom)rbufsrrr_nl_recvsrcCstj|j|ddS)N)_netlinkrfileno)rrrrrrscCstj|jS)N)rrr)rrrrr scCstj|j|dS)N)rr r)rr rrrr "scCstj|j|S)N)rrr)rrrrrr$sc@s0eZdZdejfdejfdejfdejfgZdS) SOCKADDR_NLZ nl_familyZnl_padnl_pid nl_groupsN)__name__ __module__ __qualname__ctypesZc_ushortc_intZ_fields_rrrrr,srcCs2ttjdtjd}tj|jtj |tj |S)Nr) rsocket AF_NETLINKosgetpidlibcrrrpointersizeof)rrrrrr2s cCsHtdddd}tjtj|}tj|jtj|tj||j|j fS)Nr) rrrr"r rrr!rr)rrlenrrrr 8s   cCstj|j|t|dS)Nr)r r rr#)rr rrrr @scCsntdddd}tjtj|}tj|}tj|j||dtj|tj|}tj tj||}||j |j ffS)Nr) rrrr"Zcreate_string_bufferr rrr!Z string_atrr)rrrr#ZbufrretrrrrCs  rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)AttrcGs,||_t|r"tj|f||_n||_dS)N)typer#structpackdata)self attr_typer/valuesrrr__init__^sz Attr.__init__cCsJtjdt|jd|j}t|j}|ddd@|}||jd|S)NHHr'rr))r-r.r#r/r,)r0hdrlengthZpadrrr_dumpes z Attr._dumpcCsd|jt|jfS)Nz)r,reprr/)r0rrr__repr__ksz Attr.__repr__cCstjd|jdS)NHr)r-unpackr/)r0rrru16nszAttr.u16cCstjd|jdS)Nhr)r-r=r/)r0rrrs16pszAttr.s16cCstjd|jdS)NIr)r-r=r/)r0rrru32rszAttr.u32cCstjd|jdS)Nir)r-r=r/)r0rrrs32tszAttr.s32cCs|jS)N)r/)r0rrrstrvszAttr.strcCs|jjddS)Nr)r/split)r0rrrnulstrxsz Attr.nulstrcCs t|jS)N)parse_attributesr/)r0rrrnestedzsz Attr.nestedN) rrrr3r9r;r>r@rBrDrErHrJrrrrr+]sr+c@seZdZddZdS)StrAttrcCs"tj||dt||jddS)Nz%dszutf-8)r+r3r#encode)r0r1r/rrrr3~szStrAttr.__init__N)rrrr3rrrrrK}srKc@seZdZddZdS) NulStrAttrcCs$tj||dt||jdddS)Nz%dsBzutf-8r)r+r3r#rL)r0r1r/rrrr3szNulStrAttr.__init__N)rrrr3rrrrrMsrMc@seZdZddZdS)U32AttrcCstj||d|dS)NrA)r+r3)r0r1valrrrr3szU32Attr.__init__N)rrrr3rrrrrNsrNc@seZdZddZdS)U8AttrcCstj||d|dS)NB)r+r3)r0r1rOrrrr3szU8Attr.__init__N)rrrr3rrrrrPsrPc@seZdZddZddZdS)NestedcCs||_||_dS)N)attrsr,)r0r1rSrrrr3szNested.__init__cCsPg}x|jD]}|j|jq Wdj|}t|}tjd|d|j}||S)Nr4r')rSappendr9joinr#r-r.r,)r0contentsattrr8r7rrrr9s  z Nested._dumpN)rrrr3r9rrrrrRsrR c@s&eZdZd ddZddZdd ZdS) MessagerrNcCsb||_||_||_d|_|pg}t|trXg}x|D]}|j|jq4Wdj||_ n||_ dS)Nr) r,flagsseqpid isinstancelistrUr9rVpayload)r0msg_typerfrgrkrWrXrrrr3s  zMessage.__init__cCsZ|jdkr|j|_|j|_t|j}tjd|d|j|j|j|j}|j||jdS)NrIHHIIr'rer*) rgrhr#rkr-r.r,rfr )r0Zconnr8r7rrrr s   z Message.sendcCs d|j|j|j|jt|jfS)Nz:)r,rhrgrfr:rk)r0rrrr;szMessage.__repr__re)rreN)rrrr3r r;rrrrrcs  rcc@s.eZdZd ddZddZddZd d ZdS) ConnectionrNcCsntjtjtj||_|jjtjtjd|jjtjtjdt|jd|ft |j\|_ |_ d|_ ||_ dS)Nir)rrSOCK_RAWrZ setsockoptZ SOL_SOCKETZ SO_SNDBUFZ SO_RCVBUFrr rhgroups_seqZ unexpected)r0ZnltyperpZunexpected_msg_handlerrrrr3s zConnection.__init__cCst|j|dS)N)r r)r0r rrrr szConnection.sendc Cst|j\}\}}tjd|dd\}}}}}t||||dd} || _| jtkrtjd| jddd } | dkrt dt j | | f} | | _ | | S)Nrmr*rCr'rzNetlink error: %s (%d)) rrr-r=rcrhr, NLMSG_ERRORrkOSErrorrstrerrorerrno) r0rWZnlpidZnlgrpsZmsglenrlrfrgrhr ruerrrrrrecvs zConnection.recvcCs|jd7_|jS)Nr)rq)r0rrrrgszConnection.seq)rN)rrrr3r rwrgrrrrrns rncCs`i}xVt|rZtjd|dd\}}t||d|||<|ddd@}||d}qW|S)Nr4r'rr)r6)r#r-r=r+)r/rSZattr_lenr1rrrrIs rI)rr)r )r )r )7__doc__rrr-rroZ _dummysockrrr r rerrorr ImportErrorrZCDLLr Z StructurerZ NLM_F_REQUESTZ NLM_F_MULTIZ NLM_F_ACKZ NLM_F_ECHOZ NLMSG_NOOPrrZ NLMSG_DONEZ NLMSG_OVERRUNZNLMSG_MIN_TYPEr+rKrMrNrPrRZ NETLINK_ROUTEZNETLINK_UNUSEDZNETLINK_USERSOCKZNETLINK_FIREWALLZNETLINK_INET_DIAGZ NETLINK_NFLOGZ NETLINK_XFRMZNETLINK_SELINUXZ NETLINK_ISCSIZ NETLINK_AUDITZNETLINK_FIB_LOOKUPZNETLINK_CONNECTORZNETLINK_NETFILTERZNETLINK_IP6_FWZNETLINK_DNRTMSGZNETLINK_KOBJECT_UEVENTZNETLINK_GENERICrcrnrIrrrrsz