OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [redboot/] [v2_0/] [include/] [net/] [net.h] - Blame information for rev 449

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      net/net.h
4
//
5
//      Stand-alone networking support for RedBoot
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    gthomas
45
// Contributors: gthomas
46
// Date:         2000-07-14
47
// Purpose:      
48
// Description:  
49
//              
50
// This code is part of RedBoot (tm).
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
#ifndef _NET_H_
57
#define _NET_H_
58
 
59
#include <pkgconf/system.h>
60
#include <pkgconf/redboot.h>
61
#include <cyg/hal/hal_arch.h>
62
#include <cyg/hal/basetype.h>
63
#include <string.h>
64
 
65
extern bool net_debug;
66
#ifdef CYGPKG_IO_ETH_DRIVERS
67
#  include <pkgconf/io_eth_drivers.h>
68
# ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
69
extern int cyg_io_eth_net_debug;
70
# endif
71
#endif
72
 
73
extern unsigned long do_ms_tick(void);
74
extern unsigned long get_ms_ticks(void);
75
#define MS_TICKS() get_ms_ticks()
76
#define MS_TICKS_DELAY() do_ms_tick()
77
 
78
/* #define NET_SUPPORT_RARP  1 */
79
#define NET_SUPPORT_ICMP 1
80
#define NET_SUPPORT_UDP  1
81
#define NET_SUPPORT_TCP  1
82
 
83
#if (CYG_BYTEORDER == CYG_LSBFIRST)
84
#ifndef __LITTLE_ENDIAN__
85
#define __LITTLE_ENDIAN__
86
#endif
87
extern unsigned long  ntohl(unsigned long x);
88
extern unsigned long  ntohs(unsigned short x);
89
#else
90
#define ntohl(x)        (x)
91
#define ntohs(x)        (x)
92
#endif
93
 
94
#define htonl(x)        ntohl(x)
95
#define htons(x)        ntohs(x)
96
 
97
/*
98
 * Minimum ethernet packet length.
99
 */
100
#define ETH_MIN_PKTLEN  60
101
#define ETH_MAX_PKTLEN  (1540-14)
102
#define ETH_HDR_SIZE    14
103
 
104
typedef unsigned char enet_addr_t[6];
105
typedef unsigned char ip_addr_t[4];
106
 
107
typedef unsigned char  octet;
108
typedef unsigned short word;
109
typedef unsigned int   dword;
110
 
111
#ifndef NULL
112
#define NULL 0
113
#endif
114
 
115
// IPv4 support
116
typedef struct in_addr {
117
    unsigned long  s_addr;  // IPv4 address
118
} in_addr_t;
119
 
120
// Socket/connection information
121
struct sockaddr_in {
122
    struct in_addr sin_addr;
123
    unsigned short sin_port;
124
    unsigned short sin_family;
125
    short          sin_len;
126
};
127
#define AF_INET      1
128
#define INADDR_ANY   0
129
 
130
struct timeval {
131
    unsigned long tv_sec;
132
    unsigned long tv_usec;
133
};
134
 
135
/*
136
 * Simple timer support structure.
137
 */
138
typedef void (*tmr_handler_t)(void *user_data);
139
 
140
/*
141
 * Timer structure.
142
 * When expiration time is met or exceeded, the handler is
143
 * called and the timer struct is removed from the list.
144
 */
145
typedef struct _timer {
146
    struct _timer *next;        /* next timer in list */
147
    unsigned long delay;        /* expiration time relative to start time */
148
    unsigned long start;        /* when the timer was set */
149
    tmr_handler_t handler;      /* user procedure to call when timer 'fires' */
150
    void          *user_data;   /* user pointer passed to above procedure */
151
} timer_t;
152
 
153
 
154
/*
155
 * Ethernet header.
156
 */
157
typedef struct {
158
    enet_addr_t   destination;
159
    enet_addr_t   source;
160
    word          type;
161
#define ETH_TYPE_IP   0x800
162
#define ETH_TYPE_ARP  0x806
163
#define ETH_TYPE_RARP 0x8053
164
} eth_header_t;
165
 
