util_sock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. Samba utility functions
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. const char *unix_error_string (int error_num);
  20. #ifdef WITH_SSL
  21. #include <ssl.h>
  22. #undef Realloc /* SSLeay defines this and samba has a function of this name */
  23. extern SSL *ssl;
  24. extern int sslFd;
  25. #endif /* WITH_SSL */
  26. extern int DEBUGLEVEL;
  27. BOOL passive = False;
  28. /* the client file descriptor */
  29. int Client = -1;
  30. /* the last IP received from */
  31. struct in_addr lastip;
  32. /* the last port received from */
  33. int lastport=0;
  34. int smb_read_error = 0;
  35. /****************************************************************************
  36. determine if a file descriptor is in fact a socket
  37. ****************************************************************************/
  38. BOOL is_a_socket(int fd)
  39. {
  40. int v;
  41. unsigned int l;
  42. l = sizeof(int);
  43. return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
  44. }
  45. enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
  46. static const struct
  47. {
  48. const char *name;
  49. int level;
  50. int option;
  51. int value;
  52. int opttype;
  53. } socket_options[] = {
  54. {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
  55. {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
  56. {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
  57. #ifdef TCP_NODELAY
  58. {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
  59. #endif
  60. #ifdef IPTOS_LOWDELAY
  61. {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
  62. #endif
  63. #ifdef IPTOS_THROUGHPUT
  64. {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
  65. #endif
  66. #ifdef SO_SNDBUF
  67. {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
  68. #endif
  69. #ifdef SO_RCVBUF
  70. {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
  71. #endif
  72. #ifdef SO_SNDLOWAT
  73. {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
  74. #endif
  75. #ifdef SO_RCVLOWAT
  76. {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
  77. #endif
  78. #ifdef SO_SNDTIMEO
  79. {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
  80. #endif
  81. #ifdef SO_RCVTIMEO
  82. {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
  83. #endif
  84. {NULL,0,0,0,0}};
  85. /****************************************************************************
  86. set user socket options
  87. ****************************************************************************/
  88. void set_socket_options(int fd, char *options)
  89. {
  90. fstring tok;
  91. while (next_token(&options,tok," \t,", sizeof(tok)))
  92. {
  93. int ret=0,i;
  94. int value = 1;
  95. char *p;
  96. BOOL got_value = False;
  97. if ((p = strchr(tok,'=')))
  98. {
  99. *p = 0;
  100. value = atoi(p+1);
  101. got_value = True;
  102. }
  103. for (i=0;socket_options[i].name;i++)
  104. if (strequal(socket_options[i].name,tok))
  105. break;
  106. if (!socket_options[i].name)
  107. {
  108. DEBUG(0,("Unknown socket option %s\n",tok));
  109. continue;
  110. }
  111. switch (socket_options[i].opttype)
  112. {
  113. case OPT_BOOL:
  114. case OPT_INT:
  115. ret = setsockopt(fd,socket_options[i].level,
  116. socket_options[i].option,(char *)&value,sizeof(int));
  117. break;
  118. case OPT_ON:
  119. if (got_value)
  120. DEBUG(0,("syntax error - %s does not take a value\n",tok));
  121. {
  122. int on = socket_options[i].value;
  123. ret = setsockopt(fd,socket_options[i].level,
  124. socket_options[i].option,(char *)&on,sizeof(int));
  125. }
  126. break;
  127. }
  128. if (ret != 0)
  129. DEBUG(0,("Failed to set socket option %s\n",tok));
  130. }
  131. }
  132. /****************************************************************************
  133. close the socket communication
  134. ****************************************************************************/
  135. void close_sockets(void )
  136. {
  137. #ifdef WITH_SSL
  138. sslutil_disconnect(Client);
  139. #endif /* WITH_SSL */
  140. close(Client);
  141. Client = -1;
  142. }
  143. /****************************************************************************
  144. write to a socket
  145. ****************************************************************************/
  146. ssize_t write_socket(int fd,char *buf,size_t len)
  147. {
  148. ssize_t ret=0;
  149. if (passive)
  150. return(len);
  151. DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
  152. ret = write_data(fd,buf,len);
  153. DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
  154. if(ret <= 0)
  155. DEBUG(1,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
  156. (int)len, fd, unix_error_string (errno) ));
  157. return(ret);
  158. }
  159. /****************************************************************************
  160. read from a socket
  161. ****************************************************************************/
  162. ssize_t read_udp_socket(int fd,char *buf,size_t len)
  163. {
  164. ssize_t ret;
  165. struct sockaddr_in sock;
  166. unsigned int socklen;
  167. socklen = sizeof(sock);
  168. memset((char *)&sock,'\0',socklen);
  169. memset((char *)&lastip,'\0',sizeof(lastip));
  170. ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
  171. if (ret <= 0) {
  172. DEBUG(2,("read socket failed. ERRNO=%s\n", unix_error_string (errno)));
  173. return(0);
  174. }
  175. lastip = sock.sin_addr;
  176. lastport = ntohs(sock.sin_port);
  177. DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
  178. inet_ntoa(lastip), lastport, (int)ret));
  179. return(ret);
  180. }
  181. /****************************************************************************
  182. read data from a device with a timout in msec.
  183. mincount = if timeout, minimum to read before returning
  184. maxcount = number to be read.
  185. time_out = timeout in milliseconds
  186. ****************************************************************************/
  187. ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
  188. {
  189. fd_set fds;
  190. int selrtn;
  191. ssize_t readret;
  192. size_t nread = 0;
  193. struct timeval timeout;
  194. /* just checking .... */
  195. if (maxcnt <= 0) return(0);
  196. smb_read_error = 0;
  197. /* Blocking read */
  198. if (time_out <= 0) {
  199. if (mincnt == 0) mincnt = maxcnt;
  200. while (nread < mincnt) {
  201. #ifdef WITH_SSL
  202. if(fd == sslFd){
  203. readret = SSL_read(ssl, buf + nread, maxcnt - nread);
  204. }else{
  205. readret = read(fd, buf + nread, maxcnt - nread);
  206. }
  207. #else /* WITH_SSL */
  208. readret = read(fd, buf + nread, maxcnt - nread);
  209. #endif /* WITH_SSL */
  210. if (readret == 0) {
  211. DEBUG(5,("read_with_timeout: blocking read. EOF from client.\n"));
  212. smb_read_error = READ_EOF;
  213. return -1;
  214. }
  215. if (readret == -1) {
  216. DEBUG(0,("read_with_timeout: read error = %s.\n", unix_error_string (errno) ));
  217. smb_read_error = READ_ERROR;
  218. return -1;
  219. }
  220. nread += readret;
  221. }
  222. return((ssize_t)nread);
  223. }
  224. /* Most difficult - timeout read */
  225. /* If this is ever called on a disk file and
  226. mincnt is greater then the filesize then
  227. system performance will suffer severely as
  228. select always returns true on disk files */
  229. /* Set initial timeout */
  230. timeout.tv_sec = (time_t)(time_out / 1000);
  231. timeout.tv_usec = (long)(1000 * (time_out % 1000));
  232. for (nread=0; nread < mincnt; )
  233. {
  234. FD_ZERO(&fds);
  235. FD_SET(fd,&fds);
  236. selrtn = sys_select(fd+1,&fds,&timeout);
  237. /* Check if error */
  238. if(selrtn == -1) {
  239. /* something is wrong. Maybe the socket is dead? */
  240. DEBUG(0,("read_with_timeout: timeout read. select error = %s.\n", unix_error_string (errno) ));
  241. smb_read_error = READ_ERROR;
  242. return -1;
  243. }
  244. /* Did we timeout ? */
  245. if (selrtn == 0) {
  246. DEBUG(10,("read_with_timeout: timeout read. select timed out.\n"));
  247. smb_read_error = READ_TIMEOUT;
  248. return -1;
  249. }
  250. #ifdef WITH_SSL
  251. if(fd == sslFd){
  252. readret = SSL_read(ssl, buf + nread, maxcnt - nread);
  253. }else{
  254. readret = read(fd, buf + nread, maxcnt - nread);
  255. }
  256. #else /* WITH_SSL */
  257. readret = read(fd, buf+nread, maxcnt-nread);
  258. #endif /* WITH_SSL */
  259. if (readret == 0) {
  260. /* we got EOF on the file descriptor */
  261. DEBUG(5,("read_with_timeout: timeout read. EOF from client.\n"));
  262. smb_read_error = READ_EOF;
  263. return -1;
  264. }
  265. if (readret == -1) {
  266. /* the descriptor is probably dead */
  267. DEBUG(0,("read_with_timeout: timeout read. read error = %s.\n", unix_error_string (errno) ));
  268. smb_read_error = READ_ERROR;
  269. return -1;
  270. }
  271. nread += readret;
  272. }
  273. /* Return the number we got */
  274. return((ssize_t)nread);
  275. }
  276. /****************************************************************************
  277. send a keepalive packet (rfc1002)
  278. ****************************************************************************/
  279. BOOL send_keepalive(int client)
  280. {
  281. unsigned char buf[4];
  282. buf[0] = 0x85;
  283. buf[1] = buf[2] = buf[3] = 0;
  284. return(write_data(client,(char *)buf,4) == 4);
  285. }
  286. /****************************************************************************
  287. read data from the client, reading exactly N bytes.
  288. ****************************************************************************/
  289. ssize_t read_data(int fd,char *buffer,size_t N)
  290. {
  291. ssize_t ret;
  292. size_t total=0;
  293. smb_read_error = 0;
  294. while (total < N)
  295. {
  296. #ifdef WITH_SSL
  297. if(fd == sslFd){
  298. ret = SSL_read(ssl, buffer + total, N - total);
  299. }else{
  300. ret = read(fd,buffer + total,N - total);
  301. }
  302. #else /* WITH_SSL */
  303. ret = read(fd,buffer + total,N - total);
  304. #endif /* WITH_SSL */
  305. if (ret == 0)
  306. {
  307. DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
  308. smb_read_error = READ_EOF;
  309. return 0;
  310. }
  311. if (ret == -1)
  312. {
  313. DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
  314. smb_read_error = READ_ERROR;
  315. return -1;
  316. }
  317. total += ret;
  318. }
  319. return (ssize_t)total;
  320. }
  321. /****************************************************************************
  322. write data to a fd
  323. ****************************************************************************/
  324. ssize_t write_data(int fd,char *buffer,size_t N)
  325. {
  326. size_t total=0;
  327. ssize_t ret;
  328. while (total < N)
  329. {
  330. #ifdef WITH_SSL
  331. if(fd == sslFd){
  332. ret = SSL_write(ssl,buffer + total,N - total);
  333. }else{
  334. ret = write(fd,buffer + total,N - total);
  335. }
  336. #else /* WITH_SSL */
  337. ret = write(fd,buffer + total,N - total);
  338. #endif /* WITH_SSL */
  339. if (ret == -1) {
  340. DEBUG(1,("write_data: write failure. Error = %s\n", unix_error_string (errno) ));
  341. return -1;
  342. }
  343. if (ret == 0) return total;
  344. total += ret;
  345. }
  346. return (ssize_t)total;
  347. }
  348. /****************************************************************************
  349. read 4 bytes of a smb packet and return the smb length of the packet
  350. store the result in the buffer
  351. This version of the function will return a length of zero on receiving
  352. a keepalive packet.
  353. timeout is in milliseconds.
  354. ****************************************************************************/
  355. static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
  356. {
  357. ssize_t len=0;
  358. int msg_type;
  359. BOOL ok = False;
  360. while (!ok)
  361. {
  362. if (timeout > 0)
  363. ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
  364. else
  365. ok = (read_data(fd,inbuf,4) == 4);
  366. if (!ok)
  367. return(-1);
  368. len = smb_len(inbuf);
  369. msg_type = CVAL(inbuf,0);
  370. if (msg_type == 0x85)
  371. DEBUG(5,("Got keepalive packet\n"));
  372. }
  373. DEBUG(10,("got smb length of %d\n", (int)len));
  374. return(len);
  375. }
  376. #if 0
  377. /****************************************************************************
  378. read 4 bytes of a smb packet and return the smb length of the packet
  379. store the result in the buffer. This version of the function will
  380. never return a session keepalive (length of zero).
  381. timeout is in milliseconds.
  382. ****************************************************************************/
  383. ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
  384. {
  385. ssize_t len;
  386. for(;;)
  387. {
  388. len = read_smb_length_return_keepalive(fd, inbuf, timeout);
  389. if(len < 0)
  390. return len;
  391. /* Ignore session keepalives. */
  392. if(CVAL(inbuf,0) != 0x85)
  393. break;
  394. }
  395. DEBUG(10,("read_smb_length: got smb length of %d\n",len));
  396. return len;
  397. }
  398. #endif /* 0 */
  399. /****************************************************************************
  400. read an smb from a fd. Note that the buffer *MUST* be of size
  401. BUFFER_SIZE+SAFETY_MARGIN.
  402. The timeout is in milliseconds.
  403. This function will return on a
  404. receipt of a session keepalive packet.
  405. ****************************************************************************/
  406. BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
  407. {
  408. ssize_t len,ret;
  409. smb_read_error = 0;
  410. memset(buffer,'\0',smb_size + 100);
  411. len = read_smb_length_return_keepalive(fd,buffer,timeout);
  412. if (len < 0)
  413. {
  414. DEBUG(10,("receive_smb: length < 0!\n"));
  415. return(False);
  416. }
  417. if (len > BUFFER_SIZE) {
  418. DEBUG(0,("Invalid packet length! (%d bytes).\n", (int)len));
  419. if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
  420. {
  421. exit(1);
  422. }
  423. }
  424. if(len > 0) {
  425. ret = read_data(fd,buffer+4,len);
  426. if (ret != len) {
  427. smb_read_error = READ_ERROR;
  428. return False;
  429. }
  430. }
  431. return(True);
  432. }
  433. /****************************************************************************
  434. read an smb from a fd ignoring all keepalive packets. Note that the buffer
  435. *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
  436. The timeout is in milliseconds
  437. This is exactly the same as receive_smb except that it never returns
  438. a session keepalive packet (just as receive_smb used to do).
  439. receive_smb was changed to return keepalives as the oplock processing means this call
  440. should never go into a blocking read.
  441. ****************************************************************************/
  442. BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
  443. {
  444. BOOL ret;
  445. for(;;)
  446. {
  447. ret = receive_smb(fd, buffer, timeout);
  448. if (!ret)
  449. {
  450. DEBUG(10,("client_receive_smb failed\n"));
  451. show_msg(buffer);
  452. return ret;
  453. }
  454. /* Ignore session keepalive packets. */
  455. if(CVAL(buffer,0) != 0x85)
  456. break;
  457. }
  458. show_msg(buffer);
  459. return ret;
  460. }
  461. /****************************************************************************
  462. send an null session message to a fd
  463. ****************************************************************************/
  464. #if 0
  465. BOOL send_null_session_msg(int fd)
  466. {
  467. ssize_t ret;
  468. uint32 blank = 0;
  469. size_t len = 4;
  470. size_t nwritten=0;
  471. char *buffer = (char *)&blank;
  472. while (nwritten < len)
  473. {
  474. ret = write_socket(fd,buffer+nwritten,len - nwritten);
  475. if (ret <= 0)
  476. {
  477. DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
  478. close_sockets();
  479. exit(1);
  480. }
  481. nwritten += ret;
  482. }
  483. DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
  484. return True;
  485. }
  486. /****************************************************************************
  487. send an smb to a fd
  488. ****************************************************************************/
  489. BOOL send_smb(int fd,char *buffer)
  490. {
  491. size_t len;
  492. size_t nwritten=0;
  493. ssize_t ret;
  494. len = smb_len(buffer) + 4;
  495. while (nwritten < len)
  496. {
  497. ret = write_socket(fd,buffer+nwritten,len - nwritten);
  498. if (ret <= 0)
  499. {
  500. DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
  501. close_sockets();
  502. exit(1);
  503. }
  504. nwritten += ret;
  505. }
  506. return True;
  507. }
  508. /****************************************************************************
  509. send a single packet to a port on another machine
  510. ****************************************************************************/
  511. BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
  512. {
  513. BOOL ret;
  514. int out_fd;
  515. struct sockaddr_in sock_out;
  516. if (passive)
  517. return(True);
  518. /* create a socket to write to */
  519. out_fd = socket(AF_INET, type, 0);
  520. if (out_fd == -1)
  521. {
  522. DEBUG(0,("socket failed"));
  523. return False;
  524. }
  525. /* set the address and port */
  526. memset((char *)&sock_out,'\0',sizeof(sock_out));
  527. putip((char *)&sock_out.sin_addr,(char *)&ip);
  528. sock_out.sin_port = htons( port );
  529. sock_out.sin_family = AF_INET;
  530. if (DEBUGLEVEL > 0)
  531. DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
  532. len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
  533. /* send it */
  534. ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
  535. if (!ret)
  536. DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
  537. inet_ntoa(ip),port,unix_error_string (errno)));
  538. close(out_fd);
  539. return(ret);
  540. }
  541. #endif /* 0 */
  542. /****************************************************************************
  543. open a socket of the specified type, port and address for incoming data
  544. ****************************************************************************/
  545. int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
  546. {
  547. struct hostent *hp;
  548. struct sockaddr_in sock;
  549. pstring host_name;
  550. int res;
  551. /* get my host name */
  552. if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
  553. { DEBUG(0,("gethostname failed\n")); return -1; }
  554. /* get host info */
  555. if ((hp = Get_Hostbyname(host_name)) == 0)
  556. {
  557. DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
  558. return -1;
  559. }
  560. memset((char *)&sock,'\0',sizeof(sock));
  561. memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
  562. #ifdef HAVE_SOCK_SIN_LEN
  563. sock.sin_len = sizeof(sock);
  564. #endif
  565. sock.sin_port = htons( port );
  566. sock.sin_family = hp->h_addrtype;
  567. sock.sin_addr.s_addr = socket_addr;
  568. res = socket(hp->h_addrtype, type, 0);
  569. if (res == -1)
  570. { DEBUG(0,("socket failed\n")); return -1; }
  571. {
  572. int val=1;
  573. if(rebind)
  574. val=1;
  575. else
  576. val=0;
  577. setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
  578. }
  579. /* now we've got a socket - we need to bind it */
  580. if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
  581. {
  582. if (port) {
  583. if (port == SMB_PORT || port == NMB_PORT)
  584. DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
  585. port,inet_ntoa(sock.sin_addr),unix_error_string (errno)));
  586. close(res);
  587. if (dlevel > 0 && port < 1000)
  588. port = 7999;
  589. if (port >= 1000 && port < 9000)
  590. return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
  591. }
  592. return(-1);
  593. }
  594. DEBUG(3,("bind succeeded on port %d\n",port));
  595. return res;
  596. }
  597. /****************************************************************************
  598. create an outgoing socket. timeout is in milliseconds.
  599. **************************************************************************/
  600. int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
  601. {
  602. struct sockaddr_in sock_out;
  603. int res,ret;
  604. int connect_loop = 250; /* 250 milliseconds */
  605. int loops = (timeout) / connect_loop;
  606. /* create a socket to write to */
  607. res = socket(PF_INET, type, 0);
  608. if (res == -1)
  609. { DEBUG(0,("socket error\n")); return -1; }
  610. if (type != SOCK_STREAM) return(res);
  611. memset((char *)&sock_out,'\0',sizeof(sock_out));
  612. putip((char *)&sock_out.sin_addr,(char *)addr);
  613. sock_out.sin_port = htons( port );
  614. sock_out.sin_family = PF_INET;
  615. /* set it non-blocking */
  616. set_blocking(res,False);
  617. DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
  618. /* and connect it to the destination */
  619. connect_again:
  620. ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
  621. /* Some systems return EAGAIN when they mean EINPROGRESS */
  622. if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
  623. errno == EAGAIN) && loops--) {
  624. msleep(connect_loop);
  625. goto connect_again;
  626. }
  627. if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
  628. errno == EAGAIN)) {
  629. DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
  630. close(res);
  631. return -1;
  632. }
  633. #ifdef EISCONN
  634. if (ret < 0 && errno == EISCONN) {
  635. errno = 0;
  636. ret = 0;
  637. }
  638. #endif
  639. if (ret < 0) {
  640. DEBUG(1,("error connecting to %s:%d (%s)\n",
  641. inet_ntoa(*addr),port,unix_error_string (errno)));
  642. close(res);
  643. return -1;
  644. }
  645. /* set it blocking again */
  646. set_blocking(res,True);
  647. return res;
  648. }
  649. /*******************************************************************
  650. Reset the 'done' variables so after a client process is created
  651. from a fork call these calls will be re-done. This should be
  652. expanded if more variables need reseting.
  653. ******************************************************************/
  654. static BOOL global_client_name_done = False;
  655. static BOOL global_client_addr_done = False;
  656. /*******************************************************************
  657. return the DNS name of the client
  658. ******************************************************************/
  659. char *client_name(int fd)
  660. {
  661. struct sockaddr sa;
  662. struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
  663. unsigned int length = sizeof(sa);
  664. static pstring name_buf;
  665. struct hostent *hp;
  666. static int last_fd=-1;
  667. if (global_client_name_done && last_fd == fd)
  668. return name_buf;
  669. last_fd = fd;
  670. global_client_name_done = False;
  671. pstrcpy(name_buf,"UNKNOWN");
  672. if (fd == -1) {
  673. return name_buf;
  674. }
  675. if (getpeername(fd, &sa, &length) < 0) {
  676. DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
  677. return name_buf;
  678. }
  679. /* Look up the remote host name. */
  680. if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
  681. sizeof(sockin->sin_addr),
  682. AF_INET)) == 0) {
  683. DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
  684. StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
  685. } else {
  686. StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
  687. if (!matchname(name_buf, sockin->sin_addr)) {
  688. DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
  689. pstrcpy(name_buf,"UNKNOWN");
  690. }
  691. }
  692. global_client_name_done = True;
  693. return name_buf;
  694. }
  695. /*******************************************************************
  696. return the IP addr of the client as a string
  697. ******************************************************************/
  698. char *client_addr(int fd)
  699. {
  700. struct sockaddr sa;
  701. struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
  702. unsigned int length = sizeof(sa);
  703. static fstring addr_buf;
  704. static int last_fd = -1;
  705. if (global_client_addr_done && fd == last_fd)
  706. return addr_buf;
  707. last_fd = fd;
  708. global_client_addr_done = False;
  709. fstrcpy(addr_buf,"0.0.0.0");
  710. if (fd == -1) {
  711. return addr_buf;
  712. }
  713. if (getpeername(fd, &sa, &length) < 0) {
  714. DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
  715. return addr_buf;
  716. }
  717. fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
  718. global_client_addr_done = True;
  719. return addr_buf;
  720. }