socketcalls.c 7.9 KB

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