1/* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of 5 * the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 15 * MA 02111-1307 USA 16 */ 17#include <sys/types.h> 18#include <sys/socket.h> 19#include <netinet/in.h> 20#include <arpa/inet.h> 21#include <sys/stat.h> 22#include <sys/time.h> 23#include <sys/select.h> 24#include <sys/resource.h> 25#include <sys/ioctl.h> 26#include <unistd.h> 27#include <stdio.h> 28#include <string.h> 29#include <stdlib.h> 30#include <errno.h> 31#include <dirent.h> 32#include <fcntl.h> 33#include <netdb.h> 34#include <time.h> 35#include <syslog.h> 36 37#include <net/if.h> 38#include <shutils.h> 39#include <notify_rc.h> 40#include <bcmnvram.h> 41 42#include "rc.h" 43#ifdef RTCONFIG_USB 44#include <disk_io_tools.h> 45#endif 46 47#define DEFAULT_SCAN_INTERVAL 5 48#define TCPCHECK_TIMEOUT 3 49#define PING_RESULT_FILE "/tmp/ping_success" 50#define RX_THRESHOLD 40 51 52/* Paul modify 2013/4/2, DSL takes some time to sync up */ 53#ifdef RTCONFIG_DSL 54#define DEFAULT_MAX_DISCONN_COUNT 24 55#else 56#define DEFAULT_MAX_DISCONN_COUNT 12 57#endif 58 59#define PROC_NET_DEV "/proc/net/dev" 60#define WANDUCK_PID_FILE "/var/run/wanduck.pid" 61#define DETECT_FILE "/tmp/internet_check_result" 62 63#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" 64 65#define MAXLINE 2048 66#define PATHLEN 256 67#define MAX_USER 100 68 69#define DFL_HTTP_SERV_PORT "18017" 70#define DFL_DNS_SERV_PORT "18018" 71#define T_HTTP 'H' 72#define T_DNS 'D' 73 74#define DISCONN 0 75#define CONNED 1 76#define C2D 3 77#define D2C 4 78#define PHY_RECONN 5 79#define SET_ETH_MODEM 6 80#define SET_PIN 7 81#define SET_USBSCAN 8 82 83#define CASE_NONE 0 84#define CASE_DISWAN 1 85#define CASE_PPPFAIL 2 86#define CASE_DHCPFAIL 3 87#define CASE_MISROUTE 4 88#define CASE_OTHERS 5 89#define CASE_THESAMESUBNET 6 90#define CASE_PPPNOIP 7 91#define CASE_AP_DISCONN 8 92#define CASE_DATALIMIT 9 93#define CASE_DYN_MODEM 10 94 95#pragma pack(1) // let struct be neat by byte. 96 97typedef struct Flags_Pack { 98 uint16_t reply_1:4, // bit:0-3 99 non_auth_ok:1, // bit:4 100 answer_auth:1, // bit:5 101 reserved:1, // bit:6 102 recur_avail:1, // bit:7 103 recur_desired:1, // bit:8 104 truncated:1, // bit:9 105 authori:1, // bit:10 106 opcode:4, // bit:11-14 107 response:1; // bit:15 108} flag_pack; 109 110typedef struct DNS_HEADER { 111 uint16_t tid; 112 union { 113 uint16_t flag_num; 114 flag_pack flags; 115 } flag_set; 116 uint16_t questions; 117 uint16_t answer_rrs; 118 uint16_t auth_rrs; 119 uint16_t additional_rss; 120} dns_header; 121 122typedef struct QUERIES { 123 char name[PATHLEN]; 124 uint16_t type; 125 uint16_t ip_class; 126} dns_queries; 127 128typedef struct ANSWER{ 129 uint16_t name; 130 uint16_t type; 131 uint16_t ip_class; 132 uint32_t ttl; // time to live 133 uint16_t data_len; 134 uint32_t addr; 135} dns_answer; 136 137typedef struct DNS_REQUEST{ 138 dns_header header; 139 dns_queries queries; 140} dns_query_packet; 141 142typedef struct DNS_RESPONSE{ 143 dns_header header; 144 char *q_name; 145 uint16_t q_type; 146 uint16_t q_class; 147 dns_answer answers; 148} dns_response_packet; 149 150typedef struct REQCLIENT{ 151 int sfd; 152 char type; 153} clients; 154 155#pragma pack() // End. 156 157// var 158int test_log = 0; 159char router_name[PATHLEN]; 160int sw_mode, isFirstUse; 161int boot_end; 162#ifdef RTCONFIG_DUALWAN 163char dualwan_mode[8]; 164char dualwan_wans[16]; 165char wandog_target[PATH_MAX]; 166int wandog_delay, delay_detect; 167int WAN_FB_UNIT; 168#endif 169#ifdef RTCONFIG_USB_MODEM 170char modem_type[32]; 171int sim_lock = 0; 172#ifdef RTCONFIG_INTERNAL_GOBI 173char usb_if[16]; 174#endif 175#endif 176 177int scan_interval; 178int wandog_enable, wandog_maxfail; 179int max_disconn_count[WAN_UNIT_MAX]; 180int max_wait_time[WAN_UNIT_MAX]; 181int max_fb_count; 182int max_fb_wait_time; 183 184int http_sock, dns_sock, maxfd; 185clients client[MAX_USER]; 186fd_set rset, allset; 187int fd_i, cur_sockfd; 188char dst_url[256]; 189 190#define S_IDLE -1 191#define S_COUNT 0 192int conn_changed_state[WAN_UNIT_MAX], changed_count[WAN_UNIT_MAX]; 193unsigned int loop_sec; 194 195int conn_state_old[WAN_UNIT_MAX], conn_state[WAN_UNIT_MAX]; 196int cross_state = 0; 197int disconn_case_old[WAN_UNIT_MAX], disconn_case[WAN_UNIT_MAX]; 198int ppp_fail_state; 199int rule_setup; 200int link_setup[WAN_UNIT_MAX], link_wan[WAN_UNIT_MAX]; 201int got_notify; 202int modem_act_reset; 203 204char prefix_lan[8]; 205int current_lan_unit = 0; 206char current_lan_ifname[16]; 207char current_lan_proto[16]; 208char current_lan_ipaddr[16]; 209char current_lan_netmask[16]; 210char current_lan_gateway[16]; 211char current_lan_dns[256]; 212char current_lan_subnet[11]; 213 214int current_wan_unit = WAN_UNIT_FIRST; 215int other_wan_unit = WAN_UNIT_SECOND; 216int current_state[WAN_UNIT_MAX]; 217 218char nvram_state[WAN_UNIT_MAX][16], nvram_sbstate[WAN_UNIT_MAX][16], nvram_auxstate[WAN_UNIT_MAX][16]; 219 220static int nat_redirect_enable; 221static int nat_redirect_enable_old; 222 223#ifdef RTCONFIG_WIRELESSREPEATER 224int setAP, wlc_state = 0; 225#endif 226 227// func 228void handle_wan_line(int wan_unit, int action); 229void record_wan_state_nvram(int wan_unit, int state, int sbstate, int auxstate); 230