util_sock.c 23 KB

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