Code Comments
Programming Forum and web based access to our favorite programming groups.Okay I'm having some problems with an NNTP client I am trying to write.
I have sent the username after recieving the requires authentification
command, but the program hangs on recv().
Basically I'm meant to recv a command saying that the server requires a
password as to be expected. I know that I have sent the username
command correctly as the send() command has returned the correct number
but the server just does not respond.
I then tried to implement the socket using O_NONBLOCKING but that did
not even let me connect. I have a feeling I am missing something very
very simple here but it is not jumping out at me.
int nntpLogin(char *buffer, size_t bufsize, int sock)
{
char loginName[] = "AUTHINFO USER <username>\r\n";
char passwd[] = "AUTHINFO PASS <password>\r\n";
send(sock, loginName, sizeof(loginName), 0);
recv(sock, buffer, bufsize, 0);
printf("%s", buffer);
send(sock, passwd, sizeof(passwd), 0);
recv(sock, buffer, bufsize, 0);
printf("%s", buffer);
return 0;
}
The username and password have been removed. Does anyone know what
might be going wrong here? It seems to be a blocking issue but I have
no idea how to correct it.
Any help is appreciated.
Post Follow-up to this messageNiz wrote:
> Okay I'm having some problems with an NNTP client I am trying to write.
> I have sent the username after recieving the requires authentification
> command, but the program hangs on recv().
>
> Basically I'm meant to recv a command saying that the server requires a
> password as to be expected. I know that I have sent the username command
> correctly as the send() command has returned the correct number but the
> server just does not respond.
>
> I then tried to implement the socket using O_NONBLOCKING but that did
> not even let me connect. I have a feeling I am missing something very
> very simple here but it is not jumping out at me.
>
> int nntpLogin(char *buffer, size_t bufsize, int sock)
> {
> char loginName[] = "AUTHINFO USER <username>\r\n";
> char passwd[] = "AUTHINFO PASS <password>\r\n";
>
> send(sock, loginName, sizeof(loginName), 0);
I don't know that this is the cause of your problem, but won't
sizeof() return the number of characters including the null
terminator, thus causing send() to send a zero byte after the
CRLF? That seems like not what you want to do.
- Logan
Post Follow-up to this messageOn 2008-03-16 19:05:58 +0000, Logan Shaw <lshaw-usenet@austin.rr.com> said: > > I don't know that this is the cause of your problem, but won't > sizeof() return the number of characters including the null > terminator, thus causing send() to send a zero byte after the > CRLF? That seems like not what you want to do. > > - Logan Thanks for the reply. I changed it to use strlen() instead but still get the same problem as noted in my original post. Even using a constant still produces the same problem so I doubt it has anything to do with that.
Post Follow-up to this messageIn article <2008031618095616807-niz@nicetrycom>, Niz <niz@nicetry.com>
wrote:
> Okay I'm having some problems with an NNTP client I am trying to write.
> I have sent the username after recieving the requires authentification
> command, but the program hangs on recv().
>
> Basically I'm meant to recv a command saying that the server requires a
> password as to be expected. I know that I have sent the username
> command correctly as the send() command has returned the correct number
> but the server just does not respond.
Send() returning just means that the bytes have been buffered in the
local kernel, it doesn't mean anything has actually been sent. Although
unless something's wrong, they should be sent in a fraction of a second.
>
> I then tried to implement the socket using O_NONBLOCKING but that did
> not even let me connect. I have a feeling I am missing something very
> very simple here but it is not jumping out at me.
>
> int nntpLogin(char *buffer, size_t bufsize, int sock)
> {
> char loginName[] = "AUTHINFO USER <username>\r\n";
> char passwd[] = "AUTHINFO PASS <password>\r\n";
>
> send(sock, loginName, sizeof(loginName), 0);
> recv(sock, buffer, bufsize, 0);
> printf("%s", buffer);
> send(sock, passwd, sizeof(passwd), 0);
> recv(sock, buffer, bufsize, 0);
> printf("%s", buffer);
>
> return 0;
> }
>
> The username and password have been removed. Does anyone know what
> might be going wrong here? It seems to be a blocking issue but I have
> no idea how to correct it.
>
> Any help is appreciated.
I have you tried doing a packet capture to see what's being sent and
received on the wire?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Post Follow-up to this messageOn Mar 16, 11:09 am, Niz <n...@nicetry.com> wrote:
I know you may be thinking that you'll just do things the quick way
first and clean it up later, but it will save you a lot of trouble to
do it right from the beginning.
> int nntpLogin(char *buffer, size_t bufsize, int sock)
> {
> char loginName[] = "AUTHINFO USER <username>\r\n";
> char passwd[] = "AUTHINFO PASS <password>\r\n";
>
> send(sock, loginName, sizeof(loginName), 0);
> recv(sock, buffer, bufsize, 0);
You throw away the return value. What if you only received one byte
and didn't get the whole message?
> printf("%s", buffer);
The '%s' specifier is for C-style strings. You don't have a C-style
string, you just have an arbitrary collection of characters. So why
are you treating it as a string?
> send(sock, passwd, sizeof(passwd), 0);
> recv(sock, buffer, bufsize, 0);
> printf("%s", buffer);
Same problem, you ignore the return value and then pretend you have a
string when you don't.
> return 0;
>
> }
If you 'telnet' to the NNTP server and send these two strings, do you
normally get a reply after the 'AUTHINFO PASS' line?
DS
Post Follow-up to this messageOn 2008-03-17 05:53:20 +0000, David Schwartz <davids@webmaster.com> said: > On Mar 16, 11:09 am, Niz <n...@nicetry.com> wrote: > > I know you may be thinking that you'll just do things the quick way > first and clean it up later, but it will save you a lot of trouble to > do it right from the beginning. > > You throw away the return value. What if you only received one byte > and didn't get the whole message? That is just for ease of reading on the newsgroup. The return value is checked in my actual code. > > > The '%s' specifier is for C-style strings. You don't have a C-style > string, you just have an arbitrary collection of characters. So why > are you treating it as a string? I was under the impression that a string was an arbitary collection of characters. Anyway it is irrelavant at the moment due to the fact that it never reaches this point anyway :). The %s printf statement works fine for a malloced array of char, what is the best option to use? > > If you 'telnet' to the NNTP server and send these two strings, do you > normally get a reply after the 'AUTHINFO PASS' line? > > DS Ah, doh never thought to use Telnet to check this.
Post Follow-up to this messageOn Mar 17, 1:21 am, Niz <n...@nicetry.com> wrote: > I was under the impression that a string was an arbitary collection of > characters. Anyway it is irrelavant at the moment due to the fact that > it never reaches this point anyway :). No. A C-style string is an array of non-zero characters with a terminating zero. An arbitrary collection of characters is *NOT* a C- style string and can be disastrous to pass it to functions that take C- style strings. > The %s printf statement works fine for a malloced array of char, what > is the best option to use? You can loop through all of the received characters, using the length returned from 'recv', and output then with '%c' if they are printable. You can also convert them into a C-style string by adding a terminating zero. (You may or may not need to handle any embedded zeroes, it depends on the protocol.) > Ah, doh never thought to use Telnet to check this. It may be that the server simply does not send a reply to this line. DS
Post Follow-up to this messageIn article <05b4e0dc-cdf7-455f-9ff3-00d16596518c@x41g2000hsb.googlegroups.com>, David Schwartz <davids@webmaster.com> wrote: > On Mar 17, 1:21 am, Niz <n...@nicetry.com> wrote: > > > > > No. A C-style string is an array of non-zero characters with a > terminating zero. An arbitrary collection of characters is *NOT* a C- > style string and can be disastrous to pass it to functions that take C- > style strings. > > > You can loop through all of the received characters, using the length > returned from 'recv', and output then with '%c' if they are printable. > You can also convert them into a C-style string by adding a > terminating zero. (You may or may not need to handle any embedded > zeroes, it depends on the protocol.) NNTP is a text-based protocol, there should never be any embedded nulls. > > > > It may be that the server simply does not send a reply to this line. NNTP is a simple request/reply protocol. Every request generates a reply. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE don't copy me on replies, I'll read them in the group ***
Post Follow-up to this messageOn Mar 17, 2:21 pm, Barry Margolin <bar...@alum.mit.edu> wrote: > NNTP is a text-based protocol, there should never be any embedded nulls. Right, but if you do get one, you really don't want to blow up. For example, if you keep accumulating data and use 'strchr' to check for a newline, a single received zero will cause your program to fail. That's just not robust. > NNTP is a simple request/reply protocol. Every request generates a > reply. Which leads to the question, is that a [defined] request? I don't know this particular aspect of NNTP in enough detail, but it may be legal, for example, for the server to return a "no password is needed" reply to the "AUTHINFO USER" command, leaving the "AUTHINFO PASS" not being a legal request in that context. DS
Post Follow-up to this messageIn article <9ad242a2-dd7f-43ca-8cae-e69acfdb1dbd@b1g2000hsg.googlegroups.com>, David Schwartz <davids@webmaster.com> wrote: > On Mar 17, 2:21 pm, Barry Margolin <bar...@alum.mit.edu> wrote: > > > Right, but if you do get one, you really don't want to blow up. For > example, if you keep accumulating data and use 'strchr' to check for a > newline, a single received zero will cause your program to fail. > That's just not robust. > > > > Which leads to the question, is that a [defined] request? I don't know > this particular aspect of NNTP in enough detail, but it may be legal, Yes, it's the standard way to specify a username and password. > for example, for the server to return a "no password is needed" reply > to the "AUTHINFO USER" command, leaving the "AUTHINFO PASS" not being > a legal request in that context. But his problem is that he's not getting ANY reply to the AUTHINFO USER command. His next call to recv() is not returning. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE don't copy me on replies, I'll read them in the group ***
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.