// File: /workspace/github_c_makefile_star2_O3/jelaas[P]egetty/econsole.c
// Function: main
int main(int argc, char **argv)
{
	struct sockaddr_ll from;
	socklen_t fromlen = sizeof(from);
	char *device = "eth0", *ps;
	uint8_t *buf, *p;
	int n, i, err=0;
	unsigned int len;
	struct sk_buff *skb;

	conf.ifindex=-1;
	conf.debug = 0;

	if(jelopt(argv, 'h', "help", NULL, &err)) {
		printf("econsole [DEV] [CONSOLE] [DESTMAC] [(scan|debug)]\n"); 
		exit(0);
	}
	argc = jelopt_final(argv, &err);
	if(err) {
		printf("Syntax error in arguments.\n");
		exit(2);
	}

	while(--argc > 0) {
		if(strcmp(argv[argc], "scan")==0) {
			printf("Scanning for econsoles\n");
			conf.scan = 1;
			continue;
		}
		if(strcmp(argv[argc], "debug")==0) {
			printf("Debug mode\n");
			conf.debug++;
			continue;
		}
		if( (strlen(argv[argc]) < 3) && isdigit(*argv[argc])) {
			conf.console = atoi(argv[argc]);
			continue;
		}
		if(strchr(argv[argc], ':' )) {
			unsigned int a;
			ps = argv[argc];
			for(i=0;i<6;i++) {
				sscanf(ps, "%x", &a);
				conf.dest.sll_addr[i] = a;
				ps = strchr(ps, ':');
				if(!ps) break;
				ps++;
			}
			conf.ucast = 1;
			continue;
		} else {
			device = argv[argc];
		}
	}
	
	conf.devsocket = devsocket();
	
	while(set_flag(device, (IFF_UP | IFF_RUNNING))) {
		printf("Waiting for interface to be available\n");
		sleep(1);
	}
	
	if(device)
	{
		conf.ifindex = if_nametoindex(device);
		if(!conf.ifindex)
		{
			fprintf(stderr, "no such device %s\n", device);
			exit(1);
		}
	}

	conf.s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_EGETTY));
	if(conf.s == -1)
	{
		fprintf(stderr, "socket(): %s\n", strerror(errno));
		exit(1);
	}


	if(conf.ifindex >= 0)
	{
		struct sockaddr_ll addr;
		memset(&addr, 0, sizeof(addr));
		
		addr.sll_family = AF_PACKET;
		addr.sll_protocol = htons(ETH_P_EGETTY);
		addr.sll_ifindex = conf.ifindex;
		
		if(bind(conf.s, (const struct sockaddr *)&addr, sizeof(addr)))
		{
			fprintf(stderr, "bind failed: %s\n", strerror(errno));
			exit(1);
		}
	}

	if(!conf.scan) {
		terminal_settings();
		signals_init();
		winch_handler(0);
		fprintf(stderr, "Use CTRL-] to close connection.\n");
	}

	skb = alloc_skb(1500);

	if(conf.scan)
		console_scan(conf.s, conf.ifindex, skb);

	while(1)
	{
		struct pollfd fds[2];

		fds[0].fd = 0;
		fds[0].events = POLLIN;
		fds[0].revents = 0;
		
		fds[1].fd = conf.s;
		fds[1].events = POLLIN;
		fds[1].revents = 0;

		n = poll(fds, 2, -1);
		if(n == 0) {
			printf("timeout\n");
			continue;
		}

		if(fds[0].revents & POLLIN) {
			skb_reset(skb);
			skb_reserve(skb, 4);
			buf = skb_put(skb, 0);
			n = read(0, buf, skb_tailroom(skb));
			if(n == -1) {
				fprintf(stderr, "read() failed\n");
				exit(1);
			}
			if(n == 0) {
				fprintf(stderr, "read() EOF\n");
				exit(0);
			}
			buf[n] = 0;
			if(conf.debug) printf("read %d bytes from stdin\n", n);
			if(conf.debug > 1) printf("buf[0] == %d\n", buf[0]);
			if(n==1 && buf[0] == 0x1d) {
				console_hup(conf.s, conf.ifindex);
				tcsetattr(0, TCSANOW, &conf.term);
				exit(0);
			}
			skb_put(skb, n);
			if(!conf.scan) console_put(conf.s, conf.ifindex, skb);
		}
		if(fds[1].revents) {
			skb_reset(skb);
			buf = skb_put(skb, 0);
			n = recvfrom(conf.s, buf, skb_tailroom(skb), 0, (struct sockaddr *)&from, &fromlen);
			if(n == -1) {
				fprintf(stderr, "recvfrom() failed. ifconfig up?\n");
				continue;
			}
			skb_put(skb, n);

			if(conf.ucast)
				if(memcmp(conf.dest.sll_addr, from.sll_addr, 6))
					continue;
			
			if(ntohs(from.sll_protocol) == ETH_P_EGETTY) {
				if(conf.debug) printf("Received EGETTY\n");
				p = skb->data;
				if(*p == EGETTY_HELLO) {
					if(conf.scan) {
						p++;
						printf("Console: %d ", *p);
						for(i=0;i<6;i++)
							printf("%02x%s", from.sll_addr[i], i==5?"":":");
						printf("\n");
					}
					continue;
				}
				if(*p == EGETTY_OUT || *p == EGETTY_KMSG) {
					p++;
					if(*p++ != conf.console) continue;
					len = *p++ << 8;
					len += *p;
					skb_trim(skb, len);
					skb_pull(skb, 4);
					if(!conf.scan) write(1, skb->data, skb->len);
				}
				continue;
			}
		}
		
	}
	
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movl $0x14, 0xc(%rsp)
xorl %eax, %eax
leaq 0x8(%rsp), %r8
movl %eax, (%r8)
movl $0xffffffff, 0xf18a8(%rip) # imm = 0xFFFFFFFF
movl %eax, 0xf1882(%rip)     # 0x4f3310
leaq 0xbd574(%rip), %rdx     # 0x4bf009
movq %rsi, %rdi
movl $0x68, %esi
xorl %ecx, %ecx
callq 0x402d66
testl %eax, %eax
jne 0x401c24
leaq 0x8(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x40279c
cmpl $0x0, (%rbx)
jne 0x401c37
movl %eax, %r15d
leaq 0xbd535(%rip), %rbx     # 0x4bf004
cmpl $0x2, %eax
jl 0x401c52
leaq 0xbd534(%rip), %r13     # 0x4bf013
leaq 0x30(%rsp), %rbp
leaq 0xf1825(%rip), %r12     # 0x4f3310
movq %r14, 0x80(%rsp)
movq %rbx, 0x78(%rsp)
movl %r15d, %r15d
leaq -0x1(%r15), %rax
movq %rax, 0x28(%rsp)
movl %eax, %eax
movq (%r14,%rax,8), %rbx
movq %rbx, %rdi
leaq 0xbd4fa(%rip), %rsi     # 0x4bf00e
callq 0x4010b0
testl %eax, %eax
je 0x401bc8
movq %rbx, %rdi
leaq 0xc383a(%rip), %rsi     # 0x4c5365
callq 0x4010b0
testl %eax, %eax
je 0x401be0
movq %rbx, %rdi
callq 0x4010e0
cmpq $0x2, %rax
ja 0x401b5d
callq 0x405af0
movq (%rax), %rax
movsbq (%rbx), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x401bf4
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x4010e8
testq %rax, %rax
je 0x401c13
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x40d780
movb 0x30(%rsp), %al
movb %al, 0x30(%r14,%r12)
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x4010e8
testq %rax, %rax
je 0x401bb4
movq %rax, %rbx
incq %rbx
leaq 0x1(%r14), %rax
cmpq $0x5, %r14
movq %rax, %r14
jb 0x401b76
movl $0x1, 0xf1762(%rip)     # 0x4f3320
movq 0x80(%rsp), %r14
jmp 0x401c02
leaq 0xbd592(%rip), %rdi     # 0x4bf161
callq 0x41c260
movl $0x1, 0xf173e(%rip)     # 0x4f331c
jmp 0x401c02
leaq 0xbd56f(%rip), %rdi     # 0x4bf156
callq 0x41c260
incl 0xf171e(%rip)           # 0x4f3310
jmp 0x401c02
movq %rbx, %rdi
callq 0x40b910
movl %eax, 0xf1712(%rip)     # 0x4f3314
cmpl $0x2, %r15d
movq 0x28(%rsp), %r15
jg 0x401afb
jmp 0x401c4d
cmpl $0x3, %r15d
movq 0x28(%rsp), %r15
jge 0x401af3
jmp 0x401c52
leaq 0xbd568(%rip), %rdi     # 0x4bf193
callq 0x41c260
xorl %edi, %edi
callq 0x40c490
leaq 0xbd53a(%rip), %rdi     # 0x4bf178
callq 0x41c260
movl $0x2, %edi
callq 0x40c490
movq 0x78(%rsp), %rbx
xorl %r14d, %r14d
leaq 0xbd56c(%rip), %r15     # 0x4bf1c8
movl (%r14,%r15), %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x45c810
testl %eax, %eax
jns 0x401c81
addq $0x4, %r14
cmpq $0xc, %r14
jne 0x401c5c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x401c83
movl %eax, %ebp
movl %ebp, 0xf168f(%rip)     # 0x4f3318
leaq 0x30(%rsp), %r14
leaq 0xbd49b(%rip), %r15     # 0x4bf130
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x50(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %ebp, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x45a740
testl %eax, %eax
js 0x401cf4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x401020
orb $0x41, 0x40(%rsp)
movl 0xf1637(%rip), %edi     # 0x4f3318
movl $0x8914, %esi           # imm = 0x8914
movq %r14, %rdx
xorl %eax, %eax
callq 0x45a740
testl %eax, %eax
jns 0x401d0e
movq %r15, %rdi
callq 0x41c260
movl $0x1, %edi
callq 0x458d20
movl 0xf160c(%rip), %ebp     # 0x4f3318
jmp 0x401c95
testq %rbx, %rbx
je 0x401d3b
movq %rbx, %rdi
callq 0x45cba0
movl %eax, 0xf160f(%rip)     # 0x4f3330
testl %eax, %eax
jne 0x401d3b
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rdi
leaq 0xbd2e0(%rip), %rsi     # 0x4bf016
movq %rbx, %rdx
jmp 0x401d7d
movl $0x11, %edi
movl $0x2, %esi
movl $0x1168, %edx           # imm = 0x1168
callq 0x45c810
movl %eax, 0xf15d7(%rip)     # 0x4f332c
cmpl $-0x1, %eax
jne 0x401d8e
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405930
movl (%rax), %edi
callq 0x42c040
leaq 0xbd2b2(%rip), %rsi     # 0x4bf029
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d530
movl $0x1, %edi
callq 0x40c490
movl 0xf159c(%rip), %ecx     # 0x4f3330
testl %ecx, %ecx
js 0x401dc9
leaq 0x30(%rsp), %rsi
movl $0x0, 0x10(%rsi)
movq $0x0, 0x8(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ecx, 0x4(%rsi)
movl %eax, %edi
movl $0x14, %edx
callq 0x45c640
testl %eax, %eax
jne 0x4022f2
cmpl $0x0, 0xf154c(%rip)     # 0x4f331c
jne 0x401eb0
leaq 0xf156b(%rip), %rsi     # 0x4f3348
xorl %edi, %edi
callq 0x45a550
leaq 0x30(%rsp), %rsi
xorl %edi, %edi
callq 0x45a550
testl %eax, %eax
je 0x401e1d
cmpl $0x0, 0xf1515(%rip)     # 0x4f3310
je 0x401e1d
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd2d3(%rip), %rdi     # 0x4bf0e1
movl $0x11, %esi
movl $0x1, %edx
callq 0x41bf90
movl $0xffffe01b, %eax       # imm = 0xFFFFE01B
leaq 0x30(%rsp), %rdx
andl (%rdx), %eax
orl $0x4, %eax
movl %eax, (%rdx)
andl $0xffff7f84, 0xc(%rdx)  # imm = 0xFFFF7F84
andb $-0x2, 0x4(%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x45a360
leaq 0xf153f(%rip), %rbx     # 0x4f3388
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4010a0
leaq 0xf1531(%rip), %rdi     # 0x4f3390
callq 0x40b8e0
leaq 0x4cf(%rip), %rax       # 0x40233a
movq %rax, 0xf1516(%rip)     # 0x4f3388
movl $0x0, 0xf1594(%rip)     # 0x4f3410
movl $0x1c, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x40b6a0
callq 0x40233a
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd1a7(%rip), %rdi     # 0x4bf048
movl $0x20, %esi
movl $0x1, %edx
callq 0x41bf90
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x402568
movq %rax, %rbx
cmpl $0x0, 0xf1458(%rip)     # 0x4f331c
je 0x401f73
movl 0xf145c(%rip), %ebp     # 0x4f332c
movl 0xf1459(%rip), %r14d    # 0x4f3330
movq %rbx, %rdi
movl $0x4, %esi
callq 0x40276f
movb $0x0, (%rax)
movb 0xf1427(%rip), %cl      # 0x4f3314
movb %cl, 0x1(%rax)
movb 0x1(%rbx), %cl
movb %cl, 0x2(%rax)
movb (%rbx), %cl
movb %cl, 0x3(%rax)
movl (%rbx), %edx
movq 0x38(%rbx), %rsi
leaq 0x30(%rsp), %r8
movq $0x0, 0x8(%r8)
movl $0x0, 0x10(%r8)
movl $0x11680011, (%r8)      # imm = 0x11680011
movb $0x6, 0xb(%r8)
movl %r14d, 0x4(%r8)
movl $0xffffffff, 0xc(%r8)   # imm = 0xFFFFFFFF
movw $0xffff, 0x10(%r8)      # imm = 0xFFFF
movl %ebp, %edi
xorl %ecx, %ecx
movl $0x14, %r9d
callq 0x45c740
cmpl $-0x1, %eax
jne 0x401f73
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %r14
callq 0x405930
movl (%rax), %edi
callq 0x42c040
leaq 0xbd18d(%rip), %rsi     # 0x4bf0f3
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d530
leaq 0x10(%rsp), %r14
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x10(%rsp)
movl 0xf139f(%rip), %eax     # 0x4f332c
movl %eax, 0x18(%rsp)
movl $0x1, 0x1c(%rsp)
movl $0x2, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x45a040
testl %eax, %eax
je 0x40217b
testb $0x1, 0x16(%rsp)
je 0x4020ce
movq %rbx, %rdi
callq 0x4025b9
movq %rbx, %rdi
movl $0x4, %esi
callq 0x40274c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40275f
movq %rax, %r15
movq %rbx, %rdi
callq 0x402758
movslq %eax, %rdx
xorl %edi, %edi
movq %r15, %rsi
callq 0x4597b0
movq %rax, %r12
testl %r12d, %r12d
je 0x4022a8
cmpl $-0x1, %r12d
je 0x4022cd
movslq %r12d, %rax
movb $0x0, (%r15,%rax)
cmpl $0x0, 0xf12f6(%rip)     # 0x4f3310
je 0x402048
leaq 0xbd061(%rip), %rdi     # 0x4bf084
movl %r12d, %esi
xorl %eax, %eax
callq 0x40d5f0
cmpl $0x2, 0xf12dc(%rip)     # 0x4f3310
jl 0x402048
movzbl (%r15), %esi
leaq 0xbd05d(%rip), %rdi     # 0x4bf09e
xorl %eax, %eax
callq 0x40d5f0
cmpl $0x1, %r12d
jne 0x402058
cmpb $0x1d, (%r15)
je 0x402314
movq %rbx, %rdi
movl %r12d, %esi
callq 0x40275f
cmpl $0x0, 0xf12b2(%rip)     # 0x4f331c
jne 0x4020ce
movl 0xf12b9(%rip), %r15d    # 0x4f332c
movl 0xf12b6(%rip), %r12d    # 0x4f3330
movq %rbx, %rdi
movl $0x4, %esi
callq 0x40276f
movb $0x4, (%rax)
movb 0xf1284(%rip), %cl      # 0x4f3314
movb %cl, 0x1(%rax)
movb 0x1(%rbx), %cl
movb %cl, 0x2(%rax)
movb (%rbx), %cl
movb %cl, 0x3(%rax)
movl %r15d, %edi
movl %r12d, %esi
movq %rbx, %rdx
callq 0x402478
cmpl $-0x1, %eax
jne 0x4020ce
callq 0x405930
movl (%rax), %edi
callq 0x42c040
leaq 0xbd02f(%rip), %rdi     # 0x4bf0f3
movq %rax, %rsi
xorl %eax, %eax
callq 0x40d5f0
cmpw $0x0, 0x1e(%rsp)
je 0x401f78
movq %rbx, %rdi
callq 0x4025b9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40275f
movq %rax, %r15
movl 0xf1236(%rip), %r12d    # 0x4f332c
movq %rbx, %rdi
callq 0x402758
movslq %eax, %rdx
movl %r12d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x45c670
cmpl $-0x1, %eax
je 0x402283
movq %rbx, %rdi
movl %eax, %esi
callq 0x40275f
cmpl $0x0, 0xf11ee(%rip)     # 0x4f3320
je 0x40218c
movl 0xf1206(%rip), %eax     # 0x4f3340
xorl 0x3c(%rsp), %eax
movzwl 0xf11ff(%rip), %ecx     # 0x4f3344
xorw 0x40(%rsp), %cx
movzwl %cx, %ecx
orl %eax, %ecx
leaq 0xd8d27(%rip), %r12     # 0x4dae7d
leaq 0xc2d39(%rip), %rbp     # 0x4c4e96
leaq 0xbcf76(%rip), %r13     # 0x4bf0da
jne 0x401f78
movzwl 0x32(%rsp), %eax
cmpl $0x1168, %eax           # imm = 0x1168
je 0x4021ae
jmp 0x401f78
leaq 0xbcfa6(%rip), %rdi     # 0x4bf128
callq 0x41c260
jmp 0x401f78
cmpw $0x1168, 0x32(%rsp)     # imm = 0x1168
leaq 0xd8ce3(%rip), %r12     # 0x4dae7d
leaq 0xc2cf5(%rip), %rbp     # 0x4c4e96
leaq 0xbcf32(%rip), %r13     # 0x4bf0da
jne 0x401f78
cmpl $0x0, 0xf115b(%rip)     # 0x4f3310
je 0x4021c3
leaq 0xbcf5a(%rip), %rdi     # 0x4bf118
callq 0x41c260
movq 0x38(%rbx), %rax
movzbl (%rax), %ecx
cmpl $0x1, %ecx
je 0x402231
cmpl $0x5, %ecx
je 0x402231
cmpl $0x3, %ecx
jne 0x401f78
cmpl $0x0, 0xf1138(%rip)     # 0x4f331c
je 0x401f78
movzbl 0x1(%rax), %esi
leaq 0xbced8(%rip), %rdi     # 0x4bf0cd
xorl %eax, %eax
callq 0x40d5f0
movq $-0x6, %r15
movzbl 0x42(%rsp,%r15), %esi
incq %r15
movq %rbp, %rdx
cmoveq %r12, %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x40d5f0
testq %r15, %r15
jne 0x402203
movl $0xa, %edi
callq 0x41c400
jmp 0x401f78
movzbl 0x1(%rax), %ecx
cmpl %ecx, 0xf10d9(%rip)     # 0x4f3314
jne 0x401f78
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x40278c
movq %rbx, %rdi
movl $0x4, %esi
callq 0x40277f
cmpl $0x0, 0xf10b4(%rip)     # 0x4f331c
jne 0x401f78
movq 0x38(%rbx), %rsi
movl (%rbx), %edx
movl $0x1, %edi
callq 0x459850
jmp 0x401f78
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
movl $0x20, %esi
movl $0x1, %edx
leaq 0xbce0e(%rip), %rdi     # 0x4bf0ac
callq 0x41bf90
jmp 0x401f78
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbcdbf(%rip), %rdi     # 0x4bf078
movl $0xb, %esi
movl $0x1, %edx
callq 0x41bf90
jmp 0x401c30
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbcd8b(%rip), %rdi     # 0x4bf069
movl $0xe, %esi
movl $0x1, %edx
callq 0x41bf90
jmp 0x401d84
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405930
movl (%rax), %edi
callq 0x42c040
leaq 0xbcd28(%rip), %rsi     # 0x4bf037
jmp 0x401d77
movl 0xf1012(%rip), %edi     # 0x4f332c
movl 0xf1010(%rip), %esi     # 0x4f3330
callq 0x402404
leaq 0xf101c(%rip), %rdx     # 0x4f3348
xorl %edi, %edi
xorl %esi, %esi
callq 0x45a360
jmp 0x401c30