socketcalls.c 9.4 KB

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