home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Resources / Partition Logic 0.61 / partlogic-0.61.iso / system / headers / sys / network.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-31  |  6.3 KB  |  212 lines

  1. // 
  2. //  Visopsys
  3. //  Copyright (C) 1998-2006 J. Andrew McLaughlin
  4. //  
  5. //  This library is free software; you can redistribute it and/or modify it
  6. //  under the terms of the GNU Lesser General Public License as published by
  7. //  the Free Software Foundation; either version 2.1 of the License, or (at
  8. //  your option) any later version.
  9. //
  10. //  This library is distributed in the hope that it will be useful, but
  11. //  WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
  13. //  General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU Lesser General Public License
  16. //  along with this library; if not, write to the Free Software Foundation,
  17. //  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. //  network.h
  20. //
  21.  
  22. // This file contains definitions and structures for using network
  23. // functions in Visopsys.
  24.  
  25. #if !defined(_NETWORK_H)
  26.  
  27. #define NETWORK_MAX_HOSTNAMELENGTH      32   
  28. #define NETWORK_MAX_ADAPTERS            16
  29. #define NETWORK_ADAPTER_MAX_NAMELENGTH  32
  30.  
  31. // Flags for network devices
  32. #define NETWORK_ADAPTERFLAG_INITIALIZED  0x8000
  33. #define NETWORK_ADAPTERFLAG_RUNNING      0x4000
  34. #define NETWORK_ADAPTERFLAG_LINK         0x2000
  35. #define NETWORK_ADAPTERFLAG_AUTOCONF     0x1000
  36. #define NETWORK_ADAPTERFLAG_PROMISCUOUS  0x0008
  37. #define NETWORK_ADAPTERFLAG_AUTOSTRIP    0x0004
  38. #define NETWORK_ADAPTERFLAG_AUTOPAD      0x0002
  39. #define NETWORK_ADAPTERFLAG_AUTOCRC      0x0001
  40.  
  41. // Since for now, we only support ethernet at the link layer, max packet
  42. // size is the upper size limit of an ethernet frame.
  43. #define NETWORK_PACKET_MAX_LENGTH       1518
  44. #define NETWORK_MAX_ETHERDATA_LENGTH    1500
  45.  
  46. // Lengths of addresses for protocols
  47. #define NETWORK_ADDRLENGTH_ETHERNET     6
  48. #define NETWORK_ADDRLENGTH_IP           4
  49.  
  50. // Supported protocols.  Link layer protocols:
  51. #define NETWORK_LINKPROTOCOL_ETHERNET   1
  52. // Supported network layer protocols:
  53. #define NETWORK_NETPROTOCOL_IP          1
  54. // Supported top-level (transport layer) protocols, or network layer ones
  55. // that have no corresponding transport protocol.  Where applicable,
  56. // these match the IANA assigned IP protocol numbers:
  57. #define NETWORK_TRANSPROTOCOL_ICMP      1
  58. #define NETWORK_TRANSPROTOCOL_TCP       6
  59. #define NETWORK_TRANSPROTOCOL_UDP       17
  60.  
  61. // TCP/UDP port numbers we care about
  62. #define NETWORK_PORT_BOOTPSERVER        67  // TCP/UDP: BOOTP/DHCP Server
  63. #define NETWORK_PORT_BOOTPCLIENT        68  // TCP/UDP: BOOTP/DHCP Client
  64.  
  65. // Types of network connections, in order of ascending abstraction
  66. #define NETWORK_HEADERS_NONE            0
  67. #define NETWORK_HEADERS_TRANSPORT       1
  68. #define NETWORK_HEADERS_NET             2
  69. #define NETWORK_HEADERS_RAW             3
  70.  
  71. // Mode flags for network connections
  72. #define NETWORK_MODE_LISTEN             0x04
  73. #define NETWORK_MODE_READ               0x02
  74. #define NETWORK_MODE_WRITE              0x01
  75. #define NETWORK_MODE_READWRITE          (NETWORK_MODE_READ | \
  76.                      NETWORK_MODE_WRITE)
  77. // ICMP message types
  78. #define NETWORK_ICMP_ECHOREPLY          0
  79. #define NETWORK_ICMP_DESTUNREACHABLE    3
  80. #define NETWORK_ICMP_SOURCEQUENCH       4
  81. #define NETWORK_ICMP_REDIRECT           5
  82. #define NETWORK_ICMP_ECHO               8
  83. #define NETWORK_ICMP_TIMEEXCEEDED       11
  84. #define NETWORK_ICMP_PARAMPROBLEM       12
  85. #define NETWORK_ICMP_TIMESTAMP          13
  86. #define NETWORK_ICMP_TIMESTAMPREPLY     14
  87. #define NETWORK_ICMP_INFOREQUEST        15
  88. #define NETWORK_ICMP_INFOREPLY          16
  89.  
  90. #define NETWORK_PING_DATASIZE           56
  91.  
  92. #define networkAddressesEqual(addr1, addr2, addrSize)                        \
  93.   ((((addr1)->quad) & (0xFFFFFFFFFFFFFFFFULL >> (8 * (8 - (addrSize))))) ==  \
  94.    (((addr2)->quad) & (0xFFFFFFFFFFFFFFFFULL >> (8 * (8 - (addrSize))))))
  95.  
  96. // Generic 64-bit, byte-addressable network address, logical or physical.
  97. // Actual length is obviously protocol-specific
  98. typedef union {
  99.   unsigned char bytes[8];
  100.   unsigned long long quad;
  101.  
  102. } __attribute__((packed)) networkAddress;
  103.  
  104. // A network adapter device
  105. typedef struct {
  106.   char name[NETWORK_ADAPTER_MAX_NAMELENGTH];
  107.   unsigned flags;
  108.   // Physical network address
  109.   networkAddress hardwareAddress;
  110.   // Logical host address
  111.   networkAddress hostAddress;
  112.   // Net mask
  113.   networkAddress netMask;
  114.   // Broadcast address
  115.   networkAddress broadcastAddress;
  116.   // Gateway address
  117.   networkAddress gatewayAddress;
  118.   // Link protocol
  119.   int linkProtocol;
  120.   // Interrupt lint
  121.   int interruptNum;
  122.   // Queues
  123.   int recvQueued;
  124.   int recvQueueLen;
  125.   int transQueued;
  126.   int transQueueLen;
  127.   // Statistics
  128.   unsigned recvPackets;
  129.   unsigned recvErrors;
  130.   unsigned recvDropped;
  131.   unsigned recvOverruns;
  132.   unsigned transPackets;
  133.   unsigned transErrors;
  134.   unsigned transDropped;
  135.   unsigned transOverruns;
  136.   unsigned collisions;
  137.  
  138. } networkDevice;
  139.  
  140. // This structure is used to filter packets to network connections.
  141. typedef struct {
  142.   int headers;
  143.   int linkProtocol;
  144.   int netProtocol;
  145.   int transProtocol;
  146.   int subProtocol;
  147.   int localPort;
  148.   int remotePort;
  149.  
  150. } networkFilter;
  151.  
  152. // Protocol things
  153.  
  154. typedef struct {
  155.   unsigned char dest[NETWORK_ADDRLENGTH_ETHERNET];
  156.   unsigned char source[NETWORK_ADDRLENGTH_ETHERNET];
  157.   unsigned short type;
  158.  
  159. } __attribute__((packed)) networkEthernetHeader;
  160.  
  161. typedef struct {
  162.   unsigned char versionHeaderLen;
  163.   unsigned char typeOfService;
  164.   unsigned short totalLength;
  165.   unsigned short identification;
  166.   unsigned short flagsFragOffset;
  167.   unsigned char timeToLive;
  168.   unsigned char protocol;
  169.   unsigned short headerChecksum;
  170.   unsigned srcAddress;
  171.   unsigned destAddress;
  172.  
  173. } __attribute__((packed)) networkIpHeader;
  174.  
  175. typedef struct {
  176.   unsigned char type;
  177.   unsigned char code;
  178.   unsigned short checksum;
  179.  
  180. } __attribute__((packed)) networkIcmpHeader;
  181.  
  182. typedef struct {
  183.   unsigned short srcPort;
  184.   unsigned short destPort;
  185.   unsigned sequenceNum;
  186.   unsigned ackNum;
  187.   unsigned short dataOffsetFlags;
  188.   unsigned short window;
  189.   unsigned short checksum;
  190.   unsigned short urgentPointer;
  191.  
  192. } __attribute__((packed)) networkTcpHeader;
  193.  
  194. typedef struct {
  195.   unsigned short srcPort;
  196.   unsigned short destPort;
  197.   unsigned short length;
  198.   unsigned short checksum;
  199.  
  200. } __attribute__((packed)) networkUdpHeader;
  201.  
  202. typedef struct {
  203.   networkIcmpHeader icmpHeader;
  204.   unsigned short identifier;
  205.   unsigned short sequenceNum;
  206.   unsigned char data[NETWORK_PING_DATASIZE];
  207.  
  208. } __attribute__((packed)) networkPingPacket;
  209.  
  210. #define _NETWORK_H
  211. #endif
  212.