// File: /workspace/github_c_makefile_star2_O2/jelaas[P]egetty/egetty.c
// Function: main
int main(int argc, char **argv, char **arge)
{
	int s;
	struct sockaddr_ll from;
	socklen_t fromlen = sizeof(from);
	int ifindex=-1;
	uint8_t *buf, *p;
	ssize_t n;
	unsigned int len;
	int count=1;
	int timeout = -1;
	int loginfd = -1;
	pid_t pid=-1;
	struct sk_buff *skb;
	
	envp = arge;
	conf.debug = 0;
	conf.device = "eth0";
	conf.devsocket = -1;
	
	while(--argc > 0) {
		if(strcmp(argv[argc], "debug")==0) {
			printf("Debug mode\n");
			conf.debug = 1;
			continue;
		}
		if(strcmp(argv[argc], "console")==0) {
			conf.kmsg = 1;
			continue;
		}
		if(strcmp(argv[argc], "waitif")==0) {
			conf.waitif = 1;
			continue;
		}
		if( (strlen(argv[argc]) < 3) && isdigit(*argv[argc])) {
			conf.console = atoi(argv[argc]);
			continue;
		}
		conf.device = argv[argc];
	}

	conf.devsocket = devsocket();
	
	if(conf.waitif) {
		/* wait for interface to become available */
		while(check_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
			sleep(1);
		}
	} else {
		/* active interface */
		while(set_flag(conf.device, (IFF_UP | IFF_RUNNING))) {
			sleep(1);
		}
	}
	
	memset(conf.client.sll_addr, 255, 6);
	
	s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_EGETTY));
	if(s == -1)
	{
		fprintf(stderr, "socket(): %s\n", strerror(errno));
		exit(1);
	}
	
	if(conf.device)
	{
		ifindex = if_nametoindex(conf.device);
		if(!ifindex)
		{
			fprintf(stderr, "Not an interface\n");
			exit(1);
		}
	}
	
	
	if(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 = ifindex;
		
		if(bind(s, (const struct sockaddr *)&addr, sizeof(addr)))
		{
			fprintf(stderr, "bind() to interface failed\n");
			exit(1);
		}
	}
	
	skb = alloc_skb(1500);

	skb_reset(skb);
	skb_reserve(skb, 4);
	console_hello(s, ifindex, skb);
	
	while(count)
	{
		struct pollfd fds[2];

		if(pid) {
			int status;
			if(waitpid(-1, &status, WNOHANG)>0) {
				pid = -1;
				close(loginfd);
			}
		}
		if(pid == -1) {
			pid = login(&loginfd);
			if(pid == -1)
				exit(1);
			if(conf.debug) {
				printf("child pid = %d\n", pid);
				printf("loginfd = %d\n", loginfd);
			}
		}
		
		fds[0].fd = s;
		fds[0].events = POLLIN;
		fds[0].revents = 0;
		
		fds[1].fd = loginfd;
		fds[1].events = POLLIN;
		fds[1].revents = 0;

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

		if(fds[1].revents & POLLIN) {
			if(conf.debug) printf("POLLIN child\n");
			skb_reset(skb);
			skb_reserve(skb, 4);
			buf = skb_put(skb, 0);
			n = read(loginfd, buf, skb_tailroom(skb));
			if(n == -1) {
				fprintf(stderr, "read() failed\n");
				exit(1);
			}
			
			buf[n] = 0;
			if(conf.debug)
				printf("child: %d bytes\n", n);
			skb_put(skb, n);
			console_put(s, ifindex, skb);
		}
		if(fds[0].revents) {
			skb_reset(skb);
			buf = skb_put(skb, 0);
			n = recvfrom(s, buf, skb_tailroom(skb), 0, (struct sockaddr *)&from, &fromlen);
			if(n == -1) {
				fprintf(stderr, "recvfrom() failed. ifconfig up?\n");
				exit(1);
			}

			skb_put(skb, n);
			if(conf.debug) printf("received packet %d bytes\n", skb->len);
			
			if(ntohs(from.sll_protocol) == ETH_P_EGETTY) {
				if(conf.debug)
					printf("Received EGETTY\n");
				
				p = skb->data;
				if(*p == EGETTY_HUP) {
					if(pid != -1) kill(pid, 9);
					continue;
				}
				
				if(*p == EGETTY_SCAN) {
					skb_reset(skb);
					skb_reserve(skb, 4);
					console_hello(s, ifindex, skb);
					continue;
				}
				
				if(*p == EGETTY_WINCH) {
					p++;
					if(*p != conf.console) {
						if(conf.debug)
							printf("Wrong console %d not %d\n", *p, conf.console);
						continue;
					}
					p++;
					{
						struct winsize winp;
						winp.ws_row = *p++;
						winp.ws_col = *p++;
						winp.ws_xpixel = 0;
						winp.ws_ypixel = 0;
						ioctl(loginfd, TIOCSWINSZ, &winp);
						if(conf.debug)
							printf("WINCH to %d, %d\n", winp.ws_row, winp.ws_col);
					}
					continue;
				}
				
				if(*p != EGETTY_IN) {
					if(conf.debug)
						printf("Not EGETTY_IN: %d\n", *p);
					continue;
				}
				p++;
				if(*p != conf.console) {
					if(conf.debug)
						printf("Wrong console %d not %d\n", *p, conf.console);
					continue;
				}
				memcpy(conf.client.sll_addr, from.sll_addr, 6);
				p++;
				len = *p++ << 8;
				len += *p;
				if(len > n) {
					printf("Length field too long: %d\n", len);
					continue;
				}
				skb_trim(skb, len);
				skb_pull(skb, 4);
				if(conf.debug) printf("Sent %d bytes to child\n", skb->len);
				write(loginfd, skb->data, skb->len);
				continue;
			}
		}
		
	}
  exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
