socketcalls.c 8.4 KB

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