socketcalls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #define __FORCE_GLIBC
  7. #include <features.h>
  8. #include <errno.h>
  9. #include <syscall.h>
  10. #include <sys/socket.h>
  11. #ifdef __NR_socketcall
  12. extern int __socketcall(int call, unsigned long *args) attribute_hidden;
  13. /* Various socketcall numbers */
  14. #define SYS_SOCKET 1
  15. #define SYS_BIND 2
  16. #define SYS_CONNECT 3
  17. #define SYS_LISTEN 4
  18. #define SYS_ACCEPT 5
  19. #define SYS_GETSOCKNAME 6
  20. #define SYS_GETPEERNAME 7
  21. #define SYS_SOCKETPAIR 8
  22. #define SYS_SEND 9
  23. #define SYS_RECV 10
  24. #define SYS_SENDTO 11
  25. #define SYS_RECVFROM 12
  26. #define SYS_SHUTDOWN 13
  27. #define SYS_SETSOCKOPT 14
  28. #define SYS_GETSOCKOPT 15
  29. #define SYS_SENDMSG 16
  30. #define SYS_RECVMSG 17
  31. #endif
  32. #ifdef L_accept
  33. # ifdef __NR_accept
  34. _syscall3(int, accept, int, call, struct sockaddr *, addr, socklen_t *,addrlen)
  35. # elif defined(__NR_socketcall)
  36. int accept(int s, struct sockaddr *addr, socklen_t * addrlen)
  37. {
  38. unsigned long args[3];
  39. args[0] = s;
  40. args[1] = (unsigned long) addr;
  41. args[2] = (unsigned long) addrlen;
  42. return __socketcall(SYS_ACCEPT, args);
  43. }
  44. # endif
  45. # ifndef __LINUXTHREADS_OLD__
  46. libc_hidden_def(accept)
  47. # else
  48. libc_hidden_weak(accept)
  49. strong_alias(accept,__libc_accept)
  50. # endif
  51. #endif
  52. #ifdef L_bind
  53. #ifdef __NR_bind
  54. _syscall3(int, bind, int, sockfd, const struct sockaddr *, myaddr, socklen_t, addrlen)
  55. #elif defined(__NR_socketcall)
  56. int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen)
  57. {
  58. unsigned long args[3];
  59. args[0] = sockfd;
  60. args[1] = (unsigned long) myaddr;
  61. args[2] = addrlen;
  62. return __socketcall(SYS_BIND, args);
  63. }
  64. #endif
  65. libc_hidden_def(bind)
  66. #endif
  67. #ifdef L_connect
  68. # ifdef __NR_connect
  69. _syscall3(int, connect, int, sockfd, const struct sockaddr *, saddr, socklen_t, addrlen)
  70. # elif defined(__NR_socketcall)
  71. int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
  72. {
  73. unsigned long args[3];
  74. args[0] = sockfd;
  75. args[1] = (unsigned long) saddr;
  76. args[2] = addrlen;
  77. return __socketcall(SYS_CONNECT, args);
  78. }
  79. # endif
  80. # ifndef __LINUXTHREADS_OLD__
  81. libc_hidden_def(connect)
  82. # else
  83. libc_hidden_weak(connect)
  84. strong_alias(connect,__libc_connect)
  85. # endif
  86. #endif
  87. #ifdef L_getpeername
  88. #ifdef __NR_getpeername
  89. _syscall3(int, getpeername, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
  90. #elif defined(__NR_socketcall)
  91. int getpeername(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  92. {
  93. unsigned long args[3];
  94. args[0] = sockfd;
  95. args[1] = (unsigned long) addr;
  96. args[2] = (unsigned long) paddrlen;
  97. return __socketcall(SYS_GETPEERNAME, args);
  98. }
  99. #endif
  100. #endif
  101. #ifdef L_getsockname
  102. #ifdef __NR_getsockname
  103. _syscall3(int, getsockname, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
  104. #elif defined(__NR_socketcall)
  105. int getsockname(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  106. {
  107. unsigned long args[3];
  108. args[0] = sockfd;
  109. args[1] = (unsigned long) addr;
  110. args[2] = (unsigned long) paddrlen;
  111. return __socketcall(SYS_GETSOCKNAME, args);
  112. }
  113. #endif
  114. libc_hidden_def(getsockname)
  115. #endif
  116. #ifdef L_getsockopt
  117. #ifdef __NR_getsockopt
  118. _syscall5(int, getsockopt, int, fd, int, level, int, optname, __ptr_t, optval, socklen_t *, optlen)
  119. #elif defined(__NR_socketcall)
  120. int getsockopt(int fd, int level, int optname, __ptr_t optval,
  121. socklen_t * optlen)
  122. {
  123. unsigned long args[5];
  124. args[0] = fd;
  125. args[1] = level;
  126. args[2] = optname;
  127. args[3] = (unsigned long) optval;
  128. args[4] = (unsigned long) optlen;
  129. return (__socketcall(SYS_GETSOCKOPT, args));
  130. }
  131. #endif
  132. #endif
  133. #ifdef L_listen
  134. #ifdef __NR_listen
  135. _syscall2(int, listen, int, sockfd, int, backlog)
  136. #elif defined(__NR_socketcall)
  137. int listen(int sockfd, int backlog)
  138. {
  139. unsigned long args[2];
  140. args[0] = sockfd;
  141. args[1] = backlog;
  142. return __socketcall(SYS_LISTEN, args);
  143. }
  144. #endif
  145. libc_hidden_def(listen)
  146. #endif
  147. #ifdef L_recv
  148. # ifdef __NR_recv
  149. _syscall4(ssize_t, recv, int, sockfd, __ptr_t, buffer, size_t, len,
  150. int, flags)
  151. # elif defined(__NR_socketcall)
  152. /* recv, recvfrom added by bir7@leland.stanford.edu */
  153. ssize_t recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  154. {
  155. unsigned long args[4];
  156. args[0] = sockfd;
  157. args[1] = (unsigned long) buffer;
  158. args[2] = len;
  159. args[3] = flags;
  160. return (__socketcall(SYS_RECV, args));
  161. }
  162. # elif defined(__NR_recvfrom)
  163. ssize_t recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  164. {
  165. return (recvfrom(sockfd, buffer, len, flags, NULL, NULL));
  166. }
  167. # endif
  168. # ifndef __LINUXTHREADS_OLD__
  169. libc_hidden_def(recv)
  170. # else
  171. libc_hidden_weak(recv)
  172. strong_alias(recv,__libc_recv)
  173. # endif
  174. #endif
  175. #ifdef L_recvfrom
  176. # ifdef __NR_recvfrom
  177. _syscall6(ssize_t, recvfrom, int, sockfd, __ptr_t, buffer, size_t, len,
  178. int, flags, struct sockaddr *, to, socklen_t *, tolen)
  179. # elif defined(__NR_socketcall)
  180. /* recv, recvfrom added by bir7@leland.stanford.edu */
  181. ssize_t recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
  182. struct sockaddr *to, socklen_t * tolen)
  183. {
  184. unsigned long args[6];
  185. args[0] = sockfd;
  186. args[1] = (unsigned long) buffer;
  187. args[2] = len;
  188. args[3] = flags;
  189. args[4] = (unsigned long) to;
  190. args[5] = (unsigned long) tolen;
  191. return (__socketcall(SYS_RECVFROM, args));
  192. }
  193. # endif
  194. # ifndef __LINUXTHREADS_OLD__
  195. libc_hidden_def(recvfrom)
  196. # else
  197. libc_hidden_weak(recvfrom)
  198. strong_alias(recvfrom,__libc_recvfrom)
  199. # endif
  200. #endif
  201. #ifdef L_recvmsg
  202. # ifdef __NR_recvmsg
  203. _syscall3(ssize_t, recvmsg, int, sockfd, struct msghdr *, msg, int, flags)
  204. # elif defined(__NR_socketcall)
  205. ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
  206. {
  207. unsigned long args[3];
  208. args[0] = sockfd;
  209. args[1] = (unsigned long) msg;
  210. args[2] = flags;
  211. return (__socketcall(SYS_RECVMSG, args));
  212. }
  213. # endif
  214. # ifndef __LINUXTHREADS_OLD__
  215. libc_hidden_def(recvmsg)
  216. # else
  217. libc_hidden_weak(recvmsg)
  218. strong_alias(recvmsg,__libc_recvmsg)
  219. # endif
  220. #endif
  221. #ifdef L_send
  222. # ifdef __NR_send
  223. _syscall4(ssize_t, send, int, sockfd, const void *, buffer, size_t, len, int, flags)
  224. # elif defined(__NR_socketcall)
  225. /* send, sendto added by bir7@leland.stanford.edu */
  226. ssize_t send(int sockfd, const void *buffer, size_t len, int flags)
  227. {
  228. unsigned long args[4];
  229. args[0] = sockfd;
  230. args[1] = (unsigned long) buffer;
  231. args[2] = len;
  232. args[3] = flags;
  233. return (__socketcall(SYS_SEND, args));
  234. }
  235. # elif defined(__NR_sendto)
  236. ssize_t send(int sockfd, const void *buffer, size_t len, int flags)
  237. {
  238. return (sendto(sockfd, buffer, len, flags, NULL, 0));
  239. }
  240. # endif
  241. # ifndef __LINUXTHREADS_OLD__
  242. libc_hidden_def(send)
  243. # else
  244. libc_hidden_weak(send)
  245. strong_alias(send,__libc_send)
  246. # endif
  247. #endif
  248. #ifdef L_sendmsg
  249. # ifdef __NR_sendmsg
  250. _syscall3(ssize_t, sendmsg, int, sockfd, const struct msghdr *, msg, int, flags)
  251. # elif defined(__NR_socketcall)
  252. ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
  253. {
  254. unsigned long args[3];
  255. args[0] = sockfd;
  256. args[1] = (unsigned long) msg;
  257. args[2] = flags;
  258. return (__socketcall(SYS_SENDMSG, args));
  259. }
  260. # endif
  261. # ifndef __LINUXTHREADS_OLD__
  262. libc_hidden_def(sendmsg)
  263. # else
  264. libc_hidden_weak(sendmsg)
  265. strong_alias(sendmsg,__libc_sendmsg)
  266. # endif
  267. #endif
  268. #ifdef L_sendto
  269. # ifdef __NR_sendto
  270. _syscall6(ssize_t, sendto, int, sockfd, const void *, buffer,
  271. size_t, len, int, flags, const struct sockaddr *, to, socklen_t, tolen)
  272. # elif defined(__NR_socketcall)
  273. /* send, sendto added by bir7@leland.stanford.edu */
  274. ssize_t sendto(int sockfd, const void *buffer, size_t len, int flags,
  275. const struct sockaddr *to, socklen_t tolen)
  276. {
  277. unsigned long args[6];
  278. args[0] = sockfd;
  279. args[1] = (unsigned long) buffer;
  280. args[2] = len;
  281. args[3] = flags;
  282. args[4] = (unsigned long) to;
  283. args[5] = tolen;
  284. return (__socketcall(SYS_SENDTO, args));
  285. }
  286. # endif
  287. # ifndef __LINUXTHREADS_OLD__
  288. libc_hidden_def(sendto)
  289. # else
  290. libc_hidden_weak(sendto)
  291. strong_alias(sendto,__libc_sendto)
  292. # endif
  293. #endif
  294. #ifdef L_setsockopt
  295. #ifdef __NR_setsockopt
  296. _syscall5(int, setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen)
  297. #elif defined(__NR_socketcall)
  298. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  299. int setsockopt(int fd, int level, int optname, const void *optval,
  300. socklen_t optlen)
  301. {
  302. unsigned long args[5];
  303. args[0] = fd;
  304. args[1] = level;
  305. args[2] = optname;
  306. args[3] = (unsigned long) optval;
  307. args[4] = optlen;
  308. return (__socketcall(SYS_SETSOCKOPT, args));
  309. }
  310. #endif
  311. libc_hidden_def(setsockopt)
  312. #endif
  313. #ifdef L_shutdown
  314. #ifdef __NR_shutdown
  315. _syscall2(int, shutdown, int, sockfd, int, how)
  316. #elif defined(__NR_socketcall)
  317. /* shutdown by bir7@leland.stanford.edu */
  318. int shutdown(int sockfd, int how)
  319. {
  320. unsigned long args[2];
  321. args[0] = sockfd;
  322. args[1] = how;
  323. return (__socketcall(SYS_SHUTDOWN, args));
  324. }
  325. #endif
  326. #endif
  327. #ifdef L_socket
  328. #ifdef __NR_socket
  329. _syscall3(int, socket, int, family, int, type, int, protocol)
  330. #elif defined(__NR_socketcall)
  331. int socket(int family, int type, int protocol)
  332. {
  333. unsigned long args[3];
  334. args[0] = family;
  335. args[1] = type;
  336. args[2] = (unsigned long) protocol;
  337. return __socketcall(SYS_SOCKET, args);
  338. }
  339. #endif
  340. libc_hidden_def(socket)
  341. #endif
  342. #ifdef L_socketpair
  343. #ifdef __NR_socketpair
  344. _syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec)
  345. #elif defined(__NR_socketcall)
  346. int socketpair(int family, int type, int protocol, int sockvec[2])
  347. {
  348. unsigned long args[4];
  349. args[0] = family;
  350. args[1] = type;
  351. args[2] = protocol;
  352. args[3] = (unsigned long) sockvec;
  353. return __socketcall(SYS_SOCKETPAIR, args);
  354. }
  355. #endif
  356. #endif