src/node/sim_interface.h
changeset 4685 ae536d9e0d6d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/node/sim_interface.h	Wed Jul 15 18:46:14 2009 +0200
     1.3 @@ -0,0 +1,211 @@
     1.4 +#ifndef __SIM_INTERFACE_H__
     1.5 +#define __SIM_INTERFACE_H__
     1.6 +/*
     1.7 +  Network Simulation Cradle
     1.8 +  Copyright (C) 2003-2005 Sam Jansen
     1.9 +
    1.10 +  This program is free software; you can redistribute it and/or modify it
    1.11 +  under the terms of the GNU General Public License as published by the Free
    1.12 +  Software Foundation; either version 2 of the License, or (at your option)
    1.13 +  any later version.
    1.14 +
    1.15 +  This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 +  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.17 +  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
    1.18 +  more details.
    1.19 +
    1.20 +  You should have received a copy of the GNU General Public License along
    1.21 +  with this program; if not, write to the Free Software Foundation, Inc., 59
    1.22 +  Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1.23 +
    1.24 +*/
    1.25 +
    1.26 +#define NSC_VERSION 0x000500
    1.27 +
    1.28 +struct INetStack
    1.29 +{
    1.30 +    virtual ~INetStack() {}
    1.31 +
    1.32 +    virtual void init(int hz) = 0;
    1.33 +    
    1.34 +    virtual void if_receive_packet(int if_id, const void *data, unsigned int datalen) = 0;
    1.35 +
    1.36 +    /*
    1.37 +     * called by NSCs network driver. It invokes ISendCallback->send_callback() to pass
    1.38 +     * the packet to the simulator.
    1.39 +     */
    1.40 +    virtual void if_send_packet(const void *data, unsigned int datalen) = 0;
    1.41 +    //virtual void if_send_packet(int if_id, const void *data, unsigned int datalen) = 0;
    1.42 +
    1.43 +    /*
    1.44 +     * called by network simulator after packet tx was sucessful.
    1.45 +     * on Linux, this wakes up the netdev xmit queue.
    1.46 +     */
    1.47 +    virtual void if_send_finish(int if_id) = 0;
    1.48 +
    1.49 +    virtual void if_attach(const char *addr, const char *mask, int mtu) = 0;
    1.50 +    virtual void add_default_gateway(const char *addr) = 0;
    1.51 +
    1.52 +    /** Purely for debugging/diagnostic purposes. This returns the internal id
    1.53 +     * of the stack instance.
    1.54 +     */
    1.55 +    virtual int get_id() = 0;
    1.56 +
    1.57 +    /** Should return a short one-word name of the stack. Eg. Linux 2.4.x ->
    1.58 +     * linux24, FreeBSD 5.x -> freebsd5. This can be used to identify output
    1.59 +     * from a stack, for example a packet trace file. */
    1.60 +    virtual const char *get_name() = 0;
    1.61 +
    1.62 +    /** This is used so the simulator can call the stack timer_interrupt function
    1.63 +     * the correct amount of times per second. For example, lwip has a hz of 10,
    1.64 +     * which it returns here to say that it's timer_interrupt should be called
    1.65 +     * 10 times a second. FreeBSD uses 100, as does Linux 2.4, while Linux 2.6
    1.66 +     * uses 1000. (This is often configurable in the kernel in question, also.)
    1.67 +     */
    1.68 +    virtual int get_hz() = 0;
    1.69 +
    1.70 +    virtual void timer_interrupt() = 0;
    1.71 +    virtual void increment_ticks() = 0;
    1.72 +
    1.73 +    virtual void buffer_size(int size) = 0;
    1.74 +    
    1.75 +    virtual struct INetDatagramSocket *new_udp_socket() { return NULL; }
    1.76 +    virtual struct INetStreamSocket *new_tcp_socket() { return NULL; }
    1.77 +    virtual struct INetStreamSocket *new_sctp_socket() { return NULL; }
    1.78 +
    1.79 +    // The following I've made optional to implement for now. Eases
    1.80 +    // integration of new features.
    1.81 +    virtual int sysctl(const char *sysctl_name, void *oldval, size_t *oldlenp,
    1.82 +        void *newval, size_t newlen)
    1.83 +    {
    1.84 +        return -1;
    1.85 +    }
    1.86 +
    1.87 +    // alternate, simpler interface. the stack cradle code is expected
    1.88 +    // to convert the string-value to something that the stack can handle.
    1.89 +    // The idea here is that this is a front-end to the sysctl(2) call,
    1.90 +    // much like the sysctl(8) program.
    1.91 +    virtual int sysctl_set(const char *name, const char *value)
    1.92 +    {
    1.93 +        return -1;
    1.94 +    }
    1.95 +
    1.96 +    // same as above, cradle code is expected to convert the sysctl value
    1.97 +    // into a string.
    1.98 +    // returns length of the string in value, i.e. retval > len: 'output truncated'.
    1.99 +    virtual int sysctl_get(const char *name, char *value, size_t len)
   1.100 +    {
   1.101 +        return -1;
   1.102 +    }
   1.103 +
   1.104 +    // this tells the cradle code to put the name of sysctl number 'idx'
   1.105 +    // into name[].
   1.106 +    // The idea is that this can be used to get a list of all available sysctls:
   1.107 +    // char buf[256]
   1.108 +    // for (i=0; sysctl_getnum(i, buf, sizeof(buf)) > 0 ;i++)
   1.109 +    //    puts(buf);
   1.110 +    // returns -1 if idx is out of range and the length of the sysctl name otherwise.
   1.111 +    virtual int sysctl_getnum(size_t idx, char *name, size_t len)
   1.112 +    {
   1.113 +        return -1;
   1.114 +    }
   1.115 +
   1.116 +    virtual void show_config()
   1.117 +    {
   1.118 +        ;
   1.119 +    }
   1.120 +
   1.121 +    /* Optional functions used to get and set variables for this stack */
   1.122 +    virtual bool get_var(const char *var, char *result, int result_len)
   1.123 +    {
   1.124 +        return false;
   1.125 +    }
   1.126 +    
   1.127 +    virtual bool set_var(const char *var, const char *val)
   1.128 +    {
   1.129 +        return false;
   1.130 +    }
   1.131 +
   1.132 +    /** The level of debugging or diagnostic information to print out. This 
   1.133 +     * normally means kernel messages printed out during initialisation but
   1.134 +     * may also include extra debugging messages that are part of NSC. */
   1.135 +    virtual void set_diagnostic(int level) {}
   1.136 +
   1.137 +    /** Simple interface to support sending any textual command to a stack 
   1.138 +     *
   1.139 +     * @returns 0 on success
   1.140 +     */
   1.141 +    virtual int cmd(const char *) 
   1.142 +    {
   1.143 +        return 1;
   1.144 +    }
   1.145 +};
   1.146 +
   1.147 +struct INetStreamSocket
   1.148 +{
   1.149 +    virtual ~INetStreamSocket() {}
   1.150 +
   1.151 +    virtual void connect(const char *, int) = 0;
   1.152 +    virtual void disconnect() = 0;
   1.153 +    virtual void listen(int) = 0;
   1.154 +    virtual int accept(INetStreamSocket **) = 0;
   1.155 +    virtual int send_data(const void *data, int datalen) = 0;
   1.156 +    virtual int read_data(void *buf, int *buflen) = 0;
   1.157 +    /* We need to pass the option name in as a string here. The reason for
   1.158 +     * this is that different operating systems you compile on will have
   1.159 +     * different numbers defined for the constants SO_SNDBUF and so on. */
   1.160 +    virtual int setsockopt(char *optname, void *val, size_t valsize) = 0;
   1.161 +    virtual void print_state(FILE *) = 0;
   1.162 +    virtual bool is_connected() = 0;
   1.163 +    virtual bool is_listening() = 0;
   1.164 +
   1.165 +    virtual int getpeername(struct sockaddr *sa, size_t *salen) {
   1.166 +	    return -1;
   1.167 +    }
   1.168 +    virtual int getsockname(struct sockaddr *sa, size_t *salen) {
   1.169 +	    return -1;
   1.170 +    }
   1.171 +    /* Optional functions used to get and set variables for this TCP
   1.172 +     * connection. */
   1.173 +    virtual bool get_var(const char *var, char *result, int result_len)
   1.174 +    {
   1.175 +        return false;
   1.176 +    }
   1.177 +    
   1.178 +    virtual bool set_var(const char *var, const char *val)
   1.179 +    {
   1.180 +        return false;
   1.181 +    }
   1.182 +};
   1.183 +
   1.184 +struct INetDatagramSocket
   1.185 +{
   1.186 +    virtual ~INetDatagramSocket() {}
   1.187 +
   1.188 +    virtual void set_destination(const char *, int) = 0;
   1.189 +    virtual void send_data(const void *data, int datalen) = 0;
   1.190 +};
   1.191 +struct ISendCallback
   1.192 +{
   1.193 +    virtual ~ISendCallback() {}
   1.194 +
   1.195 +//  virtual void send_callback(int id, const void *data, unsigned int datalen) = 0;
   1.196 +    virtual void send_callback(const void *data, unsigned int datalen) = 0;
   1.197 +};
   1.198 +
   1.199 +struct IInterruptCallback
   1.200 +{
   1.201 +    virtual ~IInterruptCallback() {}
   1.202 +
   1.203 +    virtual void wakeup() = 0;
   1.204 +    virtual void gettime(unsigned int *, unsigned int *) = 0;
   1.205 +};
   1.206 +
   1.207 +typedef int (*FRandom)();
   1.208 +typedef INetStack *(*FCreateStack)(ISendCallback *, IInterruptCallback *, 
   1.209 +        FRandom);
   1.210 +
   1.211 +#define CREATE_STACK_FUNC(a,b,c) extern "C" INetStack *nsc_create_stack(\
   1.212 +        ISendCallback *a, IInterruptCallback *b, FRandom c)
   1.213 +
   1.214 +#endif