#!/usr/bin/env python3
# Copyright (c) 2010-2011 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

# pylint: disable=wrong-import-position

import ArPyUtils
ArPyUtils.runMeAsRoot()

import array
import fcntl
import sys, socket
from SysConstants.if_ether_h import ETH_P_ALL
from EbraTestBridgePhyControlLib import rawPhyControlPacket

# should really have somewhere to import this from
# This is copied from ethxmit
SIOCGIFHWADDR = 0x8927
SADATA = 18

oper = sys.argv[ 1 ]
intfs = sys.argv[ 2: ]
for intf in intfs:
   sock = socket.socket( socket.PF_PACKET, socket.SOCK_RAW )
   sock.bind( ( intf, ETH_P_ALL ) )
   buf = array.array( 'b', bytes( intf, 'utf-8' ) + b'\0' * 32 )
   fcntl.ioctl( sock.fileno(), SIOCGIFHWADDR, buf )
   srcAddr = buf[ SADATA : SADATA + 6 ]
   raw = rawPhyControlPacket( oper == 'up', src=srcAddr.tobytes() )
   sock.send( raw )