166
 
167
/*
168
 * ARP/RARP header.
169
 */
170
typedef struct {
171
    word        hw_type;
172
#define ARP_HW_ETHER 1
173
#define ARP_HW_EXP_ETHER 2
174
    word        protocol;
175
    octet       hw_len;
176
    octet       proto_len;
177
    word        opcode;
178
#define ARP_REQUEST     1
179
#define ARP_REPLY       2
180
#define RARP_REQUEST    3
181
#define RARP_REPLY      4
182
    enet_addr_t sender_enet;
183
    ip_addr_t   sender_ip;
184
    enet_addr_t target_enet;
185
    ip_addr_t   target_ip;
186
} arp_header_t;
187
 
188
 
189
#define ARP_PKT_SIZE  (sizeof(arp_header_t) + ETH_HDR_SIZE)
190
 
191
/*
192
 * Internet Protocol header.
193
 */
194
typedef struct {
195
#ifdef __LITTLE_ENDIAN__
196
    octet       hdr_len:4,
197
                version:4;
198
#else
199
    octet       version:4,
200
                hdr_len:4;
201
#endif
202
    octet       tos;
203
    word        length;
204
    word        ident;
205
    word        fragment;
206
    octet       ttl;
207
    octet       protocol;
208
#define IP_PROTO_ICMP  1
209
#define IP_PROTO_TCP   6
210
#define IP_PROTO_UDP  17
211
    word        checksum;
212
    ip_addr_t   source;
213
    ip_addr_t   destination;
214
} ip_header_t;
215
 
216
 
217
#define IP_PKT_SIZE (60 + ETH_HDR_SIZE)
218
 
219
 
220
/*
221
 * A IP<->ethernet address mapping.
222
 */
223
typedef struct {
224
    ip_addr_t    ip_addr;
225
    enet_addr_t  enet_addr;
226
} ip_route_t;
227
 
228
 
229
/*
230
 * UDP header.
231
 */
232
typedef struct {
233
    word        src_port;
234
    word        dest_port;
235
    word        length;
236
    word        checksum;
237
} udp_header_t;
238
 
239
 
240
/*
241
 * TCP header.
242
 */
243
typedef struct {
244
    word        src_port;
245
    word        dest_port;
246
    dword       seqnum;
247
    dword       acknum;
248
#ifdef __LITTLE_ENDIAN__
249
    octet       reserved:4,
250
                hdr_len:4;
251
#else
252
    octet       hdr_len:4,
253
                reserved:4;
254
#endif
255
    octet       flags;
256
#define TCP_FLAG_FIN  1
257
#define TCP_FLAG_SYN  2
258
#define TCP_FLAG_RST  4
259
#define TCP_FLAG_PSH  8
260
#define TCP_FLAG_ACK 16
261
#define TCP_FLAG_URG 32
262
    word        window;
263
    word        checksum;
264
    word        urgent;
265
} tcp_header_t;
266
 
267
 
268
/*
269
 * ICMP header.
270
 */
271
typedef struct {
272
    octet       type;
273
#define ICMP_TYPE_ECHOREPLY   0
274
#define ICMP_TYPE_ECHOREQUEST 8
275
    octet       code;
276
    word        checksum;
277
    word        ident;
278
    word        seqnum;
279
} icmp_header_t;
280
 
281
typedef struct _pktbuf {
282
    struct _pktbuf *next;
283
    union {
284
        ip_header_t *__iphdr;           /* pointer to IP header */
285
        arp_header_t *__arphdr;         /* pointer to ARP header */
286
    } u1;
287
#define ip_hdr u1.__iphdr
288
#define arp_hdr u1.__arphdr
289
    union {
290
        udp_header_t *__udphdr;         /* pointer to UDP header */
291
        tcp_header_t *__tcphdr;         /* pointer to TCP header */
292
        icmp_header_t *__icmphdr;       /* pointer to ICMP header */
293
    } u2;
294
#define udp_hdr u2.__udphdr
295
#define tcp_hdr u2.__tcphdr
296
#define icmp_hdr u2.__icmphdr
297
    word        pkt_bytes;              /* number of data bytes in buf */
298
    word        bufsize;                /* size of buf */
299
    word        *buf;
300
} pktbuf_t;
301
 
