// File: dcdolson[P]nsh-sf-devkit/src/PacketSocket/PacketSocket.cpp
// Function: nshdev::PacketSocket::PacketSocket(int, unsigned short)
PacketSocket::PacketSocket(int ifIndex, uint16_t etherType)
    {
	// looking for specified Ethertype, with Ethernet header removed.
	m_socket = socket(AF_PACKET, SOCK_DGRAM, htons(etherType));
	if(m_socket < 0)
	{
	    throw ErrMsg("Couldn't open socket", errno);
	}
	struct sockaddr_ll  bind_addr;
	memset(&bind_addr, 0, sizeof(bind_addr));
	bind_addr.sll_family = AF_PACKET;
	bind_addr.sll_ifindex = ifIndex;
	bind_addr.sll_protocol = htons(etherType);
	int const bind_result = bind(m_socket, reinterpret_cast<const struct sockaddr*>(&bind_addr), sizeof(bind_addr));
	if(bind_result != 0)
	{
	    close(m_socket);
	    throw ErrMsg("Unable to bind socket", errno);
	}
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movw %dx, %ax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movw %ax, -0xe(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x5900
movq -0x90(%rbp), %rax
leaq 0x5879(%rip), %rcx      # 0xac20
addq $0x10, %rcx
movq %rcx, (%rax)
movzwl -0xe(%rbp), %edi
callq 0x3050
movzwl %ax, %edx
movl $0x11, %edi
movl $0x2, %esi
callq 0x30a0
movl %eax, %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x10(%rax)
cmpl $0x0, 0x10(%rax)
jge 0x549e
movb $0x1, -0x45(%rbp)
movl $0x28, %edi
callq 0x3130
movq %rax, -0xa0(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x33a0
movq -0x98(%rbp), %rdx
leaq 0x1da6(%rip), %rsi      # 0x71b8
leaq -0x30(%rbp), %rdi
callq 0x4100
jmp 0x541d
callq 0x3030
movq -0xa0(%rbp), %rdi
movl (%rax), %edx
leaq -0x30(%rbp), %rsi
callq 0x5930
jmp 0x5436
movq -0xa0(%rbp), %rdi
movb $0x0, -0x45(%rbp)
leaq 0x5828(%rip), %rsi      # 0xac70
leaq 0x681(%rip), %rdx       # 0x5ad0
callq 0x3360
jmp 0x55f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x547c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3150
leaq -0x31(%rbp), %rdi
callq 0x3270
testb $0x1, -0x45(%rbp)
jne 0x548d
jmp 0x5499
movq -0xa0(%rbp), %rdi
callq 0x31a0
jmp 0x55df
leaq -0x5c(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x3140
movw $0x11, -0x5c(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x58(%rbp)
movzwl -0xe(%rbp), %edi
callq 0x3050
movw %ax, %cx
movq -0x90(%rbp), %rax
movw %cx, -0x5a(%rbp)
movl 0x10(%rax), %edi
leaq -0x5c(%rbp), %rsi
movl $0x14, %edx
callq 0x3070
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0x55d6
movq -0x90(%rbp), %rax
movl 0x10(%rax), %edi
callq 0x33e0
jmp 0x5500
movb $0x1, -0x82(%rbp)
movl $0x28, %edi
callq 0x3130
movq %rax, -0xb0(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x33a0
movq -0xa8(%rbp), %rdx
leaq 0x1c94(%rip), %rsi      # 0x71cd
leaq -0x80(%rbp), %rdi
callq 0x4100
jmp 0x5544
callq 0x3030
movq -0xb0(%rbp), %rdi
movl (%rax), %edx
leaq -0x80(%rbp), %rsi
callq 0x5930
jmp 0x555d
movq -0xb0(%rbp), %rdi
movb $0x0, -0x82(%rbp)
leaq 0x56fe(%rip), %rsi      # 0xac70
leaq 0x557(%rip), %rdx       # 0x5ad0
callq 0x3360
jmp 0x55f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x55df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x55b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3150
leaq -0x81(%rbp), %rdi
callq 0x3270
testb $0x1, -0x82(%rbp)
jne 0x55c8
jmp 0x55d4
movq -0xb0(%rbp), %rdi
callq 0x31a0
jmp 0x55df
addq $0xb0, %rsp
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0x5ba0
movq -0x40(%rbp), %rdi
callq 0x3390
nopw %cs:(%rax,%rax)