socketcalls.c 8.7 KB

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