302
 
303
/* protocol handler */
304
typedef void (*pkt_handler_t)(pktbuf_t *pkt, eth_header_t *eth_hdr);
305
 
306
/* ICMP fielder */
307
typedef void (*icmp_handler_t)(pktbuf_t *pkt, ip_route_t *src_route);
308
 
309
typedef struct _udp_socket {
310
    struct _udp_socket  *next;
311
    word                our_port;
312
    word                pad;
313
    void                (*handler)(struct _udp_socket *skt, char *buf, int len,
314
                                   ip_route_t *src_route, word src_port);
315
} udp_socket_t;
316
 
317
 
318
typedef void (*udp_handler_t)(udp_socket_t *skt, char *buf, int len,
319
                              ip_route_t *src_route, word src_port);
320
 
321
 
322
typedef struct _tcp_socket {
323
    struct _tcp_socket *next;
324
    int                state;       /* connection state */
325
#define _CLOSED      0
326
#define _LISTEN      1
327
#define _SYN_RCVD    2
328
#define _SYN_SENT    3
329
#define _ESTABLISHED 4
330
#define _CLOSE_WAIT  5
331
#define _LAST_ACK    6
332
#define _FIN_WAIT_1  7
333
#define _FIN_WAIT_2  8
334
#define _CLOSING     9
335
#define _TIME_WAIT  10
336
    ip_route_t         his_addr;    /* address of other end of connection */
337
    word               our_port;
338
    word               his_port;
339
    word               data_bytes;   /* number of data bytes in pkt */
340
    char               reuse;        /* SO_REUSEADDR, no 2MSL */
341
    timer_t            timer;
342
    pktbuf_t           pkt;         /* dedicated xmit packet */
343
    pktbuf_t           *rxlist;     /* list of unread incoming data packets */
344
    char               *rxptr;      /* pointer to next byte to read */
345
    int                rxcnt;       /* bytes left in current read packet */
346
    dword              ack;
347
    dword              seq;
348
    char               pktbuf[ETH_MAX_PKTLEN];
349
} tcp_socket_t;
350
 
351
/*
352
 * Our address.
353
 */
354
extern enet_addr_t __local_enet_addr;
355
extern ip_addr_t   __local_ip_addr;
356
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
357
extern ip_addr_t   __local_ip_gate;
358
extern ip_addr_t   __local_ip_mask;
359
#endif
360
 
361
/*
362
 * Set a timer. Caller is responsible for providing the timer_t struct.
363
 */
364
extern void __timer_set(timer_t *t, unsigned long delay,
365
                        tmr_handler_t handler, void *user_data);
366
 
367
/*
368
 * Cancel the given timer.
369
 */
370
extern void __timer_cancel(timer_t *t);
371
 
372
/*
373
 * Poll timer list for timer expirations.
374
 */
375
extern void __timer_poll(void);
376
 
377
/*
378
 * Initialize the free list.
379
 */
380
extern void __pktbuf_init(void);
381
 
382
/*
383
 * simple pktbuf allocation.
384
 * allocates at least nbytes for packet data including ethernet header.
385
 */
386
extern pktbuf_t *__pktbuf_alloc(int nbytes);
387
 
388
/*
389
 * return a pktbuf for reuse.
390
 */
391
extern void __pktbuf_free(pktbuf_t *pkt);
392
 
393
/*
394
 * Dump packet structures (debug, in case of running out)
395
 */
396
extern void __pktbuf_dump(void);
397
 
398
 
399
/*
400
 * Install handlers for ethernet packets.
401
 * Returns old handler.
402
 */
