socketcalls.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. weak_alias(__libc_recv, recv);
  144. #elif defined(__NR_socketcall)
  145. /* recv, recvfrom added by bir7@leland.stanford.edu */
  146. ssize_t __libc_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. ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  157. {
  158. return (__recvfrom(sockfd, buffer, len, flags, NULL, NULL));
  159. }
  160. #endif
  161. hidden_weak_alias(__libc_recv,__recv)
  162. weak_alias(__libc_recv,recv)
  163. #endif
  164. #ifdef L_recvfrom
  165. #ifdef __NR_recvfrom
  166. #define __NR___libc_recvfrom __NR_recvfrom
  167. _syscall6(ssize_t, __libc_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len, int, flags,
  168. struct sockaddr *, to, socklen_t *, tolen);
  169. #elif defined(__NR_socketcall)
  170. /* recv, recvfrom added by bir7@leland.stanford.edu */
  171. ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
  172. struct sockaddr *to, socklen_t * tolen)
  173. {
  174. unsigned long args[6];
  175. args[0] = sockfd;
  176. args[1] = (unsigned long) buffer;
  177. args[2] = len;
  178. args[3] = flags;
  179. args[4] = (unsigned long) to;
  180. args[5] = (unsigned long) tolen;
  181. return (__socketcall(SYS_RECVFROM, args));
  182. }
  183. #endif
  184. hidden_weak_alias(__libc_recvfrom,__recvfrom)
  185. weak_alias(__libc_recvfrom,recvfrom)
  186. #endif
  187. #ifdef L_recvmsg
  188. #ifdef __NR_recvmsg
  189. #define __NR___libc_recvmsg __NR_recvmsg
  190. _syscall3(ssize_t, __libc_recvmsg, int, sockfd, struct msghdr *, msg, int, flags);
  191. #elif defined(__NR_socketcall)
  192. ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
  193. {
  194. unsigned long args[3];
  195. args[0] = sockfd;
  196. args[1] = (unsigned long) msg;
  197. args[2] = flags;
  198. return (__socketcall(SYS_RECVMSG, args));
  199. }
  200. #endif
  201. hidden_weak_alias(__libc_recvmsg,__recvmsg)
  202. weak_alias(__libc_recvmsg,recvmsg)
  203. #endif
  204. #ifdef L_send
  205. #ifdef __NR_send
  206. #define __NR___libc_send __NR_send
  207. _syscall4(ssize_t, __libc_send, int, sockfd, const void *, buffer, size_t, len, int, flags);
  208. #elif defined(__NR_socketcall)
  209. /* send, sendto added by bir7@leland.stanford.edu */
  210. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  211. {
  212. unsigned long args[4];
  213. args[0] = sockfd;
  214. args[1] = (unsigned long) buffer;
  215. args[2] = len;
  216. args[3] = flags;
  217. return (__socketcall(SYS_SEND, args));
  218. }
  219. #elif defined(__NR_sendto)
  220. extern ssize_t __sendto (int __fd, __const void *__buf, size_t __n,
  221. int __flags, __CONST_SOCKADDR_ARG __addr,
  222. socklen_t __addr_len) attribute_hidden;
  223. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  224. {
  225. return (__sendto(sockfd, buffer, len, flags, NULL, 0));
  226. }
  227. #endif
  228. hidden_weak_alias(__libc_send,__send)
  229. weak_alias(__libc_send,send)
  230. #endif
  231. #ifdef L_sendmsg
  232. #ifdef __NR_sendmsg
  233. #define __NR___libc_sendmsg __NR_sendmsg
  234. _syscall3(ssize_t, __libc_sendmsg, int, sockfd, const struct msghdr *, msg, int, flags);
  235. #elif defined(__NR_socketcall)
  236. ssize_t __libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
  237. {
  238. unsigned long args[3];
  239. args[0] = sockfd;
  240. args[1] = (unsigned long) msg;
  241. args[2] = flags;
  242. return (__socketcall(SYS_SENDMSG, args));
  243. }
  244. #endif
  245. hidden_weak_alias(__libc_sendmsg,__sendmsg)
  246. weak_alias(__libc_sendmsg,sendmsg)
  247. #endif
  248. #ifdef L_sendto
  249. #ifdef __NR_sendto
  250. #define __NR___libc_sendto __NR_sendto
  251. _syscall6(ssize_t, __libc_sendto, int, sockfd, const void *, buffer, size_t, len,
  252. int, flags, const struct sockaddr *, to, socklen_t, tolen);
  253. #elif defined(__NR_socketcall)
  254. /* send, sendto added by bir7@leland.stanford.edu */
  255. ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags,
  256. const struct sockaddr *to, socklen_t tolen)
  257. {
  258. unsigned long args[6];
  259. args[0] = sockfd;
  260. args[1] = (unsigned long) buffer;
  261. args[2] = len;
  262. args[3] = flags;
  263. args[4] = (unsigned long) to;
  264. args[5] = tolen;
  265. return (__socketcall(SYS_SENDTO, args));
  266. }
  267. #endif
  268. hidden_weak_alias(__libc_sendto,__sendto)
  269. weak_alias(__libc_sendto,sendto)
  270. #endif
  271. #ifdef L_setsockopt
  272. #ifdef __NR_setsockopt
  273. #define __NR___setsockopt __NR_setsockopt
  274. attribute_hidden _syscall5(int, __setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen);
  275. #elif defined(__NR_socketcall)
  276. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  277. int attribute_hidden __setsockopt(int fd, int level, int optname, const void *optval,
  278. socklen_t optlen)
  279. {
  280. unsigned long args[5];
  281. args[0] = fd;
  282. args[1] = level;
  283. args[2] = optname;
  284. args[3] = (unsigned long) optval;
  285. args[4] = optlen;
  286. return (__socketcall(SYS_SETSOCKOPT, args));
  287. }
  288. #endif
  289. strong_alias(__setsockopt,setsockopt)
  290. #endif
  291. #ifdef L_shutdown
  292. #ifdef __NR_shutdown
  293. _syscall2(int, shutdown, int, sockfd, int, how);
  294. #elif defined(__NR_socketcall)
  295. /* shutdown by bir7@leland.stanford.edu */
  296. int shutdown(int sockfd, int how)
  297. {
  298. unsigned long args[2];
  299. args[0] = sockfd;
  300. args[1] = how;
  301. return (__socketcall(SYS_SHUTDOWN, args));
  302. }
  303. #endif
  304. #endif
  305. #ifdef L_socket
  306. #ifdef __NR_socket
  307. #define __NR___socket __NR_socket
  308. attribute_hidden _syscall3(int, __socket, int, family, int, type, int, protocol);
  309. #elif defined(__NR_socketcall)
  310. int attribute_hidden __socket(int family, int type, int protocol)
  311. {
  312. unsigned long args[3];
  313. args[0] = family;
  314. args[1] = type;
  315. args[2] = (unsigned long) protocol;
  316. return __socketcall(SYS_SOCKET, args);
  317. }
  318. #endif
  319. strong_alias(__socket,socket)
  320. #endif
  321. #ifdef L_socketpair
  322. #ifdef __NR_socketpair
  323. _syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec);
  324. #elif defined(__NR_socketcall)
  325. int socketpair(int family, int type, int protocol, int sockvec[2])
  326. {
  327. unsigned long args[4];
  328. args[0] = family;
  329. args[1] = type;
  330. args[2] = protocol;
  331. args[3] = (unsigned long) sockvec;
  332. return __socketcall(SYS_SOCKETPAIR, args);
  333. }
  334. #endif
  335. #endif