// File: Stygain[P]bandwidth-monitor/src/interface.cpp
// Function: Interface::Update()
void Interface::Update()
{
	extern Logger *logger;
	FILE *fp = fopen("/proc/net/dev", "r");
	char buf[200];
	char ifname[20];
	unsigned long int r_bytes;
	unsigned long int t_bytes;
	unsigned long int r_packets;
	unsigned long int t_packets;

	if (fp != NULL)
	{
		// skip first two lines
		for (int i = 0; i < 2; i++) {
			fgets(buf, 200, fp);
		}

		while (fgets(buf, 200, fp)) {
			sscanf(buf, "%s %lu %lu %*lu %*lu %*lu %*lu %*lu %*lu %lu %lu",
					ifname, &r_bytes, &r_packets, &t_bytes, &t_packets);
			ifname[strlen(ifname) - 1] = '\0';

			if (strcmp(ifname, this->name) == 0)
			{
				this->r_bytesLastLast = this->r_bytesLast;
				this->r_bytesLast = this->r_bytes;
				this->r_bytes = r_bytes;
				this->t_bytesLastLast = this->t_bytesLast;
				this->t_bytesLast = this->t_bytes;
				this->t_bytes = t_bytes;
				this->r_packetsLast = this->r_packets;
				this->r_packets = r_packets;
				this->t_packetsLast = this->t_packets;
				this->t_packets = t_packets;

				if (((this->r_bytes - this->r_bytesLast) + (this->r_bytesLast - this->r_bytesLastLast)) == 0)
				{
					this->r_bps = 0;
				}
				else
				{
					this->r_bps = (int)(((this->r_bytes - this->r_bytesLast) + (this->r_bytesLast - this->r_bytesLastLast)) / 2);
				}
				if (((this->t_bytes - this->t_bytesLast) + (this->t_bytesLast - this->t_bytesLastLast)) == 0)
				{
					this->t_bps = 0;
				}
				else
				{
					this->t_bps = (int)(((this->t_bytes - this->t_bytesLast) + (this->t_bytesLast - this->t_bytesLastLast)) / 2);
				}
				break;
			}
		}

		fclose(fp);
	}

	char ifaceType[] = "/sys/class/net/";
	strcat(ifaceType, this->name);
	strcat(ifaceType, "/operstate");
	fp = fopen(ifaceType, "r");

	logger->Log("Iface file: ");
	logger->Log(ifaceType);
	logger->Log("\n");
	if (fp != NULL)
	{
		fgets(buf, 200, fp);
		sscanf(buf, "%s", this->operstate);
		logger->Log("This operstate: ");
		logger->Log(this->operstate);
		logger->Log("\n");

		fclose(fp);
	}

	if (this->wireless)
	{
		fp = fopen("/proc/net/wireless", "r");

		if (fp != NULL)
		{
			logger->Log("In /proc/net/wireless\n");
			// Skip first two lines
			for (int i = 0; i < 2; i++) {
				fgets(buf, 200, fp);
			}

			int linkQual;
			int levelQual;
			int noiseQual;

			char wirelessInterfaceName[20];
			while (fgets(buf, 200, fp)) {
				sscanf(buf, "%s %*lu %d. %d. %d",
						wirelessInterfaceName, &linkQual, &levelQual, &noiseQual);
				wirelessInterfaceName[strlen(wirelessInterfaceName) - 1] = '\0';

				if (strcmp(wirelessInterfaceName, this->name) == 0)
				{
					logger->Log("Read: ");
					logger->Log(std::to_string(linkQual));
					logger->Log(", ");
					logger->Log(std::to_string(levelQual));
					logger->Log(", ");
					logger->Log(std::to_string(noiseQual));
					logger->Log("\n");
					this->linkQual = linkQual;
					this->levelQual = levelQual;
					this->noiseQual = noiseQual;
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq 0x3a8c(%rip), %rdi      # 0x160fd
leaq 0x4216(%rip), %rsi      # 0x1688e
callq 0x33e0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x12874
movl $0x0, -0x124(%rbp)
cmpl $0x2, -0x124(%rbp)
jge 0x126c5
leaq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0xc8, %esi
callq 0x3520
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x12696
jmp 0x126c7
leaq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0xc8, %esi
callq 0x3520
cmpq $0x0, %rax
je 0x1286b
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rdx
leaq 0x3edb(%rip), %rsi      # 0x165d6
leaq -0x108(%rbp), %rcx
leaq -0x118(%rbp), %r8
leaq -0x110(%rbp), %r9
leaq -0x120(%rbp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0x32f0
leaq -0x100(%rbp), %rdi
callq 0x3190
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
subq $0x1, %rcx
movb $0x0, -0x100(%rbp,%rcx)
leaq -0x100(%rbp), %rdi
movq (%rax), %rsi
callq 0x3460
cmpl $0x0, %eax
jne 0x12866
movq -0x1e8(%rbp), %rdx
movq 0x10(%rdx), %rax
movq %rax, 0x18(%rdx)
movq 0x8(%rdx), %rax
movq %rax, 0x10(%rdx)
movq -0x108(%rbp), %rax
movq %rax, 0x8(%rdx)
movq 0x30(%rdx), %rax
movq %rax, 0x38(%rdx)
movq 0x28(%rdx), %rax
movq %rax, 0x30(%rdx)
movq -0x110(%rbp), %rax
movq %rax, 0x28(%rdx)
movq 0x48(%rdx), %rax
movq %rax, 0x50(%rdx)
movq -0x118(%rbp), %rax
movq %rax, 0x48(%rdx)
movq 0x60(%rdx), %rax
movq %rax, 0x68(%rdx)
movq -0x120(%rbp), %rax
movq %rax, 0x60(%rdx)
movq 0x8(%rdx), %rax
subq 0x10(%rdx), %rax
movq 0x10(%rdx), %rcx
subq 0x18(%rdx), %rcx
addq %rcx, %rax
cmpq $0x0, %rax
jne 0x127eb
movq -0x1e8(%rbp), %rax
movl $0x0, 0x9c(%rax)
jmp 0x1280e
movq -0x1e8(%rbp), %rax
movq 0x8(%rax), %rcx
subq 0x10(%rax), %rcx
movq 0x10(%rax), %rdx
subq 0x18(%rax), %rdx
addq %rdx, %rcx
shrq %rcx
movl %ecx, 0x9c(%rax)
movq -0x1e8(%rbp), %rdx
movq 0x28(%rdx), %rax
subq 0x30(%rdx), %rax
movq 0x30(%rdx), %rcx
subq 0x38(%rdx), %rcx
addq %rcx, %rax
cmpq $0x0, %rax
jne 0x12841
movq -0x1e8(%rbp), %rax
movl $0x0, 0xa0(%rax)
jmp 0x12864
movq -0x1e8(%rbp), %rax
movq 0x28(%rax), %rcx
subq 0x30(%rax), %rcx
movq 0x30(%rax), %rdx
subq 0x38(%rax), %rdx
addq %rdx, %rcx
shrq %rcx
movl %ecx, 0xa0(%rax)
jmp 0x1286b
jmp 0x126c7
movq -0x10(%rbp), %rdi
callq 0x3310
movq -0x1e8(%rbp), %rax
movq 0x3e7e(%rip), %rcx      # 0x16700
movq %rcx, -0x140(%rbp)
movq 0x3e78(%rip), %rcx      # 0x16708
movq %rcx, -0x138(%rbp)
leaq -0x140(%rbp), %rdi
movq (%rax), %rsi
callq 0x35e0
leaq -0x140(%rbp), %rdi
leaq 0x3d53(%rip), %rsi      # 0x16607
callq 0x35e0
leaq -0x140(%rbp), %rdi
leaq 0x3fc7(%rip), %rsi      # 0x1688e
callq 0x33e0
movq %rax, -0x10(%rbp)
leaq 0xbaf1(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x3d31(%rip), %rsi      # 0x16612
callq 0x13910
leaq 0xbadb(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq -0x140(%rbp), %rsi
callq 0x138e0
leaq 0xbac5(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x3956(%rip), %rsi      # 0x16263
callq 0x13910
cmpq $0x0, -0x10(%rbp)
je 0x129a1
leaq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0xc8, %esi
callq 0x3520
movq -0x1e8(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
addq $0x78, %rdx
leaq 0x3d56(%rip), %rsi      # 0x166a1
movb $0x0, %al
callq 0x32f0
leaq 0xba6f(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x3cbc(%rip), %rsi      # 0x1661f
callq 0x13910
movq -0x1e8(%rbp), %rsi
leaq 0xba52(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
addq $0x78, %rsi
callq 0x138e0
leaq 0xba3f(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x38d0(%rip), %rsi      # 0x16263
callq 0x13910
movq -0x10(%rbp), %rdi
callq 0x3310
movq -0x1e8(%rbp), %rax
testb $0x1, 0x8c(%rax)
je 0x12c96
leaq 0x3714(%rip), %rdi      # 0x160d0
leaq 0x3ecb(%rip), %rsi      # 0x1688e
callq 0x33e0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x12c94
leaq 0xb9ea(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x3c48(%rip), %rsi      # 0x16630
callq 0x13910
movl $0x0, -0x144(%rbp)
cmpl $0x2, -0x144(%rbp)
jge 0x12a26
leaq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0xc8, %esi
callq 0x3520
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0x129f7
jmp 0x12a28
leaq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0xc8, %esi
callq 0x3520
cmpq $0x0, %rax
je 0x12c92
leaq -0xe0(%rbp), %rdi
leaq -0x170(%rbp), %rdx
leaq 0x3beb(%rip), %rsi      # 0x16647
leaq -0x148(%rbp), %rcx
leaq -0x14c(%rbp), %r8
leaq -0x150(%rbp), %r9
movb $0x0, %al
callq 0x32f0
leaq -0x170(%rbp), %rdi
callq 0x3190
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
subq $0x1, %rcx
movb $0x0, -0x170(%rbp,%rcx)
leaq -0x170(%rbp), %rdi
movq (%rax), %rsi
callq 0x3460
cmpl $0x0, %eax
jne 0x12c8d
leaq 0xb90f(%rip), %rax      # 0x1e3c8
movq %rax, -0x200(%rbp)
movq (%rax), %rdi
leaq 0x3b90(%rip), %rsi      # 0x1665a
callq 0x13910
movq -0x200(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1f8(%rbp)
movl -0x148(%rbp), %esi
leaq -0x190(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x9d40
movq -0x1f8(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x13940
jmp 0x12b0e
leaq -0x190(%rbp), %rdi
callq 0x3250
leaq 0xb8a7(%rip), %rax      # 0x1e3c8
movq %rax, -0x218(%rbp)
movq (%rax), %rdi
leaq 0x3b2f(%rip), %rsi      # 0x16661
callq 0x13910
movq -0x218(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x210(%rbp)
movl -0x14c(%rbp), %esi
leaq -0x1c0(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x9d40
movq -0x210(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0x13940
jmp 0x12b76
leaq -0x1c0(%rbp), %rdi
callq 0x3250
leaq 0xb83f(%rip), %rax      # 0x1e3c8
movq %rax, -0x230(%rbp)
movq (%rax), %rdi
leaq 0x3ac7(%rip), %rsi      # 0x16661
callq 0x13910
movq -0x230(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x228(%rbp)
movl -0x150(%rbp), %esi
leaq -0x1e0(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x9d40
movq -0x228(%rbp), %rdi
movq -0x220(%rbp), %rsi
callq 0x13940
jmp 0x12bde
leaq -0x1e0(%rbp), %rdi
callq 0x3250
leaq 0xb7d7(%rip), %rax      # 0x1e3c8
movq (%rax), %rdi
leaq 0x3668(%rip), %rsi      # 0x16263
callq 0x13910
movq -0x1e8(%rbp), %rax
movl -0x148(%rbp), %ecx
movl %ecx, 0x90(%rax)
movl -0x14c(%rbp), %ecx
movl %ecx, 0x94(%rax)
movl -0x150(%rbp), %ecx
movl %ecx, 0x98(%rax)
jmp 0x12c8d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x198(%rbp)
movl %eax, -0x19c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x3250
jmp 0x12c9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x198(%rbp)
movl %eax, -0x19c(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x3250
jmp 0x12c9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x198(%rbp)
movl %eax, -0x19c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3250
jmp 0x12c9f
jmp 0x12a28
jmp 0x12c94
jmp 0x12c96
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
movq -0x198(%rbp), %rdi
callq 0x35c0
nopl (%rax,%rax)