comparison megawifi.c @ 1970:41b9509ede38

megawifi: implement CMD_DATETIME
author doragasu <doragasu@hotmail.com>
date Fri, 08 May 2020 00:26:34 -0700
parents 852393cdec7c
children a8e3e816a50d
comparison
equal deleted inserted replaced
1969:852393cdec7c 1970:41b9509ede38
4 #include <sys/types.h> 4 #include <sys/types.h>
5 #ifdef _WIN32 5 #ifdef _WIN32
6 #define WINVER 0x501 6 #define WINVER 0x501
7 #include <winsock2.h> 7 #include <winsock2.h>
8 #include <ws2tcpip.h> 8 #include <ws2tcpip.h>
9 #include <sys/param.h>
9 #else 10 #else
10 #include <sys/socket.h> 11 #include <sys/socket.h>
11 #include <arpa/inet.h> 12 #include <arpa/inet.h>
12 #include <unistd.h> 13 #include <unistd.h>
13 #include <netinet/in.h> 14 #include <netinet/in.h>
18 #include <time.h> 19 #include <time.h>
19 #include "genesis.h" 20 #include "genesis.h"
20 #include "net.h" 21 #include "net.h"
21 #include "util.h" 22 #include "util.h"
22 23
24 #ifdef _WIN32
25 # if BYTE_ORDER == LITTLE_ENDIAN
26 #define htobe64(val) ((((uint64_t)htonl((val)&0xFFFFFFFF))<<32) | htonl((val)>>32))
27 # else
28 #define htobe64(val) (val)
29 # endif
30 #endif
31
23 enum { 32 enum {
24 TX_IDLE, 33 TX_IDLE,
25 TX_LEN1, 34 TX_LEN1,
26 TX_LEN2, 35 TX_LEN2,
27 TX_PAYLOAD, 36 TX_PAYLOAD,
45 54
46 #ifndef MSG_NOSIGNAL 55 #ifndef MSG_NOSIGNAL
47 #define MSG_NOSIGNAL 0 56 #define MSG_NOSIGNAL 0
48 #endif 57 #endif
49 58
50 enum { 59 enum mw_state {
51 STATE_IDLE=1, 60 STATE_IDLE=1,
52 STATE_AP_JOIN, 61 STATE_AP_JOIN,
53 STATE_SCAN, 62 STATE_SCAN,
54 STATE_READY, 63 STATE_READY,
55 STATE_TRANSPARENT 64 STATE_TRANSPARENT
97 if (!gen->extra) { 106 if (!gen->extra) {
98 socket_init(); 107 socket_init();
99 gen->extra = calloc(1, sizeof(megawifi)); 108 gen->extra = calloc(1, sizeof(megawifi));
100 megawifi *mw = gen->extra; 109 megawifi *mw = gen->extra;
101 mw->module_state = STATE_IDLE; 110 mw->module_state = STATE_IDLE;
111 mw->flags = 0xE0; // cfg_ok, dt_ok, online
102 for (int i = 0; i < 15; i++) { 112 for (int i = 0; i < 15; i++) {
103 mw->sock_fds[i] = -1; 113 mw->sock_fds[i] = -1;
104 } 114 }
105 } 115 }
106 return gen->extra; 116 return gen->extra;
510 mw_putc(mw, rand()); 520 mw_putc(mw, rand());
511 } 521 }
512 end_reply(mw); 522 end_reply(mw);
513 } 523 }
514 524
525 static void cmd_datetime(megawifi *mw)
526 {
527 start_reply(mw, CMD_OK);
528 #ifdef _WIN32
529 __time64_t t = _time64(NULL);
530 int64_t t_be = htobe64(t);
531 mw_copy(mw, (uint8_t*)&t_be, sizeof(int64_t));
532 mw_puts(mw, _ctime64(&t));
533 #else
534 time_t t = time(NULL);
535 int64_t t_be = htobe64(t);
536 mw_copy(mw, (uint8_t*)&t_be, sizeof(int64_t));
537 mw_puts(mw, ctime(&t));
538 #endif
539
540 mw_putc(mw, '\0');
541 end_reply(mw);
542 }
543
515 static void process_command(megawifi *mw) 544 static void process_command(megawifi *mw)
516 { 545 {
517 uint32_t command = mw->transmit_buffer[0] << 8 | mw->transmit_buffer[1]; 546 uint32_t command = mw->transmit_buffer[0] << 8 | mw->transmit_buffer[1];
518 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3]; 547 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3];
519 if (size > mw->transmit_bytes - 4) { 548 if (size > mw->transmit_bytes - 4) {
526 start_reply(mw, CMD_OK); 555 start_reply(mw, CMD_OK);
527 mw_putc(mw, 1); 556 mw_putc(mw, 1);
528 mw_putc(mw, 3); 557 mw_putc(mw, 3);
529 mw_putc(mw, 0); 558 mw_putc(mw, 0);
530 mw_puts(mw, "blastem"); 559 mw_puts(mw, "blastem");
560 mw_putc(mw, '\0');
531 end_reply(mw); 561 end_reply(mw);
532 break; 562 break;
533 case CMD_ECHO: 563 case CMD_ECHO:
534 mw->receive_bytes = mw->transmit_bytes; 564 mw->receive_bytes = mw->transmit_bytes;
535 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes); 565 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes);
600 start_reply(mw, CMD_ERROR); 630 start_reply(mw, CMD_ERROR);
601 end_reply(mw); 631 end_reply(mw);
602 break; 632 break;
603 } 633 }
604 int value = 1; 634 int value = 1;
605 setsockopt(mw->sock_fds[channel], SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); 635 setsockopt(mw->sock_fds[channel], SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(value));
606 struct sockaddr_in bind_addr; 636 struct sockaddr_in bind_addr;
607 memset(&bind_addr, 0, sizeof(bind_addr)); 637 memset(&bind_addr, 0, sizeof(bind_addr));
608 bind_addr.sin_family = AF_INET; 638 bind_addr.sin_family = AF_INET;
609 bind_addr.sin_port = htons(mw->transmit_buffer[8] << 8 | mw->transmit_buffer[9]); 639 bind_addr.sin_port = htons(mw->transmit_buffer[8] << 8 | mw->transmit_buffer[9]);
610 if (bind(mw->sock_fds[channel], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) != 0) { 640 if (bind(mw->sock_fds[channel], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) != 0) {
646 start_reply(mw, CMD_OK); 676 start_reply(mw, CMD_OK);
647 mw_putc(mw, mw->channel_state[channel]); 677 mw_putc(mw, mw->channel_state[channel]);
648 end_reply(mw); 678 end_reply(mw);
649 break; 679 break;
650 } 680 }
681 case CMD_DATETIME:
682 cmd_datetime(mw);
683 break;
651 case CMD_SYS_STAT: 684 case CMD_SYS_STAT:
652 poll_all_sockets(mw); 685 poll_all_sockets(mw);
653 start_reply(mw, CMD_OK); 686 start_reply(mw, CMD_OK);
654 mw_putc(mw, mw->module_state); 687 mw_putc(mw, mw->module_state);
655 mw_putc(mw, mw->flags); 688 mw_putc(mw, mw->flags);
670 break; 703 break;
671 case CMD_SERVER_URL_GET: 704 case CMD_SERVER_URL_GET:
672 start_reply(mw, CMD_OK); 705 start_reply(mw, CMD_OK);
673 // FIXME: This should be get from config file 706 // FIXME: This should be get from config file
674 mw_puts(mw, "doragasu.com"); 707 mw_puts(mw, "doragasu.com");
708 mw_putc(mw,'\0');
675 end_reply(mw); 709 end_reply(mw);
676 break; 710 break;
677 default: 711 default:
678 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size); 712 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size);
679 break; 713 break;