// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
leaq 0x8(%rsp), %r8
andl $0x0, (%r8)
movq %rsi, %rbp
orl $-0x1, 0xf18b7(%rip)    # 0x4f3330
movl $0x14, 0xc(%rsp)
andl $0x0, 0xf1888(%rip)     # 0x4f3310
leaq 0xbd57a(%rip), %rdx     # 0x4bf009
pushq $0x68
popq %rsi
movq %rbp, %rdi
xorl %ecx, %ecx
callq 0x402c8a
testl %eax, %eax
jne 0x401c00
leaq 0x8(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x4026e0
cmpl $0x0, (%rbx)
jne 0x401c13
movl %eax, %r15d
leaq 0xbd53d(%rip), %rax     # 0x4bf004
movq %rax, 0x28(%rsp)
pushq $0x3a
popq %r13
leaq 0x30(%rsp), %r12
leaq 0xf1834(%rip), %r14     # 0x4f3310
movq %rbp, 0x70(%rsp)
movl %r15d, %r15d
cmpl $0x1, %r15d
jle 0x401c26
movq -0x8(%rbp,%r15,8), %rbx
decq %r15
movq %rbx, %rdi
leaq 0xbd50e(%rip), %rsi     # 0x4bf00e
callq 0x4010b8
testl %eax, %eax
je 0x401b9d
movq %rbx, %rdi
leaq 0xc384e(%rip), %rsi     # 0x4c5365
callq 0x4010b8
testl %eax, %eax
je 0x401bcc
movq %rbx, %rdi
callq 0x4010e8
cmpq $0x2, %rax
ja 0x401b49
callq 0x405a00
movq (%rax), %rax
movsbq (%rbx), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x401be3
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4010f0
testq %rax, %rax
je 0x401bf6
pushq $-0x6
popq %rbp
testq %rbp, %rbp
je 0x401bb8
movq %rbx, %rdi
leaq 0xbd4a4(%rip), %rsi     # 0x4bf013
movq %r12, %rdx
xorl %eax, %eax
callq 0x40d690
movb 0x30(%rsp), %al
movb %al, 0x36(%rbp,%r14)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4010f0
testq %rax, %rax
je 0x401bb8
movq %rax, %rbx
incq %rbx
incq %rbp
jmp 0x401b60
leaq 0xbd5bd(%rip), %rdi     # 0x4bf161
callq 0x41c170
movl $0x1, 0xf1769(%rip)     # 0x4f331c
jmp 0x401ae4
movl $0x1, 0xf175e(%rip)     # 0x4f3320
movq 0x70(%rsp), %rbp
jmp 0x401ae4
leaq 0xbd583(%rip), %rdi     # 0x4bf156
callq 0x41c170
incl 0xf1732(%rip)           # 0x4f3310
jmp 0x401ae4
movq %rbx, %rdi
callq 0x40b820
movl %eax, 0xf1723(%rip)     # 0x4f3314
jmp 0x401ae4
movq %rbx, 0x28(%rsp)
jmp 0x401ae1
leaq 0xbd58c(%rip), %rdi     # 0x4bf193
callq 0x41c170
xorl %edi, %edi
callq 0x40c3a0
leaq 0xbd55e(%rip), %rdi     # 0x4bf178
callq 0x41c170
pushq $0x2
jmp 0x401d59
xorl %r14d, %r14d
pushq $-0x1
popq %r12
leaq 0xbd594(%rip), %r15     # 0x4bf1c8
pushq $0x2
popq %rbx
cmpq $0xc, %r14
je 0x401c55
movl (%r14,%r15), %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x45c750
addq $0x4, %r14
testl %eax, %eax
js 0x401c37
movl %eax, %r12d
movl %r12d, 0xf16bc(%rip)    # 0x4f3318
leaq 0x30(%rsp), %rbx
leaq 0xbd4c8(%rip), %r15     # 0x4bf130
pushq $0x1
popq %rbp
movq 0x28(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x50(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %r12d, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x45a680
testl %eax, %eax
js 0x401ccd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401020
orb $0x41, 0x40(%rsp)
movl 0xf165e(%rip), %edi     # 0x4f3318
movl $0x8914, %esi           # imm = 0x8914
movq %rbx, %rdx
xorl %eax, %eax
callq 0x45a680
testl %eax, %eax
jns 0x401ce5
movq %r15, %rdi
callq 0x41c170
movl %ebp, %edi
callq 0x458c60
movl 0xf1635(%rip), %r12d    # 0x4f3318
jmp 0x401c70
testq %r14, %r14
je 0x401d12
movq %r14, %rdi
callq 0x45cae0
movl %eax, 0xf1638(%rip)     # 0x4f3330
testl %eax, %eax
jne 0x401d12
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rdi
leaq 0xbd309(%rip), %rsi     # 0x4bf016
movq %r14, %rdx
jmp 0x401d50
pushq $0x11
popq %rdi
pushq $0x2
popq %rsi
movl $0x1168, %edx           # imm = 0x1168
callq 0x45c750
movl %eax, 0xf1604(%rip)     # 0x4f332c
cmpl $-0x1, %eax
jne 0x401d5f
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405840
movl (%rax), %edi
callq 0x42bf50
leaq 0xbd2df(%rip), %rsi     # 0x4bf029
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d440
pushq $0x1
popq %rdi
callq 0x40c3a0
movl 0xf15cb(%rip), %ecx     # 0x4f3330
testl %ecx, %ecx
js 0x401d92
leaq 0x30(%rsp), %rsi
andl $0x0, 0x10(%rsi)
andq $0x0, 0x8(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ecx, 0x4(%rsi)
pushq $0x14
popq %rdx
movl %eax, %edi
callq 0x45c580
testl %eax, %eax
jne 0x40225e
cmpl $0x0, 0xf1583(%rip)     # 0x4f331c
jne 0x401e6c
leaq 0xf15a2(%rip), %rsi     # 0x4f3348
xorl %edi, %edi
callq 0x45a490
leaq 0x30(%rsp), %rsi
xorl %edi, %edi
callq 0x45a490
testl %eax, %eax
je 0x401de2
cmpl $0x0, 0xf154c(%rip)     # 0x4f3310
je 0x401de2
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd30a(%rip), %rdi     # 0x4bf0e1
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x41bea0
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 0x45a2a0
leaq 0xf157a(%rip), %rbx     # 0x4f3388
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xf156c(%rip), %rdi     # 0x4f3390
callq 0x40b7f0
leaq 0x476(%rip), %rax       # 0x4022a6
movq %rax, 0xf1551(%rip)     # 0x4f3388
andl $0x0, 0xf15d2(%rip)     # 0x4f3410
pushq $0x1c
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x40b5b0
callq 0x4022a6
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbd1e7(%rip), %rdi     # 0x4bf048
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x41bea0
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x4024bc
movq %rax, %rbx
cmpl $0x0, 0xf149c(%rip)     # 0x4f331c
je 0x401f21
movl 0xf14a0(%rip), %ebp     # 0x4f332c
movl 0xf149d(%rip), %r14d    # 0x4f3330
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x4026b6
movb $0x0, (%rax)
movb 0xf146d(%rip), %cl      # 0x4f3314
movb %cl, 0x1(%rax)
movb 0x1(%rbx), %cl
movb %cl, 0x2(%rax)
movb (%rbx), %cl
movb %cl, 0x3(%rax)
leaq 0x30(%rsp), %r8
andq $0x0, 0x8(%r8)
andl $0x0, 0x10(%r8)
movl $0x11680011, (%r8)      # imm = 0x11680011
movb $0x6, 0xb(%r8)
movl %r14d, 0x4(%r8)
orl $-0x1, 0xc(%r8)
orw $-0x1, 0x10(%r8)
movq 0x38(%rbx), %rsi
movl (%rbx), %edx
pushq $0x14
popq %r9
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x45c680
cmpl $-0x1, %eax
jne 0x401f21
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %r14
callq 0x405840
movl (%rax), %edi
callq 0x42bf50
leaq 0xbd1df(%rip), %rsi     # 0x4bf0f3
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40d440
leaq 0x10(%rsp), %r15
pushq $0x2
popq %r12
pushq $-0x1
popq %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x10(%rsp)
movl 0xf13ea(%rip), %eax     # 0x4f332c
movl %eax, 0x18(%rsp)
movl $0x1, 0x1c(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x459f80
testl %eax, %eax
je 0x40218f
testb $0x1, 0x16(%rsp)
je 0x40207a
movq %rbx, %rdi
callq 0x402509
movq %rbx, %rdi
pushq $0x4
popq %rsi
callq 0x402693
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4026a6
movq %rax, %r14
movq %rbx, %rdi
callq 0x40269f
movslq %eax, %rdx
xorl %edi, %edi
movq %r14, %rsi
callq 0x4596f0
movq %rax, %r13
testl %r13d, %r13d
je 0x40221c
cmpl $-0x1, %r13d
je 0x40223d
movslq %r13d, %rax
movb $0x0, (%r14,%rax)
cmpl $0x0, 0xf1348(%rip)     # 0x4f3310
je 0x401ff6
leaq 0xbd0b3(%rip), %rdi     # 0x4bf084
movl %r13d, %esi
xorl %eax, %eax
callq 0x40d500
cmpl $0x2, 0xf132e(%rip)     # 0x4f3310
jl 0x401ff6
movzbl (%r14), %esi
leaq 0xbd0af(%rip), %rdi     # 0x4bf09e
xorl %eax, %eax
callq 0x40d500
cmpl $0x1, %r13d
jne 0x402006
cmpb $0x1d, (%r14)
je 0x402280
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4026a6
cmpl $0x0, 0xf1304(%rip)     # 0x4f331c
jne 0x40207a
movl 0xf130b(%rip), %r14d    # 0x4f332c
movl 0xf1308(%rip), %r13d    # 0x4f3330
movq %rbx, %rdi
pushq $0x4
popq %rsi
callq 0x4026b6
movb $0x4, (%rax)
movb 0xf12d8(%rip), %cl      # 0x4f3314
movb %cl, 0x1(%rax)
movb 0x1(%rbx), %cl
movb %cl, 0x2(%rax)
movb (%rbx), %cl
movb %cl, 0x3(%rax)
movl %r14d, %edi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x4023dc
cmpl $-0x1, %eax
jne 0x40207a
callq 0x405840
movl (%rax), %edi
callq 0x42bf50
leaq 0xbd083(%rip), %rdi     # 0x4bf0f3
movq %rax, %rsi
xorl %eax, %eax
callq 0x40d500
cmpw $0x0, 0x1e(%rsp)
je 0x401f2d
movq %rbx, %rdi
callq 0x402509
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4026a6
movq %rax, %r14
movl 0xf128a(%rip), %r13d    # 0x4f332c
movq %rbx, %rdi
callq 0x40269f
movslq %eax, %rdx
movl %r13d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x45c5b0
cmpl $-0x1, %eax
je 0x4021fb
movq %rbx, %rdi
movl %eax, %esi
callq 0x4026a6
cmpl $0x0, 0xf1242(%rip)     # 0x4f3320
je 0x4020fc
leaq 0xf1259(%rip), %rdi     # 0x4f3340
leaq 0x3c(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x401028
testl %eax, %eax
jne 0x401f2d
cmpw $0x1168, 0x32(%rsp)     # imm = 0x1168
jne 0x401f2d
cmpl $0x0, 0xf1200(%rip)     # 0x4f3310
je 0x40211e
leaq 0xbcfff(%rip), %rdi     # 0x4bf118
callq 0x41c170
movq 0x38(%rbx), %rax
movzbl (%rax), %ecx
cmpl $0x1, %ecx
je 0x4021a0
cmpl $0x5, %ecx
je 0x4021a0
cmpl $0x3, %ecx
jne 0x401f2d
cmpl $0x0, 0xf11dd(%rip)     # 0x4f331c
je 0x401f2d
movzbl 0x1(%rax), %esi
leaq 0xbcf7d(%rip), %rdi     # 0x4bf0cd
xorl %eax, %eax
callq 0x40d500
pushq $-0x6
popq %r14
testq %r14, %r14
je 0x4021ee
movzbl 0x42(%rsp,%r14), %esi
incq %r14
leaq 0xc2d22(%rip), %rdx     # 0x4c4e96
leaq 0xd8d02(%rip), %rax     # 0x4dae7d
cmoveq %rax, %rdx
leaq 0xbcf54(%rip), %rdi     # 0x4bf0da
xorl %eax, %eax
callq 0x40d500
jmp 0x40215b
leaq 0xbcf92(%rip), %rdi     # 0x4bf128
callq 0x41c170
jmp 0x401f2d
movzbl 0x1(%rax), %ecx
cmpl %ecx, 0xf116a(%rip)     # 0x4f3314
jne 0x401f2d
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x4026d3
movq %rbx, %rdi
pushq $0x4
popq %rsi
callq 0x4026c6
cmpl $0x0, 0xf1147(%rip)     # 0x4f331c
jne 0x401f2d
movq 0x38(%rbx), %rsi
movl (%rbx), %edx
pushq $0x1
popq %rdi
callq 0x459790
jmp 0x401f2d
pushq $0xa
popq %rdi
callq 0x41c310
jmp 0x401f2d
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbcea0(%rip), %rdi     # 0x4bf0ac
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x41bea0
jmp 0x401f2d
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbce4b(%rip), %rdi     # 0x4bf078
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x41bea0
jmp 0x401c0c
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rcx
leaq 0xbce1b(%rip), %rdi     # 0x4bf069
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x41bea0
jmp 0x401d57
movq $0x4f1708, %rax         # imm = 0x4F1708
movq (%rax), %rbx
callq 0x405840
movl (%rax), %edi
callq 0x42bf50
leaq 0xbcdbc(%rip), %rsi     # 0x4bf037
jmp 0x401d4a
movl 0xf10a6(%rip), %edi     # 0x4f332c
movl 0xf10a4(%rip), %esi     # 0x4f3330
callq 0x40236c
leaq 0xf10b0(%rip), %rdx     # 0x4f3348
xorl %edi, %edi
xorl %esi, %esi
callq 0x45a2a0
jmp 0x401c0c