socketcalls.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. extern __typeof(accept) __libc_accept;
  34. #ifdef __NR_accept
  35. #define __NR___libc_accept __NR_accept
  36. _syscall3(int, __libc_accept, int, call, struct sockaddr *, addr, socklen_t *,addrlen);
  37. #elif defined(__NR_socketcall)
  38. int __libc_accept(int s, struct sockaddr *addr, socklen_t * addrlen)
  39. {
  40. unsigned long args[3];
  41. args[0] = s;
  42. args[1] = (unsigned long) addr;
  43. args[2] = (unsigned long) addrlen;
  44. return __socketcall(SYS_ACCEPT, args);
  45. }
  46. #endif
  47. libc_hidden_proto(accept)
  48. weak_alias(__libc_accept,accept)
  49. libc_hidden_weak(accept)
  50. #endif
  51. #ifdef L_bind
  52. libc_hidden_proto(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. extern __typeof(connect) __libc_connect;
  69. #ifdef __NR_connect
  70. #define __NR___libc_connect __NR_connect
  71. _syscall3(int, __libc_connect, int, sockfd, const struct sockaddr *, saddr, socklen_t, addrlen);
  72. #elif defined(__NR_socketcall)
  73. int __libc_connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
  74. {
  75. unsigned long args[3];
  76. args[0] = sockfd;
  77. args[1] = (unsigned long) saddr;
  78. args[2] = addrlen;
  79. return __socketcall(SYS_CONNECT, args);
  80. }
  81. #endif
  82. libc_hidden_proto(connect)
  83. weak_alias(__libc_connect,connect)
  84. libc_hidden_weak(connect)
  85. #endif
  86. #ifdef L_getpeername
  87. #ifdef __NR_getpeername
  88. _syscall3(int, getpeername, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen);
  89. #elif defined(__NR_socketcall)
  90. int getpeername(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  91. {
  92. unsigned long args[3];
  93. args[0] = sockfd;
  94. args[1] = (unsigned long) addr;
  95. args[2] = (unsigned long) paddrlen;
  96. return __socketcall(SYS_GETPEERNAME, args);
  97. }
  98. #endif
  99. #endif
  100. #ifdef L_getsockname
  101. libc_hidden_proto(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. libc_hidden_proto(listen)
  135. #ifdef __NR_listen
  136. _syscall2(int, listen, int, sockfd, int, backlog);
  137. #elif defined(__NR_socketcall)
  138. int listen(int sockfd, int backlog)
  139. {
  140. unsigned long args[2];
  141. args[0] = sockfd;
  142. args[1] = backlog;
  143. return __socketcall(SYS_LISTEN, args);
  144. }
  145. #endif
  146. libc_hidden_def(listen)
  147. #endif
  148. #ifdef L_recv
  149. extern __typeof(recv) __libc_recv;
  150. #ifdef __NR_recv
  151. #define __NR___libc_recv __NR_recv
  152. _syscall4(ssize_t, __libc_recv, int, sockfd, __ptr_t, buffer, size_t, len, int, flags);
  153. #elif defined(__NR_socketcall)
  154. /* recv, recvfrom added by bir7@leland.stanford.edu */
  155. ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  156. {
  157. unsigned long args[4];
  158. args[0] = sockfd;
  159. args[1] = (unsigned long) buffer;
  160. args[2] = len;
  161. args[3] = flags;
  162. return (__socketcall(SYS_RECV, args));
  163. }
  164. #elif defined(__NR_recvfrom)
  165. libc_hidden_proto(recvfrom)
  166. ssize_t __libc_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. libc_hidden_proto(recv)
  172. weak_alias(__libc_recv,recv)
  173. libc_hidden_weak(recv)
  174. #endif
  175. #ifdef L_recvfrom
  176. extern __typeof(recvfrom) __libc_recvfrom;
  177. #ifdef __NR_recvfrom
  178. #define __NR___libc_recvfrom __NR_recvfrom
  179. _syscall6(ssize_t, __libc_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len, int, flags,
  180. struct sockaddr *, to, socklen_t *, tolen);
  181. #elif defined(__NR_socketcall)
  182. /* recv, recvfrom added by bir7@leland.stanford.edu */
  183. ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
  184. struct sockaddr *to, socklen_t * tolen)
  185. {
  186. unsigned long args[6];
  187. args[0] = sockfd;
  188. args[1] = (unsigned long) buffer;
  189. args[2] = len;
  190. args[3] = flags;
  191. args[4] = (unsigned long) to;
  192. args[5] = (unsigned long) tolen;
  193. return (__socketcall(SYS_RECVFROM, args));
  194. }
  195. #endif
  196. libc_hidden_proto(recvfrom)
  197. weak_alias(__libc_recvfrom,recvfrom)
  198. libc_hidden_weak(recvfrom)
  199. #endif
  200. #ifdef L_recvmsg
  201. extern __typeof(recvmsg) __libc_recvmsg;
  202. #ifdef __NR_recvmsg
  203. #define __NR___libc_recvmsg __NR_recvmsg
  204. _syscall3(ssize_t, __libc_recvmsg, int, sockfd, struct msghdr *, msg, int, flags);
  205. #elif defined(__NR_socketcall)
  206. ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
  207. {
  208. unsigned long args[3];
  209. args[0] = sockfd;
  210. args[1] = (unsigned long) msg;
  211. args[2] = flags;
  212. return (__socketcall(SYS_RECVMSG, args));
  213. }
  214. #endif
  215. libc_hidden_proto(recvmsg)
  216. weak_alias(__libc_recvmsg,recvmsg)
  217. libc_hidden_weak(recvmsg)
  218. #endif
  219. #ifdef L_send
  220. extern __typeof(send) __libc_send;
  221. #ifdef __NR_send
  222. #define __NR___libc_send __NR_send
  223. _syscall4(ssize_t, __libc_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 __libc_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. libc_hidden_proto(sendto)
  237. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  238. {
  239. return (sendto(sockfd, buffer, len, flags, NULL, 0));
  240. }
  241. #endif
  242. libc_hidden_proto(send)
  243. weak_alias(__libc_send,send)
  244. libc_hidden_weak(send)
  245. #endif
  246. #ifdef L_sendmsg
  247. extern __typeof(sendmsg) __libc_sendmsg;
  248. #ifdef __NR_sendmsg
  249. #define __NR___libc_sendmsg __NR_sendmsg
  250. _syscall3(ssize_t, __libc_sendmsg, int, sockfd, const struct msghdr *, msg, int, flags);
  251. #elif defined(__NR_socketcall)
  252. ssize_t __libc_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. libc_hidden_proto(sendmsg)
  262. weak_alias(__libc_sendmsg,sendmsg)
  263. libc_hidden_weak(sendmsg)
  264. #endif
  265. #ifdef L_sendto
  266. extern __typeof(sendto) __libc_sendto;
  267. #ifdef __NR_sendto
  268. #define __NR___libc_sendto __NR_sendto
  269. _syscall6(ssize_t, __libc_sendto, int, sockfd, const void *, buffer, size_t, len,
  270. int, flags, const struct sockaddr *, to, socklen_t, tolen);
  271. #elif defined(__NR_socketcall)
  272. /* send, sendto added by bir7@leland.stanford.edu */
  273. ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags,
  274. const struct sockaddr *to, socklen_t tolen)
  275. {
  276. unsigned long args[6];
  277. args[0] = sockfd;
  278. args[1] = (unsigned long) buffer;
  279. args[2] = len;
  280. args[3] = flags;
  281. args[4] = (unsigned long) to;
  282. args[5] = tolen;
  283. return (__socketcall(SYS_SENDTO, args));
  284. }
  285. #endif
  286. libc_hidden_proto(sendto)
  287. weak_alias(__libc_sendto,sendto)
  288. libc_hidden_weak(sendto)
  289. #endif
  290. #ifdef L_setsockopt
  291. libc_hidden_proto(setsockopt)
  292. #ifdef __NR_setsockopt
  293. _syscall5(int, setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen);
  294. #elif defined(__NR_socketcall)
  295. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  296. int setsockopt(int fd, int level, int optname, const void *optval,
  297. socklen_t optlen)
  298. {
  299. unsigned long args[5];
  300. args[0] = fd;
  301. args[1] = level;
  302. args[2] = optname;
  303. args[3] = (unsigned long) optval;
  304. args[4] = optlen;
  305. return (__socketcall(SYS_SETSOCKOPT, args));
  306. }
  307. #endif
  308. libc_hidden_def(setsockopt)
  309. #endif
  310. #ifdef L_shutdown
  311. #ifdef __NR_shutdown
  312. _syscall2(int, shutdown, int, sockfd, int, how);
  313. #elif defined(__NR_socketcall)
  314. /* shutdown by bir7@leland.stanford.edu */
  315. int shutdown(int sockfd, int how)
  316. {
  317. unsigned long args[2];
  318. args[0] = sockfd;
  319. args[1] = how;
  320. return (__socketcall(SYS_SHUTDOWN, args));
  321. }
  322. #endif
  323. #endif
  324. #ifdef L_socket
  325. libc_hidden_proto(socket)
  326. #ifdef __NR_socket
  327. _syscall3(int, socket, int, family, int, type, int, protocol);
  328. #elif defined(__NR_socketcall)
  329. int socket(int family, int type, int protocol)
  330. {
  331. unsigned long args[3];
  332. args[0] = family;
  333. args[1] = type;
  334. args[2] = (unsigned long) protocol;
  335. return __socketcall(SYS_SOCKET, args);
  336. }
  337. #endif
  338. libc_hidden_def(socket)
  339. #endif
  340. #ifdef L_socketpair
  341. #ifdef __NR_socketpair
  342. _syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec);
  343. #elif defined(__NR_socketcall)
  344. int socketpair(int family, int type, int protocol, int sockvec[2])
  345. {
  346. unsigned long args[4];
  347. args[0] = family;
  348. args[1] = type;
  349. args[2] = protocol;
  350. args[3] = (unsigned long) sockvec;
  351. return __socketcall(SYS_SOCKETPAIR, args);
  352. }
  353. #endif
  354. #endif