-
return true;*
-
}*
-
}*
-
// DHCP and DNS is okay*
-
if (IPH_PROTO(ip_h) == IP_PROTO_UDP) {*
-
if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct udp_hdr))*
-
return false;*
udp_he = (struct udp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
-
if (ntohs(udp_he->dest) == DHCP_PORT)*
-
return true;*
-
if (ntohs(udp_he->dest) == DNS_PORT)*
-
return true;*
-
return false;*
-
}*
-
// HTTP is redirected*
-
if (IPH_PROTO(ip_h) == IP_PROTO_TCP) {*
-
if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct tcp_hdr))*
-
return false;*
tcp_h = (struct tcp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
-
if (ntohs(tcp_h->dest) == HTTP_PORT) {*
-
curr_mac = mac_h->src;*
-
curr_IP = ip_h->dest.addr;*
-
ip_napt_modify_addr_tcp(tcp_h, &ip_h->dest, (uint32_t)myIP);*
-
ip_napt_modify_addr(ip_h, &ip_h->dest, (uint32_t)myIP);*
-
return true;*
-
}*
-
}*
-
// drop anything else*
-
return false;*
}
err_t my_input_ap (struct pbuf *p, struct netif *inp) {
-
if (check_packet_in(p)) {*
-
return orig_input_ap(p, inp);*
-
} else {*
-
pbuf_free(p);*
-
return ERR_OK;*
-
}*
}
bool check_packet_out(struct pbuf *p) {
struct eth_hdr *mac_h;
struct ip_hdr *ip_h;
struct tcp_hdr *tcp_h;
-
if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct tcp_hdr))*
-
return true;*
ip_h = (struct ip_hdr *)(p->payload + sizeof(struct eth_hdr));
-
if (IPH_PROTO(ip_h) != IP_PROTO_TCP)*
-
return true;*
tcp_h = (struct tcp_hdr *)(p->payload + sizeof(struct eth_hdr) + sizeof(struct ip_hdr));
-
// rewrite packet from our HTTP server*
-
if (ntohs(tcp_h->src) == HTTP_PORT && ip_h->src.addr == (uint32_t)myIP) {*
-
ip_napt_modify_addr_tcp(tcp_h, &ip_h->src, curr_IP);*
-
ip_napt_modify_addr(ip_h, &ip_h->src, curr_IP);*
-
}*
-
return true;*
}
err_t my_output_ap (struct netif *outp, struct pbuf *p) {
-
if (check_packet_out(p)) {*
-
return orig_output_ap(outp, p);*
-
} else {*
-
pbuf_free(p);*
-
return ERR_OK;*
-
}*
}
// patches the netif to insert the filter functions
void patch_netif(ip_addr_t netif_ip, netif_input_fn ifn, netif_input_fn *orig_ifn, netif_linkoutput_fn ofn, netif_linkoutput_fn *orig_ofn)
{
struct netif *nif;
-
for (nif = netif_list; nif != NULL && nif->ip_addr.addr != netif_ip.addr; nif = nif->next);*
-
if (nif == NULL) return;*
-
if (ifn != NULL && nif->input != ifn) {*
*orig_ifn = nif->input;
-
nif->input = ifn;*
-
}*
-
if (ofn != NULL && nif->linkoutput != ofn) {*
*orig_ofn = nif->linkoutput;
-
nif->linkoutput = ofn;*
-
}*
}
void setup()
{
-
Serial.begin(115200);*
-
Serial.println();*
-
WiFi.mode(WIFI_AP_STA);*
-
Serial.println("Connecting to STA");*
-
WiFi.begin(sta_ssid, sta_password);*
-
//Wifi connection*
-
while (WiFi.status() != WL_CONNECTED) {*
-
delay(500);*
-
Serial.print(".");*
-
}*
-
Serial.println("");*
-
Serial.print("Connected to ");*
-
Serial.println(sta_ssid);*
-
Serial.print("IP address: ");*
-
Serial.println(WiFi.localIP());*
-
Serial.print("dnsIP address: ");*
-
Serial.println(WiFi.dnsIP());*
-
Serial.print("gatewayIP address: ");*
-
Serial.println(WiFi.gatewayIP());*
-
Serial.print("subnetMask address: ");*
-
Serial.println(WiFi.subnetMask());*
-
Serial.println("");*
-
Serial.println("Configuring access point...");*
-
WiFi.softAP(AP_SSID, NULL, 1, 0, 8);*
-
myIP = WiFi.softAPIP();*
-
Serial.print("AP IP address: ");*
-
Serial.println(myIP);*
-
// Insert the filter functions*
-
patch_netif(myIP, my_input_ap, &orig_input_ap, my_output_ap, &orig_output_ap);*
-
// Initialize the NAT feature*
-
ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);*
-
// Enable NAT on the AP interface*
-
ip_napt_enable_no(1, 1);*
-
// Set the DNS server for clients of the AP to the one we also use for the STA interface*
-
dhcps_set_DNS(WiFi.dnsIP());*
Serial.println("Connecting To MySQL Database");
-
if (conn.connect(server_addr, 3306, user, password)) {*
-
delay(1000);*
-
}*
-
else*
-
Serial.println("Connection failed.");*
-
char query[] = "SELECT code FROM gilde.code LIMIT 1";*
-
Serial.println("\nRunning SELECT from CODE and printing results\n");*
-
// Initiate the query class instance*
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
-
// Execute the query with the PROGMEM option*
-
cur_mem->execute(query, true);*
-
// Show the results*
-
//cur_mem->show_results();*
-
// Deleting the cursor also frees up memory used*
column_names *cols = cur_mem->get_columns();
-
Serial.println();*
row_values *row = NULL;
-
row = cur_mem->get_next_row();*
-
Serial.println("\nThe Code:");*
-
String thecode = row->values[0];*
-
Serial.println(thecode);*
-
Serial.println("\nVerwijder SQL Memory");*
-
delete cur_mem;*
-
conn.close();*
-
webServer.on("/", {*
-
webServer.send(200, "text/html", s);*
-
});*
-
webServer.on("/pass", {*
-
webServer.send(200, "text/html", v);*
-
});*
-
webServer.on("/accepted", {*
-
for (int i = 0; i < 6; i++) {*
Serial.print(curr_mac.addr*);Serial.print(":");
_ }_
_ Serial.println(" allowed");_
if (max_client < MAX_CLIENTS) {
allowed_macs[max_client++] = curr_mac;
_ }_
_ webServer.send(200, "text/html", acceptedHTML);_
_ });_
_ // redirect all other URIs to our "/"_
_ webServer.onNotFound( {_
_ webServer.sendHeader("Location", String("http://")+myIP.toString()+String("/"), true);_
_ webServer.send (302, "text/plain", "");_
_ });_
_ webServer.begin();_
_}_
void loop()
_{_
_ webServer.handleClient();_
_}*_