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. Should have been suspicious when it compiled cleanly without
error message.</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </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> </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> </DIV>
<DIV><FONT size=2>and then quits with errnum for "Operation not
supported".</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>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 </FONT><FONT size=2>to run on FreeBSD? References
for the correct way to write code to establish tcp connections?</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>Thanks</FONT></DIV>
<DIV><FONT color=#000000 size=2>Jim Flowers <<A
href="mailto:jflowers@ezo.net">jflowers@ezo.net</A>></FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV> </DIV>
<DIV><FONT color=#000000
size=2>//------------------------------------------------------------<BR>//
RRListen() - Creates a listen socket for session status
and<BR>//
restart requests<BR>int RRListen(unsigned short
*listenport)<BR>{<BR> unsigned short port;<BR> int
s;<BR> struct sockaddr_in saddr;</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2> if ((s = socket (AF_INET,
SOCK_DGRAM, 0)) < 0) {<BR> syslog(LOG_ERR,
"Error creating listen socket: %m");<BR>
return -1;<BR> }</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2> // Bind first available port starting at
7770<BR> port = 7770;<BR> saddr.sin_family =
AF_INET;<BR> saddr.sin_addr.s_addr = INADDR_ANY;<BR> do
{<BR> saddr.sin_port =
htons(port);<BR> errno =
0;<BR> if (bind(s, (struct sockaddr *) &saddr,
sizeof(struct sockaddr_in)) < 0) {<BR> if (errno != EADDRINUSE)
{<BR>
syslog(LOG_ERR, "Error binding listen socket:
%m");<BR>
close(s);<BR> return
-1;<BR> } else
{<BR>
port++;<BR>
}<BR> }<BR> } while (errno ==
EADDRINUSE);</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2> if (listen(s, 1) < 0)
{<BR> syslog(LOG_ERR, "Error listening on socket:
%m");<BR> close(s);<BR>
return -1;<BR> }</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2> *listenport = port;<BR>
syslog(LOG_INFO, "Established listener on port: %i",
port);<BR> 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>
