2003-01-18 18:18:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwip/debug.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
2004-08-10 14:06:19 +00:00
|
|
|
/*#include <netinet/in.h> */
|
|
|
|
/*#include <arpa/inet.h> */
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/pbuf.h"
|
2005-11-08 12:00:45 +00:00
|
|
|
#include "netif/list.h"
|
2003-01-18 18:18:02 +00:00
|
|
|
#include "netif/unixif.h"
|
|
|
|
#include "lwip/sys.h"
|
2010-01-07 10:21:13 +00:00
|
|
|
#include "lwip/timers.h"
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "netif/tcpdump.h"
|
|
|
|
|
|
|
|
#define UNIXIF_BPS 512000
|
|
|
|
#define UNIXIF_QUEUELEN 6
|
|
|
|
/*#define UNIXIF_DROP_FIRST */
|
|
|
|
|
2010-02-21 11:26:15 +00:00
|
|
|
#ifndef UNIXIF_DEBUG
|
|
|
|
#define UNIXIF_DEBUG LWIP_DBG_OFF
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
struct unixif_buf {
|
|
|
|
struct pbuf *p;
|
|
|
|
unsigned short len, tot_len;
|
2010-03-26 14:20:39 +00:00
|
|
|
void *payload;
|
2003-01-18 18:18:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct unixif {
|
|
|
|
int fd;
|
|
|
|
sys_sem_t sem;
|
|
|
|
struct list *q;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static int
|
2010-03-26 14:20:39 +00:00
|
|
|
unix_socket_client(const char *name)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2012-03-01 18:15:38 +00:00
|
|
|
int fd;
|
|
|
|
#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
|
|
|
|
int len;
|
|
|
|
#endif
|
2003-01-18 18:18:02 +00:00
|
|
|
struct sockaddr_un unix_addr;
|
|
|
|
|
|
|
|
/* create a Unix domain stream socket */
|
2003-05-01 13:27:52 +00:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_client: socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* fill socket address structure w/our address */
|
|
|
|
memset(&unix_addr, 0, sizeof(unix_addr));
|
|
|
|
unix_addr.sun_family = AF_UNIX;
|
2005-10-10 07:25:35 +00:00
|
|
|
snprintf(unix_addr.sun_path, sizeof(unix_addr.sun_path), "%s%05d", "/var/tmp/", getpid());
|
2010-02-09 13:19:01 +00:00
|
|
|
#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
|
2003-01-18 18:18:02 +00:00
|
|
|
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
|
|
|
strlen(unix_addr.sun_path) + 1;
|
|
|
|
unix_addr.sun_len = len;
|
|
|
|
#endif /* linux */
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
unlink(unix_addr.sun_path); /* in case it already exists */
|
2003-05-01 13:27:52 +00:00
|
|
|
if (bind(fd, (struct sockaddr *) &unix_addr,
|
2012-03-01 18:15:38 +00:00
|
|
|
sizeof(struct sockaddr_un)) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_client: socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
2003-05-01 13:27:52 +00:00
|
|
|
if (chmod(unix_addr.sun_path, S_IRWXU | S_IRWXO) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_client: socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* fill socket address structure w/server's addr */
|
|
|
|
memset(&unix_addr, 0, sizeof(unix_addr));
|
|
|
|
unix_addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(unix_addr.sun_path, name);
|
2010-02-09 13:19:01 +00:00
|
|
|
#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
|
2003-01-18 18:18:02 +00:00
|
|
|
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
|
|
|
strlen(unix_addr.sun_path) + 1;
|
|
|
|
unix_addr.sun_len = len;
|
|
|
|
#endif /* linux */
|
2003-05-01 13:27:52 +00:00
|
|
|
if (connect(fd, (struct sockaddr *) &unix_addr,
|
2012-03-01 18:15:38 +00:00
|
|
|
sizeof(struct sockaddr_un)) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_client: socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static int
|
2010-03-26 14:20:39 +00:00
|
|
|
unix_socket_server(const char *name)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
2012-03-01 18:15:38 +00:00
|
|
|
int fd;
|
|
|
|
#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
|
|
|
|
int len;
|
|
|
|
#endif
|
2003-01-18 18:18:02 +00:00
|
|
|
struct sockaddr_un unix_addr;
|
|
|
|
|
|
|
|
/* create a Unix domain stream socket */
|
2003-05-01 13:27:52 +00:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_server: socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
unlink(name); /* in case it already exists */
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* fill in socket address structure */
|
|
|
|
memset(&unix_addr, 0, sizeof(unix_addr));
|
|
|
|
unix_addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(unix_addr.sun_path, name);
|
2010-02-09 13:19:01 +00:00
|
|
|
#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
|
2003-01-18 18:18:02 +00:00
|
|
|
len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
|
|
|
|
strlen(unix_addr.sun_path) + 1;
|
|
|
|
unix_addr.sun_len = len;
|
|
|
|
#endif /* linux */
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
/* bind the name to the descriptor */
|
2003-05-01 13:27:52 +00:00
|
|
|
if (bind(fd, (struct sockaddr *) &unix_addr,
|
2012-03-01 18:15:38 +00:00
|
|
|
sizeof(struct sockaddr_un)) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_server: bind");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (chmod(unix_addr.sun_path, S_IRWXU | S_IRWXO) < 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_server: chmod");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (listen(fd, 5) < 0) { /* tell kernel we're a server */
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif: unix_socket_server: listen");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(fd);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
unixif_input_handler(void *data)
|
|
|
|
{
|
|
|
|
struct netif *netif;
|
|
|
|
struct unixif *unixif;
|
2010-03-26 14:20:39 +00:00
|
|
|
char buf[1532], *bufptr;
|
2003-01-18 18:18:02 +00:00
|
|
|
int len, plen, rlen;
|
|
|
|
struct pbuf *p, *q;
|
|
|
|
|
2010-03-26 14:20:39 +00:00
|
|
|
netif = (struct netif *)data;
|
|
|
|
unixif = (struct unixif *)netif->state;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
len = read(unixif->fd, &plen, sizeof(int));
|
2003-05-01 13:27:52 +00:00
|
|
|
if (len == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_irq_handler: read");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: len == %d plen == %d bytes\n", len, plen));
|
2003-05-01 13:27:52 +00:00
|
|
|
if (len == sizeof(int)) {
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (plen < 20 || plen > 1500) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("plen %d!\n", plen));
|
2003-01-18 18:18:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = read(unixif->fd, buf, plen);
|
2003-05-01 13:27:52 +00:00
|
|
|
if (len == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_irq_handler: read");
|
|
|
|
abort();
|
|
|
|
}
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: read %d bytes\n", len));
|
2003-01-18 18:18:02 +00:00
|
|
|
p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (p != NULL) {
|
2003-01-18 18:18:02 +00:00
|
|
|
rlen = len;
|
|
|
|
bufptr = buf;
|
|
|
|
q = p;
|
2003-05-01 13:27:52 +00:00
|
|
|
while (rlen > 0) {
|
2010-03-31 08:07:07 +00:00
|
|
|
memcpy(q->payload, bufptr, rlen > q->len? q->len: rlen);
|
2003-01-18 18:18:02 +00:00
|
|
|
rlen -= q->len;
|
|
|
|
bufptr += q->len;
|
|
|
|
q = q->next;
|
|
|
|
}
|
|
|
|
pbuf_realloc(p, len);
|
2010-02-21 11:26:15 +00:00
|
|
|
LINK_STATS_INC(link.recv);
|
2003-01-18 18:18:02 +00:00
|
|
|
tcpdump(p);
|
|
|
|
netif->input(p, netif);
|
|
|
|
} else {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: could not allocate pbuf\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
unixif_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct netif *netif;
|
|
|
|
struct unixif *unixif;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread: started.\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2010-03-26 14:20:39 +00:00
|
|
|
netif = (struct netif *)arg;
|
|
|
|
unixif = (struct unixif *)netif->state;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
while (1) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_wait(&unixif->sem);
|
2003-01-18 18:18:02 +00:00
|
|
|
unixif_input_handler(netif);
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
unixif_thread2(void *arg)
|
|
|
|
{
|
|
|
|
struct netif *netif;
|
|
|
|
struct unixif *unixif;
|
|
|
|
fd_set fdset;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread2: started.\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2010-03-26 14:20:39 +00:00
|
|
|
netif = (struct netif *)arg;
|
|
|
|
unixif = (struct unixif *)netif->state;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
while (1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(unixif->fd, &fdset);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (select(unixif->fd + 1, &fdset, NULL, NULL, NULL) > 0) {
|
2010-02-12 13:57:58 +00:00
|
|
|
sys_sem_signal(&unixif->sem);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void unixif_output_timeout(void *arg);
|
|
|
|
|
|
|
|
static err_t
|
2010-02-09 13:19:01 +00:00
|
|
|
unixif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
|
2003-01-18 18:18:02 +00:00
|
|
|
{
|
|
|
|
struct unixif *unixif;
|
|
|
|
struct unixif_buf *buf;
|
2010-03-26 14:20:39 +00:00
|
|
|
LWIP_UNUSED_ARG(ipaddr);
|
|
|
|
|
|
|
|
unixif = (struct unixif *)netif->state;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2010-03-26 14:20:39 +00:00
|
|
|
buf = (struct unixif_buf *)malloc(sizeof(struct unixif_buf));
|
2003-01-18 18:18:02 +00:00
|
|
|
buf->p = p;
|
|
|
|
buf->len = p->len;
|
|
|
|
buf->tot_len = p->tot_len;
|
|
|
|
buf->payload = p->payload;
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (list_elems(unixif->q) == 0) {
|
2003-03-31 22:10:09 +00:00
|
|
|
pbuf_ref(p);
|
2003-01-18 18:18:02 +00:00
|
|
|
list_push(unixif->q, buf);
|
|
|
|
sys_timeout((double)p->tot_len * 8000.0 / UNIXIF_BPS, unixif_output_timeout,
|
2012-03-01 18:15:38 +00:00
|
|
|
netif);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: first on list\n"));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
} else {
|
2003-03-31 22:10:09 +00:00
|
|
|
pbuf_ref(p);
|
2003-05-01 13:27:52 +00:00
|
|
|
if (list_push(unixif->q, buf) == 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
#ifdef UNIXIF_DROP_FIRST
|
|
|
|
struct unixif_buf *buf2;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
buf2 = list_pop(unixif->q);
|
|
|
|
pbuf_free(buf2->p);
|
|
|
|
free(buf2);
|
|
|
|
list_push(unixif->q, buf);
|
|
|
|
#else
|
|
|
|
free(buf);
|
|
|
|
pbuf_free(p);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: drop\n"));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
#endif /* UNIXIF_DROP_FIRST */
|
2010-02-21 11:26:15 +00:00
|
|
|
LINK_STATS_INC(link.drop);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
} else {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: on list\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
unixif_output_timeout(void *arg)
|
2010-03-26 14:20:39 +00:00
|
|
|
{
|
|
|
|
struct pbuf *p, *q;
|
2003-01-18 18:18:02 +00:00
|
|
|
int i, j, len;
|
|
|
|
unsigned short plen, ptot_len;
|
|
|
|
struct unixif_buf *buf;
|
|
|
|
void *payload;
|
|
|
|
struct netif *netif;
|
|
|
|
struct unixif *unixif;
|
|
|
|
char *data;
|
|
|
|
|
2010-03-26 14:20:39 +00:00
|
|
|
netif = (struct netif *)arg;
|
|
|
|
unixif = (struct unixif *)netif->state;
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output_timeout\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
/* buf = unixif->q[0];
|
|
|
|
unixif->q[0] = unixif->q[1];
|
|
|
|
unixif->q[1] = NULL;*/
|
2010-03-26 14:20:39 +00:00
|
|
|
buf = (struct unixif_buf *)list_pop(unixif->q);
|
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
p = buf->p;
|
|
|
|
|
|
|
|
plen = p->len;
|
|
|
|
ptot_len = p->tot_len;
|
|
|
|
payload = p->payload;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
p->len = buf->len;
|
|
|
|
p->tot_len = buf->tot_len;
|
|
|
|
p->payload = buf->payload;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (p->tot_len == 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("p->len!\n"));
|
2003-01-18 18:18:02 +00:00
|
|
|
abort();
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
data = (char *)malloc(p->tot_len);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(q = p; q != NULL; q = q->next) {
|
|
|
|
for(j = 0; j < q->len; j++) {
|
|
|
|
data[i] = ((char *)q->payload)[j];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: sending %d (%d) bytes\n",
|
2012-03-01 18:15:38 +00:00
|
|
|
p->len, p->tot_len));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
len = p->tot_len;
|
2003-05-01 13:27:52 +00:00
|
|
|
if (write(unixif->fd, &len, sizeof(int)) == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_output: write");
|
|
|
|
abort();
|
|
|
|
}
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (write(unixif->fd, data, p->tot_len) == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_output: write");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
tcpdump(p);
|
2010-02-21 11:26:15 +00:00
|
|
|
LINK_STATS_INC(link.xmit);
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
free(buf);
|
|
|
|
p->len = plen;
|
|
|
|
p->tot_len = ptot_len;
|
|
|
|
p->payload = payload;
|
|
|
|
|
|
|
|
pbuf_free(p);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
/* if (unixif->q[0] != NULL) {
|
2003-01-18 18:18:02 +00:00
|
|
|
sys_timeout(unixif->q[0]->tot_len * 8000 / UNIXIF_BPS,
|
2012-03-01 18:15:38 +00:00
|
|
|
unixif_output_timeout, netif);
|
|
|
|
}*/
|
2003-05-01 13:27:52 +00:00
|
|
|
if (list_elems(unixif->q) > 0) {
|
2003-01-18 18:18:02 +00:00
|
|
|
sys_timeout(((struct unixif_buf *)list_first(unixif->q))->tot_len *
|
2012-03-01 18:15:38 +00:00
|
|
|
8000.0 / UNIXIF_BPS,
|
|
|
|
unixif_output_timeout, netif);
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-20 16:24:39 +00:00
|
|
|
err_t
|
2003-01-18 18:18:02 +00:00
|
|
|
unixif_init_server(struct netif *netif)
|
|
|
|
{
|
|
|
|
int fd, fd2;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t len;
|
|
|
|
struct unixif *unixif;
|
|
|
|
|
|
|
|
fd = unix_socket_server("/tmp/unixif");
|
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (fd == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_server");
|
|
|
|
abort();
|
|
|
|
}
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_server: fd %d\n", fd));
|
2010-03-26 14:20:39 +00:00
|
|
|
|
|
|
|
unixif = (struct unixif *)malloc(sizeof(struct unixif));
|
|
|
|
if (!unixif) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
netif->state = unixif;
|
|
|
|
netif->name[0] = 'u';
|
|
|
|
netif->name[1] = 'n';
|
|
|
|
netif->output = unixif_output;
|
|
|
|
unixif->q = list_new(UNIXIF_QUEUELEN);
|
|
|
|
|
|
|
|
printf("Now run ./simnode.\n");
|
|
|
|
len = sizeof(addr);
|
|
|
|
fd2 = accept(fd, (struct sockaddr *)&addr, &len);
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-05-01 13:27:52 +00:00
|
|
|
if (fd2 == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_accept");
|
|
|
|
abort();
|
2010-03-26 14:20:39 +00:00
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_accept: %d\n", fd2));
|
2003-01-18 18:18:02 +00:00
|
|
|
|
|
|
|
unixif->fd = fd2;
|
2010-02-12 13:57:58 +00:00
|
|
|
if(sys_sem_new(&unixif->sem, 0) != ERR_OK) {
|
|
|
|
LWIP_ASSERT("Failed to create semaphore", 0);
|
|
|
|
}
|
2007-09-05 16:48:11 +00:00
|
|
|
sys_thread_new("unixif_thread", unixif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
|
|
|
sys_thread_new("unixif_thread2", unixif_thread2, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
2003-02-20 16:24:39 +00:00
|
|
|
return ERR_OK;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-20 16:24:39 +00:00
|
|
|
err_t
|
2003-01-18 18:18:02 +00:00
|
|
|
unixif_init_client(struct netif *netif)
|
|
|
|
{
|
|
|
|
struct unixif *unixif;
|
2010-03-26 14:20:39 +00:00
|
|
|
unixif = (struct unixif *)malloc(sizeof(struct unixif));
|
|
|
|
if (!unixif) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2003-01-18 18:18:02 +00:00
|
|
|
netif->state = unixif;
|
|
|
|
netif->name[0] = 'u';
|
|
|
|
netif->name[1] = 'n';
|
|
|
|
netif->output = unixif_output;
|
2010-03-26 14:20:39 +00:00
|
|
|
|
2003-01-18 18:18:02 +00:00
|
|
|
unixif->fd = unix_socket_client("/tmp/unixif");
|
2003-05-01 13:27:52 +00:00
|
|
|
if (unixif->fd == -1) {
|
2003-01-18 18:18:02 +00:00
|
|
|
perror("unixif_init");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
unixif->q = list_new(UNIXIF_QUEUELEN);
|
2010-02-12 13:57:58 +00:00
|
|
|
if(sys_sem_new(&unixif->sem, 0) != ERR_OK) {
|
|
|
|
LWIP_ASSERT("Failed to create semaphore", 0);
|
|
|
|
}
|
2007-09-05 16:48:11 +00:00
|
|
|
sys_thread_new("unixif_thread", unixif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
|
|
|
sys_thread_new("unixif_thread2", unixif_thread2, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
2003-02-20 16:24:39 +00:00
|
|
|
return ERR_OK;
|
2003-01-18 18:18:02 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|