Click to See Complete Forum and Search --> : Socket question


goon12
07-30-2002, 01:22 PM
Ok I am just learning socket programming and I am trying to write a simeple program to check for an open relay. Normally I do this by doing this:
1. "telnet mail.some.server 25"
2. "helo my.domain.com"
3. "MAIL FROM: me@mydomain.com"
4. "RCPT TO: me@anotherdomain.com"

I usually get the normal messages, either Relay denied or sender ok.

Here is what my program looks like thus far..

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>

#define PORT 25

#define MAXDATASIZE 512 //max num of bytes we can get at once

int main( int argc, char *argv[])
{
int z;
int sockfd, numbytes;
char msg[] = "MAIL FROM: joe@crankhouse.com\n";
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; //connectors address info

if( argc != 2 )
{
fprintf(stderr, "user: clients <hostname>\n");
exit(1);
}

if( ( he=gethostbyname(argv[1]) ) == NULL )
{
perror("gethostbyname");
exit(2);
}

if( ( sockfd = socket(AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
perror("socket");
exit(3);
}

their_addr.sin_family = AF_INET; //network byte order
their_addr.sin_port = htons(PORT); // short, net byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);

if( connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1 )
{
perror("connect");
exit(4);
}

if( (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0 )) == -1 )
{
perror("recv");
exit(5);
}

buf[numbytes] = '\0';

printf("1st Message received: %s\n", buf);

// send message
if ( send(sockfd, "HELO my.domain.com\n", 19, 0) == -1)
{
perror("send()");
exit(7);
}

if( (numbytes=recv(sockfd, buf, sizeof(buf), 0 )) == -1 )
{
perror("recv()");
exit(8);
}

buf[numbytes] = '\0';
printf("2nd message received: %s\n", buf);

if ( send(sockfd, "MAIL FROM: joe@xxx.com\n", 23, 0) == -1)
{
perror("send()");
exit(7);
}

if( (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0))== -1)
{
perror("recv()");
exit(8);
}
buf[numbytes] = '\0';
printf("3rd message received: %s\n", buf);

close(sockfd);

return 0;
}


The program seems to work fine until it sends the second message, "MAIL FROM: joe@xxx.com". The message that is sent back to me is "501 invalid syntax" or "500 Unknown command".

Could someone please point me in the right direction, I am at a total loss.

Thanks,
goon12

The Kooman
07-31-2002, 12:04 AM
I'm not too sure on this but IIRC, you have to send a "\r" (if not "\r\n") instead of "\n" at the end of your commands. Just check up the rfc to be sure (no I don't know the number but I'm sure a google would get you there :)).

goon12
08-01-2002, 08:16 AM
Sorry it took so long. I am in the middle of reading the rfc, but I tried using '\r' and '\r\n' at the end of the string. I will let you know what does it for me.


Thanks,
goon12

dfx
08-01-2002, 03:45 PM
Try with "MAIL FROM:<joe@xxx.com>\r\n"