Skip to content

Commit

Permalink
Added support for (virtual) W5100 DNS Offloading.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschmidt committed Apr 22, 2022
1 parent 18f687a commit 7afa74e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 37 deletions.
54 changes: 43 additions & 11 deletions apps/w5100.c
Expand Up @@ -108,13 +108,23 @@ static void set_quad(uint16_t addr, uint32_t data)
*w5100_data = data >> 24;
}

void w5100_config(uint8_t eth_init)
bool w5100_init(uint8_t eth_init)
{
w5100_mode = (uint8_t*)(eth_init << 4 | 0xC084);
w5100_addr_hi = w5100_mode + 1;
w5100_addr_lo = w5100_mode + 2;
w5100_data = w5100_mode + 3;

// PPP Link Control Protocol Request Timer Register defaults to 0x28
// on a real W5100. However, AppleWin features a virtual W5100 that
// supports DNS offloading. On that virtual W5100, the (otherwise
// anyhow unused) register defaults to 0x00 as detection mechanism.
// https://github.com/a2retrosystems/uthernet2/wiki/Virtual-W5100-with-DNS
return get_byte(0x0028) == 0x00;
}

void w5100_config(void)
{
#ifdef SINGLE_SOCKET

// IP65 is inhibited so disable the W5100 Ping Block Mode.
Expand Down Expand Up @@ -169,14 +179,14 @@ void w5100_config(uint8_t eth_init)
}
}

bool w5100_connect(uint32_t addr, uint16_t port)
static bool w5100_connect(uint16_t port)
{
// Socket x Mode Register: TCP
set_byte(SOCK_REG(0x00), 0x01);

// Socket x Source Port Register
set_word(SOCK_REG(0x04), ip65_random_word());

// Socket x Destination Port Register
set_word(SOCK_REG(0x10), port);

// Socket x Command Register: OPEN
set_byte(SOCK_REG(0x01), 0x01);

Expand All @@ -189,12 +199,6 @@ bool w5100_connect(uint32_t addr, uint16_t port)
}
}

// Socket x Destination IP Address Register
set_quad(SOCK_REG(0x0C), addr);

// Socket x Destination Port Register
set_word(SOCK_REG(0x10), port);

// Socket x Command Register: CONNECT
set_byte(SOCK_REG(0x01), 0x04);

Expand All @@ -214,6 +218,34 @@ bool w5100_connect(uint32_t addr, uint16_t port)
}
}

bool w5100_connect_addr(uint32_t addr, uint16_t port)
{
// Socket x Mode Register: TCP, Use No Delayed ACK
set_byte(SOCK_REG(0x00), 0x21);

// Socket x Destination IP Address Register
set_quad(SOCK_REG(0x0C), addr);

return w5100_connect(port);
}

bool w5100_connect_name(const char* name, uint8_t length, uint16_t port)
{
// Socket x Mode Register: TCP, Use No Delayed ACK, Use DNS Offloading
set_byte(SOCK_REG(0x00), 0x29);

// Socket x DNS name length
set_byte(SOCK_REG(0x2A), length);

// Socket x DNS name chars
while (length--)
{
*w5100_data = *name++;
}

return w5100_connect(port);
}

