socketcalls.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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,
  153. int, flags);
  154. #elif defined(__NR_socketcall)
  155. /* recv, recvfrom added by bir7@leland.stanford.edu */
  156. ssize_t __libc_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. libc_hidden_proto(recvfrom)
  167. ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  168. {
  169. return (recvfrom(sockfd, buffer, len, flags, NULL, NULL));
  170. }
  171. #endif
  172. libc_hidden_proto(recv)
  173. weak_alias(__libc_recv,recv)
  174. libc_hidden_weak(recv)
  175. #endif
  176. #ifdef L_recvfrom
  177. extern __typeof(recvfrom) __libc_recvfrom;
  178. #ifdef __NR_recvfrom
  179. #define __NR___libc_recvfrom __NR_recvfrom
  180. _syscall6(ssize_t, __libc_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 __libc_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. libc_hidden_proto(recvfrom)
  198. weak_alias(__libc_recvfrom,recvfrom)
  199. libc_hidden_weak(recvfrom)
  200. #endif
  201. #ifdef L_recvmsg
  202. extern __typeof(recvmsg) __libc_recvmsg;
  203. #ifdef __NR_recvmsg
  204. #define __NR___libc_recvmsg __NR_recvmsg
  205. _syscall3(ssize_t, __libc_recvmsg, int, sockfd, struct msghdr *, msg, int, flags);
  206. #elif defined(__NR_socketcall)
  207. ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
  208. {
  209. unsigned long args[3];
  210. args[0] = sockfd;
  211. args[1] = (unsigned long) msg;
  212. args[2] = flags;
  213. return (__socketcall(SYS_RECVMSG, args));
  214. }
  215. #endif
  216. libc_hidden_proto(recvmsg)
  217. weak_alias(__libc_recvmsg,recvmsg)
  218. libc_hidden_weak(recvmsg)
  219. #endif
  220. #ifdef L_send
  221. extern __typeof(send) __libc_send;
  222. #ifdef __NR_send
  223. #define __NR___libc_send __NR_send
  224. _syscall4(ssize_t, __libc_send, int, sockfd, const void *, buffer, size_t, len, int, flags);
  225. #elif defined(__NR_socketcall)
  226. /* send, sendto added by bir7@leland.stanford.edu */
  227. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  228. {
  229. unsigned long args[4];
  230. args[0] = sockfd;
  231. args[1] = (unsigned long) buffer;
  232. args[2] = len;
  233. args[3] = flags;
  234. return (__socketcall(SYS_SEND, args));
  235. }
  236. #elif defined(__NR_sendto)
  237. libc_hidden_proto(sendto)
  238. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  239. {
  240. return (sendto(sockfd, buffer, len, flags, NULL, 0));
  241. }
  242. #endif
  243. libc_hidden_proto(send)
  244. weak_alias(__libc_send,send)
  245. libc_hidden_weak(send)
  246. #endif
  247. #ifdef L_sendmsg
  248. extern __typeof(sendmsg) __libc_sendmsg;
  249. #ifdef __NR_sendmsg
  250. #define __NR___libc_sendmsg __NR_sendmsg
  251. _syscall3(ssize_t, __libc_sendmsg, int, sockfd, const struct msghdr *, msg, int, flags);
  252. #elif defined(__NR_socketcall)
  253. ssize_t __libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
  254. {
  255. unsigned long args[3];
  256. args[0] = sockfd;
  257. args[1] = (unsigned long) msg;
  258. args[2] = flags;
  259. return (__socketcall(SYS_SENDMSG, args));
  260. }
  261. #endif
  262. libc_hidden_proto(sendmsg)
  263. weak_alias(__libc_sendmsg,sendmsg)
  264. libc_hidden_weak(sendmsg)
  265. #endif
  266. #ifdef L_sendto
  267. extern __typeof(sendto) __libc_sendto;
  268. #ifdef __NR_sendto
  269. #define __NR___libc_sendto __NR_sendto
  270. _syscall6(ssize_t, __libc_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 __libc_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. libc_hidden_proto(sendto)
  288. weak_alias(__libc_sendto,sendto)
  289. libc_hidden_weak(sendto)
  290. #endif
  291. #ifdef L_setsockopt
  292. libc_hidden_proto(setsockopt)
  293. #ifdef __NR_setsockopt
  294. _syscall5(int, setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen);
  295. #elif defined(__NR_socketcall)
  296. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  297. int setsockopt(int fd, int level, int optname, const void *optval,
  298. socklen_t optlen)
  299. {
  300. unsigned long args[5];
  301. args[0] = fd;
  302. args[1] = level;
  303. args[2] = optname;
  304. args[3] = (unsigned long) optval;
  305. args[4] = optlen;
  306. return (__socketcall(SYS_SETSOCKOPT, args));
  307. }
  308. #endif
  309. libc_hidden_def(setsockopt)
  310. #endif
  311. #ifdef L_shutdown
  312. #ifdef __NR_shutdown
  313. _syscall2(int, shutdown, int, sockfd, int, how);
  314. #elif defined(__NR_socketcall)
  315. /* shutdown by bir7@leland.stanford.edu */
  316. int shutdown(int sockfd, int how)
  317. {
  318. unsigned long args[2];
  319. args[0] = sockfd;
  320. args[1] = how;
  321. return (__socketcall(SYS_SHUTDOWN, args));
  322. }
  323. #endif
  324. #endif
  325. #ifdef L_socket
  326. libc_hidden_proto(socket)
  327. #ifdef __NR_socket
  328. _syscall3(int, socket, int, family, int, type, int, protocol);
  329. #elif defined(__NR_socketcall)
  330. int socket(int family, int type, int protocol)
  331. {
  332. unsigned long args[3];
  333. args[0] = family;
  334. args[1] = type;
  335. args[2] = (unsigned long) protocol;
  336. return __socketcall(SYS_SOCKET, args);
  337. }
  338. #endif
  339. libc_hidden_def(socket)
  340. #endif
  341. #ifdef L_socketpair
  342. #ifdef __NR_socketpair
  343. _syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec);
  344. #elif defined(__NR_socketcall)
  345. int socketpair(int family, int type, int protocol, int sockvec[2])
  346. {
  347. unsigned long args[4];
  348. args[0] = family;
  349. args[1] = type;
  350. args[2] = protocol;
  351. args[3] = (unsigned long) sockvec;
  352. return __socketcall(SYS_SOCKETPAIR, args);
  353. }
  354. #endif
  355. #endif