src/node/sim_interface.h
author Florian Westphal <fw@strlen.de>
Wed Jul 15 18:46:14 2009 +0200 (2009-07-15)
changeset 4685 ae536d9e0d6d
permissions -rw-r--r--
nsc: move nsc glue code from nsc-tcp-l4-protocol to node/nsc-glue.cc.

known problems:
- sim_interface.h is duplicated
- nsc-glue.cc adds hooks in node.cc, "hijacks" incoming packets
- nsc-glue exports NSCs INetStack (instead of wrapping it completely)
- nsc-tcp-l4-protocol and nsc-tcp-socket-impl make calls into nsc-glue
- nsc-tcp-socket-impl should really be "nsc-socket-core" (or something
like that)

needs fixing on nsc side:
- no support for multiple interfaces yet (also not yet supported
on nsc side)
- nsc initialisation still tied to IP (Adding an Interface and assigning the
IP address is a single step; it should be separate)

maybe there is more.

There is a NSC_NEXT define in nsc-glue.h, its main purpose is to flag
the places where the NSC API needs to be adapted to support multiple
interfaces in nsc.
fw@4685
     1
#ifndef __SIM_INTERFACE_H__
fw@4685
     2
#define __SIM_INTERFACE_H__
fw@4685
     3
/*
fw@4685
     4
  Network Simulation Cradle
fw@4685
     5
  Copyright (C) 2003-2005 Sam Jansen
fw@4685
     6
fw@4685
     7
  This program is free software; you can redistribute it and/or modify it
fw@4685
     8
  under the terms of the GNU General Public License as published by the Free
fw@4685
     9
  Software Foundation; either version 2 of the License, or (at your option)
fw@4685
    10
  any later version.
fw@4685
    11
fw@4685
    12
  This program is distributed in the hope that it will be useful, but WITHOUT
fw@4685
    13
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fw@4685
    14
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
fw@4685
    15
  more details.
fw@4685
    16
fw@4685
    17
  You should have received a copy of the GNU General Public License along
fw@4685
    18
  with this program; if not, write to the Free Software Foundation, Inc., 59
fw@4685
    19
  Temple Place, Suite 330, Boston, MA 02111-1307 USA
fw@4685
    20
fw@4685
    21
*/
fw@4685
    22
fw@4685
    23
#define NSC_VERSION 0x000500
fw@4685
    24
fw@4685
    25
