sockserv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #if defined(CFG_TelnetOption)
  2. /*+-------------------------------------------------------------------------
  3. sockserv.c -- TCP server handler
  4. I ain't redy yit fo' IPv6
  5. Defined functions:
  6. --------------------------------------------------------------------------*/
  7. /*+:EDITS:*/
  8. /*:04-26-2000-11:16-wht@bob-RELEASE 4.42 */
  9. /*:01-01-2000-20:46-wht@WIN32-more work */
  10. /*:11-10-1999-00:16-wht@menlo-creation as shaped charge for ECU */
  11. #include "ecu.h"
  12. #include "ecuerror.h"
  13. #include "esd.h"
  14. #include "var.h"
  15. #include "sockserv.h"
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <sys/resource.h>
  21. #include <setjmp.h>
  22. #include <net/if.h>
  23. #include <netinet/in.h>
  24. #include <netdb.h>
  25. static fd_set _listen_sockets;
  26. static char _exception_label[64];
  27. /*+-------------------------------------------------------------------------
  28. listenTCPWithTimeout(interfaceIP,tcpPortNum,timeoutMsecs,fdReturned)
  29. --------------------------------------------------------------------------*/
  30. int
  31. listenTCPWithTimeout(interfaceIP,tcpPortNum,timeoutMsecs,fdReturned)
  32. UINT32 interfaceIP;
  33. UINT16 tcpPortNum;
  34. UINT32 timeoutMsecs;
  35. int *fdReturned;
  36. {
  37. int iv0Set = 0;
  38. int i;
  39. int fd = -1;
  40. int erc = 0;
  41. struct stat _st, *st = &_st;
  42. fd_set selectFDSET;
  43. struct timeval timer;
  44. struct sockaddr_in _myAddr,*myAddr = &_myAddr;
  45. struct sockaddr_in _hisAddr,*hisAddr = &_hisAddr;
  46. memset((char *)&_listen_sockets,0,sizeof(_listen_sockets));
  47. errno = 0;
  48. fd = socket(PF_INET, SOCK_STREAM, 0);
  49. if (fd < 0)
  50. {
  51. pperror("\nsocket creation");
  52. erc = eConnectFailed;
  53. goto EXIT;
  54. }
  55. i = 1;
  56. if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i)) < 0)
  57. {
  58. pperror("sockserv: setsockopt SO_REUSEADDR");
  59. erc = eConnectFailed;
  60. goto EXIT;
  61. }
  62. memset((char *)myAddr, 0, sizeof(*myAddr));
  63. myAddr->sin_family = AF_INET;
  64. myAddr->sin_addr.s_addr = htonl(interfaceIP);
  65. myAddr->sin_port = htons(tcpPortNum);
  66. i = bind(fd,myAddr,sizeof(*myAddr));
  67. if(i < 0)
  68. {
  69. char s128[128];
  70. sprintf(s128,"sockserv: bind error on port %d",tcpPortNum);
  71. pperror(s128);
  72. erc = eConnectFailed;
  73. goto EXIT;
  74. }
  75. i = 1;
  76. if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i)) < 0)
  77. {
  78. pperror("sockserv: setsockopt SO_REUSEADDR");
  79. erc = eConnectFailed;
  80. goto EXIT;
  81. }
  82. if(listen(fd,5))
  83. {
  84. pperror("sockserv: start_stream_listen listen");
  85. erc = eConnectFailed;
  86. goto EXIT;
  87. }
  88. if(proc_trace)
  89. {
  90. pprintf("sockserv: stream socket established port %d (fd=%d)\n",
  91. tcpPortNum,fd);
  92. }
  93. FD_SET(fd,&_listen_sockets);
  94. memcpy((char *)&selectFDSET,(char *)&_listen_sockets,sizeof(selectFDSET));
  95. timer.tv_sec = timeoutMsecs / 1000;
  96. timer.tv_usec = (timeoutMsecs % 1000) * 1000;
  97. errno = 0;
  98. if((i = select(fd + 1,&selectFDSET,0,0,&timer)) < 0)
  99. {
  100. if(proc_trace)
  101. {
  102. pprintf("sockserv: errno=%d", errno);
  103. pperror("");
  104. }
  105. /* interrupt or error */
  106. iv[0] = 2;
  107. iv0Set = 1;
  108. }
  109. else if(!i)
  110. {
  111. /* timeout */
  112. iv[0] = 1;
  113. iv0Set = 1;
  114. }
  115. else
  116. {
  117. /* we got one */
  118. int accept_fd = fd;
  119. i = sizeof(*hisAddr);
  120. if(proc_trace)
  121. pprintf("listenTCPWithTimeout accepting on fd=%d\n",fd);
  122. if((fd = accept(accept_fd, (struct sockaddr *)hisAddr, &i)) < 0)
  123. {
  124. close(accept_fd);
  125. pputs("accept() error\n");
  126. erc = eConnectFailed;
  127. iv[0] = 40;
  128. iv0Set = 1;
  129. goto EXIT;
  130. }
  131. if(fd != accept_fd)
  132. close(accept_fd);
  133. /*
  134. * see if we really have a connection
  135. */
  136. if (fstat(fd, st))
  137. {
  138. pputs("connection closed by remote\n");
  139. erc = eConnectFailed;
  140. iv[0] = 41;
  141. iv0Set = 1;
  142. goto EXIT;
  143. }
  144. if(proc_trace)
  145. {
  146. char s32[32];
  147. mode_map(st->st_mode, s32);
  148. pprintf("socket fd=%d mode=%s\n",fd,s32);
  149. }
  150. fchmod(fd,0666);
  151. if(proc_trace)
  152. {
  153. char s32[32];
  154. fstat(fd, st);
  155. mode_map(st->st_mode, s32);
  156. pprintf("socket fd=%d mode=%s\n",fd,s32);
  157. }
  158. iv[0] = 0;
  159. iv0Set = 1;
  160. }
  161. EXIT:
  162. if(proc_trace && iv0Set)
  163. pprintf("listenTCPWithTimeout set $i0 to %d\n",(int)iv[0]);
  164. *fdReturned = fd;
  165. if(!erc)
  166. return(0);
  167. close(fd);
  168. return (0);
  169. } /* end of listenTCPWithTimeout */
  170. /*+-------------------------------------------------------------------------
  171. sockserveException() -- I/O error
  172. --------------------------------------------------------------------------*/
  173. void
  174. sockserveException()
  175. {
  176. ESD *e = esdalloc(256); /* trusting about memory errors in old age :/ */
  177. shm->Lsockserve = 0;
  178. shm->Ltelnet = 0;
  179. sprintf(e->pb,"goto %s",_exception_label);
  180. e->cb = strlen(e->pb);
  181. if(proc_trace)
  182. pprintf("SOCKET EXCEPTION - forcing %s\n",e->pb);
  183. execute_esd(e);
  184. esdfree(e);
  185. } /* end of sockserveException */
  186. /*+-------------------------------------------------------------------------
  187. pcmd_sockserv(param)
  188. --------------------------------------------------------------------------*/
  189. int
  190. pcmd_sockserve(param)
  191. ESD *param;
  192. {
  193. char interfaceIpAddrString[32];
  194. ulong ipaddr;
  195. long portnum;
  196. int fd;
  197. int i;
  198. int erc;
  199. struct sockaddr_in _hisAddr,*hisAddr = &_hisAddr;
  200. char switches[32];
  201. strcpy(interfaceIpAddrString,"0.0.0.0");
  202. get_switches(param, switches, sizeof(switches));
  203. if(strchr(switches,'i'))
  204. {
  205. if (erc = get_ipaddr_zstr(param, interfaceIpAddrString,
  206. sizeof(interfaceIpAddrString)))
  207. return (erc);
  208. }
  209. if (erc = gint(param, &portnum))
  210. return (erc);
  211. if (erc = get_alphanum_zstr(param, _exception_label, sizeof(_exception_label)))
  212. return (erc);
  213. if (!find_cproc_labelled_lcb(_exception_label))
  214. {
  215. pprintf("goto/gosub label not found: %s\n", _exception_label);
  216. return (eFATAL_ALREADY);
  217. }
  218. if(interfaceIpAddrString[0] == '0')
  219. ipaddr = INADDR_ANY;
  220. else
  221. ipaddr = inet_atou(interfaceIpAddrString);
  222. if (proc_trace)
  223. pprintf("sockserv interface = %s\n",inet_utoa(ipaddr));
  224. if(erc = listenTCPWithTimeout(ipaddr,(UINT16)portnum,240 * 1000,&fd))
  225. {
  226. pprintf("listenTCPWithTimeout erc = %04X (%s)\n",erc,
  227. erc_text(erc));
  228. goto EXIT;
  229. }
  230. shm->Liofd = fd;
  231. shm->Ltelnet = 1;
  232. shm->Lconnected = 1;
  233. shm->Lsockserve = 1;
  234. if (proc_trace)
  235. pprintf("sockserv wire fd= %d\n",fd);
  236. i = sizeof(*hisAddr);
  237. getpeername(fd, (struct sockaddr *)hisAddr, &i);
  238. sprintf(shm->Lipaddr_str, "%s:%d",
  239. inet_utoa(htonl(hisAddr->sin_addr.s_addr)),
  240. htons(hisAddr->sin_port));
  241. if (proc_trace)
  242. pprintf("sockserv connected to %s\n", shm->Lipaddr_str);
  243. iv[0] = 0;
  244. EXIT:;
  245. if(proc_trace)
  246. pprintf("sockserv exiting with $i0 to %d\n",(int)iv[0]);
  247. return(0);
  248. } /* end of pcmd_sockserv */
  249. /*+-------------------------------------------------------------------------
  250. pcmd_sockclose(param)
  251. --------------------------------------------------------------------------*/
  252. int
  253. pcmd_sockclose(param)
  254. ESD *param;
  255. {
  256. if (!shm->Lsockserve)
  257. {
  258. pprintf("line not opened by sockserve\n");
  259. return (eFATAL_ALREADY);
  260. }
  261. close(shm->Liofd);
  262. shm->Ltelnet = 0;
  263. shm->Lconnected = 0;
  264. shm->Lsockserve = 0;
  265. if (proc_trace)
  266. pprintf("sockclose closed fd= %d\n",shm->Liofd);
  267. return(0);
  268. } /* end of pcmd_sockclose */
  269. #endif /* CFG_TelnetOption */