socketcalls.c 9.6 KB

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