#include <oskittcp.h>
#include <oskitdebug.h>
+#include <net/raw_cb.h>
+
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
-#ifdef WIN32
-#define snprintf _snprintf
-#endif//WIN32
-
struct linker_set domain_set;
OSKITTCP_EVENT_HANDLERS OtcpEvent = { 0 };
-OSK_UINT OskitDebugTraceLevel = OSK_DEBUG_ULTRA;
-//OSK_UINT OskitDebugTraceLevel = 0;
+//OSK_UINT OskitDebugTraceLevel = OSK_DEBUG_ULTRA;
+OSK_UINT OskitDebugTraceLevel = 0;
/* SPL */
unsigned cpl;
unsigned volatile ipending;
struct timeval boottime;
-void *fbsd_malloc( unsigned int bytes, ... ) {
+void clock_init();
+int _snprintf(char * buf, size_t cnt, const char *fmt, ...);
+
+void *fbsd_malloc( unsigned int bytes, char *file, unsigned line, ... ) {
if( !OtcpEvent.TCPMalloc ) panic("no malloc");
return OtcpEvent.TCPMalloc
- ( OtcpEvent.ClientData, (OSK_UINT)bytes, "*", 0 );
+ ( OtcpEvent.ClientData, (OSK_UINT)bytes, file, line );
}
-void fbsd_free( void *data, ... ) {
+void fbsd_free( void *data, char *file, unsigned line, ... ) {
if( !OtcpEvent.TCPFree ) panic("no free");
- OtcpEvent.TCPFree( OtcpEvent.ClientData, data, "*", 0 );
+ OtcpEvent.TCPFree( OtcpEvent.ClientData, data, file, line );
}
void InitOskitTCP() {
raw_init();
OS_DbgPrint(OSK_MID_TRACE,("Route Init\n"));
route_init();
- OS_DbgPrint(OSK_MID_TRACE,("Init fake freebsd scheduling\n"));
- init_freebsd_sched();
OS_DbgPrint(OSK_MID_TRACE,("Init clock\n"));
clock_init();
OS_DbgPrint(OSK_MID_TRACE,("Init TCP\n"));
void DeinitOskitTCP() {
}
-void TimerOskitTCP() {
- tcp_slowtimo();
- tcp_fasttimo();
+void TimerOskitTCP( int FastTimer, int SlowTimer ) {
+ if ( SlowTimer ) {
+ tcp_slowtimo();
+ }
+ if ( FastTimer ) {
+ tcp_fasttimo();
+ }
}
void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers ) {
memcpy( &OtcpEvent, EventHandlers, sizeof(OtcpEvent) );
- if( OtcpEvent.PacketSend )
+ if( OtcpEvent.PacketSend )
OS_DbgPrint(OSK_MID_TRACE,("SendPacket handler registered: %x\n",
OtcpEvent.PacketSend));
}
if ( !align )
{
if ( i ) DbgPrint( line );
- snprintf ( line, sizeof(line)-1, "%08x: \n", &Data[i] );
+ _snprintf ( line, sizeof(line)-1, "%08x: \n", &Data[i] );
line[sizeof(line)-1] = '\0';
}
/* From uipc_syscalls.c */
int OskitTCPSocket( void *context,
- void **aso,
- int domain,
- int type,
- int proto )
+ void **aso,
+ int domain,
+ int type,
+ int proto )
{
struct socket *so;
int error = socreate(domain, &so, type, proto);
so->so_connection = context;
so->so_state = SS_NBIO;
so->so_error = 0;
+ so->so_q = so->so_q0 = NULL;
+ so->so_qlen = 0;
+ so->so_head = NULL;
*aso = so;
}
return error;
OSK_UINT Len,
OSK_UINT *OutLen,
OSK_UINT Flags ) {
- char *output_ptr = Data;
struct uio uio = { 0 };
struct iovec iov = { 0 };
int error = 0;
int tcp_flags = 0;
- int tocopy = 0;
*OutLen = 0;
- printf("so->so_state %x\n", ((struct socket *)connection)->so_state);
+ OS_DbgPrint(OSK_MID_TRACE,
+ ("so->so_state %x\n", ((struct socket *)connection)->so_state));
if( Flags & OSK_MSG_OOB ) tcp_flags |= MSG_OOB;
if( Flags & OSK_MSG_DONTWAIT ) tcp_flags |= MSG_DONTWAIT;
iov.iov_base = Data;
OS_DbgPrint(OSK_MID_TRACE,("Reading %d bytes from TCP:\n", Len));
-
- error = soreceive( connection, NULL, &uio, NULL, NULL /* SCM_RIGHTS */,
+
+ error = soreceive( connection, NULL, &uio, NULL, NULL /* SCM_RIGHTS */,
&tcp_flags );
if( error == 0 ) {
return error;
}
-
-static int
-getsockaddr(namp, uaddr, len)
-/* [<][>][^][v][top][bottom][index][help] */
- struct sockaddr **namp;
-caddr_t uaddr;
-size_t len;
-{
- struct sockaddr *sa;
- int error;
-
- if (len > SOCK_MAXADDRLEN)
- return ENAMETOOLONG;
- MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
- error = copyin(uaddr, sa, len);
- if (error) {
- FREE(sa, M_SONAME);
- } else {
- *namp = sa;
- }
- return error;
-}
int OskitTCPBind( void *socket, void *connection,
void *nam, OSK_UINT namelen ) {
int error = EFAULT;
struct socket *so = socket;
- struct mbuf sabuf = { 0 };
+ struct mbuf sabuf;
struct sockaddr addr;
OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
if( nam )
addr = *((struct sockaddr *)nam);
+ RtlZeroMemory(&sabuf, sizeof(sabuf));
sabuf.m_data = (void *)&addr;
sabuf.m_len = sizeof(addr);
-
+
addr.sa_family = addr.sa_len;
addr.sa_len = sizeof(struct sockaddr);
error = sobind(so, &sabuf);
OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
- return (error);
+ return (error);
}
-int OskitTCPConnect( void *socket, void *connection,
+int OskitTCPConnect( void *socket, void *connection,
void *nam, OSK_UINT namelen ) {
struct socket *so = socket;
- struct connect_args _uap = {
- 0, nam, namelen
- }, *uap = &_uap;
- int error = EFAULT, s;
- struct mbuf sabuf = { 0 };
+ int error = EFAULT;
+ struct mbuf sabuf;
struct sockaddr addr;
OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
if( nam )
addr = *((struct sockaddr *)nam);
+ RtlZeroMemory(&sabuf, sizeof(sabuf));
sabuf.m_data = (void *)&addr;
sabuf.m_len = sizeof(addr);
-
+
addr.sa_family = addr.sa_len;
addr.sa_len = sizeof(struct sockaddr);
done:
OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
- return (error);
+ return (error);
}
int OskitTCPShutdown( void *socket, int disconn_type ) {
- struct socket *so = socket;
return soshutdown( socket, disconn_type );
}
return 0;
}
-int OskitTCPSend( void *socket, OSK_PCHAR Data, OSK_UINT Len,
+int OskitTCPSend( void *socket, OSK_PCHAR Data, OSK_UINT Len,
OSK_UINT *OutLen, OSK_UINT flags ) {
- struct mbuf* m = m_devget( Data, Len, 0, NULL, NULL );
- int error = 0;
- if ( !m )
- return ENOBUFS;
- error = sosend( socket, NULL, NULL, m, NULL, 0 );
- *OutLen = Len;
+ int error;
+ struct uio uio;
+ struct iovec iov;
+
+ iov.iov_len = Len;
+ iov.iov_base = Data;
+ uio.uio_iov = &iov;
+ uio.uio_iovcnt = 1;
+ uio.uio_offset = 0;
+ uio.uio_resid = Len;
+ uio.uio_segflg = UIO_SYSSPACE;
+ uio.uio_rw = UIO_WRITE;
+ uio.uio_procp = NULL;
+
+ error = sosend( socket, NULL, &uio, NULL, NULL, 0 );
+ if (OSK_EWOULDBLOCK == error) {
+ ((struct socket *) socket)->so_snd.sb_flags |= SB_WAIT;
+ }
+ *OutLen = uio.uio_offset;
+
return error;
}
-int OskitTCPAccept( void *socket,
+int OskitTCPAccept( void *socket,
void **new_socket,
- void *AddrOut,
+ void *AddrOut,
OSK_UINT AddrLen,
OSK_UINT *OutAddrLen,
OSK_UINT FinishAccepting ) {
struct socket *head = (void *)socket;
struct sockaddr *name = (struct sockaddr *)AddrOut;
- struct socket **newso = (struct socket **)new_socket;
+ struct socket **newso = (struct socket **)new_socket;
struct socket *so = socket;
struct sockaddr_in sa;
struct mbuf mnam;
+ struct inpcb *inp;
int namelen = 0, error = 0, s;
OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: Doing accept (Finish %d)\n",
FinishAccepting));
-
+
*OutAddrLen = AddrLen;
- if (name)
+ if (name)
/* that's a copyin actually */
namelen = *OutAddrLen;
s = splnet();
- OskitDumpBuffer( so, sizeof(*so) );
-
#if 0
if ((head->so_options & SO_ACCEPTCONN) == 0) {
splx(s);
}
#endif
- OS_DbgPrint(OSK_MID_TRACE,("head->so_q = %x, head->so_state = %x\n",
+ OS_DbgPrint(OSK_MID_TRACE,("head->so_q = %x, head->so_state = %x\n",
head->so_q, head->so_state));
if ((head->so_state & SS_NBIO) && head->so_q == NULL) {
error = EWOULDBLOCK;
goto out;
}
-
+
OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
while (head->so_q == NULL && head->so_error == 0) {
if (head->so_state & SS_CANTRCVMORE) {
*/
so = head->so_q;
+ inp = so ? (struct inpcb *)so->so_pcb : NULL;
+ if( inp ) {
+ ((struct sockaddr_in *)AddrOut)->sin_addr.s_addr =
+ inp->inp_faddr.s_addr;
+ ((struct sockaddr_in *)AddrOut)->sin_port = inp->inp_fport;
+ }
+
OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
if( FinishAccepting ) {
head->so_q = so->so_q;
head->so_qlen--;
-
+
*newso = so;
-
+
/*so->so_state &= ~SS_COMP;*/
- so->so_q = NULL;
- mnam.m_data = &sa;
+ mnam.m_data = (char *)&sa;
mnam.m_len = sizeof(sa);
-
+
(void) soaccept(so, &mnam);
so->so_state = SS_NBIO | SS_ISCONNECTED;
-
- OskitDumpBuffer( so, sizeof(*so) );
+ so->so_q = so->so_q0 = NULL;
+ so->so_qlen = 0;
+ so->so_head = 0;
OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
if (name) {
}
/* The story so far
- *
+ *
* We have a packet. While we store the fields we want in host byte order
* outside the original packet, the bsd stack modifies them in place.
*/
-void OskitTCPReceiveDatagram( OSK_PCHAR Data, OSK_UINT Len,
+void OskitTCPReceiveDatagram( OSK_PCHAR Data, OSK_UINT Len,
OSK_UINT IpHeaderLen ) {
struct mbuf *Ip = m_devget( Data, Len, 0, NULL, NULL );
struct ip *iph;
-
+
if( !Ip ) return; /* drop the segment */
//memcpy( Ip->m_data, Data, Len );
NTOHS(iph->ip_len);
iph->ip_len -= sizeof(struct ip);
- OS_DbgPrint(OSK_MAX_TRACE,
+ OS_DbgPrint(OSK_MAX_TRACE,
("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len,
IpHeaderLen));
int OskitTCPListen( void *socket, int backlog ) {
int error;
-
+
OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
error = solisten( socket, backlog );
OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
return error;
}
-void OskitTCPSetAddress( void *socket,
+void OskitTCPSetAddress( void *socket,
OSK_UINT LocalAddress,
OSK_UI16 LocalPort,
OSK_UINT RemoteAddress,
OSK_UI16 RemotePort ) {
struct socket *so = socket;
- struct inpcb *inp = so->so_pcb;
+ struct inpcb *inp = (struct inpcb *)so->so_pcb;
inp->inp_laddr.s_addr = LocalAddress;
inp->inp_lport = LocalPort;
inp->inp_faddr.s_addr = RemoteAddress;
inp->inp_fport = RemotePort;
}
-void OskitTCPGetAddress( void *socket,
+void OskitTCPGetAddress( void *socket,
OSK_UINT *LocalAddress,
OSK_UI16 *LocalPort,
OSK_UINT *RemoteAddress,
OSK_UI16 *RemotePort ) {
struct socket *so = socket;
- struct inpcb *inp = so ? so->so_pcb : 0;
+ struct inpcb *inp = so ? (struct inpcb *)so->so_pcb : NULL;
if( inp ) {
*LocalAddress = inp->inp_laddr.s_addr;
*LocalPort = inp->inp_lport;
struct ifaddr *ifa_iffind(struct sockaddr *addr, int type)
{
- if( OtcpEvent.FindInterface )
+ if( OtcpEvent.FindInterface )
return OtcpEvent.FindInterface( OtcpEvent.ClientData,
PF_INET,
type,
if( ifaddr )
{
- sin = (struct sockaddr *)&ifaddr->ifa_addr;
+ sin = (struct sockaddr_in *)&ifaddr->ifa_addr;
- OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n",
+ OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n",
sin->sin_addr.s_addr));
}