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 */
virtual void if_receive_packet(int if_id, const void *data, int datalen)=0
Deliver complete packet to the NSC network stack.
virtual bool is_listening()=0
Check the listening state.
virtual void show_config()
Show the NSC configuration.
virtual void set_destination(const char *, int)=0
Set the destination address and port.
virtual ~INetDatagramSocket()
virtual bool get_var(const char *var, char *result, int result_len)
Optional function to get variables for this stack.
virtual int setsockopt(char *optname, void *val, size_t valsize)=0
Set the socket options.
virtual ~INetStreamSocket()
virtual bool get_var(const char *var, char *result, int result_len)
Optional function used to get variables for this TCP connection.
virtual void if_send_packet(const void *data, int datalen)=0
Send complete packet to the NSC network stack.
virtual void connect(const char *, int)=0
Connect to a remote peer.
virtual void increment_ticks()=0
Increment the time ticks.
Struct interface to NSC send capabilities.
int(* FRandom)()
virtual void wakeup()=0
Called by the NSC stack whenever something of interest has happened.
virtual int get_hz()=0
Get the timer_interrupt frequency.
uint8_t data[writeSize]
virtual void send_callback(const void *data, int datalen)=0
Invoked by NSCs 'ethernet driver' to re-inject a packet into ns-3.
virtual const char * get_name()=0
Return a short one-word name of the stack.
virtual int send_data(const void *data, int datalen)=0
Send some data.
Struct interface to NSC stack.
Definition: sim_interface.h:31
virtual void send_data(const void *data, int datalen)=0
Send a datagram.
virtual void buffer_size(int size)=0
Set the buffer size.
virtual bool is_connected()=0
Check the connection state.
virtual void print_state(FILE *)=0
Print the socket state.
virtual int sysctl_set(const char *name, const char *value)
Set system parameters using sysctl.
Struct interface to NSC soft interrupt capabilities.
virtual void timer_interrupt()=0
The stack timer_interrupt function.
virtual void set_diagnostic(int level)
Set the level of debugging or diagnostic information to print out.
virtual struct INetStreamSocket * new_sctp_socket()
Create a new SCTP socket.
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[].
virtual void gettime(unsigned int *, unsigned int *)=0
Get the actual time.
virtual void add_default_gateway(const char *addr)=0
Add a default gateway to the interface.
virtual int getpeername(struct sockaddr *sa, size_t *salen)
Get the peer name.
virtual int getsockname(struct sockaddr *sa, size_t *salen)
Get the socket local name.
virtual void listen(int)=0
Put the socket in Listening state on a port.
virtual bool set_var(const char *var, const char *val)
Optional function used to set variables for this TCP connection.
virtual int sysctl(const char *sysctl_name, void *oldval, size_t *oldlenp, void *newval, size_t newlen)
use sysctl to modify system parameters
virtual ~IInterruptCallback()
virtual void if_attach(const char *addr, const char *mask, int mtu)=0
Attach an interface to the stack.
Struct interface to NSC Stream (i.e., TCP) Sockets.
virtual void disconnect()=0
Disconnect from a remote peer.
Struct interface to NSC Datagram (i.e., UDP) Sockets.
virtual void if_send_finish(int if_id)=0
Signal the completion of send procedure to the NSC network stack.
virtual int read_data(void *buf, int *buflen)=0
Read some data.
virtual struct INetStreamSocket * new_tcp_socket()
Create a new TCP socket.
virtual void init(int hz)=0
Initialize the stack.
virtual int accept(INetStreamSocket **handler)=0
Accept an incoming connection.
virtual bool set_var(const char *var, const char *val)
Optional function to set variables for this stack.
virtual ~INetStack()
Definition: sim_interface.h:33
virtual ~ISendCallback()
virtual int cmd(const char *)
Simple interface to support sending any textual command to a stack.
virtual int get_id()=0
Returns the internal id of the stack instance.
virtual struct INetDatagramSocket * new_udp_socket()
Create a new UDP socket.
virtual int sysctl_get(const char *name, char *value, size_t len)
Get system parameters using sysctl.