2 ==================================================================================
3 Copyright (c) 2020 Nokia
4 Copyright (c) 2020 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
21 Mnemonic: test_transport.c
22 Abstract: This supplies a bunch of dummy system functions which emulate
23 things like connect() bind() etc.
25 This module must be directly included to be used.
27 Author: E. Scott Daniels
30 #ifndef _test_transport_c
31 #define _sitransport_h // prevent the transport defs when including SI95
34 char tpem_last_addr[1024]; // last address to simulate connection to ourself
35 int tpem_last_len = 0;
37 int tpem_addr_dup = 0; // getsockname duplicates last addr if true
38 int tpem_conn_state = 0; // states returned by emulated functions allowing failures to be driven
39 int tpem_sock_state = 0; // if sock state 0, then socket call returns good fd
40 int tpem_listen_state = 0;
41 int tpem_bind_state = 0;
42 int tpem_accept_fd = 5; // file desc returned by accept
43 int tpem_sel_ef = -1; // select sets this fd's error if >= 0
44 int tpem_sel_block = 0; // set if select call inidcates would block
45 int tpem_send_err = 0; // set to cause send to return error
47 // ------------ emulation control -------------------------------------------
50 Allow test prog to set various things
52 static void tpem_set_conn_state( int s ) {
56 static void tpem_set_addr_dup_state( int s ) {
60 static void tpem_set_sock_state( int s ) {
64 static void tpem_set_bind_state( int s ) {
68 static void tpem_set_accept_fd( int s ) {
72 static void tpem_set_selef_fd( int s ) {
76 static void tpem_set_sel_blk( int s ) {
80 static void tpem_set_send_err( int s ) {
84 // ---- emulated functions ---------------------------------------------------
86 static int tpem_bind( int socket, struct sockaddr* addr, socklen_t alen ) {
87 return tpem_bind_state;
90 static int tpem_connect( int socket, struct sockaddr* addr, socklen_t alen ) {
91 memcpy( tpem_last_addr, addr, alen );
93 fprintf( stderr, "<SYSEM> connection simulated rc=%d\n", tpem_conn_state );
94 return tpem_conn_state;
98 This gets the last address connected to if dup is true; else returns 0s
99 which should be enough to test that connection didn't loop back to us.
101 static int tpem_getsockname( int socket, struct sockaddr* address, socklen_t* alen ) {
102 int clen; // copy len
104 if( tpem_last_len > 0 ) {
105 clen = tpem_last_len > *alen ? *alen : tpem_last_len;
106 if( tpem_addr_dup ) {
107 memcpy( address, tpem_last_addr, clen );
109 memset( address, 0, clen );
113 memset( address, 0, *alen );
119 static int tpem_listen( int socket, int backlog ) {
120 return tpem_listen_state;
123 static int tpem_socket( int domain, int type, int protocol ) {
126 if( tpem_sock_state == 0 ) {
128 fd = 3; // ensure we don't stomp on std* descriptors
137 static int tpem_accept( int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) {
138 return tpem_accept_fd;
142 Emulate a select. If tpem_sel_ef is set, then the error fd set for the fd is set to true.
143 If sel_woudl_block is set, then the select returns blocking
145 static int tpem_select( int fd_count, fd_set* rf, fd_set* wf, fd_set* ef, void* time ) {
146 fprintf( stderr, "<SYSTEM> select returns %d (1==no-block)\n", tpem_sel_block ? -1 : 1 );
148 if( tpem_sel_block ) {
152 if( tpem_sel_ef >= 0 ) {
153 FD_SET( tpem_sel_ef, ef );
162 If tpem_send_err is set, we return less than count;
164 static int tpem_send( int fd, void* buf, int count, int flags ) {
165 errno = tpem_send_err;
167 fprintf( stderr, "<SYSTEM> send on fd=%d for %d bytes ret=%d\n", fd, count, tpem_send_err ? -1 : count );
168 return tpem_send_err ? -1 : count;
172 // ---------------------------------------------------------------------------------------
175 redefine all system calls to reference functions here. There are two defs
176 SI functions should use the capitalised verision so that sliding ff under
177 it is possible. There might be instances wehre the actual system call is
178 needed, so we also define the lowercase value.
180 #define BIND tpem_bind
181 #define bind tpem_bind
182 #define CONNECT tpem_connect
183 #define connect tpem_connect
184 #define getsockname tpem_getsockname
185 #define SOCKET tpem_socket
186 #define socket tpem_socket
187 #define LISTEN tpem_listen
188 #define listen tpem_listen
189 #define accept tpem_accept
190 #define ACCEPT tpem_accept
191 #define SEND tpem_send
192 #define SELECT tpem_select
193 #define select tpem_select
196 these are defined in SI so that we can use the system stack or FFstack
197 they must exist and reference system calls if not defined above.
200 #define SHUTDOWN shutdown
201 #define GETSOCKOPT getscokopt
202 #define SETSOCKOPT setsockopt
205 #define SENDTO sendto
207 #define RECVFROM recvfrom
208 #define RECVMSG recvmsg