// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x14, -0x28(%rbp)
leaq 0xbe5ed(%rip), %rax     # 0x4c0004
movq %rax, -0x30(%rbp)
movl $0x0, -0x54(%rbp)
movl $0xffffffff, 0xf2924(%rip) # imm = 0xFFFFFFFF
movl $0x0, 0xf28fa(%rip)     # 0x4f4330
movq -0x10(%rbp), %rdi
movl $0x68, %esi
leaq 0xbe5c3(%rip), %rdx     # 0x4c0009
xorl %eax, %eax
movl %eax, %ecx
leaq -0x54(%rbp), %r8
callq 0x403950
cmpl $0x0, %eax
je 0x401a6d
leaq 0xbe5af(%rip), %rdi     # 0x4c000e
movb $0x0, %al
callq 0x40e240
xorl %edi, %edi
callq 0x40d0e0
movq -0x10(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x402f30
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x401a9b
leaq 0xbe5b7(%rip), %rdi     # 0x4c0041
movb $0x0, %al
callq 0x40e240
movl $0x2, %edi
callq 0x40d0e0
jmp 0x401a9d
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
cmpl $0x0, %eax
jle 0x401c3d
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xbe59b(%rip), %rsi     # 0x4c005d
callq 0x4010b0
cmpl $0x0, %eax
jne 0x401ae6
leaq 0xbe58f(%rip), %rdi     # 0x4c0062
movb $0x0, %al
callq 0x40e240
movl $0x1, 0xf2858(%rip)     # 0x4f433c
jmp 0x401a9d
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xc486c(%rip), %rsi     # 0x4c6365
callq 0x4010b0
cmpl $0x0, %eax
jne 0x401b25
leaq 0xbe570(%rip), %rdi     # 0x4c007a
movb $0x0, %al
callq 0x40e240
movl 0xf2819(%rip), %eax     # 0x4f4330
addl $0x1, %eax
movl %eax, 0xf2810(%rip)     # 0x4f4330
jmp 0x401a9d
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4010e0
cmpq $0x3, %rax
jae 0x401b80
callq 0x406740
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movslq -0x8(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x401b80
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x40c560
movl %eax, 0xf27b9(%rip)     # 0x4f4334
jmp 0x401a9d
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x3a, %esi
callq 0x4010e8
cmpq $0x0, %rax
je 0x401c28
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x50(%rbp)
cmpl $0x6, -0x50(%rbp)
jge 0x401c19
movq -0x38(%rbp), %rdi
leaq 0xbe4be(%rip), %rsi     # 0x4c0086
leaq -0x64(%rbp), %rdx
movb $0x0, %al
callq 0x40e3d0
movl -0x64(%rbp), %eax
movb %al, %dl
movslq -0x50(%rbp), %rcx
leaq 0xf274d(%rip), %rax     # 0x4f4330
movb %dl, 0x30(%rax,%rcx)
movq -0x38(%rbp), %rdi
movl $0x3a, %esi
callq 0x4010e8
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x401c02
jmp 0x401c19
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x401bb7
movl $0x1, 0xf271d(%rip)     # 0x4f4340
jmp 0x401a9d
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
jmp 0x401a9d
callq 0x402230
movl %eax, 0xf26f0(%rip)     # 0x4f4338
movq -0x30(%rbp), %rdi
movl $0x41, %esi
callq 0x402290
cmpl $0x0, %eax
je 0x401c75
leaq 0xbe427(%rip), %rdi     # 0x4c0089
movb $0x0, %al
callq 0x40e240
movl $0x1, %edi
callq 0x459560
jmp 0x401c48
cmpq $0x0, -0x30(%rbp)
je 0x401cbc
movq -0x30(%rbp), %rdi
callq 0x45d3f0
movl %eax, 0xf26c5(%rip)     # 0x4f4350
cmpl $0x0, 0xf26be(%rip)     # 0x4f4350
jne 0x401cba
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rdi
movq -0x30(%rbp), %rdx
leaq 0xbe407(%rip), %rsi     # 0x4c00b0
movb $0x0, %al
callq 0x40e180
movl $0x1, %edi
callq 0x40d0e0
jmp 0x401cbc
movl $0x6811, %edi           # imm = 0x6811
callq 0x45d3e0
movzwl %ax, %edx
movl $0x11, %edi
movl $0x2, %esi
callq 0x45d050
movl %eax, 0xf266e(%rip)     # 0x4f434c
cmpl $-0x1, 0xf2667(%rip)    # 0x4f434c
jne 0x401d26
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rax
movq %rax, -0x98(%rbp)
callq 0x406580
movl (%rax), %edi
callq 0x42c850
movq -0x98(%rbp), %rdi
movq %rax, %rdx
leaq 0xbe3ae(%rip), %rsi     # 0x4c00c3
movb $0x0, %al
callq 0x40e180
movl $0x1, %edi
callq 0x40d0e0
cmpl $0x0, 0xf2623(%rip)     # 0x4f4350
jl 0x401dba
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x4010a0
movw $0x11, -0x78(%rbp)
movl $0x6811, %edi           # imm = 0x6811
callq 0x45d3e0
movw %ax, -0x76(%rbp)
movl 0xf25f3(%rip), %eax     # 0x4f4350
movl %eax, -0x74(%rbp)
movl 0xf25e6(%rip), %edi     # 0x4f434c
leaq -0x78(%rbp), %rsi
movl $0x14, %edx
callq 0x45ce80
cmpl $0x0, %eax
je 0x401db8
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
callq 0x406580
movl (%rax), %edi
callq 0x42c850
movq -0xa0(%rbp), %rdi
movq %rax, %rdx
leaq 0xbe32a(%rip), %rsi     # 0x4c00d1
movb $0x0, %al
callq 0x40e180
movl $0x1, %edi
callq 0x40d0e0
jmp 0x401dba
cmpl $0x0, 0xf257b(%rip)     # 0x4f433c
jne 0x401dec
callq 0x402620
callq 0x4026e0
xorl %edi, %edi
callq 0x402340
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rdi
leaq 0xbe2fd(%rip), %rsi     # 0x4c00e2
movb $0x0, %al
callq 0x40e180
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x402a20
movq %rax, -0x60(%rbp)
cmpl $0x0, 0xf253b(%rip)     # 0x4f433c
je 0x401e18
movl 0xf2543(%rip), %edi     # 0x4f434c
movl 0xf2541(%rip), %esi     # 0x4f4350
movq -0x60(%rbp), %rdx
callq 0x4023a0
jmp 0x401e1a
movl $0x0, -0x90(%rbp)
movw $0x1, -0x8c(%rbp)
movw $0x0, -0x8a(%rbp)
movl 0xf2510(%rip), %eax     # 0x4f434c
movl %eax, -0x88(%rbp)
movw $0x1, -0x84(%rbp)
movw $0x0, -0x82(%rbp)
leaq -0x90(%rbp), %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x45a880
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x401e83
leaq 0xbe289(%rip), %rdi     # 0x4c0103
movb $0x0, %al
callq 0x40e240
jmp 0x401e1a
movswl -0x8a(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x401fdf
movq -0x60(%rbp), %rdi
callq 0x402ad0
movq -0x60(%rbp), %rdi
movl $0x4, %esi
callq 0x402df0
movq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x402e50
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x60(%rbp), %rdi
callq 0x402e30
movq -0xa8(%rbp), %rsi
movslq %eax, %rdx
xorl %edi, %edi
callq 0x459ff0
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x401f0c
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rdi
leaq 0xbe211(%rip), %rsi     # 0x4c010c
movb $0x0, %al
callq 0x40e180
movl $0x1, %edi
callq 0x40d0e0
cmpl $0x0, -0x4c(%rbp)
jne 0x401f31
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rdi
leaq 0xbe1f8(%rip), %rsi     # 0x4c011b
movb $0x0, %al
callq 0x40e180
xorl %edi, %edi
callq 0x40d0e0
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0x0, 0xf23ec(%rip)     # 0x4f4330
je 0x401f57
movl -0x4c(%rbp), %esi
leaq 0xbe1d7(%rip), %rdi     # 0x4c0127
movb $0x0, %al
callq 0x40e240
cmpl $0x1, 0xf23d2(%rip)     # 0x4f4330
jle 0x401f75
movq -0x40(%rbp), %rax
movzbl (%rax), %esi
leaq 0xbe1d3(%rip), %rdi     # 0x4c0141
movb $0x0, %al
callq 0x40e240
cmpl $0x1, -0x4c(%rbp)
jne 0x401fb3
movq -0x40(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1d, %eax
jne 0x401fb3
movl 0xf23bf(%rip), %edi     # 0x4f434c
movl 0xf23bd(%rip), %esi     # 0x4f4350
callq 0x402480
xorl %esi, %esi
leaq 0xf238f(%rip), %rdx     # 0x4f4330
addq $0x38, %rdx
movl %esi, %edi
callq 0x45aba0
xorl %edi, %edi
callq 0x40d0e0
movq -0x60(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x402e50
cmpl $0x0, 0xf2376(%rip)     # 0x4f433c
jne 0x401fdd
movl 0xf237e(%rip), %edi     # 0x4f434c
movl 0xf237c(%rip), %esi     # 0x4f4350
movq -0x60(%rbp), %rdx
callq 0x402550
jmp 0x401fdf
cmpw $0x0, -0x82(%rbp)
je 0x402224
movq -0x60(%rbp), %rdi
callq 0x402ad0
movq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x402e50
movq %rax, -0x40(%rbp)
movl 0xf2341(%rip), %eax     # 0x4f434c
movl %eax, -0xb4(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x60(%rbp), %rdi
callq 0x402e30
movl -0xb4(%rbp), %edi
movq -0xb0(%rbp), %rsi
movslq %eax, %rdx
xorl %ecx, %ecx
leaq -0x24(%rbp), %r8
leaq -0x28(%rbp), %r9
callq 0x45ceb0
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x40206a
movq $0x4f2728, %rax         # imm = 0x4F2728
movq (%rax), %rdi
leaq 0xbe0f1(%rip), %rsi     # 0x4c014f
movb $0x0, %al
callq 0x40e180
jmp 0x401e1a
movq -0x60(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x402e50
cmpl $0x0, 0xf22c3(%rip)     # 0x4f4340
je 0x4020a4
leaq -0x24(%rbp), %rsi
addq $0xc, %rsi
leaq 0xf22d2(%rip), %rdi     # 0x4f4360
movl $0x6, %edx
callq 0x401088
cmpl $0x0, %eax
je 0x4020a2
jmp 0x401e1a
jmp 0x4020a4
movzwl -0x22(%rbp), %edi
callq 0x45d3e0
movzwl %ax, %eax
cmpl $0x6811, %eax           # imm = 0x6811
jne 0x402222
cmpl $0x0, 0xf226e(%rip)     # 0x4f4330
je 0x4020d2
leaq 0xbe0a5(%rip), %rdi     # 0x4c0170
movb $0x0, %al
callq 0x40e240
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
jne 0x402172
cmpl $0x0, 0xf2247(%rip)     # 0x4f433c
je 0x40216d
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax), %esi
leaq 0xbe070(%rip), %rdi     # 0x4c0181
movb $0x0, %al
callq 0x40e240
movl $0x0, -0x50(%rbp)
cmpl $0x6, -0x50(%rbp)
jge 0x40215f
movslq -0x50(%rbp), %rax
movzbl -0x18(%rbp,%rax), %esi
movl -0x50(%rbp), %ecx
leaq 0xc3d5e(%rip), %rdx     # 0x4c5e96
leaq 0xd9d3e(%rip), %rax     # 0x4dbe7d
cmpl $0x5, %ecx
cmoveq %rax, %rdx
leaq 0xbe041(%rip), %rdi     # 0x4c018e
movb $0x0, %al
callq 0x40e240
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x40211f
leaq 0xbe5e4(%rip), %rdi     # 0x4c074a
movb $0x0, %al
callq 0x40e240
jmp 0x401e1a
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5, %eax
je 0x40218e
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x40221d
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x48(%rbp)
movzbl (%rax), %eax
cmpl 0xf2182(%rip), %eax     # 0x4f4334
je 0x4021b9
jmp 0x401e1a
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x48(%rbp)
movzbl (%rax), %eax
shll $0x8, %eax
movl %eax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x60(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x402f00
movq -0x60(%rbp), %rdi
movl $0x4, %esi
callq 0x402ed0
cmpl $0x0, 0xf213d(%rip)     # 0x4f433c
jne 0x40221b
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, %edx
movl $0x1, %edi
callq 0x45a090
jmp 0x40221d
jmp 0x401e1a
jmp 0x402224
jmp 0x401e1a
nopl (%rax)