socketcalls.c 8.6 KB

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