File: Socket.c

package info (click to toggle)
mlton 20100608-5.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,628 kB
  • ctags: 70,047
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (211 lines) | stat: -rw-r--r-- 5,599 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "platform.h"

C_Errno_t(C_Int_t) Socket_accept (C_Sock_t s, Array(Word8_t) addr, Ref(C_Socklen_t) addrlen) {
  int out;
  
  MLton_initSockets ();
  out = accept (s, (struct sockaddr*)addr, (socklen_t*)addrlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) Socket_bind (C_Sock_t s, Vector(Word8_t) addr, C_Socklen_t addrlen) {
  int out;
  
  MLton_initSockets ();
  out = bind (s, (const struct sockaddr*)addr, (socklen_t)addrlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) Socket_close(C_Sock_t s) {
#ifdef __MINGW32__
  int out;
  
  MLton_initSockets ();
  out = closesocket(s);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
#else
  return close(s);
#endif
}

C_Errno_t(C_Int_t) Socket_connect (C_Sock_t s, Vector(Word8_t) addr, C_Socklen_t addrlen) {
  int out;
  
  MLton_initSockets ();
  out = connect (s, (const struct sockaddr*)addr, (socklen_t)addrlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Int_t Socket_familyOfAddr(Vector(Word8_t) addr) {
  return ((const struct sockaddr*)addr)->sa_family;
}

C_Errno_t(C_Int_t) Socket_listen (C_Sock_t s, C_Int_t backlog) {
  int out;
  
  MLton_initSockets ();
  out = listen (s, backlog);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_SSize_t) 
Socket_recv (C_Sock_t s, Array(Word8_t) msg, 
             C_Int_t start, C_Size_t len, C_Int_t flags) {
  int out;
  
  MLton_initSockets ();
  out = MLton_recv (s, (void*)((char *)msg + start), len, flags);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_SSize_t) 
Socket_recvFrom (C_Sock_t s, Array(Word8_t) msg, 
                 C_Int_t start, C_Size_t len, C_Int_t flags,
                 Array(Word8_t) addr, Ref(C_Socklen_t) addrlen) {
  int out;
  
  MLton_initSockets ();
  out = MLton_recvfrom (s, (void*)((char *)msg + start), len, flags,
                        (struct sockaddr*)addr, (socklen_t*)addrlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

static inline C_Errno_t(C_SSize_t)
Socket_send (C_Sock_t s, Pointer msg, 
             C_Int_t start, C_Size_t len, C_Int_t flags) {
  int out;
  
  MLton_initSockets ();
  out = send (s, (void*)((char *)msg + start), len, flags);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_SSize_t)
Socket_sendArr (C_Sock_t s, Array(Word8_t) msg, 
                C_Int_t start, C_Size_t len, C_Int_t flags) {
  return Socket_send (s, (Pointer)msg, start, len, flags);
}
C_Errno_t(C_SSize_t)
Socket_sendVec (C_Sock_t s, Vector(Word8_t) msg, 
                C_Int_t start, C_Size_t len, C_Int_t flags) {
  return Socket_send (s, (Pointer)msg, start, len, flags);
}

static inline C_Errno_t(C_SSize_t) 
Socket_sendTo (C_Sock_t s, Pointer msg, 
               C_Int_t start, C_Size_t len, C_Int_t flags,
               Vector(Word8_t) addr, C_Socklen_t addrlen) {
  int out;
  
  MLton_initSockets ();
  out = sendto (s, (void*)((char *)msg + start), len, flags,
                (const struct sockaddr*)addr, (socklen_t)addrlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_SSize_t) 
Socket_sendArrTo (C_Sock_t s, Array(Word8_t) msg, 
                  C_Int_t start, C_Size_t len, C_Int_t flags,
                  Vector(Word8_t) addr, C_Socklen_t addrlen) {
  return Socket_sendTo (s, (Pointer)msg, start, len, flags, addr, addrlen);
}
C_Errno_t(C_SSize_t) 
Socket_sendVecTo (C_Sock_t s, Vector(Word8_t) msg, 
                  C_Int_t start, C_Size_t len, C_Int_t flags,
                  Vector(Word8_t) addr, C_Socklen_t addrlen) {
  return Socket_sendTo (s, (Pointer)msg, start, len, flags, addr, addrlen);
}

C_Errno_t(C_Int_t) Socket_shutdown (C_Sock_t s, C_Int_t how) {
  int out;
  
  MLton_initSockets ();
  out = shutdown (s, how);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) 
Socket_Ctl_getSockOpt (C_Sock_t s, C_Int_t level, C_Int_t optname, 
                       Array(Word8_t) optval, Ref(C_Socklen_t) optlen) {
  int out;
  
  MLton_initSockets ();
  out = getsockopt (s, level, optname, (void*)optval, (socklen_t*)optlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t)
Socket_Ctl_setSockOpt (C_Sock_t s, C_Int_t level, C_Int_t optname, 
                       Vector(Word8_t) optval, C_Socklen_t optlen) {
  int out;
  
  MLton_initSockets ();
  out = setsockopt (s, level, optname, (const void*)optval, (socklen_t)optlen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) 
Socket_Ctl_getIOCtl (C_Sock_t s, C_Int_t request, Array(Word8_t) argp) {
  int out;
  
  MLton_initSockets ();
  out = ioctl (s, request, (void*)argp);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) 
Socket_Ctl_setIOCtl (C_Sock_t s, C_Int_t request, Vector(Word8_t) argp) {
  int out;
  
  MLton_initSockets ();
  out = ioctl (s, request, (const void*)argp);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) Socket_Ctl_getPeerName (C_Sock_t s, Array(Word8_t) name, Ref(C_Socklen_t) namelen) {
  int out;
  
  MLton_initSockets ();
  out = getpeername (s, (struct sockaddr*)name, (socklen_t*)namelen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}

C_Errno_t(C_Int_t) Socket_Ctl_getSockName (C_Sock_t s, Array(Word8_t) name, Ref(C_Socklen_t) namelen) {
  int out;
  
  MLton_initSockets ();
  out = getsockname (s, (struct sockaddr*)name, (socklen_t*)namelen);
  if (out == -1) MLton_fixSocketErrno ();
  
  return out;
}