Subversion Repositories Kolibri OS

Rev

Rev 8522 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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