using Hybrid's NativeInterop

08/30/2010 21:14 pro4never#16
Quote:
Originally Posted by CptSky View Post
If you can't call the DLL. Add the functions to your C# code.

Code:
static long holdrand = 1L;

void srand(unsigned int seed)
{
  holdrand = (long) seed;
}

int rand()
{
  return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
}
I've tried both ways.

using the dll as an added reference you CAN do msvcrt.msvcrt.srand/rand. It's how it works in immune's source and the way I did it with my last test source.

It causes the same problem if I use a dll import + function for it as well. Hell, I don't even have to call ANY of the functions and the error happens... no clue why :S

here's a copy of the VERY basic test bed I was using (it has nothing added or finished so not like nubs are gonna use it to get very far lol)

[Only registered and activated users can see links. Click Here To Register...]

<edit>
sorry, re-read what you said and yes.. I can call them (either via dllimport or referencing the dll...) but the problem is that I'm getting the error OUTSIDE of when I'm actively invoking them (it has nothing to do with rand or srand... I'm guessing it's hybrid's dll using memcpy)


<edit x2>

yes... memcpy seems to be part of hybrids socket system (checked the 5018 version of it as easy reference)

Very strange... it only seems to be happening now even though I haven't really changed much.
08/30/2010 21:32 CptSky#17
I try to understand. If you use the rand() & srand() functions, the socket throw an exception when using the memcpy function?
08/30/2010 22:19 Kiyono#18
Quote:
Originally Posted by CptSky View Post
I try to understand. If you use the rand() & srand() functions, the socket throw an exception when using the memcpy function?
As I understand it, he gets an error as soon as he imports the dll without even using it.
08/30/2010 22:58 Basser#19
Efficient way to split packets?

Code:
        public static List<byte[]> Split(byte[] buffer)
        {
            List<byte[]> packets = new List<byte[]>();
            int offset = 0;
            while (offset < buffer.Length + 8)
            {
                int length = BitConverter.ToInt16(buffer, 0) + 8;
                byte[] packet = new byte[length];
                offset += length;
                packets.Add(packet);
            }
            if (offset < buffer.Length)
            {
                packets.Add(buffer);
            }
            return packets;
        }
(You can change this a bit, since the original is different but theres something very private in it)
How to use this?
foreach packet in TQPacket.Split(received data)
-> Process packet

AND
About password cryptography, you will need to rewrite it, the current thing downloads the file, causing the old/original file to be lost, It's very annoying, I simply rewrote it.
08/30/2010 23:02 pro4never#20
Actually seem to have traced it to the way hybrid's socket system manages incoming packets.

That's the ONLY thing using memcpy, not my source. I may have to rip his socket system from the dll cause it simply refuses to let it work all of a sudden (worked fine before)

Quote:
Originally Posted by Kiyono View Post
As I understand it, he gets an error as soon as he imports the dll without even using it.
08/31/2010 13:41 InfamousNoone#21
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConquerServer_v2
{
    public class NetworkClient
    {
        private Socket socket;
        private byte[] buffer;
        private NetworkServerSocket server;
        public Socket Socket { get { return socket; } }
        public NetworkServerSocket Server { get { return server; } }
        public object Owner;
        public string IP;
        public bool Alive;

        public NetworkClient(NetworkServerSocket _server, Socket _socket, int buffer_len)
        {
            Alive = true;
            server = _server;
            socket = _socket;
            buffer = new byte[buffer_len];
            try { IP = (socket.RemoteEndPoint as IPEndPoint).Address.ToString(); } 
            catch (SocketException) {}
        }
        public void BeginReceive()
        {
            try
            {
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), null);
            }
            catch (SocketException)
            {
                server.InvokeDisconnect(this);
            }
        }
        private void Receive(IAsyncResult res)
        {
            if (socket != null)
            {
                try
                {
                    int len = socket.EndReceive(res);
                    if (this.Alive)
                    {
                        if (len > 0)
                        {
                            byte[] received = new byte[len];
                            unsafe
                            {
                                fixed (byte* recv_ptr = received, buf_ptr = buffer)
                                {
                                    MSVCRT.memcpy(recv_ptr, buf_ptr, len);
                                }
                            }
                            if (server.OnReceive != null)
                            {
                                server.OnReceive(this, received);
                            }

                            BeginReceive();
                        }
                        else
                        {
                            server.InvokeDisconnect(this);
                        }
                    }
                }
                catch (SocketException)
                {
                    server.InvokeDisconnect(this);
                }
            }
        }
        public void Send(byte[] Packet)
        {
            if (Alive)
            {
                try
                {
                    socket.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(EndSend), null);
                }
                catch (SocketException)
                {
                    Server.InvokeDisconnect(this);
                }
            }
        }
        private void EndSend(IAsyncResult res)
        {
            try
            {
                socket.EndSend(res);
            }
            catch (SocketException)
            {
                server.InvokeDisconnect(this);
            }
        }
        public void Disconnect()
        {
            try { socket.Disconnect(false); }
            catch (SocketException) { }
            server.InvokeDisconnect(this);
        }
    }

    public delegate void NetworkClientConnection(NetworkClient Client);
    public delegate void NetworkClientReceive(NetworkClient Client, byte[] Packet);

    public class NetworkServerSocket
    {
        private Socket server;
        private int m_Port;

        public Socket Socket { get { return server; } }
        public int Port { get { return m_Port; } }

        public NetworkClientConnection OnConnect;
        public NetworkClientReceive OnReceive;
        public NetworkClientConnection OnDisconnect;

        public int ClientBufferSize;

        public NetworkServerSocket()
        {
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        public void Prepare(int port, int backlog)
        {
            m_Port = port;
            server.Bind(new IPEndPoint(IPAddress.Any, m_Port));
            server.Listen(backlog);
        }
        public void BeginAccept()
        {
            server.BeginAccept(new AsyncCallback(Accept), null);
        }
        private void Accept(IAsyncResult res)
        {
            Socket client_socket;
            try { client_socket = server.EndAccept(res); }
            catch (SocketException)
            {
                BeginAccept();
                return;
            }

            client_socket.ReceiveBufferSize = ClientBufferSize;
            NetworkClient client = new NetworkClient(this, client_socket, ClientBufferSize);
            if (OnConnect != null)
            {
                OnConnect(client);
            }
            client.BeginReceive();
            BeginAccept();
        }
        public void InvokeDisconnect(NetworkClient Client)
        {
            if (!Client.Alive)
                return;
            Client.Alive = false;
            if (OnDisconnect != null)
                OnDisconnect(Client);
        }
    }
}
decryption should take place in the "OnReceive" callback, the handshake should be sent on the "OnConnect" callback, yada yada.

encryption should take place before the Send() function.