// File: dguglielmi[P]dlan-linux-software/tools/dlanpasswd.cpp
// Function: main
int main(int argc, char** argv)
{
  int result = 0;
  CMACAddress addrDevice;
  
  if((argc < 3) || !addrDevice.FromString(argv[1]))
  {
    printf("Usage: dlanpasswd MAC-ADDRESS PASSWORD\n"
           "   or: dlanpasswd MAC-ADDRESS PASSWORD SECURITY-ID\n"
           "\n"
           "In the 1st form, assign PASSWORD to the local dLAN adapter MAC-ADDRESS.\n"
           "\n"    
           "In the 2nd form, assign PASSWORD to the remote dLAN 200 AV adapter identified \n"
           "by SECURITY-ID, using the local dLAN 200 AV adapter MAC-ADDRESS to forward the\n"
           "request to the PowerLine.\n"
           "\n");
           
    result = -1;
  }
  else
  {
    std::string strPassword = argv[2];
    std::string strSecurityID = (argc > 3) ? argv[3] : "";

    CPacketInterfaceLinux pktIfc;
    CFindLocalDevice finder(pktIfc, addrDevice);
    if(CPacketTaskRunner(pktIfc, finder).Run(3000, MSCounter()))
    {
      if(finder.itDevice->GetType() == CDlanDevice::eThunderbolt)
      {
        if(strSecurityID.empty())
        {
          printf("assigning password to local device %s: ", 
            finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugAvSecurity::CSetKeyTask setKey(pktIfc,
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
        else
        {
          printf("assigning password to remote device %s via %s: ", 
            strSecurityID.c_str(), finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugAvSecurity::CSetKeyTask setKey(pktIfc, 
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword, 
            CMACAddress::Broadcast(), strSecurityID);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
      }
      else
      {
        if(strSecurityID.empty())
        {
          printf("assigning password to local device %s: ", 
            finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugSecurity::CSetKeyTask setKey(pktIfc, 
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
        else
        {
          printf("error: assigning remote password is only supported for dLAN 200 AV devices\n");
          result = 3;
        }
      }
    }
    else
    {
      printf("no device found\n");
      result = 1;
    }
  }

  return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x710, %rsp            # imm = 0x710
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
leaq -0x1a(%rbp), %rdi
xorl %r9d, %r9d
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
callq 0x6230
movl -0x8(%rbp), %ecx
movb $0x0, -0x42(%rbp)
movb $0x0, -0x55(%rbp)
movb $0x1, %al
cmpl $0x3, %ecx
movb %al, -0x619(%rbp)
jl 0x57ca
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x630(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x628(%rbp)
callq 0x5540
movq -0x630(%rbp), %rsi
movq -0x628(%rbp), %rdx
movb $0x1, -0x42(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x62a0
jmp 0x57a3
movb $0x1, -0x55(%rbp)
leaq -0x1a(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x328a0
movb %al, -0x631(%rbp)
jmp 0x57bc
movb -0x631(%rbp), %al
xorb $-0x1, %al
movb %al, -0x619(%rbp)
movb -0x619(%rbp), %al
movb %al, -0x632(%rbp)
testb $0x1, -0x55(%rbp)
jne 0x57de
jmp 0x57e7
leaq -0x40(%rbp), %rdi
callq 0x5240
testb $0x1, -0x42(%rbp)
jne 0x57ef
jmp 0x57f8
leaq -0x41(%rbp), %rdi
callq 0x5380
movb -0x632(%rbp), %al
testb $0x1, %al
jne 0x5804
jmp 0x5874
leaq 0x327f9(%rip), %rdi     # 0x38004
xorl %eax, %eax
callq 0x5060
jmp 0x5814
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
jmp 0x6191
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x584d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
testb $0x1, -0x55(%rbp)
jne 0x5842
jmp 0x584b
leaq -0x40(%rbp), %rdi
callq 0x5240
jmp 0x584d
testb $0x1, -0x42(%rbp)
jne 0x5855
jmp 0x585e
leaq -0x41(%rbp), %rdi
callq 0x5380
jmp 0x61ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x61ac
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x648(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x640(%rbp)
callq 0x5540
movq -0x648(%rbp), %rsi
movq -0x640(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x62a0
jmp 0x58ac
leaq -0x79(%rbp), %rdi
callq 0x5380
cmpl $0x3, -0x8(%rbp)
jle 0x58cc
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x650(%rbp)
jmp 0x58dc
leaq 0x3288e(%rip), %rax     # 0x38161
movq %rax, -0x650(%rbp)
jmp 0x58dc
movq -0x650(%rbp), %rax
movq %rax, -0x660(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x658(%rbp)
callq 0x5540
movq -0x660(%rbp), %rsi
movq -0x658(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0x62a0
jmp 0x5919
leaq -0xa1(%rbp), %rdi
callq 0x5380
xorps %xmm0, %xmm0
movaps %xmm0, -0x170(%rbp)
movaps %xmm0, -0x180(%rbp)
movaps %xmm0, -0x190(%rbp)
leaq -0x190(%rbp), %rdi
movq %rdi, -0x668(%rbp)
callq 0x6350
movq -0x668(%rbp), %rsi
leaq -0x158(%rbp), %rdi
callq 0x329c0
jmp 0x5965
leaq -0x190(%rbp), %rdi
callq 0x6370
leaq -0x290(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x1a(%rbp), %rdx
callq 0x6390
jmp 0x598a
leaq -0x288(%rbp), %rdx
leaq -0x2a0(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x64b0
jmp 0x59a6
leaq -0x2a0(%rbp), %rdi
movl $0xbb8, %esi            # imm = 0xBB8
callq 0x64e0
movb %al, -0x669(%rbp)
jmp 0x59bf
movb -0x669(%rbp), %al
testb $0x1, %al
jne 0x59ce
jmp 0x611c
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x65f0
movl %eax, -0x670(%rbp)
jmp 0x59ea
movl -0x670(%rbp), %eax
cmpl $0x2, %eax
jne 0x5ef3
leaq -0xa0(%rbp), %rdi
callq 0x5510
testb $0x1, %al
jne 0x5a0e
jmp 0x5c89
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x678(%rbp)
jmp 0x5a2b
leaq -0x2e9(%rbp), %rdi
movq %rdi, -0x680(%rbp)
callq 0x5540
movq -0x680(%rbp), %rdx
leaq 0x3273e(%rip), %rsi     # 0x3818a
leaq -0x2e8(%rbp), %rdi
callq 0x62a0
jmp 0x5a5a
movq -0x678(%rbp), %rsi
leaq -0x2c8(%rbp), %rdi
leaq -0x2e8(%rbp), %rdx
callq 0x32560
jmp 0x5a76
leaq -0x2c8(%rbp), %rdi
callq 0x5170
movq %rax, %rsi
leaq 0x326d6(%rip), %rdi     # 0x38162
xorl %eax, %eax
callq 0x5060
jmp 0x5a95
leaq -0x2c8(%rbp), %rdi
callq 0x5240
leaq -0x2e8(%rbp), %rdi
callq 0x5240
leaq -0x2e9(%rbp), %rdi
callq 0x5380
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6620
movq %rax, -0x688(%rbp)
jmp 0x5ad6
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x690(%rbp)
jmp 0x5af3
movq -0x690(%rbp), %rcx
movq -0x688(%rbp), %rdx
movq %rsp, %rax
movl $0x0, (%rax)
leaq -0x3c8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x78(%rbp), %r8
movl $0x1388, %r9d           # imm = 0x1388
callq 0x6640
jmp 0x5b29
leaq -0x3d8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x3c8(%rbp), %rdx
callq 0x64b0
jmp 0x5b45
leaq -0x3d8(%rbp), %rdi
callq 0x6830
movb %al, -0x691(%rbp)
jmp 0x5b59
movb -0x691(%rbp), %al
testb $0x1, %al
jne 0x5b68
jmp 0x5c61
leaq 0x3261d(%rip), %rdi     # 0x3818c
xorl %eax, %eax
callq 0x5060
jmp 0x5b78
jmp 0x5c78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x5380
jmp 0x61ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xa1(%rbp), %rdi
callq 0x5380
jmp 0x6186
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x6370
jmp 0x617a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x616e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x5c33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x5c27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x5240
leaq -0x2e8(%rbp), %rdi
callq 0x5240
leaq -0x2e9(%rbp), %rdi
callq 0x5380
jmp 0x6162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x3c8(%rbp), %rdi
callq 0x68b0
jmp 0x6162
leaq 0x32528(%rip), %rdi     # 0x38190
xorl %eax, %eax
callq 0x5060
jmp 0x5c71
movl $0x2, -0x14(%rbp)
leaq -0x3c8(%rbp), %rdi
callq 0x68b0
jmp 0x5eee
leaq -0xa0(%rbp), %rdi
callq 0x5170
movq %rax, -0x6a8(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x6a0(%rbp)
jmp 0x5cb9
leaq -0x419(%rbp), %rdi
movq %rdi, -0x6b0(%rbp)
callq 0x5540
movq -0x6b0(%rbp), %rdx
leaq 0x324b0(%rip), %rsi     # 0x3818a
leaq -0x418(%rbp), %rdi
callq 0x62a0
jmp 0x5ce8
movq -0x6a0(%rbp), %rsi
leaq -0x3f8(%rbp), %rdi
leaq -0x418(%rbp), %rdx
callq 0x32560
jmp 0x5d04
leaq -0x3f8(%rbp), %rdi
callq 0x5170
movq -0x6a8(%rbp), %rsi
movq %rax, %rdx
leaq 0x32477(%rip), %rdi     # 0x38198
xorl %eax, %eax
callq 0x5060
jmp 0x5d2a
leaq -0x3f8(%rbp), %rdi
callq 0x5240
leaq -0x418(%rbp), %rdi
callq 0x5240
leaq -0x419(%rbp), %rdi
callq 0x5380
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6620
movq %rax, -0x6b8(%rbp)
jmp 0x5d6b
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x6c0(%rbp)
jmp 0x5d88
leaq -0x4fe(%rbp), %rdi
callq 0x6940
jmp 0x5d96
movq -0x6c0(%rbp), %rcx
movq -0x6b8(%rbp), %rdx
movq %rsp, %rax
leaq -0xa0(%rbp), %rsi
movq %rsi, (%rax)
movl $0x0, 0x10(%rax)
movq $0x1388, 0x8(%rax)      # imm = 0x1388
leaq -0x4f8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x78(%rbp), %r8
leaq -0x4fe(%rbp), %r9
callq 0x6980
jmp 0x5de0
leaq -0x4fe(%rbp), %rdi
callq 0x6b70
leaq -0x510(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x4f8(%rbp), %rdx
callq 0x64b0
jmp 0x5e08
leaq -0x510(%rbp), %rdi
callq 0x6830
movb %al, -0x6c1(%rbp)
jmp 0x5e1c
movb -0x6c1(%rbp), %al
testb $0x1, %al
jne 0x5e2b
jmp 0x5ecb
leaq 0x3235a(%rip), %rdi     # 0x3818c
xorl %eax, %eax
callq 0x5060
jmp 0x5e3b
jmp 0x5ee2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x5e80
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x5e74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x3f8(%rbp), %rdi
callq 0x5240
leaq -0x418(%rbp), %rdi
callq 0x5240
leaq -0x419(%rbp), %rdi
callq 0x5380
jmp 0x6162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x4fe(%rbp), %rdi
callq 0x6b70
jmp 0x6162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x4f8(%rbp), %rdi
callq 0x68b0
jmp 0x6162
leaq 0x322be(%rip), %rdi     # 0x38190
xorl %eax, %eax
callq 0x5060
jmp 0x5edb
movl $0x2, -0x14(%rbp)
leaq -0x4f8(%rbp), %rdi
callq 0x68b0
jmp 0x611a
leaq -0xa0(%rbp), %rdi
callq 0x5510
testb $0x1, %al
jne 0x5f08
jmp 0x6101
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x6d0(%rbp)
jmp 0x5f25
leaq -0x551(%rbp), %rdi
movq %rdi, -0x6d8(%rbp)
callq 0x5540
movq -0x6d8(%rbp), %rdx
leaq 0x32244(%rip), %rsi     # 0x3818a
leaq -0x550(%rbp), %rdi
callq 0x62a0
jmp 0x5f54
movq -0x6d0(%rbp), %rsi
leaq -0x530(%rbp), %rdi
leaq -0x550(%rbp), %rdx
callq 0x32560
jmp 0x5f70
leaq -0x530(%rbp), %rdi
callq 0x5170
movq %rax, %rsi
leaq 0x321dc(%rip), %rdi     # 0x38162
xorl %eax, %eax
callq 0x5060
jmp 0x5f8f
leaq -0x530(%rbp), %rdi
callq 0x5240
leaq -0x550(%rbp), %rdi
callq 0x5240
leaq -0x551(%rbp), %rdi
callq 0x5380
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6620
movq %rax, -0x6e0(%rbp)
jmp 0x5fd0
leaq -0x198(%rbp), %rdi
callq 0x65c0
movq %rax, %rdi
callq 0x6610
movq %rax, -0x6e8(%rbp)
jmp 0x5fed
movq -0x6e8(%rbp), %rcx
movq -0x6e0(%rbp), %rdx
leaq -0x608(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x78(%rbp), %r8
movl $0x1388, %r9d           # imm = 0x1388
callq 0x6b80
jmp 0x601a
leaq -0x618(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x608(%rbp), %rdx
callq 0x64b0
jmp 0x6036
leaq -0x618(%rbp), %rdi
callq 0x6830
movb %al, -0x6e9(%rbp)
jmp 0x604a
movb -0x6e9(%rbp), %al
testb $0x1, %al
jne 0x6059
jmp 0x60dc
leaq 0x3212c(%rip), %rdi     # 0x3818c
xorl %eax, %eax
callq 0x5060
jmp 0x6069
jmp 0x60f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x60ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x60a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x530(%rbp), %rdi
callq 0x5240
leaq -0x550(%rbp), %rdi
callq 0x5240
leaq -0x551(%rbp), %rdi
callq 0x5380
jmp 0x6162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x6d10
jmp 0x6162
leaq 0x320ad(%rip), %rdi     # 0x38190
xorl %eax, %eax
callq 0x5060
jmp 0x60ec
movl $0x2, -0x14(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x6d10
jmp 0x6118
leaq 0x320c0(%rip), %rdi     # 0x381c8
xorl %eax, %eax
callq 0x5060
jmp 0x6111
movl $0x3, -0x14(%rbp)
jmp 0x611a
jmp 0x6133
leaq 0x320f1(%rip), %rdi     # 0x38214
xorl %eax, %eax
callq 0x5060
jmp 0x612c
movl $0x1, -0x14(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x6d90
leaq -0x158(%rbp), %rdi
callq 0x32ac0
leaq -0xa0(%rbp), %rdi
callq 0x5240
leaq -0x78(%rbp), %rdi
callq 0x5240
jmp 0x6191
leaq -0x290(%rbp), %rdi
callq 0x6d90
leaq -0x158(%rbp), %rdi
callq 0x32ac0
leaq -0xa0(%rbp), %rdi
callq 0x5240
leaq -0x78(%rbp), %rdi
callq 0x5240
jmp 0x61ac
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
leaq -0x1a(%rbp), %rdi
callq 0x6b70
movl -0x4(%rbp), %eax
addq $0x710, %rsp            # imm = 0x710
popq %rbp
retq
leaq -0x1a(%rbp), %rdi
callq 0x6b70
movq -0x50(%rbp), %rdi
callq 0x5530
nop