bool w5100_connected(void)
{
// Socket x Status Register: SOCK_ESTABLISHED ?
Expand Down
16 changes: 13 additions & 3 deletions apps/w5100.h
Expand Up @@ -46,13 +46,23 @@ void w5100_data_commit(bool do_send, uint16_t size);
// to be sent to the server.
extern volatile uint8_t* w5100_data;

// Initialize this module after the IP65 TCP/IP stack has been initialized.
// Return true if the (virtual) W5100 supports DNS Offloading.
// https://github.com/a2retrosystems/uthernet2/wiki/Virtual-W5100-with-DNS
bool w5100_init(uint8_t eth_init);

// Configure W5100 Ethernet controller with additional information from IP65
// after the IP65 TCP/IP stack has been configured.
void w5100_config(uint8_t eth_init);
void w5100_config(void);

// Connect to server with IP address <addr> on TCP port <port>.
// Return true if the connection is established, return false otherwise.
bool w5100_connect_addr(uint32_t addr, uint16_t port);

// Connect to server with IP address <server_addr> on TCP port <server_port>.
// Connect to server with name <name>, <length> on TCP port <port> using
// DNS Offloading.
// Return true if the connection is established, return false otherwise.
bool w5100_connect(uint32_t addr, uint16_t port);
bool w5100_connect_name(const char* name, uint8_t length, uint16_t port);

// Check if still connected to server.
// Return true if the connection is established, return false otherwise.
Expand Down
39 changes: 29 additions & 10 deletions apps/w5100_http.c
Expand Up @@ -39,19 +39,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma optimize (on)
#pragma static-locals (on)

bool w5100_http_open(uint32_t addr, uint16_t port, const char* selector,
char* buffer, size_t length)
static bool w5100_http_open(const char* selector, char* buffer, size_t length)
{
register volatile uint8_t *data = w5100_data;

printf("Connecting to %s:%d ", dotted_quad(addr), port);

if (!w5100_connect(addr, port))
{
printf("- Connect failed\n");
return false;
}

printf("- Ok\n\nSending request ");
{
uint16_t snd;
Expand Down Expand Up @@ -177,3 +168,31 @@ bool w5100_http_open(uint32_t addr, uint16_t port, const char* selector,
}
return true;
}

bool w5100_http_open_addr(uint32_t addr, uint16_t port, const char* selector,
char* buffer, size_t length)
{
printf("Connecting to %s:%d ", dotted_quad(addr), port);

if (!w5100_connect_addr(addr, port))
{
printf("- Connect failed\n");
return false;
}

return w5100_http_open(selector, buffer, length);
}

bool w5100_http_open_name(const char* name, uint8_t name_length, uint16_t port,
const char* selector, char* buffer, size_t buffer_length)
{
printf("Connecting to port %d ", port);

if (!w5100_connect_name(name, name_length, port))
{
printf("- Connect failed\n");
return false;
}

return w5100_http_open(selector, buffer, buffer_length);
}
20 changes: 14 additions & 6 deletions apps/w5100_http.h
Expand Up @@ -37,12 +37,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdint.h>
#include <stdbool.h>

// Connect to server with IP address <server_addr> on TCP port <server_port>,
// then HTTP GET <selector> and consume HTTP response header. Provide feedback
// on progress to the user via STDOUT. After returning from w5100_http_open()
// the connection is ready to consume the HTTP body.
// Connect to server with IP address <addr> on TCP port <port>, then HTTP GET
// <selector> and consume HTTP response header. Provide feedback on progress
// to the user via STDOUT. After returning from w5100_http_open_addr(), the
// connection is ready to consume the HTTP body.
// Return true if the connection is established, return false otherwise.
bool w5100_http_open(uint32_t addr, uint16_t port, const char* selector,
char* buffer, size_t length);
bool w5100_http_open_addr(uint32_t addr, uint16_t port, const char* selector,
char* buffer, size_t length);

// Connect to server with name <name>, <name_length> on TCP port <port> using
// DNS Offloading, then HTTP GET <selector> and consume HTTP response header.
// Provide feedback on progress to the user via STDOUT. After returning from
// w5100_http_open_name(), the connection is ready to consume the HTTP body.
// Return true if the connection is established, return false otherwise.
bool w5100_http_open_name(const char* name, uint8_t name_length, uint16_t port,
const char* selector, char* buffer, size_t buffer_length);

#endif
32 changes: 25 additions & 7 deletions apps/wget65.c
Expand Up @@ -435,6 +435,7 @@ int main(int, char *argv[])
uint16_t i;
char *arg;
char device;
bool Offload_DNS;
uint8_t eth_init = ETH_INIT_DEFAULT;

if (doesclrscrafterexit())
Expand Down Expand Up @@ -481,23 +482,28 @@ int main(int, char *argv[])
// Abort on Ctrl-C to be consistent with Linenoise
abort_key = 0x83;

printf("- Ok\n\nObtaining IP address ");
if (dhcp_init())
Offload_DNS = w5100_init(eth_init);

if (!Offload_DNS)
{
ip65_error_exit();
printf("- Ok\n\nObtaining IP address ");
if (dhcp_init())
{
ip65_error_exit();
}
}
printf("- Ok\n\n");

// Copy IP config from IP65 to W5100
w5100_config(eth_init);
w5100_config();

load_argument("wget.urls");
while (true)
{
arg = get_argument(1, "URL", url_completion);

printf("\n\nProcessing URL ");
if (!url_parse(arg, true))
if (!url_parse(arg, !Offload_DNS))
{
break;
}
Expand Down Expand Up @@ -650,9 +656,21 @@ int main(int, char *argv[])
save_argument("wget.files");

printf("\n\n");
if (!w5100_http_open(url_ip, url_port, url_selector, buffer, sizeof(buffer)))
if (Offload_DNS)
{
return EXIT_FAILURE;
if (!w5100_http_open_name(url_host, strlen(url_host) - 4, url_port,
url_selector, buffer, sizeof(buffer)))
{
return EXIT_FAILURE;
}
}
else
{
if (!w5100_http_open_addr(url_ip, url_port,
url_selector, buffer, sizeof(buffer)))
{
return EXIT_FAILURE;
}
}

if (device)
Expand Down

0 comments on commit 7afa74e

Please sign in to comment.