new version of OskitDumpBuffer() that shows ascii characters, too, for easier debugging
[reactos.git] / reactos / drivers / lib / oskittcp / oskittcp / interface.c
index 03da96d..f422f90 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <oskittcp.h>
 #include <oskitdebug.h>
 #include <sys/param.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 };
@@ -26,17 +31,15 @@ unsigned net_imask;
 unsigned volatile ipending;
 struct timeval boottime;
 
-void *fbsd_malloc( unsigned int bytes, const char *file, int line, ... ) {
+void *fbsd_malloc( unsigned int bytes, ... ) {
     if( !OtcpEvent.TCPMalloc ) panic("no malloc");
     return OtcpEvent.TCPMalloc
-       ( OtcpEvent.ClientData,
-         (OSK_UINT)bytes, (OSK_PCHAR)file, (OSK_UINT)line );
+       ( OtcpEvent.ClientData, (OSK_UINT)bytes, "*", 0 );
 }
 
-void fbsd_free( void *data, const char *file, int line, ... ) {
+void fbsd_free( void *data, ... ) {
     if( !OtcpEvent.TCPFree ) panic("no free");
-    OtcpEvent.TCPFree( OtcpEvent.ClientData,
-                   data, (OSK_PCHAR)file, (OSK_UINT)line );
+    OtcpEvent.TCPFree( OtcpEvent.ClientData, data, "*", 0 );
 }
 
 void InitOskitTCP() {
@@ -56,7 +59,6 @@ void InitOskitTCP() {
     tcp_init();
     OS_DbgPrint(OSK_MID_TRACE,("Init routing\n"));
     domaininit();
-    memset( &OtcpEvent, 0, sizeof( OtcpEvent ) );
     OS_DbgPrint(OSK_MID_TRACE,("Init Finished\n"));
     tcp_iss = 1024;
 }
@@ -76,15 +78,32 @@ void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers ) {
                                   OtcpEvent.PacketSend));
 }
 
-void OskitDumpBuffer( OSK_PCHAR Data, OSK_UINT Len ) {
-    unsigned int i;
-    
-    for( i = 0; i < Len; i++ ) {
-       if( i && !(i & 0xf) ) DbgPrint( "\n" );
-       if( !(i & 0xf) ) DbgPrint( "%08x: ", (OSK_UINT)(Data + i) );
-       DbgPrint( " %02x", Data[i] );
-    }
-    DbgPrint("\n");
+void OskitDumpBuffer( OSK_PCHAR Data, OSK_UINT Len )
+{
+       unsigned int i;
+       char line[81];
+       static const char* hex = "0123456789abcdef";
+
+       for ( i = 0; i < Len; i++ )
+       {
+               int align = i & 0xf;
+               int align3 = (align<<1) + align;
+               unsigned char c = Data[i];
+               if ( !align )
+               {
+                       if ( i ) DbgPrint( line );
+                       snprintf ( line, sizeof(line)-1, "%08x:                                                                  \n", Data );
+                       line[sizeof(line)-1] = '\0';
+               }
+
+               line[10+align3] = hex[(c>>4)&0xf];
+               line[11+align3] = hex[c&0xf];
+               if ( !isprint(c) )
+                       c = '.';
+               line[59+align] = c;
+       }
+       if ( Len & 0xf )
+               DbgPrint ( line );
 }
 
 /* From uipc_syscalls.c */
@@ -110,35 +129,37 @@ int OskitTCPRecv( void *connection,
                  OSK_UINT Len,
                  OSK_UINT *OutLen,
                  OSK_UINT Flags ) {
-    struct mbuf *paddr = 0;
-    struct mbuf m, *mp;
+    char *output_ptr = Data;
     struct uio uio = { 0 };
+    struct iovec iov = { 0 };
     int error = 0;
     int tcp_flags = 0;
+    int tocopy = 0;
+
+    *OutLen = 0;
 
     if( Flags & OSK_MSG_OOB )      tcp_flags |= MSG_OOB;
     if( Flags & OSK_MSG_DONTWAIT ) tcp_flags |= MSG_DONTWAIT;
     if( Flags & OSK_MSG_PEEK )     tcp_flags |= MSG_PEEK;
 
     uio.uio_resid = Len;
-    m.m_len = Len;
-    m.m_data = Data;
-    m.m_type = MT_DATA;
-    m.m_flags = M_PKTHDR | M_EOR;
-
-    mp = &m;
+    uio.uio_iov = &iov;
+    uio.uio_rw = UIO_READ;
+    uio.uio_iovcnt = 1;
+    iov.iov_len = Len;
+    iov.iov_base = Data;
 
     OS_DbgPrint(OSK_MID_TRACE,("Reading %d bytes from TCP:\n", Len));
        
-    error = soreceive( connection, &paddr, &uio, &mp, NULL /* SCM_RIGHTS */, 
+    error = soreceive( connection, NULL, &uio, NULL, NULL /* SCM_RIGHTS */, 
                       &tcp_flags );
 
     if( error == 0 ) {
        OS_DbgPrint(OSK_MID_TRACE,("Successful read from TCP:\n"));
-       OskitDumpBuffer( m.m_data, uio.uio_resid );
+       *OutLen = Len - uio.uio_resid;
+       OskitDumpBuffer( Data, *OutLen );
     }
 
-    *OutLen = uio.uio_resid;
     return error;
 }
                  
@@ -249,17 +270,13 @@ int OskitTCPClose( void *socket ) {
 
 int OskitTCPSend( void *socket, OSK_PCHAR Data, OSK_UINT Len, 
                  OSK_UINT *OutLen, OSK_UINT flags ) {
-    struct mbuf mb;
-    struct uio uio = { 0 };
+    struct mbuf* m = m_devget( Data, Len, 0, NULL, NULL );
     int error = 0;
+       if ( !m )
+               return ENOBUFS;
     OskitDumpBuffer( Data, Len );
-    uio.uio_resid = Len;
-    mb.m_data = Data;
-    mb.m_len  = Len;
-    mb.m_flags = M_EOR;
-    error = sosend( socket, NULL, &uio, (struct mbuf *)&mb, NULL, 0 );
-    printf("uio.uio_resid = %d\n", uio.uio_resid);
-    *OutLen = uio.uio_resid;
+    error = sosend( socket, NULL, NULL, m, NULL, 0 );
+    *OutLen = Len;
     return error;
 }
 
@@ -289,7 +306,7 @@ void OskitTCPReceiveDatagram( OSK_PCHAR Data, OSK_UINT Len,
     
     if( !Ip ) return; /* drop the segment */
 
-    memcpy( Ip->m_data, Data, Len );
+    //memcpy( Ip->m_data, Data, Len );
     Ip->m_pkthdr.len = IpHeaderLen;
 
     /* Do the transformations on the header that tcp_input expects */
@@ -459,9 +476,13 @@ struct ifaddr *ifa_ifwithnet(addr)
     struct sockaddr_in *sin;
     struct ifaddr *ifaddr = ifa_iffind(addr, IFF_UNICAST);
 
-    sin = (struct sockaddr *)&ifaddr->ifa_addr;
+    if( ifaddr )
+    {
+       sin = (struct sockaddr *)&ifaddr->ifa_addr;
 
-    OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n", sin->sin_addr.s_addr));
+       OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n", 
+                                  sin->sin_addr.s_addr));
+    }
 
     return ifaddr;
 }