2015-05-01 12:34:47 +08:00
|
|
|
#include <generated/csr.h>
|
|
|
|
|
|
|
|
#ifdef CSR_ETHMAC_BASE
|
|
|
|
|
|
|
|
#include <netif/etharp.h>
|
|
|
|
#include <netif/liteethif.h>
|
|
|
|
#include <lwip/init.h>
|
|
|
|
#include <lwip/memp.h>
|
|
|
|
#include <lwip/ip4_addr.h>
|
|
|
|
#include <lwip/ip4.h>
|
|
|
|
#include <lwip/netif.h>
|
|
|
|
#include <lwip/sys.h>
|
|
|
|
#include <lwip/tcp.h>
|
|
|
|
#include <lwip/timers.h>
|
|
|
|
|
|
|
|
#include "session.h"
|
2015-07-30 18:45:57 +08:00
|
|
|
#include "net_server.h"
|
2015-05-01 12:34:47 +08:00
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate {
|
2015-05-01 12:34:47 +08:00
|
|
|
int magic_recognized;
|
|
|
|
struct pbuf *rp;
|
|
|
|
int rp_offset;
|
|
|
|
};
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static struct net_server_connstate *cs_new(void)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate *cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
cs = (struct net_server_connstate *)mem_malloc(sizeof(struct net_server_connstate));
|
2015-05-01 12:34:47 +08:00
|
|
|
if(!cs)
|
|
|
|
return NULL;
|
|
|
|
cs->magic_recognized = 0;
|
|
|
|
cs->rp = NULL;
|
|
|
|
cs->rp_offset = 0;
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static void cs_free(struct net_server_connstate *cs)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
|
|
|
if(cs->rp)
|
|
|
|
pbuf_free(cs->rp);
|
|
|
|
mem_free(cs);
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static const char net_server_magic[] = "ARTIQ coredev\n";
|
2015-05-01 12:34:47 +08:00
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static int magic_ok(struct net_server_connstate *cs)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
|
|
|
return cs->magic_recognized >= 14;
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static struct net_server_connstate *active_cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
static struct tcp_pcb *active_pcb;
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static void net_server_close(struct net_server_connstate *cs, struct tcp_pcb *pcb)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
|
|
|
if(cs == active_cs) {
|
|
|
|
session_end();
|
|
|
|
active_cs = NULL;
|
|
|
|
active_pcb = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:32:37 +08:00
|
|
|
if(pcb) {
|
|
|
|
/* lwip loves to call back with broken pointers. Prevent that. */
|
|
|
|
tcp_arg(pcb, NULL);
|
|
|
|
tcp_recv(pcb, NULL);
|
|
|
|
tcp_sent(pcb, NULL);
|
|
|
|
tcp_err(pcb, NULL);
|
|
|
|
|
|
|
|
tcp_close(pcb);
|
|
|
|
}
|
2015-05-01 12:34:47 +08:00
|
|
|
cs_free(cs);
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static err_t net_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate *cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
cs = (struct net_server_connstate *)arg;
|
2015-05-01 12:34:47 +08:00
|
|
|
if(p) {
|
|
|
|
if(cs->rp)
|
|
|
|
pbuf_cat(cs->rp, p);
|
|
|
|
else {
|
|
|
|
cs->rp = p;
|
|
|
|
cs->rp_offset = 0;
|
|
|
|
}
|
|
|
|
} else
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_close(cs, pcb);
|
2015-05-01 12:34:47 +08:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static err_t net_server_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
2015-08-08 19:06:11 +08:00
|
|
|
session_ack_sent(len);
|
2015-05-01 12:34:47 +08:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcp_pcb_service(void *arg, struct tcp_pcb *pcb)
|
|
|
|
{
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate *cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
int remaining_in_pbuf;
|
|
|
|
char *rpp;
|
|
|
|
struct pbuf *next;
|
|
|
|
int r;
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
cs = (struct net_server_connstate *)arg;
|
2015-05-01 12:34:47 +08:00
|
|
|
|
|
|
|
while(cs->rp) {
|
|
|
|
remaining_in_pbuf = cs->rp->len - cs->rp_offset;
|
|
|
|
rpp = (char *)cs->rp->payload;
|
|
|
|
while(remaining_in_pbuf > 0) {
|
|
|
|
if(cs == active_cs) {
|
|
|
|
r = session_input(&rpp[cs->rp_offset], remaining_in_pbuf);
|
|
|
|
if(r > 0) {
|
|
|
|
tcp_recved(pcb, r);
|
|
|
|
cs->rp_offset += r;
|
|
|
|
remaining_in_pbuf -= r;
|
|
|
|
} else if(r == 0)
|
|
|
|
return;
|
|
|
|
else
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_close(cs, pcb);
|
2015-05-01 12:34:47 +08:00
|
|
|
} else {
|
2015-07-30 18:45:57 +08:00
|
|
|
if(rpp[cs->rp_offset] == net_server_magic[cs->magic_recognized]) {
|
2015-05-01 12:34:47 +08:00
|
|
|
cs->magic_recognized++;
|
|
|
|
if(magic_ok(cs)) {
|
|
|
|
if(active_cs)
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_close(active_cs, active_pcb);
|
2015-05-01 12:34:47 +08:00
|
|
|
session_start();
|
|
|
|
active_cs = cs;
|
|
|
|
active_pcb = pcb;
|
2015-07-30 18:45:57 +08:00
|
|
|
tcp_sent(pcb, net_server_sent);
|
2015-05-01 12:34:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_close(cs, pcb);
|
2015-05-01 12:34:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
remaining_in_pbuf--;
|
|
|
|
cs->rp_offset++;
|
|
|
|
tcp_recved(pcb, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next = cs->rp->next;
|
|
|
|
if(cs->rp->tot_len != cs->rp->len) {
|
|
|
|
pbuf_ref(next);
|
|
|
|
pbuf_free(cs->rp);
|
|
|
|
cs->rp = next;
|
|
|
|
cs->rp_offset = 0;
|
|
|
|
} else {
|
|
|
|
pbuf_free(cs->rp);
|
|
|
|
cs->rp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static void net_server_err(void *arg, err_t err)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate *cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
cs = (struct net_server_connstate *)arg;
|
2015-08-13 18:32:37 +08:00
|
|
|
net_server_close(cs, NULL);
|
2015-05-01 12:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tcp_pcb *listen_pcb;
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
static err_t net_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
2015-07-30 18:45:57 +08:00
|
|
|
struct net_server_connstate *cs;
|
2015-05-01 12:34:47 +08:00
|
|
|
|
|
|
|
cs = cs_new();
|
|
|
|
if(!cs)
|
|
|
|
return ERR_MEM;
|
|
|
|
tcp_accepted(listen_pcb);
|
|
|
|
tcp_arg(newpcb, cs);
|
2015-07-30 18:45:57 +08:00
|
|
|
tcp_recv(newpcb, net_server_recv);
|
|
|
|
tcp_err(newpcb, net_server_err);
|
2015-05-01 12:34:47 +08:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
void net_server_init(void)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
|
|
|
listen_pcb = tcp_new();
|
2015-08-13 18:32:37 +08:00
|
|
|
listen_pcb->so_options |= SOF_KEEPALIVE;
|
2015-05-01 12:34:47 +08:00
|
|
|
tcp_bind(listen_pcb, IP_ADDR_ANY, 1381);
|
|
|
|
listen_pcb = tcp_listen(listen_pcb);
|
2015-07-30 18:45:57 +08:00
|
|
|
tcp_accept(listen_pcb, net_server_accept);
|
2015-05-01 12:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern struct tcp_pcb *tcp_active_pcbs;
|
|
|
|
|
2015-07-30 18:45:57 +08:00
|
|
|
void net_server_service(void)
|
2015-05-01 12:34:47 +08:00
|
|
|
{
|
|
|
|
struct tcp_pcb *pcb;
|
|
|
|
void *data;
|
|
|
|
int len, sndbuf;
|
|
|
|
|
|
|
|
/* Assume all active TCP PCBs with a non-NULL arg are our connections. */
|
|
|
|
pcb = tcp_active_pcbs;
|
|
|
|
while(pcb) {
|
|
|
|
if(pcb->callback_arg)
|
|
|
|
tcp_pcb_service(pcb->callback_arg, pcb);
|
|
|
|
pcb = pcb->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(active_cs) {
|
|
|
|
session_poll(&data, &len);
|
|
|
|
if(len > 0) {
|
|
|
|
sndbuf = tcp_sndbuf(active_pcb);
|
|
|
|
if(len > sndbuf)
|
|
|
|
len = sndbuf;
|
|
|
|
tcp_write(active_pcb, data, len, 0);
|
2015-08-08 19:06:11 +08:00
|
|
|
session_ack_consumed(len);
|
2015-05-01 12:34:47 +08:00
|
|
|
}
|
|
|
|
if(len < 0)
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_close(active_cs, active_pcb);
|
2015-05-01 12:34:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CSR_ETHMAC_BASE */
|