// File: blakewford[P]kab/main.cpp
// Function: wemoResponse()
void wemoResponse()
{
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sock < 0) return;

    struct sockaddr_in server = buildServerType(INADDR_ANY, DISCOVER_WEMO);
    if(bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) return;

    struct ip_mreq group;
    group.imr_multiaddr.s_addr = inet_addr(SSDP);
    int ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&group, sizeof(group));
    if(ret != 0) return;

    pthread_t watchDog;
    pthread_create(&watchDog, nullptr, timeoutThread, &sock);

    const char* packet =
"HTTP/1.1 200 OK\r\n"\
"CACHE-CONTROL: max-age=86400\r\n"\
"DATE: %s, %02d %s %d %02d:%02d:%02d %s\r\n"\
"EXT:\r\n"\
"LOCATION: http://%s:49153/setup.xml\r\n"\
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"\
"01-NLS: 11111111-2222-3333-4444-555555555555\r\n"\
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"\
"X-User-Agent: redsonic\r\n"\
"ST: urn:Belkin:device:controllee:1\r\n"\
"USN: uuid:Socket-1_0-2GTbor5wqtq79E::urn:Belkin:device:controllee:1\r\n\r\n";

    struct timeval now;
    gettimeofday(&now, nullptr);
    struct tm* gmt = gmtime(&now.tv_sec);

    const char* DAYS[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    const char* MONTHS[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    socklen_t length;
    char buffer[RESPONSE_SIZE];
    while(gKeepGoing)
    {
        ssize_t recv = recvfrom(sock, buffer, RESPONSE_SIZE, 0, (sockaddr*)&server, &length);
        std::string search(buffer);
//        if(search.find("Belkin") != std::string::npos)
        {
            inet_ntop(AF_INET, &server.sin_addr, buffer, INET_ADDRSTRLEN);
            printf("%s %d\n", buffer, server.sin_port);

            int reply = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
            sendto(reply, packet, strlen(packet), 0, (sockaddr*)&server, sizeof(server));
            close(reply);
        }
    }
}

// Assembly:
subq $0x2f8, %rsp            # imm = 0x2F8
movl $0x2, %esi
movl $0x11, %edx
movl %esi, %edi
callq 0x2080
movl %eax, 0x2f4(%rsp)
cmpl $0x0, 0x2f4(%rsp)
jge 0x2ede
jmp 0x313a
xorl %edi, %edi
movl $0x76c, %esi            # imm = 0x76C
callq 0x2570
movq %rax, 0x2e4(%rsp)
movq %rdx, 0x2ec(%rsp)
movl 0x2f4(%rsp), %edi
leaq 0x2e4(%rsp), %rsi
movl $0x10, %edx
callq 0x2060
cmpl $0x0, %eax
jge 0x2f1d
jmp 0x313a
leaq 0x5138(%rip), %rdi      # 0x805c
callq 0x2250
movl %eax, 0x2dc(%rsp)
movl 0x2f4(%rsp), %edi
xorl %esi, %esi
movl $0x23, %edx
leaq 0x2dc(%rsp), %rcx
movl $0x8, %r8d
callq 0x23c0
movl %eax, 0x2d8(%rsp)
cmpl $0x0, 0x2d8(%rsp)
je 0x2f67
jmp 0x313a
leaq 0x2d0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x5ca(%rip), %rdx      # 0x29b0
leaq 0x2f4(%rsp), %rcx
callq 0x23d0
leaq 0x5154(%rip), %rax      # 0x80e2
movq %rax, 0x2c8(%rsp)
leaq 0x2b8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2360
leaq 0x2b8(%rsp), %rdi
callq 0x21d0
movq %rax, 0x2b0(%rsp)
leaq 0x270(%rsp), %rdi
leaq 0x8c25(%rip), %rsi      # 0xbbf0
movl $0x38, %edx
callq 0x21c0
leaq 0x210(%rsp), %rdi
leaq 0x8c4c(%rip), %rsi      # 0xbc30
movl $0x60, %edx
callq 0x21c0
testb $0x1, 0x921b(%rip)      # 0xc210
je 0x313a
movl 0x2f4(%rsp), %edi
leaq 0x70(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movl $0x198, %edx            # imm = 0x198
xorl %ecx, %ecx
leaq 0x2e4(%rsp), %r8
leaq 0x20c(%rsp), %r9
callq 0x21a0
movq %rax, 0x68(%rsp)
leaq 0x47(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x23b0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0x22b0
jmp 0x3052
leaq 0x47(%rsp), %rdi
callq 0x2270
leaq 0x2e8(%rsp), %rsi
movl $0x2, %edi
leaq 0x70(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movl $0x10, %ecx
callq 0x2260
movq 0x18(%rsp), %rsi
movzwl 0x2e6(%rsp), %edx
leaq 0x522e(%rip), %rdi      # 0x82bf
xorl %eax, %eax
callq 0x2030
jmp 0x309a
movl $0x2, %esi
movl $0x11, %edx
movl %esi, %edi
callq 0x2080
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x2c8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2100
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x2e4(%rsp), %r8
movl $0x10, %r9d
callq 0x2050
jmp 0x30ec
movl 0x30(%rsp), %edi
callq 0x23f0
jmp 0x30f7
leaq 0x48(%rsp), %rdi
callq 0x2150
jmp 0x2fee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x47(%rsp), %rdi
callq 0x2270
jmp 0x3142
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x2150
jmp 0x3142
addq $0x2f8, %rsp            # imm = 0x2F8
retq
movq 0x38(%rsp), %rdi
callq 0x23a0
nopl (%rax)