struct INetStack
fw@4685
    26
{
fw@4685
    27
    virtual ~INetStack() {}
fw@4685
    28
fw@4685
    29
    virtual void init(int hz) = 0;
fw@4685
    30
    
fw@4685
    31
    virtual void if_receive_packet(int if_id, const void *data, unsigned int datalen) = 0;
fw@4685
    32
fw@4685
    33
    /*
fw@4685
    34
     * called by NSCs network driver. It invokes ISendCallback->send_callback() to pass
fw@4685
    35
     * the packet to the simulator.
fw@4685
    36
     */
fw@4685
    37
    virtual void if_send_packet(const void *data, unsigned int datalen) = 0;
fw@4685
    38
    //virtual void if_send_packet(int if_id, const void *data, unsigned int datalen) = 0;
fw@4685
    39
fw@4685
    40
    /*
fw@4685
    41
     * called by network simulator after packet tx was sucessful.
fw@4685
    42
     * on Linux, this wakes up the netdev xmit queue.
fw@4685
    43
     */
fw@4685
    44
    virtual void if_send_finish(int if_id) = 0;
fw@4685
    45
fw@4685
    46
    virtual void if_attach(const char *addr, const char *mask, int mtu) = 0;
fw@4685
    47
    virtual void add_default_gateway(const char *addr) = 0;
fw@4685
    48
fw@4685
    49
    /** Purely for debugging/diagnostic purposes. This returns the internal id
fw@4685
    50
     * of the stack instance.
fw@4685
    51
     */
fw@4685
    52
    virtual int get_id() = 0;
fw@4685
    53
fw@4685
    54
    /** Should return a short one-word name of the stack. Eg. Linux 2.4.x ->
fw@4685
    55
     * linux24, FreeBSD 5.x -> freebsd5. This can be used to identify output
fw@4685
    56
     * from a stack, for example a packet trace file. */
fw@4685
    57
    virtual const char *get_name() = 0;
fw@4685
    58
fw@4685
    59
    /** This is used so the simulator can call the stack timer_interrupt function
fw@4685
    60
     * the correct amount of times per second. For example, lwip has a hz of 10,
fw@4685
    61
     * which it returns here to say that it's timer_interrupt should be called
fw@4685
    62
     * 10 times a second. FreeBSD uses 100, as does Linux 2.4, while Linux 2.6
fw@4685
    63
     * uses 1000. (This is often configurable in the kernel in question, also.)
fw@4685
    64
     */
fw@4685
    65
    virtual int get_hz() = 0;
fw@4685
    66
fw@4685
    67
    virtual void timer_interrupt() = 0;
fw@4685
    68
    virtual void increment_ticks() = 0;
fw@4685
    69
fw@4685
    70
    virtual void buffer_size(int size) = 0;
fw@4685
    71
    
fw@4685
    72
    virtual struct INetDatagramSocket *new_udp_socket() { return NULL; }
fw@4685
    73
    virtual struct INetStreamSocket *new_tcp_socket() { return NULL; }
fw@4685
    74
    virtual struct INetStreamSocket *new_sctp_socket() { return NULL; }
fw@4685
    75
fw@4685
    76
    // The following I've made optional to implement for now. Eases
fw@4685
    77
    // integration of new features.
fw@4685
    78
    virtual int sysctl(const char *sysctl_name, void *oldval, size_t *oldlenp,
fw@4685
    79
        void *newval, size_t newlen)
fw@4685
    80
    {
fw@4685
    81
        return -1;
fw@4685
    82
    }
fw@4685
    83
fw@4685
    84
    // alternate, simpler interface. the stack cradle code is expected
fw@4685
    85
    // to convert the string-value to something that the stack can handle.
fw@4685
    86
    // The idea here is that this is a front-end to the sysctl(2) call,
fw@4685
    87
    // much like the sysctl(8) program.
fw@4685
    88
    virtual int sysctl_set(const char *name, const char *value)
fw@4685
    89
    {
fw@4685
    90
        return -1;
fw@4685
    91
    }
fw@4685
    92
fw@4685
    93
    // same as above, cradle code is expected to convert the sysctl value
fw@4685
    94
    // into a string.
fw@4685
    95
    // returns length of the string in value, i.e. retval > len: 'output truncated'.
fw@4685
    96
    virtual int sysctl_get(const char *name, char *value, size_t len)
fw@4685
    97
    {
fw@4685
    98
        return -1;
fw@4685
    99
    }
fw@4685
   100
fw@4685
   101
    // this tells the cradle code to put the name of sysctl number 'idx'
fw@4685
   102
    // into name[].
fw@4685
   103
    // The idea is that this can be used to get a list of all available sysctls:
fw@4685
   104
    // char buf[256]
fw@4685
   105
    // for (i=0; sysctl_getnum(i, buf, sizeof(buf)) > 0 ;i++)
fw@4685
   106
    //    puts(buf);
fw@4685
   107
    // returns -1 if idx is out of range and the length of the sysctl name otherwise.
fw@4685
   108
    virtual int sysctl_getnum(size_t idx, char *name, size_t len)
fw@4685
   109
    {
fw@4685
   110
        return -1;
fw@4685
   111
    }
fw@4685
   112
fw@4685
   113
    virtual void show_config()
fw@4685
   114
    {
fw@4685
   115
        ;
fw@4685
   116
    }
fw@4685
   117
fw@4685
   118
    /* Optional functions used to get and set variables for this stack */
fw@4685
   119
    virtual bool get_var(const char *var, char *result, int result_len)
fw@4685
   120
    {
fw@4685
   121
        return false;
fw@4685
   122
    }
fw@4685
   123
    
fw@4685
   124
    virtual bool set_var(const char *var, const char *val)
fw@4685
   125
    {
fw@4685
   126
        return false;
fw@4685
   127
    }
fw@4685
   128
fw@4685
   129
    /** The level of debugging or diagnostic information to print out. This 
fw@4685
   130
     * normally means kernel messages printed out during initialisation but
fw@4685
   131
     * may also include extra debugging messages that are part of NSC. */
fw@4685
   132
    virtual void set_diagnostic(int level) {}
fw@4685
   133
fw@4685
   134
    /** Simple interface to support sending any textual command to a stack 
fw@4685
   135
     *
fw@4685
   136
     * @returns 0 on success
fw@4685
   137
     */
fw@4685
   138
    virtual int cmd(const char *) 
fw@4685
   139
    {
fw@4685
   140
        return 1;
fw@4685
   141
    }
fw@4685
   142
};
fw@4685
   143
