socketcalls.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #define __FORCE_GLIBC
  2. #include <features.h>
  3. #include <errno.h>
  4. #include <syscall.h>
  5. #include <sys/socket.h>
  6. extern int socketcall(int call, unsigned long *args);
  7. /* Various socketcall numbers */
  8. #define SYS_SOCKET 1
  9. #define SYS_BIND 2
  10. #define SYS_CONNECT 3
  11. #define SYS_LISTEN 4
  12. #define SYS_ACCEPT 5
  13. #define SYS_GETSOCKNAME 6
  14. #define SYS_GETPEERNAME 7
  15. #define SYS_SOCKETPAIR 8
  16. #define SYS_SEND 9
  17. #define SYS_RECV 10
  18. #define SYS_SENDTO 11
  19. #define SYS_RECVFROM 12
  20. #define SYS_SHUTDOWN 13
  21. #define SYS_SETSOCKOPT 14
  22. #define SYS_GETSOCKOPT 15
  23. #define SYS_SENDMSG 16
  24. #define SYS_RECVMSG 17
  25. #ifdef L_accept
  26. int accept(int s, struct sockaddr *addr, socklen_t * addrlen)
  27. {
  28. unsigned long args[3];
  29. args[0] = s;
  30. args[1] = (unsigned long) addr;
  31. args[2] = (unsigned long) addrlen;
  32. return socketcall(SYS_ACCEPT, args);
  33. }
  34. #endif
  35. #ifdef L_bind
  36. int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen)
  37. {
  38. unsigned long args[3];
  39. args[0] = sockfd;
  40. args[1] = (unsigned long) myaddr;
  41. args[2] = addrlen;
  42. return socketcall(SYS_BIND, args);
  43. }
  44. #endif
  45. #ifdef L_connect
  46. int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
  47. {
  48. unsigned long args[3];
  49. args[0] = sockfd;
  50. args[1] = (unsigned long) saddr;
  51. args[2] = addrlen;
  52. return socketcall(SYS_CONNECT, args);
  53. }
  54. #endif
  55. #ifdef L_getpeername
  56. int getpeername(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  57. {
  58. unsigned long args[3];
  59. args[0] = sockfd;
  60. args[1] = (unsigned long) addr;
  61. args[2] = (unsigned long) paddrlen;
  62. return socketcall(SYS_GETPEERNAME, args);
  63. }
  64. #endif
  65. #ifdef L_getsockname
  66. int getsockname(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  67. {
  68. unsigned long args[3];
  69. args[0] = sockfd;
  70. args[1] = (unsigned long) addr;
  71. args[2] = (unsigned long) paddrlen;
  72. return socketcall(SYS_GETSOCKNAME, args);
  73. }
  74. #endif
  75. #ifdef L_getsockopt
  76. int getsockopt(int fd, int level, int optname, __ptr_t optval,
  77. socklen_t * optlen)
  78. {
  79. unsigned long args[5];
  80. args[0] = fd;
  81. args[1] = level;
  82. args[2] = optname;
  83. args[3] = (unsigned long) optval;
  84. args[4] = (unsigned long) optlen;
  85. return (socketcall(SYS_GETSOCKOPT, args));
  86. }
  87. #endif
  88. #ifdef L_listen
  89. int listen(int sockfd, int backlog)
  90. {
  91. unsigned long args[2];
  92. args[0] = sockfd;
  93. args[1] = backlog;
  94. return socketcall(SYS_LISTEN, args);
  95. }
  96. #endif
  97. #ifdef L_recv
  98. /* recv, recvfrom added by bir7@leland.stanford.edu */
  99. int recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  100. {
  101. unsigned long args[4];
  102. args[0] = sockfd;
  103. args[1] = (unsigned long) buffer;
  104. args[2] = len;
  105. args[3] = flags;
  106. return (socketcall(SYS_RECV, args));
  107. }
  108. #endif
  109. #ifdef L_recvfrom
  110. /* recv, recvfrom added by bir7@leland.stanford.edu */
  111. int recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
  112. struct sockaddr *to, socklen_t * tolen)
  113. {
  114. unsigned long args[6];
  115. args[0] = sockfd;
  116. args[1] = (unsigned long) buffer;
  117. args[2] = len;
  118. args[3] = flags;
  119. args[4] = (unsigned long) to;
  120. args[5] = (unsigned long) tolen;
  121. return (socketcall(SYS_RECVFROM, args));
  122. }
  123. #endif
  124. #ifdef L_recvmsg
  125. int recvmsg(int sockfd, struct msghdr *msg, int flags)
  126. {
  127. unsigned long args[3];
  128. args[0] = sockfd;
  129. args[1] = (unsigned long) msg;
  130. args[2] = flags;
  131. return (socketcall(SYS_RECVMSG, args));
  132. }
  133. #endif
  134. #ifdef L_send
  135. /* send, sendto added by bir7@leland.stanford.edu */
  136. int send(int sockfd, const void *buffer, size_t len, int flags)
  137. {
  138. unsigned long args[4];
  139. args[0] = sockfd;
  140. args[1] = (unsigned long) buffer;
  141. args[2] = len;
  142. args[3] = flags;
  143. return (socketcall(SYS_SEND, args));
  144. }
  145. #endif
  146. #ifdef L_sendmsg
  147. int sendmsg(int sockfd, const struct msghdr *msg, int flags)
  148. {
  149. unsigned long args[3];
  150. args[0] = sockfd;
  151. args[1] = (unsigned long) msg;
  152. args[2] = flags;
  153. return (socketcall(SYS_SENDMSG, args));
  154. }
  155. #endif
  156. #ifdef L_sendto
  157. /* send, sendto added by bir7@leland.stanford.edu */
  158. int sendto(int sockfd, const void *buffer, size_t len, int flags,
  159. const struct sockaddr *to, socklen_t tolen)
  160. {
  161. unsigned long args[6];
  162. args[0] = sockfd;
  163. args[1] = (unsigned long) buffer;
  164. args[2] = len;
  165. args[3] = flags;
  166. args[4] = (unsigned long) to;
  167. args[5] = tolen;
  168. return (socketcall(SYS_SENDTO, args));
  169. }
  170. #endif
  171. #ifdef L_setsockopt
  172. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  173. int setsockopt(int fd, int level, int optname, const void *optval,
  174. socklen_t optlen)
  175. {
  176. unsigned long args[5];
  177. args[0] = fd;
  178. args[1] = level;
  179. args[2] = optname;
  180. args[3] = (unsigned long) optval;
  181. args[4] = optlen;
  182. return (socketcall(SYS_SETSOCKOPT, args));
  183. }
  184. #endif
  185. #ifdef L_shutdown
  186. /* shutdown by bir7@leland.stanford.edu */
  187. int shutdown(int sockfd, int how)
  188. {
  189. unsigned long args[2];
  190. args[0] = sockfd;
  191. args[1] = how;
  192. return (socketcall(SYS_SHUTDOWN, args));
  193. }
  194. #endif
  195. #ifdef L_socket
  196. int socket(int family, int type, int protocol)
  197. {
  198. unsigned long args[3];
  199. args[0] = family;
  200. args[1] = type;
  201. args[2] = (unsigned long) protocol;
  202. return socketcall(SYS_SOCKET, args);
  203. }
  204. #endif
  205. #ifdef L_socketpair
  206. int socketpair(int family, int type, int protocol, int sockvec[2])
  207. {
  208. unsigned long args[4];
  209. args[0] = family;
  210. args[1] = type;
  211. args[2] = protocol;
  212. args[3] = (unsigned long) sockvec;
  213. return socketcall(SYS_SOCKETPAIR, args);
  214. }
  215. #endif