403
extern pkt_handler_t __eth_install_listener(int eth_type,
404
                                            pkt_handler_t handler);
405
extern void __eth_remove_listener(int eth_type);
406
 
407
/*
408
 * Non-blocking poll of ethernet link. Processes all pending
409
 * input packets.
410
 */
411
extern void __enet_poll(void);
412
 
413
/*
414
 * Send an ethernet packet.
415
 */
416
extern void __enet_send(pktbuf_t *pkt, enet_addr_t *dest, int eth_type);
417
 
418
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
419
/*
420
 * return true if addr is on local subnet
421
 */
422
extern int __ip_addr_local(ip_addr_t *addr);
423
#endif
424
 
425
/*
426
 * Handle incoming ARP packets.
427
 */
428
extern void __arp_handler(pktbuf_t *pkt);
429
 
430
/*
431
 * Find the ethernet address of the machine with the given
432
 * ip address.
433
 * Return true and fills in 'eth_addr' if successful, false
434
 * if unsuccessful.
435
 */
436
extern int __arp_request(ip_addr_t *ip_addr, enet_addr_t *eth_addr);
437
 
438
/*
439
 * Lookup an address from the local ARP cache.  If not found,
440
 * then call 'arp_request' to find it.  [Basically just a cached
441
 * version of 'arp_request']
442
 */
443
extern int __arp_lookup(ip_addr_t *host, ip_route_t *rt);
444
 
445
/*
446
 * Do a one's complement checksum.
447
 * The data being checksum'd is in network byte order.
448
 * The returned checksum is in network byte order.
449
 */
450
extern unsigned short __sum(word *w, int len, int init_sum);
451
 
452
/*
453
 * Compute a partial checksum for the UDP/TCP pseudo header.
454
 */
455
extern int __pseudo_sum(ip_header_t *ip);
456
 
457
/*
458
 * Handle IP packets coming from the polled ethernet interface.
459
 */
460
extern void __ip_handler(pktbuf_t *pkt, enet_addr_t *src_enet_addr);
461
 
462
/*
463
 * Send an IP packet.
464
 *
465
 * The IP data field should contain pkt->pkt_bytes of data.
466
 * pkt->[udp|tcp|icmp]_hdr points to the IP data field. Any
467
 * IP options are assumed to be already in place in the IP
468
 * options field.  Returns 0 for success.
469
 */
470
extern int __ip_send(pktbuf_t *pkt, int protocol, ip_route_t *dest);
471
 
472
/*
473
 * Abort connection.
474
 */
475
extern void __tcp_abort(tcp_socket_t *s, unsigned long delay);
476
 
477
/*
478
 * Handle incoming ICMP packets.
479
 */
480
extern void __icmp_handler(pktbuf_t *pkt, ip_route_t *r);
481
extern int  __icmp_install_listener(icmp_handler_t handler);
482
extern void __icmp_remove_listener(void);
483
 
484
/*
485
 * Handle incoming UDP packets.
486
 */
487
extern void __udp_handler(pktbuf_t *pkt, ip_route_t *r);
488
 
489
/*
490
 * Install a handler for incoming udp packets.
491
 * Caller provides the udp_socket_t structure.
492
 * Returns zero if successful, -1 if socket is already used.
493
 */
494
extern int __udp_install_listener(udp_socket_t *s, word port,
495
                                  udp_handler_t handler);
496
 
497
/*
498
 * Remove the handler for the given socket.
499
 */
500
extern void __udp_remove_listener(word port);
501
 
502
/*
503
 * Send a UDP packet.
504
 */
505
extern int __udp_send(char *buf, int len, ip_route_t *dest_ip,
506
                       word dest_port, word src_port);
507
 
508
// Send a UDP packet
509
extern int __udp_sendto(char *buf, int len,
510
                        struct sockaddr_in *server, struct sockaddr_in *local);
511
 
512
// Receive a UDP packet
513
extern int __udp_recvfrom(char *buf, int len,
514
                          struct sockaddr_in *from, struct sockaddr_in *local,
515
                          struct timeval *timeout);
