Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 6 Dec 1998 12:10:53 -0500
From:      "Jim Flowers" <jflowers@ezo.net>
To:        <freebsd-hackers@FreeBSD.ORG>, <freebsd-net@FreeBSD.ORG>
Subject:   Help with TCP listen() function
Message-ID:  <000d01be213b$625d6eb0$848266ce@crocus.ezo.net>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
I am trying to port a cable modem (roadrunner) login program written for linux to freebsd 2.2.7 before the Dec 15 deadline.  Should have been suspicious when it compiled cleanly without error message.

The program runs up to the point of setting up a TCP socket to request a login sequence using functions:

socket()
htons()
bind()
listen()

and then quits with errnum for "Operation not supported".

The function calls seem to be correctly written and do not return any errors when called (except for listen()).

Is this a correct sequence to establish a passive tcp connection?  Is there a better way to do it? Is there a knowledge base for converting linux code to run on FreeBSD?  References for the correct way to write code to establish tcp connections?

If a review of the code segment would be helpful, it is brief and follows.

Thanks
Jim Flowers <jflowers@ezo.net>


//------------------------------------------------------------
// RRListen() - Creates a listen socket for session status and
//              restart requests
int RRListen(unsigned short *listenport)
{
 unsigned short port;
   int s;
   struct sockaddr_in saddr;

   if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
      syslog(LOG_ERR, "Error creating listen socket: %m");
      return -1;
   }

 // Bind first available port starting at 7770
   port = 7770;
   saddr.sin_family = AF_INET;
   saddr.sin_addr.s_addr = INADDR_ANY;
   do {
    saddr.sin_port = htons(port);
      errno = 0;
      if (bind(s, (struct sockaddr *) &saddr, sizeof(struct sockaddr_in)) < 0) {
 if (errno != EADDRINUSE) {
           syslog(LOG_ERR, "Error binding listen socket: %m");
           close(s);
           return -1;
         } else {
    port++;
         }
      }
 } while (errno == EADDRINUSE);

 if (listen(s, 1) < 0) {
     syslog(LOG_ERR, "Error listening on socket: %m");
     close(s);
     return -1;
   }

   *listenport = port;
   syslog(LOG_INFO, "Established listener on port: %i", port);
   return s;
)
-------------------------------------------------------------------------------

[-- Attachment #2 --]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=text/html;charset=iso-8859-1 http-equiv=Content-Type>
<META content='"MSHTML 4.72.2106.6"' name=GENERATOR>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT color=#000000 size=2>I am trying to port a cable modem (roadrunner) 
login program written for linux to freebsd 2.2.7 before the Dec 15 
deadline.&nbsp; Should have been suspicious when it compiled cleanly without 
error message.</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>The program runs up to the point of setting up a TCP socket to 
request a login sequence using functions:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>socket()</FONT></DIV>
<DIV><FONT size=2>htons()</FONT></DIV>
<DIV><FONT size=2>bind()</FONT></DIV>
<DIV><FONT size=2>listen()</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>and then quits with errnum for &quot;Operation not 
supported&quot;.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>The function calls seem to be correctly written and do not 
return any errors when called (except for listen()).</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Is this a correct sequence to establish a passive tcp 
connection?&nbsp; Is there a better way to do it? Is there a knowledge base for 
converting linux code </FONT><FONT size=2>to run on FreeBSD?&nbsp; References 
for the correct way to write code to establish tcp connections?</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>If a review of the code segment would be helpful, it is brief 
and follows.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Thanks</FONT></DIV>
<DIV><FONT color=#000000 size=2>Jim Flowers &lt;<A 
href="mailto:jflowers@ezo.net">jflowers@ezo.net</A>&gt;</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=#000000 
size=2>//------------------------------------------------------------<BR>// 
RRListen() - Creates a listen socket for session status 
and<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
restart requests<BR>int RRListen(unsigned short 
*listenport)<BR>{<BR>&nbsp;unsigned short port;<BR>&nbsp;&nbsp; int 
s;<BR>&nbsp;&nbsp; struct sockaddr_in saddr;</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>&nbsp;&nbsp; if ((s = socket (AF_INET, 
SOCK_DGRAM, 0)) &lt; 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; syslog(LOG_ERR, 
&quot;Error creating listen socket: %m&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return -1;<BR>&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>&nbsp;// Bind first available port starting at 
7770<BR>&nbsp;&nbsp; port = 7770;<BR>&nbsp;&nbsp; saddr.sin_family = 
AF_INET;<BR>&nbsp;&nbsp; saddr.sin_addr.s_addr = INADDR_ANY;<BR>&nbsp;&nbsp; do 
{<BR>&nbsp;&nbsp;&nbsp; saddr.sin_port = 
htons(port);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; errno = 
0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bind(s, (struct sockaddr *) &amp;saddr, 
sizeof(struct sockaddr_in)) &lt; 0) {<BR>&nbsp;if (errno != EADDRINUSE) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
syslog(LOG_ERR, &quot;Error binding listen socket: 
%m&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
close(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
-1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else 
{<BR>&nbsp;&nbsp;&nbsp; 
port++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;} while (errno == 
EADDRINUSE);</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>&nbsp;if (listen(s, 1) &lt; 0) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp; syslog(LOG_ERR, &quot;Error listening on socket: 
%m&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp; close(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
return -1;<BR>&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>&nbsp;&nbsp; *listenport = port;<BR>&nbsp;&nbsp; 
syslog(LOG_INFO, &quot;Established listener on port: %i&quot;, 
port);<BR>&nbsp;&nbsp; return s;<BR>)</FONT></DIV>
<DIV><FONT color=#000000 
size=2>-------------------------------------------------------------------------------</FONT></DIV></BODY></HTML>

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000d01be213b$625d6eb0$848266ce>