fw@4685
   144
struct INetStreamSocket
fw@4685
   145
{
fw@4685
   146
    virtual ~INetStreamSocket() {}
fw@4685
   147
fw@4685
   148
    virtual void connect(const char *, int) = 0;
fw@4685
   149
    virtual void disconnect() = 0;
fw@4685
   150
    virtual void listen(int) = 0;
fw@4685
   151
    virtual int accept(INetStreamSocket **) = 0;
fw@4685
   152
    virtual int send_data(const void *data, int datalen) = 0;
fw@4685
   153
    virtual int read_data(void *buf, int *buflen) = 0;
fw@4685
   154
    /* We need to pass the option name in as a string here. The reason for
fw@4685
   155
     * this is that different operating systems you compile on will have
fw@4685
   156
     * different numbers defined for the constants SO_SNDBUF and so on. */
fw@4685
   157
    virtual int setsockopt(char *optname, void *val, size_t valsize) = 0;
fw@4685
   158
    virtual void print_state(FILE *) = 0;
fw@4685
   159
    virtual bool is_connected() = 0;
fw@4685
   160
    virtual bool is_listening() = 0;
fw@4685
   161
fw@4685
   162
    virtual int getpeername(struct sockaddr *sa, size_t *salen) {
fw@4685
   163
	    return -1;
fw@4685
   164
    }
fw@4685
   165
    virtual int getsockname(struct sockaddr *sa, size_t *salen) {
fw@4685
   166
	    return -1;
fw@4685
   167
    }
fw@4685
   168
    /* Optional functions used to get and set variables for this TCP
fw@4685
   169
     * connection. */
fw@4685
   170
    virtual bool get_var(const char *var, char *result, int result_len)
fw@4685
   171
    {
fw@4685
   172
        return false;
fw@4685
   173
    }
fw@4685
   174
    
fw@4685
   175
    virtual bool set_var(const char *var, const char *val)
fw@4685
   176
    {
fw@4685
   177
        return false;
fw@4685
   178
    }
fw@4685
   179
};
fw@4685
   180
fw@4685
   181
struct INetDatagramSocket
fw@4685
   182
{
fw@4685
   183
    virtual ~INetDatagramSocket() {}
fw@4685
   184
fw@4685
   185
    virtual void set_destination(const char *, int) = 0;
fw@4685
   186
    virtual void send_data(const void *data, int datalen) = 0;
fw@4685
   187
};
fw@4685
   188
struct ISendCallback
fw@4685
   189
{
fw@4685
   190
    virtual ~ISendCallback() {}
fw@4685
   191
fw@4685
   192
//  virtual void send_callback(int id, const void *data, unsigned int datalen) = 0;
fw@4685
   193
    virtual void send_callback(const void *data, unsigned int datalen) = 0;
fw@4685
   194
};
fw@4685
   195
fw@4685
   196
struct IInterruptCallback
fw@4685
   197
{
fw@4685
   198
    virtual ~IInterruptCallback() {}
fw@4685
   199
fw@4685
   200
    virtual void wakeup() = 0;
fw@4685
   201
    virtual void gettime(unsigned int *, unsigned int *) = 0;
fw@4685
   202
};
fw@4685
   203
fw@4685
   204
typedef int (*FRandom)();
fw@4685
   205
typedef INetStack *(*FCreateStack)(ISendCallback *, IInterruptCallback *, 
fw@4685
   206
        FRandom);
fw@4685
   207
fw@4685
   208
#define CREATE_STACK_FUNC(a,b,c) extern "C" INetStack *nsc_create_stack(\
fw@4685
   209
        ISendCallback *a, IInterruptCallback *b, FRandom c)
fw@4685
   210
fw@4685
   211
#endif