// File: /workspace/github_c_makefile_star2_O1/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 0x402e49
testl %eax, %eax
jne 0x401c24
leaq 0x8(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4027d4
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 0x405be0
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 0x40d870
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 0x41c350
movl $0x1, 0xf173e(%rip)     # 0x4f331c
jmp 0x401c02
leaq 0xbd56f(%rip), %rdi     # 0x4bf156
callq 0x41c350
incl 0xf171e(%rip)           # 0x4f3310
jmp 0x401c02
movq %rbx, %rdi
callq 0x40ba00
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 0x41c350
xorl %edi, %edi
callq 0x40c580
leaq 0xbd53a(%rip), %rdi     # 0x4bf178
callq 0x41c350
movl $0x2, %edi
callq 0x40c580
movq 0x78(%rsp), %rbx
xorl %r14d, %r14d
leaq 0xbd56c(%rip), %r15     # 0x4bf1c8
movl (%r14,%r15), %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x45c910
testl %eax, %eax
jns 0x401c7f
addq $0x4, %r14
cmpq $0xc, %r14
jne 0x401c5c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0xf1693(%rip)     # 0x4f3318
leaq 0x30(%rsp), %r14
leaq 0xbd49f(%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 0xf1660(%rip), %edi     # 0x4f3318
movl $0x8913, %esi           # imm = 0x8913
movq %r14, %rdx
xorl %eax, %eax
callq 0x45a840
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 0x45a840
testl %eax, %eax
jns 0x401d08
movq %r15, %rdi
callq 0x41c350
movl $0x1, %edi
callq 0x458e20
jmp 0x401c91
testq %rbx, %rbx
je 0x401d35
movq %rbx, %rdi
callq 0x45cca0
movl %eax, 0xf1615(%rip)     # 0x4f3330
testl %eax, %eax
jne 0x401d35
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rdi
leaq 0xbd2e6(%rip), %rsi     # 0x4bf016
movq %rbx, %rdx
jmp 0x401d77
movl $0x11, %edi
movl $0x2, %esi
movl $0x1168, %edx           # imm = 0x1168
callq 0x45c910
movl %eax, 0xf15dd(%rip)     # 0x4f332c
cmpl $-0x1, %eax
jne 0x401d88
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405a20
movl (%rax), %edi
callq 0x42c130
leaq 0xbd2b8(%rip), %rsi     # 0x4bf029
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d620
movl $0x1, %edi
callq 0x40c580
movl 0xf15a2(%rip), %ecx     # 0x4f3330
testl %ecx, %ecx
js 0x401dc1
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movl $0x0, 0x10(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ecx, 0x4(%rsi)
movl %eax, %edi
movl $0x14, %edx
callq 0x45c740
testl %eax, %eax
jne 0x4022e9
cmpl $0x0, 0xf1554(%rip)     # 0x4f331c
jne 0x401ea8
leaq 0xf1573(%rip), %rsi     # 0x4f3348
xorl %edi, %edi
callq 0x45a650
leaq 0x30(%rsp), %rsi
xorl %edi, %edi
callq 0x45a650
testl %eax, %eax
je 0x401e15
cmpl $0x0, 0xf151d(%rip)     # 0x4f3310
je 0x401e15
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd2db(%rip), %rdi     # 0x4bf0e1
movl $0x11, %esi
movl $0x1, %edx
callq 0x41c080
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 0x45a460
leaq 0xf1547(%rip), %rbx     # 0x4f3388
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4010a0
leaq 0xf1539(%rip), %rdi     # 0x4f3390
callq 0x40b9d0
leaq 0x4ce(%rip), %rax       # 0x402331
movq %rax, 0xf151e(%rip)     # 0x4f3388
movl $0x0, 0xf159c(%rip)     # 0x4f3410
movl $0x1c, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x40b790
callq 0x402331
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd1af(%rip), %rdi     # 0x4bf048
movl $0x20, %esi
movl $0x1, %edx
callq 0x41c080
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x40255c
movq %rax, %rbx
cmpl $0x0, 0xf1460(%rip)     # 0x4f331c
je 0x401f6a
movl 0xf1464(%rip), %ebp     # 0x4f332c
movl 0xf1461(%rip), %r14d    # 0x4f3330
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4027aa
movb $0x0, (%rax)
movb 0xf142f(%rip), %cl      # 0x4f3314
movb %cl, 0x1(%rax)
movb 0x1(%rbx), %cl
movb %cl, 0x2(%rax)
movb (%rbx), %cl
movb %cl, 0x3(%rax)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r8
movaps %xmm0, (%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
movq 0x38(%rbx), %rsi
movl (%rbx), %edx
movl %ebp, %edi
xorl %ecx, %ecx
movl $0x14, %r9d
callq 0x45c840
cmpl $-0x1, %eax
jne 0x401f6a
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %r14
callq 0x405a20
movl (%rax), %edi
callq 0x42c130
leaq 0xbd196(%rip), %rsi     # 0x4bf0f3
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d620
leaq 0x10(%rsp), %r14
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x10(%rsp)
movl 0xf13a8(%rip), %eax     # 0x4f332c
movl %eax, 0x18(%rsp)
movl $0x1, 0x1c(%rsp)
movl $0x2, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x45a140
testl %eax, %eax
je 0x402172
testb $0x1, 0x16(%rsp)
je 0x4020c5
movq %rbx, %rdi
callq 0x4025ba
movq %rbx, %rdi
movl $0x4, %esi
callq 0x402787
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40279a
movq %rax, %r15
movq %rbx, %rdi
callq 0x402793
movslq %eax, %rdx
xorl %edi, %edi
movq %r15, %rsi
callq 0x4598b0
movq %rax, %r12
testl %r12d, %r12d
je 0x40229f
cmpl $-0x1, %r12d
je 0x4022c4
movslq %r12d, %rax
movb $0x0, (%r15,%rax)
cmpl $0x0, 0xf12ff(%rip)     # 0x4f3310
je 0x402024
leaq 0xbd06a(%rip), %rdi     # 0x4bf084
movl %r12d, %esi
xorl %eax, %eax
callq 0x40d6e0
cmpl $0x2, 0xf12e5(%rip)     # 0x4f3310
jl 0x40203f
movzbl (%r15), %esi
leaq 0xbd066(%rip), %rdi     # 0x4bf09e
xorl %eax, %eax
callq 0x40d6e0
cmpl $0x1, %r12d
jne 0x40204f
cmpb $0x1d, (%r15)
je 0x40230b
movq %rbx, %rdi
movl %r12d, %esi
callq 0x40279a
cmpl $0x0, 0xf12bb(%rip)     # 0x4f331c
jne 0x4020c5
movl 0xf12c2(%rip), %r15d    # 0x4f332c
movl 0xf12bf(%rip), %r12d    # 0x4f3330
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4027aa
movb $0x4, (%rax)
movb 0xf128d(%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 0x40246f
cmpl $-0x1, %eax
jne 0x4020c5
callq 0x405a20
movl (%rax), %edi
callq 0x42c130
leaq 0xbd038(%rip), %rdi     # 0x4bf0f3
movq %rax, %rsi
xorl %eax, %eax
callq 0x40d6e0
cmpw $0x0, 0x1e(%rsp)
je 0x401f6f
movq %rbx, %rdi
callq 0x4025ba
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40279a
movq %rax, %r15
movl 0xf123f(%rip), %r12d    # 0x4f332c
movq %rbx, %rdi
callq 0x402793
movslq %eax, %rdx
movl %r12d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x45c770
cmpl $-0x1, %eax
je 0x40227a
movq %rbx, %rdi
movl %eax, %esi
callq 0x40279a
cmpl $0x0, 0xf11f7(%rip)     # 0x4f3320
je 0x402183
movl 0xf120f(%rip), %eax     # 0x4f3340
xorl 0x3c(%rsp), %eax
movzwl 0xf1208(%rip), %ecx     # 0x4f3344
xorw 0x40(%rsp), %cx
movzwl %cx, %ecx
orl %eax, %ecx
leaq 0xd8d30(%rip), %r12     # 0x4dae7d
leaq 0xc2d42(%rip), %rbp     # 0x4c4e96
leaq 0xbcf7f(%rip), %r13     # 0x4bf0da
jne 0x401f6f
movzwl 0x32(%rsp), %eax
cmpl $0x1168, %eax           # imm = 0x1168
je 0x4021a5
jmp 0x401f6f
leaq 0xbcfaf(%rip), %rdi     # 0x4bf128
callq 0x41c350
jmp 0x401f6f
cmpw $0x1168, 0x32(%rsp)     # imm = 0x1168
leaq 0xd8cec(%rip), %r12     # 0x4dae7d
leaq 0xc2cfe(%rip), %rbp     # 0x4c4e96
leaq 0xbcf3b(%rip), %r13     # 0x4bf0da
jne 0x401f6f
cmpl $0x0, 0xf1164(%rip)     # 0x4f3310
je 0x4021ba
leaq 0xbcf63(%rip), %rdi     # 0x4bf118
callq 0x41c350
movq 0x38(%rbx), %rax
movzbl (%rax), %ecx
cmpl $0x1, %ecx
je 0x402228
cmpl $0x5, %ecx
je 0x402228
cmpl $0x3, %ecx
jne 0x401f6f
cmpl $0x0, 0xf1141(%rip)     # 0x4f331c
je 0x401f6f
movzbl 0x1(%rax), %esi
leaq 0xbcee1(%rip), %rdi     # 0x4bf0cd
xorl %eax, %eax
callq 0x40d6e0
movq $-0x6, %r15
movzbl 0x42(%rsp,%r15), %esi
incq %r15
movq %rbp, %rdx
cmoveq %r12, %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x40d6e0
testq %r15, %r15
jne 0x4021fa
movl $0xa, %edi
callq 0x41c4f0
jmp 0x401f6f
movzbl 0x1(%rax), %ecx
cmpl %ecx, 0xf10e2(%rip)     # 0x4f3314
jne 0x401f6f
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x4027c7
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4027ba
cmpl $0x0, 0xf10bd(%rip)     # 0x4f331c
jne 0x401f6f
movq 0x38(%rbx), %rsi
movl (%rbx), %edx
movl $0x1, %edi
callq 0x459950
jmp 0x401f6f
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
movl $0x20, %esi
movl $0x1, %edx
leaq 0xbce17(%rip), %rdi     # 0x4bf0ac
callq 0x41c080
jmp 0x401f6f
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbcdc8(%rip), %rdi     # 0x4bf078
movl $0xb, %esi
movl $0x1, %edx
callq 0x41c080
jmp 0x401c30
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbcd94(%rip), %rdi     # 0x4bf069
movl $0xe, %esi
movl $0x1, %edx
callq 0x41c080
jmp 0x401d7e
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405a20
movl (%rax), %edi
callq 0x42c130
leaq 0xbcd31(%rip), %rsi     # 0x4bf037
jmp 0x401d71
movl 0xf101b(%rip), %edi     # 0x4f332c
movl 0xf1019(%rip), %esi     # 0x4f3330
callq 0x4023fb
leaq 0xf1025(%rip), %rdx     # 0x4f3348
xorl %edi, %edi
xorl %esi, %esi
callq 0x45a460
jmp 0x401c30