Subversion Repositories Kolibri OS

Rev

Rev 8343 | Rev 8537 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #ifndef __SOCKET_H__
  2. #define __SOCKET_H__
  3.  
  4. #include <stddef.h>
  5.  
  6. // Socket Types
  7. #define SOCK_STREAM 1
  8. #define SOCK_DGRAM 2
  9. #define SOCK_RAW 3
  10.  
  11. // IP protocols
  12. #define IPPROTO_IP 0
  13. #define IPPROTO_ICMP 1
  14. #define IPPROTO_TCP 6
  15. #define IPPROTO_UDP 17
  16. #define IPPROTO_RAW 255
  17.  
  18. // IP options
  19. #define IP_TTL 2
  20.  
  21. // Address families
  22. #define AF_UNSPEC 0
  23. #define AF_LOCAL 1
  24. #define AF_INET  2     // Default INET=IPv4
  25. #define AF_INET4 2     // IPv4
  26. #define AF_INET6 10    // IPv6
  27.  
  28. #define PF_UNSPEC AF_UNSPEC
  29. #define PF_LOCAL  AF_LOCAL
  30. #define PF_INET4  AF_INET4
  31. #define PF_INET6  AF_INET6
  32.  
  33. // internal definition
  34. #define AI_SUPPORTED 0x40F
  35.  
  36. // for system function 76
  37. #define API_ETH (0<<16)
  38. #define API_IPv4 (1<<16)
  39. #define API_ICMP (2<<16)
  40. #define API_UDP (3<<16)
  41. #define API_TCP (4<<16)
  42. #define API_ARP (5<<16)
  43. #define API_PPPOE (6<<16)
  44.  
  45. // Socket flags for user calls
  46. #define MSG_NOFLAG 0
  47. #define MSG_PEEK 0x02
  48. #define MSG_DONTWAIT 0x40
  49.  
  50. // Socket levels
  51. #define SOL_SOCKET 0xffff
  52.  
  53. //Socket options
  54. #define SO_BINDTODEVICE (1<<9)
  55. #define SO_NONBLOCK (1<<31)
  56.  
  57. // Error Codes
  58. #define ENOBUFS      1
  59. #define EINPROGRESS  2
  60. #define EOPNOTSUPP   4
  61. #define EWOULDBLOCK  6
  62. #define ENOTCONN     9
  63. #define EALREADY     10
  64. #define EINVALUE     11
  65. #define EMSGSIZE     12
  66. #define ENOMEM       18
  67. #define EADDRINUSE   20
  68. #define ECONNREFUSED 61
  69. #define ECONNRESET   52
  70. #define EISCONN      56
  71. #define ETIMEDOUT    60
  72. #define ECONNABORTED 53
  73.  
  74.  
  75. #define PORT(X) (X<<8)
  76. int err_code;
  77.  
  78. #pragma pack(push,1)
  79. struct sockaddr{
  80.     unsigned short sin_family;
  81.     unsigned short sin_port;
  82.     unsigned int sin_addr;
  83.     unsigned long long sin_zero;
  84. };
  85. #pragma pack(pop)
  86.  
  87. #pragma pack(push,1)
  88. typedef struct{
  89.   unsigned int level;
  90.   unsigned int optionname;
  91.   unsigned int optlenght;
  92.   unsigned char options;
  93. }optstruct;
  94. #pragma pack(pop)
  95.  
  96. static inline int socket(int domain, int type, int protocol)
  97. {
  98.     int socket;
  99.     asm volatile(
  100.     "int $0x40"
  101.     :"=b"(err_code), "=a"(socket)
  102.     :"a"(75), "b"(0), "c"(domain), "d"(type), "S"(protocol)
  103.     );
  104.     return socket;
  105. }
  106.  
  107. static inline int close(int socket)
  108. {
  109.     int status;
  110.     asm volatile(
  111.     "int $0x40"
  112.     :"=b"(err_code), "=a"(status)
  113.     :"a"(75), "b"(1), "c"(socket)
  114.     );
  115.     return status;
  116. }
  117.  
  118. static inline int bind(int socket, const struct sockaddr *addres, int addres_len)
  119. {
  120.     int status;
  121.     asm volatile(
  122.     "int $0x40"
  123.     :"=b"(err_code), "=a"(status)
  124.     :"a"(75), "b"(2), "c"(socket), "d"(addres), "S"(addres_len)
  125.     );
  126.     return status;
  127. }
  128.  
  129. static inline int listen(int socket, int backlog)
  130. {
  131.     int status;
  132.     asm volatile(
  133.     "int $0x40"
  134.     :"=b"(err_code), "=a"(status)
  135.     :"a"(75), "b"(3), "c"(socket), "d"(backlog)
  136.     );
  137.     return status;
  138. }
  139.  
  140. static inline int connect(int socket, const struct sockaddr* address, int socket_len)
  141. {
  142.     int status;
  143.     asm volatile(
  144.     "int $0x40"
  145.     :"=b"(err_code), "=a"(status)
  146.     :"a"(75), "b"(4), "c"(socket), "d"(address), "S"(socket_len)
  147.     );
  148.     return status;
  149. }
  150.  
  151. static inline int accept(int socket, const struct sockaddr *address, int address_len)
  152. {
  153.     int new_socket;
  154.     asm volatile(
  155.     "int $0x40"
  156.     :"=b"(err_code), "=a"(new_socket)
  157.     :"a"(75), "b"(5), "c"(socket), "d"(address), "S"(address_len)
  158.     );
  159.     return new_socket;
  160. }
  161.  
  162. static inline int send(int socket, const void *message, size_t msg_len, int flag)
  163. {
  164.     int status;
  165.     asm volatile(
  166.     "int $0x40"
  167.     :"=b"(err_code), "=a"(status)
  168.     :"a"(75), "b"(6), "c"(socket), "d"(message), "S"(msg_len), "D"(flag)
  169.     );
  170.     return status;
  171. }
  172.  
  173. static inline int recv(int socket, void *buffer, size_t buff_len, int flag)
  174. {
  175.     int status;
  176.     asm volatile(
  177.     "int $0x40"
  178.     :"=b"(err_code), "=a"(status)
  179.     :"a"(75), "b"(7), "c"(socket), "d"(buffer), "S"(buff_len), "D"(flag)
  180.     );
  181.     return status;
  182. }
  183.  
  184. static inline int setsockopt(int socket,const optstruct* opt)
  185. {
  186.     int status;
  187.     asm volatile(
  188.         "int $0x40"
  189.         :"=b"(err_code), "=a"(status)
  190.         :"a"(75), "b"(8), "c"(socket),"d"(opt)
  191.     );
  192.     return status;
  193. }
  194.  
  195. static inline int getsockopt(int socket, optstruct* opt)
  196. {
  197.     int status;
  198.     asm volatile(
  199.         "int $0x40"
  200.         :"=b"(err_code), "=a"(status)
  201.         :"a"(75), "b"(9), "c"(socket),"d"(opt)
  202.     );
  203.     return status;
  204. }
  205.  
  206. static inline int socketpair(int *socket1, int *socket2)
  207. {
  208.     asm volatile(
  209.         "int $0x40"
  210.         :"=b"(*socket2), "=a"(*socket1)
  211.         :"a"(75), "b"(10)
  212.     );
  213.     err_code=*socket2;
  214.     return *socket1;
  215. }
  216. #endif
  217.