aboutsummaryrefslogtreecommitdiff
path: root/project/dclib/dclib-network-linux.h
blob: 8f9de7ed7f0680fb830252c8eb1ff878a57504e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

/***************************************************************************
 *                                                                         *
 *                     _____     ____                                      *
 *                    |  __ \   / __ \   _     _ _____                     *
 *                    | |  \ \ / /  \_\ | |   | |  _  \                    *
 *                    | |   \ \| |      | |   | | |_| |                    *
 *                    | |   | || |      | |   | |  ___/                    *
 *                    | |   / /| |   __ | |   | |  _  \                    *
 *                    | |__/ / \ \__/ / | |___| | |_| |                    *
 *                    |_____/   \____/  |_____|_|_____/                    *
 *                                                                         *
 *                       Wiimms source code library                        *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *        Copyright (c) 2012-2024 by Dirk Clemens <wiimm@wiimm.de>         *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#ifndef DCLIB_NETWORK_LINUX_H
#define DCLIB_NETWORK_LINUX_H 1

#ifndef DCLIB_NETWORK_H
  #include "dclib-network.h"
#endif

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    IP/UDP			///////////////
///////////////////////////////////////////////////////////////////////////////

ssize_t ReceiveUDPv4
(
    int			sock,		// valid socket
    void		*buf,		// buffer tostore incoming data
    size_t		buf_size,	// size of buffer
    int			flags,		// flags for recffrom() or recvmsg()
    struct sockaddr_in	*src_addr,	// not NULL: stire source address
    in_addr_t		*dest_addr	// not NULL: store destination IPv4 (NBO)
);

///////////////////////////////////////////////////////////////////////////////

ssize_t SendUDPv4
(
    int			sock,		// valid socket
    const void		*data,		// data to send
    size_t		data_size,	// size of data
    int			flags,		// flags for sendto() or sendmsg()
    struct sockaddr_in	*dest_addr,	// destination address (NULL possible)
    in_addr_t		src_addr	// not 0: source IPv4 (NBO)
);

///////////////////////////////////////////////////////////////////////////////

ssize_t recvfrom2
(
    int			sock,
    void		*buf,
    size_t		buf_size,
    int			flags,
    struct sockaddr	*src_addr,
    socklen_t		*src_addrlen,
    struct sockaddr	*dest_addr,
    socklen_t		*dest_addrlen
);

//-----------------------------------------------------------------------------

ssize_t sendto2
(
    int			sock,
    const void		*buf,
    size_t		buf_size,
    int			flags,
    struct sockaddr	*dest_addr,
    socklen_t		dest_addrlen,
    struct sockaddr_in	*src_addr,
    socklen_t		src_addrlen
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			SendRawUDP()			///////////////
///////////////////////////////////////////////////////////////////////////////

enumError SendRawUDP
(
    // Capability CAP_NET_RAW needed (or effective user ID of 0).

    int		sock,		// RAW socket to use,
				// if -1: open and close private socket

    ccp		send_addr,	// NULL or sender address:port
    u32		send_ip4,	// fall back IP4, if 'send_addr' empty
    u16		send_port,	// default sender port

    ccp		recv_addr,	// NULL or receiver address:port
    u32		recv_ip4,	// fall back IP4, if 'recv_addr' empty
    u16		recv_port,	// default receiver port

    const void	*data,		// data to send
    uint	size,		// size of 'data'
    uint	log_mode	// 0:silent, >0:print errors,
				// >=0x10: hexdump data, 'lmode' bytes max
);

//-----------------------------------------------------------------------------

enumError SendRawUDPsa
(
    // Capability CAP_NET_RAW needed (or effective user ID of 0).

    int			sock,		// RAW socket to use,
					// if -1: open and close private socket

    struct sockaddr_in	*sa_send,	// sockaddr of sender
    struct sockaddr_in	*sa_recv,	// sockaddr of receiver

    const void		*data,		// data to send
    uint		size,		// size of 'data'
    uint		log_mode	// 0:silent, >0:print errors,
					// >=0x10: hexdump data, 'lmode' bytes max
);

//-----------------------------------------------------------------------------

uint SetupRawUDPsa
(
    // returns total packet len (mabe with limited 'size')

    udp_packet_t	*pkt,		// paket header to setup (cleared and written)

    struct sockaddr_in	*sa_send,	// sockaddr of sender
    struct sockaddr_in	*sa_recv,	// sockaddr of receiver
    const void		*data,		// data to send
    uint		size		// size of 'data'
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			Routing Support			///////////////
///////////////////////////////////////////////////////////////////////////////
// all IP4 in network byte order

typedef struct RouteIP4_t
{
    //--- returned data

    int	  index;	// entry index, starts with 0, incremented for each found
			//  -1: invalid data

    char  *iface;	// pointer into 'buf': interface name
    u32	  dest;		// destination address, network byte order
    u32	  mask;		// destination network mask, network byte order
    u32	  gate;		// gateway address, network byte order
    u32	  flags;	// routing flags

    //--- internal data

    FILE  *f;		// open file
    uint  col_iface;	// column index for 'iface'
    uint  col_dest;	// column index for 'dest'
    uint  col_mask;	// column index for 'mask'
    uint  col_gate;	// column index for 'gate'
    uint  col_flags;	// column index for 'flags'
    char  buf[200];	// line buffer for /proc/net/route
}
RouteIP4_t;

///////////////////////////////////////////////////////////////////////////////

bool OpenRouteScanIP4
(
    // returns TRUE on success and FALSE if no element is found
    RouteIP4_t	*rt	// valid data, will be initialized
);

bool NextRouteScanIP4
(
    // returns TRUE on success and FALSE if no more element is found
    RouteIP4_t	*rt	// valid data
);

void CloseRouteScanIP4
(
    RouteIP4_t	*rt	// valid data
);

///////////////////////////////////////////////////////////////////////////////

bool FindGatewayIP4
(
			// returns TRUE if gateway found
    RouteIP4_t	*rt,	// valid data, will be initialized
    u32		ip4	// search gateway for this address (network byte order)
);

u32 GetIP4ByInterface
(			// return 0 or IP4 in network byte order
    ccp		iface	// interface name. If NULL: Use default gateway
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    E N D			///////////////
///////////////////////////////////////////////////////////////////////////////

#endif // DCLIB_NETWORK_LINUX_H