home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5219 < prev    next >
Encoding:
Text File  |  1992-11-09  |  6.6 KB  |  167 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!gumby!wupost!eclnews!achilles!pete
  3. From: pete@arl.wustl.edu (Peter Flugstad,,,)
  4. Subject: Re: SO_REUSEADDR (was: Re: Socket Programm
  5. Message-ID: <1992Nov9.161326.19246@wuecl.wustl.edu>
  6. Sender: usenet@wuecl.wustl.edu (Usenet Administrator)
  7. Nntp-Posting-Host: achilles
  8. Reply-To: pete@arl.wustl.edu
  9. Organization: Applied Research Lab, Washington University in St. Louis
  10. References: <101017@bu.edu>
  11. Date: Mon, 9 Nov 1992 16:13:26 GMT
  12. Lines: 153
  13.  
  14. In article 101017@bu.edu, tasos@cs.bu.edu (Anastasios Kotsikonas) writes:
  15. >In article <1992Nov7.023043.19007@wuecl.wustl.edu> pete@arl.wustl.edu writes:
  16.    [.. my comment saying SO_REUSEADDR doesn't work as advertised...]
  17. >
  18. >I am sorry but this is nonsense. I have used SO_REUSEADDR on BSD 4.1.1, AIX
  19. >3.1.5 and up, HP UX 8.05 and up, SGI OS 4.0 and up and pure SVR4 systems,
  20. >quite successfully I might add, for both privileged and non-privileged
  21. >ports.
  22. >
  23. >Tasos
  24.  
  25. hmmm, I think some more detail is needed...  I'm not saying your wrong
  26. (please, no flames :-),  just that I've got conflicting stories...
  27.  
  28. First, some context... I'm using SunOS 4.1.1, and I believe most of the poeple I 
  29. refer to are using BSD based (SunOS) UNIX as well.
  30.  
  31. (My appologies if reference someone and I don't give a name,  I didn't keep 
  32. most of the messages).
  33.  
  34. Someone had posted a message to comp.unix.internals saying that they were 
  35. having problems binding to a port twice in quick succession.   They had started 
  36. their program, killed it, then restarted it quickly again, attempting to bind to 
  37. the same local port both times.  They were getting an error back from the bind 
  38. system call stating "Address in use".   
  39.  
  40. Someone else posted a message suggesting they try using SO_REUSEADDR.  
  41.  
  42. I then posted a message saying that it has been my experience that this flag doesn't 
  43. work quite as advertised.  On occassion I have killed (ctrl-c) a running program that 
  44. had a TCP socket open.  Because my program catches ctrl-c and generally cleans up 
  45. after itself, explicitly closing open sockets and such, I assummed that re-starting it 
  46. immediatley would be no problem, and USUALLY THIS IS THE CASE.  But, I have 
  47. found that if there were outstanding messages in the TCP queue, then the socket would 
  48. be stuck in TIME_WAIT for 60 seconds, and so I'd get EADDRINUSE.  This occurred 
  49. despite my setting SO_REUSEADD, and SO_DONTLINGER (yes, I know it's going away).
  50.  
  51. Then, Chris Torek posted a followup to my message, examining some of the code that 
  52. implements this.  Here's his message:
  53.  
  54. ------ BEGIN included message ------
  55.  
  56. From torek@horse.ee.lbl.gov (Chris Torek)
  57. Path:eclnews!wupost!bcm!cs.utexas.edu!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  58. Newsgroups: comp.unix.internals
  59. Subject: Re: A problem in bind() system call.Help!
  60. Date: 5 Nov 1992 06:04:15 GMT
  61. Organization: Lawrence Berkeley Laboratory, Berkeley
  62. Lines: 82
  63. Message-ID: <27240@dog.ee.lbl.gov>
  64. References: <mahesh.720064067@depot.cis.ksu.edu.cis.ksu.edu> <1992Nov4.192429.10342@wuecl.wustl.edu>
  65. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  66. NNTP-Posting-Host: 128.3.112.15
  67.  
  68. In article <1992Nov4.192429.10342@wuecl.wustl.edu> pete@arl.wustl.edu writes:
  69. >... I tried to use SO_REUSEADDR as well, but I believe it doesn't work in
  70. >all cases.
  71.  
  72. Oddly enough, I just looked into something similar today.
  73.  
  74. SO_REUSEADDR is rather broken.  It does work for special cases.
  75.  
  76. The current 4BSD kernel---recent SunOS releases are similar---has this
  77. code in in_pcbbind():
  78.  
  79.     if ((so->so_options & SO_REUSEADDR) == 0 &&
  80.         ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
  81.          (so->so_options & SO_ACCEPTCONN) == 0))
  82.         wild = INPLOOKUP_WILDCARD;
  83.     if (in_pcblookup(..., sin->sin_addr, lport, wild))
  84.         return (EADDRINUSE);
  85.  
  86. The `wild' flag tells in_pcblookup to include all TCP or UDP sockets,
  87. rather than only those `listening'.  This is often desirable since a
  88. unique <laddr,lport> pair automatically guarantees that the 4-tuple
  89. <laddr,lport,faddr,fport> will be unique.  (TCP and UDP require the
  90. latter.)  At the time of the bind() call, the <faddr,fport> portion are
  91. unknown.
  92.  
  93. (This suggests---rather appropriately, actually---that the system
  94. really should have a combined bind and connect call, in which one of
  95. the two addresses is optional.  Then the kernel would usually have all
  96. the information it needs to pass judgement on the four-tuple, rather
  97. than having to second-guess two elements, or apply too-inclusive tests
  98. to the <laddr,lport> portion, as it does now.  But never mind that.)
  99.  
  100. The tests in in_pcbbind() use a `wildcard' lookup if:
  101.  
  102.     a) you did not set SO_REUSEADDR, and:
  103.         b1) this is a UDP socket (not PR_CONNREQUIRED), or:
  104.         b2) you have not yet done a listen() call.
  105.  
  106. Point b2) is most peculiar, because the `standard' method of writing a
  107. tcp server is:
  108.  
  109.     s = socket(...);
  110.     if (s < 0) ERROR
  111.     if (bind(s, ...)) ERROR
  112.     if (listen(s, 5)) ERROR
  113.     for (;;) {
  114.         s2 = accept(s, ...);
  115.         if (s2 < 0) ERROR
  116.         ...
  117.     }
  118.  
  119. If you put a listen() call before the bind(), it does its own internal
  120. bind(), and the system assigns you a port number.  This is only OK if
  121. you use a `service number service' a la Sun's portmap.  Even then,
  122. SO_ACCEPTCONN will not be set in in_pcbbind(), because listen() sets it
  123. *after* the internal bind.  Hence, this is effectively the same as:
  124.  
  125.     if (!SO_REUSEADDR && (is_udp || 1))
  126.         wild = INPLOOKUP_WILDCARD;
  127.  
  128. or just
  129.  
  130.     if (!SO_REUSEADDR)
  131.  
  132. Thus, all SO_REUSEADDR does is prevent setting INPLOOKUP_WILDCARD, and
  133. we can concentrate on the logic (or lack thereof?) in in_pcblookup.  It
  134. is long and twisty, but it boils down to one thing in the case of TCP
  135. servers:
  136.  
  137.  *** If you specify a local address (other than 0) in your bind()
  138.  *** call, SO_REUSEADDR will have no effect.
  139.  
  140. The code really ought to say
  141.  
  142.     if ((so->so_state & SO_REUSEADDR) == 0 && in_pcblookup(...))
  143.         return (EADDRINUSE);
  144.  
  145. and programmers who use SO_REUSEADDR should expect to get EADDRINUSE
  146. errors from connect() whenever they do not get a unique 4-tuple.
  147. -- 
  148. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  149. Berkeley, CA        Domain:    torek@ee.lbl.gov
  150.  
  151. ------ END included message ------
  152.  
  153. I really just want an answer to how to close a socket hard, in such a way that any data
  154. still in the system is discarded, and I can start up right away again.  I realise that discarding
  155. data is probably not a good idea, but usually if I'm killing the program, any pending data
  156. for it can be discarded as well.
  157.  
  158. Any definative answers, or is this another system and specific OS dependant thing???
  159.  
  160. ---
  161. Pete Flugstad                | Internet: pete@arl.wustl.edu
  162. Applied Research Laboratory        | Tel: (314) 935-6110
  163. Department of Computer Science        | "To err is Human, to really 
  164. Washington University in St. Louis    |  foul up requires a computer"
  165.  
  166.  
  167.