orl $-0x1, 0x10(%rsp)
andl $0x0, 0xd38e6(%rip)     # 0x4d5320
movl $0x14, 0x3c(%rsp)
movq %rdx, 0xd38f7(%rip)     # 0x4d5340
leaq 0xa0648(%rip), %rax     # 0x4a2098
movq %rax, 0xd38b9(%rip)     # 0x4d5310
orl $-0x1, 0xd38da(%rip)    # 0x4d5338
movl %edi, %r15d
leaq 0xa650d(%rip), %r14     # 0x4a7f75
leaq 0xa062e(%rip), %r12     # 0x4a209d
leaq 0xa062f(%rip), %r13     # 0x4a20a5
cmpl $0x1, %r15d
jle 0x401b23
movq -0x8(%rbx,%r15,8), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401add
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401af5
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4010b0
testl %eax, %eax
je 0x401b01
movq %rbp, %rdi
callq 0x4010d8
cmpq $0x2, %rax
ja 0x401ad4
callq 0x405000
movq (%rax), %rax
movsbq (%rbp), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x401b0d
movq %rbp, 0xd3835(%rip)     # 0x4d5310
jmp 0x401b1b
leaq 0xa0720(%rip), %rdi     # 0x4a2204
callq 0x413620
movl $0x1, 0xd382d(%rip)     # 0x4d5320
jmp 0x401b1b
movl $0x1, 0xd3819(%rip)     # 0x4d5318
jmp 0x401b1b
movl $0x1, 0xd3811(%rip)     # 0x4d531c
jmp 0x401b1b
movq %rbp, %rdi
callq 0x40abe0
movl %eax, 0xd37ed(%rip)     # 0x4d5308
decq %r15
jmp 0x401a76
xorl %r14d, %r14d
pushq $-0x1
popq %rbx
leaq 0xa04f0(%rip), %r15     # 0x4a2020
pushq $0x2
popq %rbp
cmpq $0xc, %r14
je 0x401b50
movl (%r14,%r15), %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x453720
addq $0x4, %r14
testl %eax, %eax
js 0x401b33
movl %eax, %ebx
movl %ebx, 0xd37e2(%rip)     # 0x4d5338
cmpl $0x0, 0xd37bf(%rip)     # 0x4d531c
jne 0x401bd9
leaq 0x40(%rsp), %r14
pushq $0x1
popq %rbp
movq 0xd37a2(%rip), %r15     # 0x4d5310
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x60(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %ebx, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x451650
testl %eax, %eax
js 0x401bca
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
orb $0x41, 0x50(%rsp)
movl 0xd3781(%rip), %edi     # 0x4d5338
movl $0x8914, %esi           # imm = 0x8914
movq %r14, %rdx
xorl %eax, %eax
callq 0x451650
testl %eax, %eax
jns 0x401c35
movl %ebp, %edi
callq 0x44fe40
movl 0xd3761(%rip), %ebx     # 0x4d5338
jmp 0x401b67
leaq 0x40(%rsp), %r14
pushq $0x1
popq %rbp
movq 0xd3728(%rip), %rsi     # 0x4d5310
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x60(%rsp)
movq %r14, %rdi
callq 0x401020
movl $0x8913, %esi           # imm = 0x8913
movl %ebx, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x451650
testl %eax, %eax
js 0x401c26
movzwl 0x50(%rsp), %eax
andl $0x41, %eax
cmpw $0x41, %ax
je 0x401c35
movl %ebp, %edi
callq 0x44fe40
movl 0xd3705(%rip), %ebx     # 0x4d5338
jmp 0x401be1
orw $-0x1, 0xd36f7(%rip)    # 0x4d5334
orl $-0x1, 0xd36ec(%rip)    # 0x4d5330
pushq $0x11
popq %rdi
pushq $0x2
popq %rsi
movl $0x1168, %edx           # imm = 0x1168
callq 0x453720
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
jne 0x401c8c
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rbx
callq 0x404e40
movl (%rax), %edi
callq 0x4232f0
leaq 0xa0432(%rip), %rsi     # 0x4a20ac
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x40c800
jmp 0x4020d4
movq 0xd367d(%rip), %rdi     # 0x4d5310
pushq $-0x1
popq %r12
movl %r12d, %ebx
testq %rdi, %rdi
jne 0x40202f
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x4020dc
movq %rax, %r15
movq %rax, %rdi
callq 0x402129
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x4022b3
movl 0xc(%rsp), %edi
movl %ebx, 0x14(%rsp)
movl %ebx, %esi
movq %r15, %rdx
callq 0x4019b4
leaq 0x20(%rsp), %rbx
pushq $0x1
popq %r13
movl %r12d, %r14d
movl %r12d, %ebp
movl %r12d, %edi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x44fd80
testl %eax, %eax
jle 0x401d44
movl %r14d, %edi
callq 0x450a70
leaq 0x10(%rsp), %rdi
callq 0x401713
cmpl $-0x1, %eax
je 0x4020d4
movl %eax, %ebp
cmpl $0x0, 0xd3606(%rip)     # 0x4d5320
je 0x401d4b
leaq 0xa03c5(%rip), %rdi     # 0x4a20e8
movl %ebp, %esi
xorl %eax, %eax
callq 0x40c8c0
movl 0x10(%rsp), %r14d
leaq 0xa03c0(%rip), %rdi     # 0x4a20f8
movl %r14d, %esi
xorl %eax, %eax
callq 0x40c8c0
jmp 0x401d50
cmpl $-0x1, %ebp
jne 0x401d50
jmp 0x401cfe
movl 0x10(%rsp), %r14d
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rsp)
movl %r13d, 0x24(%rsp)
movl %r14d, 0x28(%rsp)
movl %r13d, 0x2c(%rsp)
movq %rbx, %rdi
pushq $0x2
popq %rsi
movl %r12d, %edx
callq 0x451220
testl %eax, %eax
je 0x402095
movl %r12d, %ebx
testb $0x1, 0x2e(%rsp)
je 0x401e1d
cmpl $0x0, 0xd358e(%rip)     # 0x4d5320
je 0x401da0
leaq 0xa0444(%rip), %rdi     # 0x4a21df
callq 0x413620
movq %r15, %rdi
callq 0x402129
movq %r15, %rdi
pushq $0x4
popq %rsi
callq 0x4022b3
movq %r15, %rdi
xorl %esi, %esi
callq 0x4022c6
movq %rax, %r12
movq %r15, %rdi
callq 0x4022bf
movslq %eax, %rdx
movl %r14d, %edi
movq %r12, %rsi
callq 0x450900
cmpq $-0x1, %rax
je 0x4020b8
movq %rax, %r13
movb $0x0, (%r12,%rax)
cmpl $0x0, 0xd3531(%rip)     # 0x4d5320
je 0x401e02
leaq 0xa031d(%rip), %rdi     # 0x4a2115
movq %r13, %rsi
xorl %eax, %eax
callq 0x40c8c0
movq %r15, %rdi
movl %r13d, %esi
callq 0x4022c6
movl 0xc(%rsp), %edi
movl 0x14(%rsp), %esi
movq %r15, %rdx
callq 0x40194b
cmpw $0x0, 0x26(%rsp)
je 0x40201e
movq %r15, %rdi
callq 0x402129
movq %r15, %rdi
xorl %esi, %esi
callq 0x4022c6
movq %rax, %r12
movq %r15, %rdi
callq 0x4022bf
movslq %eax, %rdx
movl 0xc(%rsp), %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x40(%rsp), %r8
leaq 0x3c(%rsp), %r9
callq 0x453580
cmpq $-0x1, %rax
je 0x4020a3
movq %rax, %r13
movq %r15, %rdi
movl %r13d, %esi
callq 0x4022c6
cmpl $0x0, 0xd34a0(%rip)     # 0x4d5320
je 0x401e93
movl (%r15), %esi
leaq 0xa02bb(%rip), %rdi     # 0x4a2147
xorl %eax, %eax
callq 0x40c8c0
cmpw $0x1168, 0x42(%rsp)     # imm = 0x1168
jne 0x40201e
cmpl $0x0, 0xd3479(%rip)     # 0x4d5320
je 0x401eb5
leaq 0xa033c(%rip), %rdi     # 0x4a21ec
callq 0x413620
movq 0x38(%r15), %rax
movzbl (%rax), %esi
cmpq $0x6, %rsi
ja 0x401fc9
leaq 0xa0137(%rip), %rdx     # 0x4a2004
movslq (%rdx,%rsi,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r15, %rdi
callq 0x402129
movq %r15, %rdi
pushq $0x4
popq %rsi
callq 0x4022b3
movl 0xc(%rsp), %edi
movl 0x14(%rsp), %esi
movq %r15, %rdx
callq 0x4019b4
jmp 0x40201e
movzbl 0x1(%rax), %esi
movl 0xd3400(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x401fb0
leaq 0x4c(%rsp), %rdx
movzwl 0x4(%rdx), %ecx
movw %cx, 0xd3414(%rip)      # 0x4d5334
movl (%rdx), %ecx
movl %ecx, 0xd3408(%rip)     # 0x4d5330
movzwl 0x2(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %esi
cmpq %rsi, %r13
jge 0x401fe2
leaq 0xa025b(%rip), %rdi     # 0x4a219e
jmp 0x401fd9
movl %ebp, %edi
pushq $0x9
popq %rsi
callq 0x40abb0
jmp 0x40201e
movzbl 0x1(%rax), %esi
movl 0xd33a7(%rip), %edx     # 0x4d5308
cmpl %esi, %edx
jne 0x401fb0
movzbl 0x2(%rax), %ecx
movw %cx, 0x18(%rsp)
movzbl 0x3(%rax), %eax
movw %ax, 0x1a(%rsp)
andl $0x0, 0x1c(%rsp)
movl $0x5414, %esi           # imm = 0x5414
movl %r14d, %edi
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x451650
cmpl $0x0, 0xd3389(%rip)     # 0x4d5320
je 0x40201e
movzwl 0x18(%rsp), %esi
movzwl 0x1a(%rsp), %edx
leaq 0xa01cc(%rip), %rdi     # 0x4a217a
jmp 0x401fc0
cmpl $0x0, 0xd3369(%rip)     # 0x4d5320
je 0x40201e
leaq 0xa01a1(%rip), %rdi     # 0x4a2161
xorl %eax, %eax
callq 0x40c8c0
jmp 0x40201e
cmpl $0x0, 0xd3350(%rip)     # 0x4d5320
je 0x40201e
leaq 0xa01b2(%rip), %rdi     # 0x4a218b
xorl %eax, %eax
callq 0x40c8c0
jmp 0x40201e
movq %r15, %rdi
callq 0x4022f3
movq %r15, %rdi
pushq $0x4
popq %rsi
callq 0x4022e6
cmpl $0x0, 0xd3324(%rip)     # 0x4d5320
je 0x40200f
movl (%r15), %esi
leaq 0xa01b1(%rip), %rdi     # 0x4a21b9
xorl %eax, %eax
callq 0x40c8c0
movq 0x38(%r15), %rsi
movl (%r15), %edx
movl %r14d, %edi
callq 0x4509a0
movl %ebx, %r12d
leaq 0x20(%rsp), %rbx
pushq $0x1
popq %r13
jmp 0x401ce4
callq 0x453ab0
testl %eax, %eax
jne 0x40204d
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0xa0071(%rip), %rdi     # 0x4a20ba
pushq $0x11
jmp 0x4020cb
movl %eax, %ebx
js 0x401ca3
leaq 0x40(%rsp), %rsi
andl $0x0, 0x10(%rsi)
andq $0x0, 0x8(%rsi)
movl $0x11680011, (%rsi)     # imm = 0x11680011
movl %ebx, 0x4(%rsi)
pushq $0x14
popq %rdx
movl 0xc(%rsp), %edi
callq 0x453550
testl %eax, %eax
je 0x401ca3
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0xa003b(%rip), %rdi     # 0x4a20cc
pushq $0x1b
jmp 0x4020cb
leaq 0xa0160(%rip), %rdi     # 0x4a21fc
callq 0x413620
jmp 0x4020d4
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0xa0072(%rip), %rdi     # 0x4a2126
pushq $0x20
jmp 0x4020cb
movq $0x4d36e8, %rax         # imm = 0x4D36E8
movq (%rax), %rcx
leaq 0xa003d(%rip), %rdi     # 0x4a2106
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x413350
pushq $0x1
popq %rdi
callq 0x40b760