A Discrete-Event Network Simulator
API
sim_interface.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  Network Simulation Cradle
4  Copyright (C) 2003-2005 Sam Jansen
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the Free
8  Software Foundation; either version 2 of the License, or (at your option)
9  any later version.
10 
11  This program is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 */
21 
22 #ifndef NSC_SIM_INTERFACE_H
23 #define NSC_SIM_INTERFACE_H
24 
25 #define NSC_VERSION 0x000500
26 
31 struct INetStack
32 {
33  virtual ~INetStack() {}
34 
39  virtual void init (int hz) = 0;
40 
47  virtual void if_receive_packet (int if_id, const void *data, int datalen) = 0;
53  virtual void if_send_packet (const void *data, int datalen) = 0;
58  virtual void if_send_finish (int if_id) = 0;
59 
66  virtual void if_attach (const char *addr, const char *mask, int mtu) = 0;
71  virtual void add_default_gateway (const char *addr) = 0;
72 
79  virtual int get_id () = 0;
80 
89  virtual const char *get_name () = 0;
90 
102  virtual int get_hz () = 0;
103 
107  virtual void timer_interrupt () = 0;
108 
112  virtual void increment_ticks () = 0;
113 
118  virtual void buffer_size (int size) = 0;
119 
124  virtual struct INetDatagramSocket *new_udp_socket () { return NULL; }
129  virtual struct INetStreamSocket *new_tcp_socket () { return NULL; }
134  virtual struct INetStreamSocket *new_sctp_socket () { return NULL; }
135 
136  // The following I've made optional to implement for now. Eases
137  // integration of new features.
147  virtual int sysctl (const char *sysctl_name, void *oldval, size_t *oldlenp,
148  void *newval, size_t newlen)
149  {
150  return -1;
151  }
152 
153  // alternate, simpler interface. the stack cradle code is expected
154  // to convert the string-value to something that the stack can handle.
155  // The idea here is that this is a front-end to the sysctl(2) call,
156  // much like the sysctl(8) program.
163  virtual int sysctl_set (const char *name, const char *value)
164  {
165  return -1;
166  }
167 
168  // same as above, cradle code is expected to convert the sysctl value
169  // into a string.
170  // returns length of the string in value, i.e. retval > len: 'output truncated'.
178  virtual int sysctl_get (const char *name, char *value, size_t len)
179  {
180  return -1;
181  }
182 
198  virtual int sysctl_getnum (size_t idx, char *name, size_t len)
199  {
200  return -1;
201  }
202 
206  virtual void show_config ()
207  {
208  ;
209  }
210 
218  virtual bool get_var (const char *var, char *result, int result_len)
219  {
220  return false;
221  }
222 
229  virtual bool set_var (const char *var, const char *val)
230  {
231  return false;
232  }
233 
242  virtual void set_diagnostic (int level) {}
243 
249  virtual int cmd (const char *)
250  {
251  return 1;
252  }
253 };
254 
260 {
261  virtual ~INetStreamSocket() {}
262 
266  virtual void connect (const char *, int) = 0;
270  virtual void disconnect () = 0;
274  virtual void listen (int) = 0;
280  virtual int accept (INetStreamSocket **handler) = 0;
287  virtual int send_data (const void *data, int datalen) = 0;
294  virtual int read_data (void *buf, int *buflen) = 0;
307  virtual int setsockopt (char *optname, void *val, size_t valsize) = 0;
311  virtual void print_state (FILE *) = 0;
316  virtual bool is_connected () = 0;
321  virtual bool is_listening () = 0;
331  virtual int getpeername (struct sockaddr *sa, size_t *salen) {
332  return -1;
333  }
343  virtual int getsockname (struct sockaddr *sa, size_t *salen) {
344  return -1;
345  }
356  virtual bool get_var (const char *var, char *result, int result_len)
357  {
358  return false;
359  }
369  virtual bool set_var (const char *var, const char *val)
370  {
371  return false;
372  }
373 };
374 
380 {
381  virtual ~INetDatagramSocket() {}
382 
386  virtual void set_destination (const char *, int) = 0;
392  virtual void send_data (const void *data, int datalen) = 0;
393 };
394 
400 {
401  virtual ~ISendCallback() {}
402 
408  virtual void send_callback (const void *data, int datalen) = 0;
409 };
410 
416 {
417  virtual ~IInterruptCallback() {}
418 
422  virtual void wakeup () = 0;
426  virtual void gettime (unsigned int *, unsigned int *) = 0;
427 };
428 
429 typedef int (*FRandom)();
430 typedef INetStack *(*FCreateStack)(ISendCallback *, IInterruptCallback *,
431  FRandom);
432 
433 #define CREATE_STACK_FUNC(a,b,c) extern "C" INetStack *nsc_create_stack ( \
434  ISendCallback *a, IInterruptCallback *b, FRandom c)
435 
436 #endif /* NSC_SIM_INTERFACE_H */
INetStreamSocket::set_var
virtual bool set_var(const char *var, const char *val)
Optional function used to set variables for this TCP connection.
Definition: sim_interface.h:369
INetStreamSocket::get_var
virtual bool get_var(const char *var, char *result, int result_len)
Optional function used to get variables for this TCP connection.
Definition: sim_interface.h:356
INetStack::timer_interrupt
virtual void timer_interrupt()=0
The stack timer_interrupt function.
INetStreamSocket::print_state
virtual void print_state(FILE *)=0
Print the socket state.
INetDatagramSocket::set_destination
virtual void set_destination(const char *, int)=0
Set the destination address and port.
INetDatagramSocket::~INetDatagramSocket
virtual ~INetDatagramSocket()
Definition: sim_interface.h:381
IInterruptCallback::gettime
virtual void gettime(unsigned int *, unsigned int *)=0
Get the actual time.
INetStreamSocket::getsockname
virtual int getsockname(struct sockaddr *sa, size_t *salen)
Get the socket local name.
Definition: sim_interface.h:343
INetStack::increment_ticks
virtual void increment_ticks()=0
Increment the time ticks.
INetStreamSocket::~INetStreamSocket
virtual ~INetStreamSocket()
Definition: sim_interface.h:261
INetStack::~INetStack
virtual ~INetStack()
Definition: sim_interface.h:33
ISendCallback::~ISendCallback
virtual ~ISendCallback()
Definition: sim_interface.h:401
INetStack::get_name
virtual const char * get_name()=0
Return a short one-word name of the stack.
INetStack::get_var
virtual bool get_var(const char *var, char *result, int result_len)
Optional function to get variables for this stack.
Definition: sim_interface.h:218
INetStack::new_sctp_socket
virtual struct INetStreamSocket * new_sctp_socket()
Create a new SCTP socket.
Definition: sim_interface.h:134
INetStack::buffer_size
virtual void buffer_size(int size)=0
Set the buffer size.
INetStreamSocket::disconnect
virtual void disconnect()=0
Disconnect from a remote peer.
INetStack::new_udp_socket
virtual struct INetDatagramSocket * new_udp_socket()
Create a new UDP socket.
Definition: sim_interface.h:124
INetStack::new_tcp_socket
virtual struct INetStreamSocket * new_tcp_socket()
Create a new TCP socket.
Definition: sim_interface.h:129
INetStack::if_send_packet
virtual void if_send_packet(const void *data, int datalen)=0
Send complete packet to the NSC network stack.
INetStreamSocket::connect
virtual void connect(const char *, int)=0
Connect to a remote peer.
INetStreamSocket::is_connected
virtual bool is_connected()=0
Check the connection state.
INetStack::get_id
virtual int get_id()=0
Returns the internal id of the stack instance.
INetStreamSocket::is_listening
virtual bool is_listening()=0
Check the listening state.
INetDatagramSocket::send_data
virtual void send_data(const void *data, int datalen)=0
Send a datagram.
IInterruptCallback
Struct interface to NSC soft interrupt capabilities.
Definition: sim_interface.h:416
INetStreamSocket::listen
virtual void listen(int)=0
Put the socket in Listening state on a port.
IInterruptCallback::~IInterruptCallback
virtual ~IInterruptCallback()
Definition: sim_interface.h:417
INetStreamSocket::accept
virtual int accept(INetStreamSocket **handler)=0
Accept an incoming connection.
data
uint8_t data[writeSize]
Definition: socket-bound-tcp-static-routing.cc:53
INetStack::set_var
virtual bool set_var(const char *var, const char *val)
Optional function to set variables for this stack.
Definition: sim_interface.h:229
INetStack::get_hz
virtual int get_hz()=0
Get the timer_interrupt frequency.
INetStack::init
virtual void init(int hz)=0
Initialize the stack.
INetStack::if_attach
virtual void if_attach(const char *addr, const char *mask, int mtu)=0
Attach an interface to the stack.
INetStack::sysctl
virtual int sysctl(const char *sysctl_name, void *oldval, size_t *oldlenp, void *newval, size_t newlen)
use sysctl to modify system parameters
Definition: sim_interface.h:147
INetStack::sysctl_getnum
virtual int sysctl_getnum(size_t idx, char *name, size_t len)
Tell the cradle code to put the name of sysctl number 'idx' into name[].
Definition: sim_interface.h:198
test-ns3.result
result
Definition: test-ns3.py:576
INetStack::set_diagnostic
virtual void set_diagnostic(int level)
Set the level of debugging or diagnostic information to print out.
Definition: sim_interface.h:242
ISendCallback::send_callback
virtual void send_callback(const void *data, int datalen)=0
Invoked by NSCs 'ethernet driver' to re-inject a packet into ns-3.
INetStreamSocket
Struct interface to NSC Stream (i.e., TCP) Sockets.
Definition: sim_interface.h:260
INetStack::if_send_finish
virtual void if_send_finish(int if_id)=0
Signal the completion of send procedure to the NSC network stack.
INetStack::sysctl_get
virtual int sysctl_get(const char *name, char *value, size_t len)
Get system parameters using sysctl.
Definition: sim_interface.h:178
INetDatagramSocket
Struct interface to NSC Datagram (i.e., UDP) Sockets.
Definition: sim_interface.h:380
INetStack::if_receive_packet
virtual void if_receive_packet(int if_id, const void *data, int datalen)=0
Deliver complete packet to the NSC network stack.
INetStack::add_default_gateway
virtual void add_default_gateway(const char *addr)=0
Add a default gateway to the interface.
IInterruptCallback::wakeup
virtual void wakeup()=0
Called by the NSC stack whenever something of interest has happened.
ISendCallback
Struct interface to NSC send capabilities.
Definition: sim_interface.h:400
INetStreamSocket::read_data
virtual int read_data(void *buf, int *buflen)=0
Read some data.
INetStack::sysctl_set
virtual int sysctl_set(const char *name, const char *value)
Set system parameters using sysctl.
Definition: sim_interface.h:163
INetStreamSocket::send_data
virtual int send_data(const void *data, int datalen)=0
Send some data.
INetStack
Struct interface to NSC stack.
Definition: sim_interface.h:32
INetStreamSocket::setsockopt
virtual int setsockopt(char *optname, void *val, size_t valsize)=0
Set the socket options.
FRandom
int(* FRandom)()
Definition: sim_interface.h:429
INetStreamSocket::getpeername
virtual int getpeername(struct sockaddr *sa, size_t *salen)
Get the peer name.
Definition: sim_interface.h:331
INetStack::show_config
virtual void show_config()
Show the NSC configuration.
Definition: sim_interface.h:206
INetStack::cmd
virtual int cmd(const char *)
Simple interface to support sending any textual command to a stack.
Definition: sim_interface.h:249