KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
ppp.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 ppp/ppp.h
4 Copyright (C) 2014 Lawrence Sebald
5*/
6
7/** \file ppp/ppp.h
8 \brief PPP interface for network communications.
9 \ingroup networking_ppp
10
11 This file defines the API provided by libppp to interact with the PPP stack.
12 PPP is a network communication protocol used to establish a direct link
13 between two peers. It is most commonly used as the data link layer protocol
14 for dialup internet access, but can also be potentially used on broadband
15 connections (PPP over Ethernet or PPPoE) or on a direct serial line to
16 a computer.
17
18 The API presented by this library is designed to be extensible to whatever
19 devices you might want to use it with, and was designed to integrate fairly
20 simply into the rest of KOS' network stack.
21
22 \author Lawrence Sebald
23*/
24
25#ifndef __PPP_PPP_H
26#define __PPP_PPP_H
27
28#include <sys/cdefs.h>
29__BEGIN_DECLS
30
31#include <stdint.h>
32#include <sys/types.h>
33#include <sys/queue.h>
34
35/** \defgroup networking_ppp PPP
36 \brief API for interacting with the Point-to-Point
37 Protocol stack
38 \ingroup networking
39*/
40
41/** \brief PPP device structure.
42 \ingroup networking_ppp
43
44 This structure defines a basic output device for PPP packets. This structure
45 is largely modeled after netif_t from the main network stack, with a bit of
46 functionality removed that is irrelevant for PPP.
47
48 Note that we only allow one device and one connection in this library.
49
50 \headerfile ppp/ppp.h
51*/
52typedef struct ppp_device {
53 /** \brief Device name ("modem", "scif", etc). */
54 const char *name;
55
56 /** \brief Long description of the device. */
57 const char *descr;
58
59 /** \brief Unit index (starts at zero and counts upwards for multiple
60 network devices of the same type). */
61 int index;
62
63 /** \brief Device flags.
64 The lowest 16 bits of this value are reserved for use by libppp. You are
65 free to use the other 16 bits as you see fit in your driver. */
66 uint32_t flags;
67
68 /** \brief Private, device-specific data.
69 This can be used for whatever the driver deems fit. The PPP code won't
70 touch this data at all. Set to NULL if you don't need anything here. */
71 void *privdata;
72
73 /** \brief Attempt to detect the device.
74 \param self The network device in question.
75 \return 0 on success, <0 on failure.
76 */
77 int (*detect)(struct ppp_device *self);
78
79 /** \brief Initialize the device.
80 \param self The network device in question.
81 \return 0 on success, <0 on failure.
82 */
83 int (*init)(struct ppp_device *self);
84
85 /** \brief Shutdown the device.
86 \param self The network device in question.
87 \return 0 on success, <0 on failure.
88 */
89 int (*shutdown)(struct ppp_device *self);
90
91 /** \brief Transmit data on the device.
92
93 This function will be called periodically to transmit data on the
94 underlying device. The data passed in may not necessarily be a whole
95 packet (check the flags to see what's being passed in).
96
97 \param self The network device in question.
98 \param data The data to transmit.
99 \param len The length of the data to transmit in bytes.
100 \param flags Flags to describe what data is being sent in.
101 \return 0 on success, <0 on failure.
102 */
103 int (*tx)(struct ppp_device *self, const uint8_t *data, size_t len,
104 uint32_t flags);
105
106 /** \brief Poll for queued receive data.
107
108 This function will be called periodically by a thread to check the
109 device for any new incoming data.
110
111 \param self The network device in question.
112 \return A pointer to the received data on success. NULL on
113 failure or if no data is waiting.
114 */
115 const uint8_t *(*rx)(struct ppp_device *self, ssize_t *out_len);
117
118/** \brief End of packet flag.
119 \ingroup networking_ppp
120 */
121#define PPP_TX_END_OF_PKT 0x00000001
122
123/** \brief PPP Protocol structure.
124 \ingroup networking_ppp
125
126 Each protocol that the PPP library can handle must have one of these
127 registered. All protocols should be registered BEFORE attempting to actually
128 establish a PPP session to ensure that each protocol can be used in the
129 setup of the connection as needed.
130
131 \headerfile ppp/ppp.h
132*/
133typedef struct ppp_proto {
134 /** \brief Protocol list entry (not a function!). */
135 TAILQ_ENTRY(ppp_proto) entry;
136
137 /** \brief Protocol name ("lcp", "pap", etc). */
138 const char *name;
139
140 /** \brief Protocol code. */
141 uint16_t code;
142
143 /** \brief Private data (if any). */
144 void *privdata;
145
146 /** \brief Initialization function.
147
148 \param self The protocol structure for this protocol.
149 \return 0 on success, <0 on failure.
150 \note Set to NULL if this is not needed in the protocol.
151 */
152 int (*init)(struct ppp_proto *self);
153
154 /** \brief Shutdown function.
155
156 This function should perform any protocol-specific shutdown actions and
157 unregister the protocol from the PPP protocol list.
158
159 \param self The protocol structure for this protocol.
160 \return 0 on success, <0 on failure.
161 */
162 int (*shutdown)(struct ppp_proto *self);
163
164 /** \brief Protocol packet input function.
165
166 This function will be called for each packet delivered to the specified
167 protocol.
168
169 \param self The protocol structure for this protocol.
170 \param pkt The packet being delivered.
171 \param len The length of the packet in bytes.
172 \return 0 on success, <0 on failure.
173 */
174 int (*input)(struct ppp_proto *self, const uint8_t *buf, size_t len);
175
176 /** \brief Notify the protocol of a PPP phase change.
177
178 This function will be called by the PPP automaton any time that a phase
179 change is initiated. This is often used for starting up a protocol when
180 appropriate to do so (for instance, LCP uses this to begin negotiating
181 configuration options with the peer when the establish phase is entered
182 by the automaton).
183
184 \param self The protocol structure for this protocol.
185 \param oldp The old phase (the one the automaton is leaving).
186 \param newp The new phase.
187 \see ppp_phases
188 */
189 void (*enter_phase)(struct ppp_proto *self, int oldp, int newp);
190
191 /** \brief Check timeouts for resending packets.
192
193 This function will be called periodically to allow the protocol to check
194 any resend timers that it might have responsibility for.
195
196 \param self The protocol structure for this protocol.
197 \param tm The current system time for checking timeouts
198 against (in milliseconds since system startup).
199 */
200 void (*check_timeouts)(struct ppp_proto *self, uint64_t tm);
202
203/** \brief Static initializer for protocol list entry.
204 \ingroup networking_ppp
205 */
206#define PPP_PROTO_ENTRY_INIT { NULL, NULL }
207
208/** \defgroup ppp_phases Automaton Phases
209 \brief PPP automaton phases
210 \ingroup networking_ppp
211
212 This list defines the phases of the PPP automaton, as described in Section
213 3.2 of RFC 1661.
214
215 @{
216*/
217#define PPP_PHASE_DEAD 0x01 /**< \brief Pre-connection. */
218#define PPP_PHASE_ESTABLISH 0x02 /**< \brief Establishing connection. */
219#define PPP_PHASE_AUTHENTICATE 0x03 /**< \brief Authentication to peer. */
220#define PPP_PHASE_NETWORK 0x04 /**< \brief Established and working. */
221#define PPP_PHASE_TERMINATE 0x05 /**< \brief Tearing down the link. */
222/** @} */
223
224/** \brief Set the device used to do PPP communications.
225 \ingroup networking_ppp
226
227 This function sets the device that further communications over a
228 point-to-point link will take place over. The device need not be ready to
229 communicate immediately upon calling this function.
230
231 Unless you are adding support for a new device, you will probably never have
232 to call this function. For instance, if you want to use the Dreamcast serial
233 port to establish a link, the ppp_scif_init() function will call this for
234 you.
235
236 \warning Calling this function after establishing a PPP link will
237 fail.
238
239 \param dev The device to use for communication.
240
241 \return 0 on success, <0 on failure.
242*/
244
245/** \brief Set the login credentials used to authenticate to the peer.
246 \ingroup networking_ppp
247
248 This function sets the login credentials that will be used to authenticate
249 to the peer, if the peer requests authentication. The specifics of how the
250 authentication takes place depends on what options are configured when
251 establishing the link.
252
253 \warning Calling this function after establishing a PPP link will
254 fail.
255
256 \param username The username to authenticate as.
257 \param password The password to use to authenticate.
258
259 \return 0 on success, <0 on failure.
260*/
261int ppp_set_login(const char *username, const char *password);
262
263/** \brief Send a packet on the PPP link.
264 \ingroup networking_ppp
265
266 This function sends a single packet to the peer on the PPP link. Generally,
267 you should not use this function directly, but rather use the facilities
268 provided by KOS' network stack.
269
270 \param data The packet to send.
271 \param len The length of the packet, in bytes.
272 \param proto The PPP protocol number for the packet.
273
274 \return 0 on success, <0 on failure.
275*/
276int ppp_send(const uint8_t *data, size_t len, uint16_t proto);
277
278/** \brief Register a protocol with the PPP stack.
279 \ingroup networking_ppp
280
281 This function adds a new protocol to the PPP stack, allowing the stack to
282 communicate packets for the given protocol across the link. Generally, you
283 should not have any reason to call this function, as the library includes
284 a set of protocols to do normal communications.
285
286 \param hnd A protocol handler structure.
287
288 \return 0 on success, <0 on failure.
289*/
291
292/** \brief Unregister a protocol from the PPP stack.
293 \ingroup networking_ppp
294
295 This function removes protocol from the PPP stack. This should be done at
296 shutdown time of any protocols added with ppp_add_protocol().
297
298 \param hnd A protocol handler structure.
299
300 \return 0 on success, <0 on failure.
301*/
303
304/** \brief Send a Protocol Reject packet on the link.
305 \ingroup networking_ppp
306
307 This function sends a LCP protocol reject packet on the link for the
308 specified packet. Generally, you should not have to call this function, as
309 the library will handle doing so internally.
310
311 \param proto The PPP protocol number of the invalid packet.
312 \param pkt The packet itself.
313 \param len The length of the packet, in bytes.
314
315 \return 0 on success, <0 on failure.
316*/
317int ppp_lcp_send_proto_reject(uint16_t proto, const uint8_t *pkt, size_t len);
318
319/** \defgroup ppp_flags Configuration Flags
320 \brief PPP link configuration flags
321 \ingroup networking_ppp
322
323 This list defines the flags we can negotiate during link establishment.
324
325 @{
326*/
327#define PPP_FLAG_AUTH_PAP 0x00000001 /**< \brief PAP authentication */
328#define PPP_FLAG_AUTH_CHAP 0x00000002 /**< \brief CHAP authentication */
329#define PPP_FLAG_PCOMP 0x00000004 /**< \brief Protocol compression */
330#define PPP_FLAG_ACCOMP 0x00000008 /**< \brief Addr/ctrl compression */
331#define PPP_FLAG_MAGIC_NUMBER 0x00000010 /**< \brief Use magic numbers */
332#define PPP_FLAG_WANT_MRU 0x00000020 /**< \brief Specify MRU */
333#define PPP_FLAG_NO_ACCM 0x00000040 /**< \brief No ctl character map */
334/** @} */
335
336/** \brief Get the flags set for our side of the link.
337 \ingroup networking_ppp
338
339 This function retrieves the connection flags set for our side of the PPP
340 link. Before link establishment, this indicates the flags we would like to
341 establish on the link and after establishment it represents the flags that
342 were actually negotiated during link establishment.
343
344 \return Bitwise OR of \ref ppp_flags.
345*/
346uint32_t ppp_get_flags(void);
347
348/** \brief Get the flags set for the peer's side of the link.
349 \ingroup networking_ppp
350
351 This function retrieves the connection flags set for the other side of the
352 PPP link. This value is only valid after link establishment.
353
354 \return Bitwise OR of \ref ppp_flags.
355*/
356uint32_t ppp_get_peer_flags(void);
357
358/** \brief Set the flags set for our side of the link.
359 \ingroup networking_ppp
360
361 \param flags Bitwise combination of \ref ppp_flags.
362
363 This function sets the connection flags for our side of the PPP link.
364*/
365void ppp_set_flags(uint32_t flags);
366
367/** \brief Establish a point-to-point link across a previously set-up device.
368 \ingroup networking_ppp
369
370 This function establishes a point-to-point link to the peer across a device
371 that was previously set up with ppp_set_device(). Before calling this
372 function, the device must be ready to communicate with the peer. That is to
373 say, any handshaking needed to establish the underlying hardware link must
374 have already completed.
375
376 \return 0 on success, <0 on failure.
377
378 \note This function will block until the link is established.
379*/
380int ppp_connect(void);
381
382/** \brief Initialize the Dreamcast serial port for a PPP link.
383 \ingroup networking_ppp
384
385 This function sets up the Dreamcast serial port to act as a communications
386 link for a point-to-point connection. This can be used in conjunction with a
387 coder's cable (or similar) to connect the Dreamcast to a PC and the internet
388 if the target system is set up properly.
389
390 \param bps The speed to initialize the serial port at.
391
392 \return 0 on success, <0 on failure.
393*/
394int ppp_scif_init(int bps);
395
396/** \brief Initialize the Dreamcast modem for a PPP link.
397 \ingroup networking_ppp
398
399 This function sets up the Dreamcast modem to act as a communications
400 link for a point-to-point connection. This includes dialing the specified
401 phone number and establishing the low-level link.
402
403 \param number The phone number to dial out.
404 \param blind Non-zero to blind dial (don't wait for a dial tone).
405 \param conn_rate Storage for the connection rate, in bits per second. Set
406 to NULL if you do not need this value back from the
407 function.
408
409 \retval 0 On success.
410 \retval -1 If modem initialization fails.
411 \retval -2 If not using blind dial and no dial tone is detected
412 within 5 seconds of opening the line.
413 \retval -3 If dialing the modem fails.
414 \retval -4 If a connection is not established in 60 seconds after
415 dialing.
416*/
417int ppp_modem_init(const char *number, int blind, int *conn_rate);
418
419/** \brief Initialize the PPP library.
420 \ingroup networking_ppp
421
422 This function initializes the PPP library, preparing internal structures for
423 use and initializing the PPP protocols needed for normal IP communications.
424
425 \return 0 on success, <0 on failure.
426*/
427int ppp_init(void);
428
429/** \brief Shut down the PPP library.
430 \ingroup networking_ppp
431
432 This function cleans up the PPP library, shutting down any connections and
433 deinitializing any protocols that have bene registered previously.
434
435 \return 0 on success, <0 on failure.
436*/
437int ppp_shutdown(void);
438
439__END_DECLS
440#endif /* !__PPP_PPP_H */
int ppp_lcp_send_proto_reject(uint16_t proto, const uint8_t *pkt, size_t len)
Send a Protocol Reject packet on the link.
uint32_t ppp_get_peer_flags(void)
Get the flags set for the peer's side of the link.
int ppp_connect(void)
Establish a point-to-point link across a previously set-up device.
int ppp_del_protocol(ppp_protocol_t *hnd)
Unregister a protocol from the PPP stack.
int ppp_set_login(const char *username, const char *password)
Set the login credentials used to authenticate to the peer.
void ppp_set_flags(uint32_t flags)
Set the flags set for our side of the link.
int ppp_shutdown(void)
Shut down the PPP library.
int ppp_init(void)
Initialize the PPP library.
int ppp_send(const uint8_t *data, size_t len, uint16_t proto)
Send a packet on the PPP link.
uint32_t ppp_get_flags(void)
Get the flags set for our side of the link.
int ppp_scif_init(int bps)
Initialize the Dreamcast serial port for a PPP link.
int ppp_set_device(ppp_device_t *dev)
Set the device used to do PPP communications.
int ppp_modem_init(const char *number, int blind, int *conn_rate)
Initialize the Dreamcast modem for a PPP link.
int ppp_add_protocol(ppp_protocol_t *hnd)
Register a protocol with the PPP stack.
int shutdown(int socket, int how)
Shutdown socket send and receive operations.
PPP device structure.
Definition ppp.h:52
int index
Unit index (starts at zero and counts upwards for multiple network devices of the same type).
Definition ppp.h:61
void * privdata
Private, device-specific data.
Definition ppp.h:71
uint32_t flags
Device flags.
Definition ppp.h:66
const char * descr
Long description of the device.
Definition ppp.h:57
const char * name
Device name ("modem", "scif", etc).
Definition ppp.h:54
PPP Protocol structure.
Definition ppp.h:133
const char * name
Protocol name ("lcp", "pap", etc).
Definition ppp.h:138
void * privdata
Private data (if any).
Definition ppp.h:144
TAILQ_ENTRY(ppp_proto) entry
Protocol list entry (not a function!).
uint16_t code
Protocol code.
Definition ppp.h:141