// File: /workspace/github_c_makefile_star2_O0/open-mesh-mirror[P]batmand/posix/tunnel.c
// Function: get_tun_ip
static int8_t get_tun_ip(struct sockaddr_in *gw_addr, int32_t udp_sock, uint32_t *tun_addr)
{
	struct sockaddr_in sender_addr;
	struct timeval tv;
	unsigned char buff[100];
	int32_t res, buff_len;
	uint32_t addr_len;
	int8_t i = 12;
	fd_set wait_sockets;


	addr_len = sizeof(struct sockaddr_in);
	memset(&buff, 0, sizeof(buff));


	while ((!is_aborted()) && (curr_gateway != NULL) && (i > 0)) {

		buff[0] = TUNNEL_IP_REQUEST;

		if (sendto(udp_sock, buff, sizeof(buff), 0, (struct sockaddr *)gw_addr, sizeof(struct sockaddr_in)) < 0) {
			debug_output(0, "Error - can't send ip request to gateway: %s \n", strerror(errno));
			goto next_try;
		}

		tv.tv_sec = 0;
		tv.tv_usec = 250000;

		FD_ZERO(&wait_sockets);
		FD_SET(udp_sock, &wait_sockets);

		res = select(udp_sock + 1, &wait_sockets, NULL, NULL, &tv);

		if ((res < 0) && (errno != EINTR)) {
			debug_output(0, "Error - can't select (get_tun_ip): %s \n", strerror(errno));
			break;
		}

		if (res <= 0)
			goto next_try;

		/* gateway message */
		if (!FD_ISSET(udp_sock, &wait_sockets))
			goto next_try;


		if ((buff_len = recvfrom(udp_sock, buff, sizeof(buff) - 1, 0, (struct sockaddr *)&sender_addr, &addr_len)) < 0) {
			debug_output(0, "Error - can't receive ip request: %s \n", strerror(errno));
			goto next_try;
		}

		if (buff_len < 4) {
			debug_output(0, "Error - can't receive ip request: packet size is %i < 4 \n", buff_len);
			goto next_try;
		}

		/* a gateway with multiple interfaces breaks here */
		/* if (sender_addr.sin_addr.s_addr != gw_addr->sin_addr.s_addr) {
			debug_output(0, "Error - can't receive ip request: sender IP is not gateway IP \n");
			goto next_try;
		} */

		memcpy(tun_addr, buff + 1, 4);
		return 1;

next_try:
		i--;

	}

	if (i == 0)
		debug_output(0, "Error - can't receive ip from gateway: number of maximum retries reached \n");

	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movb $0xc, -0xbd(%rbp)
movl $0x10, -0xbc(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x21e0
callq 0xfaf0
movb %al, %cl
xorl %eax, %eax
cmpb $0x0, %cl
movb %al, -0x161(%rbp)
jne 0x11106
xorl %eax, %eax
leaq 0xa279(%rip), %rcx      # 0x1b360
cmpq $0x0, (%rcx)
movb %al, -0x161(%rbp)
je 0x11106
movsbl -0xbd(%rbp), %eax
cmpl $0x0, %eax
setg %al
movb %al, -0x161(%rbp)
movb -0x161(%rbp), %al
testb $0x1, %al
jne 0x11115
jmp 0x11359
movb $0x2, -0xb0(%rbp)
movl -0x14(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movq -0x10(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %r8
movl $0x64, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x2200
cmpq $0x0, %rax
jge 0x11174
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0x6a5f(%rip), %rsi      # 0x17bc7
movb $0x0, %al
callq 0x123f0
jmp 0x11346
movq $0x0, -0x40(%rbp)
movq $0x3d090, -0x38(%rbp)   # imm = 0x3D090
leaq -0x140(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpq $0x10, %rax
jae 0x111ce
movq -0x158(%rbp), %rax
movl -0x14c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x1119c
jmp 0x111d0
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x140(%rbp,%rax,8), %rcx
movq %rcx, -0x140(%rbp,%rax,8)
movl -0x14(%rbp), %edi
addl $0x1, %edi
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x40(%rbp), %r8
movq %rcx, %rdx
callq 0x22f0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
jge 0x11261
callq 0x2070
cmpl $0x4, (%rax)
je 0x11261
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0x69a1(%rip), %rsi      # 0x17bf6
movb $0x0, %al
callq 0x123f0
jmp 0x11359
cmpl $0x0, -0xb4(%rbp)
jg 0x1126f
jmp 0x11346
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x140(%rbp,%rax,8), %rax
movq %rax, -0x170(%rbp)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x170(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x112b8
jmp 0x11346
movl -0x14(%rbp), %edi
leaq -0xb0(%rbp), %rsi
leaq -0x30(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %r8
movl $0x63, %edx
xorl %ecx, %ecx
leaq -0xbc(%rbp), %r9
callq 0x2350
movl %eax, -0xb8(%rbp)
cmpl $0x0, %eax
jge 0x11313
callq 0x2070
movl (%rax), %edi
callq 0x24a0
movq %rax, %rdx
xorl %edi, %edi
leaq 0x6914(%rip), %rsi      # 0x17c1e
movb $0x0, %al
callq 0x123f0
jmp 0x11346
cmpl $0x4, -0xb8(%rbp)
jge 0x11334
movl -0xb8(%rbp), %edx
xorl %edi, %edi
leaq 0x691a(%rip), %rsi      # 0x17c45
movb $0x0, %al
callq 0x123f0
jmp 0x11346
movq -0x20(%rbp), %rax
movl -0xaf(%rbp), %ecx
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x11379
movb -0xbd(%rbp), %al
addb $-0x1, %al
movb %al, -0xbd(%rbp)
jmp 0x110ca
movsbl -0xbd(%rbp), %eax
cmpl $0x0, %eax
jne 0x11375
xorl %edi, %edi
leaq 0x6911(%rip), %rsi      # 0x17c7f
movb $0x0, %al
callq 0x123f0
movb $-0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)