516
 
517
/*
518
 * TCP poll function. Should be called as often as possible.
519
 */
520
extern void __tcp_poll(void);
521
 
522
/*
523
 * Initiate outgoing connection, waiting for at most timeout seconds.
524
 */
525
extern int __tcp_open(tcp_socket_t *s, struct sockaddr_in *host,
526
                      word port, int timeout, int *err);
527
 
528
/*
529
 * Set up a listening socket on the given port.
530
 * Does not block.
531
 */
532
extern int __tcp_listen(tcp_socket_t *s, word port);
533
 
534
/*
535
 * SO_REUSEADDR, no 2MSL.
536
 */
537
extern void __tcp_so_reuseaddr(tcp_socket_t *s);
538
 
539
/*
540
 * Block while waiting for all outstanding socket data to
541
 * be transmitted.
542
 */
543
extern void __tcp_drain(tcp_socket_t *s);
544
 
545
/*
546
 * Initiate connection close.
547
 */
548
extern void __tcp_close(tcp_socket_t *s);
549
 
550
/*
551
 * Wait until connection has fully closed.
552
 */
553
extern void __tcp_close_wait(tcp_socket_t *s);
554
 
555
/*
556
 * Read up to 'len' bytes without blocking.
557
 * Returns number of bytes read.
558
 * If connection is closed, returns -1.
559
 */
560
extern int __tcp_read(tcp_socket_t *s, char *buf, int len);
561
 
562
/*
563
 * Write up to 'len' bytes without blocking.
564
 * Returns number of bytes written.
565
 * If connection is closed, returns -1.
566
 */
567
extern int __tcp_write(tcp_socket_t *s, char *buf, int len);
568
 
569
/*
570
 * Write up to 'len' bytes, blocking until sent (not ACK'd).
571
 * Returns number of bytes written.
572
 * If connection is closed, returns -1.
573
 */
574
extern int __tcp_write_block(tcp_socket_t *s, char *buf, int len);
575
 
576
 
577
/*
578
 * The following are a higher-level tcp socket interface.
579
 */
580
 
581
/*
582
 * Initialize a socket for given port.
583
 */
584
extern void __skt_init(tcp_socket_t *s, unsigned short port);
585
 
586
/*
587
 * Return true if socket connection is closed.
588
 */
589
#define __skt_is_closed(s) (((tcp_socket_t *)(s))->state == _CLOSED)
590
 
591
/*
592
 * Block while listening for an incoming connection.
593
 */
594
extern void __skt_wait_for_connect(tcp_socket_t *s);
595
 
596
/*
597
 * Read up to 'len' bytes from the given socket.
598
 * Returns number of bytes read.
599
 * Doesn't block.
600
 */
601
extern int __skt_read(tcp_socket_t *s, char *buf, int len);
602
 
603
/*
604
 * Write 'len' bytes to the given socket.
605
 * Returns number of bytes written.
606
 * May not write all data if connection closes.
607
 */
608
extern int __skt_write(tcp_socket_t *s, char *buf, int len);
609
 
610
// Initialize the network stack - logical driver layer, etc.
611
extern void net_init(void);
612
 
613
// Test for new network I/O connections
614
extern void net_io_test(bool is_idle);
615
 
616
// Conversion between IP addresses and printable strings
617
extern bool  inet_aton(const char *, in_addr_t *);
618
extern char *inet_ntoa(in_addr_t *);
619
 
620
// FIXME
621
/* #define NET_SUPPORT_RARP  1 */
622
#define NET_SUPPORT_ICMP 1
623
#define NET_SUPPORT_UDP  1
624
#define NET_SUPPORT_TCP  1
625
 
626
#ifdef BSP_LOG
627
#define BSPLOG(x) { int old_console = start_console(); x; end_console(old_console); }
628
#define bsp_log diag_printf
629
#else
630
#define BSPLOG(x)
631
#endif
632
 
633
#endif // _NET_H_

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.