// File: /workspace/github_c_makefile_star2_O2/jech[P]shncpd/ra.c
// Function: setup_ra_socket
int
setup_ra_socket()
{
    int s, i, rc, one = 1, ff = 255;
    struct icmp6_filter filter;

    if(ra_socket >= 0) {
        close(ra_socket);
        ra_socket = -1;
    }

    s = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
    if(s < 0)
        return -1;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ff, sizeof(ff));
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ff, sizeof(ff));
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &one, sizeof(one));
    if(rc < 0)
        goto fail;

    for(i = 0; i < numinterfaces; i++) {
        struct ipv6_mreq mreq;
        const unsigned char all_routers[16] =
            {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
        if(interfaces[i].ifindex <= 0)
            continue;
        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, &all_routers, 16);
        mreq.ipv6mr_interface = interfaces[i].ifindex;
        rc = setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                        (char*)&mreq, sizeof(mreq));
        if(rc < 0)
            goto fail;
    }

    ICMP6_FILTER_SETBLOCKALL(&filter);
    ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);

    rc = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter));
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
    if(rc < 0)
        goto fail;

    ra_socket = s;
    return s;

fail:
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x1, 0xc(%rsp)
movl $0xff, 0x8(%rsp)
movl 0xabc0(%rip), %edi      # 0x111f4
testl %edi, %edi
js 0x6644
callq 0x2190
orl $-0x1, 0xabb0(%rip)     # 0x111f4
pushq $0xa
popq %rdi
pushq $0x3
popq %rsi
pushq $0x3a
popq %rdx
callq 0x23a0
pushq $-0x1
popq %rbx
testl %eax, %eax
js 0x67bc
movl %eax, %ebp
pushq $0x29
popq %rsi
pushq $0x10
popq %rdx
leaq 0x8(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x20a0
testl %eax, %eax
js 0x67bc
pushq $0x29
popq %rsi
pushq $0x12
popq %rdx
leaq 0x8(%rsp), %rcx
pushq $0x4
popq %r8
movl %ebp, %edi
callq 0x20a0
testl %eax, %eax
js 0x67bc
pushq $0x29
popq %rsi
pushq $0x33
popq %rdx
leaq 0xc(%rsp), %rcx
pushq $0x4
popq %r8
movl %ebp, %edi
callq 0x20a0
testl %eax, %eax
js 0x67bc
leaq 0xabf0(%rip), %r13      # 0x112b0
addq $0x8, %r13
leaq 0xabd5(%rip), %rax      # 0x112a0
movl (%rax), %eax
xorl %ebx, %ebx
pushq $0x29
popq %r14
leaq 0x10(%rsp), %r15
pushq $0x14
popq %r12
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x672e
movl (%r13), %ecx
testl %ecx, %ecx
jle 0x6722
movdqa 0x65cc(%rip), %xmm0     # 0xccc0
movdqa %xmm0, 0x10(%rsp)
movl %ecx, 0x20(%rsp)
movl %ebp, %edi
movl %r14d, %esi
movl %r12d, %edx
movq %r15, %rcx
movl %r12d, %r8d
callq 0x20a0
testl %eax, %eax
js 0x67b9
leaq 0xab80(%rip), %rax      # 0x112a0
movl (%rax), %eax
incq %rbx
addq $0x98, %r13
jmp 0x66dc
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rcx
movdqa %xmm0, 0x10(%rcx)
movdqa %xmm0, (%rcx)
movl $0xffffffdf, 0x10(%rcx) # imm = 0xFFFFFFDF
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
pushq $0x20
popq %r8
movl %ebp, %edi
callq 0x20a0
testl %eax, %eax
js 0x67b9
pushq $0x1
popq %rsi
movl %ebp, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
pushq $-0x1
popq %rbx
js 0x67bc
orl $0x1, %eax
pushq $0x2
popq %rsi
movl %ebp, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x67bc
pushq $0x3
popq %rsi
movl %ebp, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x67bc
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %ebp, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x67bc
movl %ebp, 0xaa3f(%rip)      # 0x111f4
movl %ebp, %ebx
jmp 0x67bc
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq