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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [redboot/] [current/] [include/] [net/] [net.h] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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