home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Utilities / Partition Logic 0.68 / partlogic-0.68.iso / system / headers / sys / network.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-10  |  6.4 KB  |  213 lines

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