summaryrefslogtreecommitdiffstats
path: root/libc/unistd/socketcalls.c
blob: 51bdf5776d5094b4a89e983e6823dde26bf9cd39 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <unistd.h>
#include <sys/socket.h>
#include <sys/linux-syscalls.h>

enum
{
    SYS_SOCKET = 1,
    SYS_BIND,
    SYS_CONNECT,
    SYS_LISTEN,
    SYS_ACCEPT,
    SYS_GETSOCKNAME,
    SYS_GETPEERNAME,
    SYS_SOCKETPAIR,
    SYS_SEND,
    SYS_RECV,
    SYS_SENDTO,
    SYS_RECVFROM,
    SYS_SHUTDOWN,
    SYS_SETSOCKOPT,
    SYS_GETSOCKOPT,
    SYS_SENDMSG,
    SYS_RECVMSG
};

#ifndef __NR_socket
int socket(int domain, int type, int protocol)
{
    unsigned long  t[3];

    t[0] = (unsigned long) domain;
    t[1] = (unsigned long) type;
    t[2] = (unsigned long) protocol;

    return (int) __socketcall( SYS_SOCKET, t );
}
#endif /* !__NR_socket */


#ifndef __NR_bind
int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
{
    unsigned long  t[3];

    t[0] = (unsigned long) sockfd;
    t[1] = (unsigned long) my_addr;
    t[2] = (unsigned long) addrlen;

    return (int) __socketcall( SYS_BIND, t );
}
#endif  /* !__NR_bind */

#ifndef __NR_connect
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t  addrlen )
{
    unsigned long  t[3];

    t[0] = (unsigned long) sockfd;
    t[1] = (unsigned long) serv_addr;
    t[2] = (unsigned long) addrlen;

    return (int) __socketcall( SYS_CONNECT, t );
}
#endif /* !__NR_connect */

#ifndef __NR_listen
int listen(int s, int backlog)
{
    unsigned long  t[2];

    t[0] = (unsigned long) s;
    t[1] = (unsigned long) backlog;

    return (int) __socketcall( SYS_LISTEN, t );
}
#endif /* __NR_listen */

#ifndef __NR_accept
int accept(int sock, struct sockaddr *adresse, socklen_t *longueur)
{
    unsigned long  t[3];

    t[0] = (unsigned long) sock;
    t[1] = (unsigned long) adresse;
    t[2] = (unsigned long) longueur;

    return (int) __socketcall( SYS_ACCEPT, t );
}
#endif /* __NR_accept */

#ifndef __NR_getsockname
int getsockname(int s, struct sockaddr * name, socklen_t * namelen )
{
    unsigned long  t[3];

    t[0] = (unsigned long) s;
    t[1] = (unsigned long) name;
    t[2] = (unsigned long) namelen;

    return (int) __socketcall( SYS_GETSOCKNAME, t );
}
#endif /* __NR_getsockname */

#ifndef __NR_getpeername
int getpeername(int s, struct sockaddr *name, socklen_t *namelen)
{
    unsigned long  t[3];

    t[0] = (unsigned long) s;
    t[1] = (unsigned long) name;
    t[2] = (unsigned long) namelen;

    return (int) __socketcall( SYS_GETPEERNAME, t );
}
#endif /* !__NR_getpeername */

#ifndef __NR_socketpair
int socketpair(int d, int type, int protocol, int sv[2])
{
    unsigned long  t[4];

    t[0] = (unsigned long) d;
    t[1] = (unsigned long) type;
    t[2] = (unsigned long) protocol;
    t[3] = (unsigned long) sv;

    return (int) __socketcall( SYS_SOCKETPAIR, t );
}
#endif /* __NR_socketpair */

#ifndef __NR_sendto
ssize_t sendto(int socket, const void *message, size_t length, int flags,
      const struct sockaddr *dest_addr, socklen_t dest_len)
{
    unsigned long  t[6];

    t[0] = (unsigned long) socket;
    t[1] = (unsigned long) message;
    t[2] = (unsigned long) length;
    t[3] = (unsigned long) flags;
    t[4] = (unsigned long) dest_addr;
    t[5] = (unsigned long) dest_len;

   return __socketcall( SYS_SENDTO, t );
}
#endif /* !__NR_sendto */

#ifndef __NR_recvfrom
ssize_t recvfrom(int socket, void *buffer, size_t length, unsigned int flags,
             const struct sockaddr *address, socklen_t *address_len)
{
    unsigned long  t[6];

    t[0] = (unsigned long) socket;
    t[1] = (unsigned long) buffer;
    t[2] = (unsigned long) length;
    t[3] = (unsigned long) flags;
    t[4] = (unsigned long) address;
    t[5] = (unsigned long) address_len;

   return __socketcall( SYS_RECVFROM, t );
}
#endif /* !__NR_recvfrom */

#ifndef __NR_shutdown
int shutdown(int socket, int how)
{
    unsigned long  t[2];

    t[0] = (unsigned long) socket;
    t[1] = (unsigned long) how;

   return (int) __socketcall( SYS_SHUTDOWN, t );
}
#endif /* !__NR_shutdown */

#ifndef __NR_setsockopt
int  setsockopt( int  s, int  level, int  optname, const void*  optval, socklen_t  optlen )
{
    unsigned long  t[5];

    t[0] = (unsigned long) s;
    t[1] = (unsigned long) level;
    t[2] = (unsigned long) optname;
    t[3] = (unsigned long) optval;
    t[4] = (unsigned long) optlen;

   return (int) __socketcall( SYS_SETSOCKOPT, t );
}
#endif /* !__NR_setsockopt */

#ifndef __NR_getsockopt
int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
{
    unsigned long  t[5];

    t[0] = (unsigned long) s;
    t[1] = (unsigned long) level;
    t[2] = (unsigned long) optname;
    t[3] = (unsigned long) optval;
    t[4] = (unsigned long) optlen;

    return (int) __socketcall( SYS_GETSOCKOPT, t );
}
#endif /* !__NR_getsockopt */

#ifndef __NR_sendmsg
int sendmsg (int socket, const struct msghdr *message, unsigned int flags)
{
    unsigned long  t[3];

    t[0] = (unsigned long) socket;
    t[1] = (unsigned long) message;
    t[2] = (unsigned long) flags;

   return __socketcall( SYS_SENDMSG, t );
}
#endif /* __NR_sendmsg */

#ifndef __NR_recvmsg
int recvmsg(int socket, struct msghdr *message, unsigned int flags)
{
    unsigned long  t[3];

    t[0] = (unsigned long) socket;
    t[1] = (unsigned long) message;
    t[2] = (unsigned long) flags;

   return __socketcall( SYS_RECVMSG, t );
}
#endif /* __NR_recvmsg */