PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Bsd Sockets Problem



kingfinn
28-10-2009, 11:04
Hi all,

der untenstehende Code funktioniert nicht ganz.
Wenn er versucht, zu connecten, passiert nichts und es kommt zu Connection Timeout.
Was ist falsch?

MfG



/**************************************
* OverTheWire.org
* Wargame: Vortex
* Level 0
* by Kingfinn
*************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <iostream>


using namespace std;

int main()
{

/* Variables */
int sock; // Socket
unsigned int output[4]; // Four Values to read
unsigned int sum; // The Values Added together
char* passwd; // The Server Password
struct sockaddr_in addr; // Adress struct
struct hostent *host;

/* Get IP-Adress */
host = gethostbyname("vortex.labs.pulltheplug.org");
if (!host)
{
herror("gethostbyname() failed");
return 1;
}

/* Initialize struct */
addr.sin_family = AF_INET;
addr.sin_port = htonl(5842);
addr.sin_addr = *(struct in_addr*) host->h_addr_list[0];

cout << "Ip Adress of Target: " << addr.sin_addr.s_addr << endl;

/* Make Socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
cout << "Failed to open Socket. Try Again." << endl;
return 1;
}

cout << "Made Socket. Deskriptor: " << sock << endl;

/* Connect */
if(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0)
{
perror("connect() failed");
return 1;
}

cout << "Succesfully connected" << endl;

/* Read sum */
for(int i=0; i<4; i++)
{
recv(sock, (void*)output[i], 4, 0);
sum+=output[i];
}

cout << "Reading of unsigned integers done." << endl;

/* Send sum */
send(sock, &sum, 4, 0);

cout << "Sending of sum done" << endl;

/* Read Pw */
recv(sock, passwd, 1, 0);
cout << passwd << endl;

}

Yonibear
28-10-2009, 20:53
Was ist das überhaupt für ein Server auf den du connecten willst?
Wenn ich den Port mit netcat kontaktiere, passier auch erstmal nichts. Das heißt, entweder tut der Server nicht was er soll, oder das Protokoll erwartet dass der Client zuerst etwas schickt.

kingfinn
30-10-2009, 03:34
Dieser Server. (http://www.overthewire.org/wargames/vortex/level0.shtml)
Wenn die Bschreibung stimmt (wovon ich einfach mal ausgehe) müsste man eigentlich zuerst was lesen können...

MfG

Yonibear
30-10-2009, 19:52
Soweit ich das sehe, antwortet der Server überhaupt nicht, d.h. nicht auf Pings, und auch nicht auf Anfragen TCP-Verbindungen aufzumachen. Sieht so aus als ob er im Moment down ist.

tanis
01-11-2009, 19:28
ich hab das ebenfalls versucht, es scheint sich um einen asynchronen Verbindungsaufbau zu handeln, leider bin ich bis jetzt nicht dahinter gekommen wo das Problem liegt in meinem Code. Ich bekomme ebenfalls einen Timeout, da ich scheinbar nicht bemerke wann die Verbindung zu stande gekommen ist, um danach den Socket wieder auf Blocking zu setzen.



#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/select.h>

#include <sys/time.h>

int main (int argc, char *argv[])
{
unsigned int buffer = 0;
struct sockaddr_in dest;
int fd;
int fdmask;
int run;
int res;
int optval;
int on;
socklen_t optlen;
int max_fd;
fd_set read_fd;
fd_set write_fd;
fd_set except_fd;
int len;
int cnt = 0;
struct timeval timeout;

if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "socket: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

memset(&dest, 0, sizeof(struct sockaddr));
dest.sin_family = AF_INET;
dest.sin_port = htons(5842);
if (!inet_aton("69.55.233.89", &dest.sin_addr))
{
fprintf(stderr, "address is not valid ...\n");
close(fd);
exit(1);
}

if ((fdmask = fcntl(fd, F_GETFL, 0)) < 0)
{
fprintf(stderr, "fcntl[F_GETFL]: %s\n", strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}

fdmask |= O_NONBLOCK;

if (fcntl(fd, F_SETFL, fdmask) < 0)
{
fprintf(stderr, "fcntl[F_SETFL]: %s\n", strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}

if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *) &on, sizeof(on)) < 0) {
fprintf(stderr, "SO_KEEPALIVE: %s\n", strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}

if (connect(fd, (struct sockaddr *) &dest, sizeof(dest)) < 0 && errno != EINPROGRESS)
{
fprintf(stderr, "connect: %s\n", strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}

fprintf(stderr, ":connected ...\n");

max_fd = fd;

run = 1;
while (run)
{
FD_ZERO(&read_fd);
FD_ZERO(&write_fd);
FD_ZERO(&except_fd);
FD_SET(fd, &read_fd);
FD_SET(fd, &write_fd);
FD_SET(fd, &except_fd);

timeout.tv_sec = 5;
timeout.tv_usec = 0;

fprintf(stderr, "waiting for data ...\n");
if ((res = select(max_fd + 1, &read_fd, &write_fd, &except_fd, &timeout)) < 0)
{
fprintf(stderr, "select: %s\n", strerror(errno));
run = 0;
continue;
}

optlen = sizeof(int);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen) < 0)
{
fprintf(stderr, "getsockopt: %s\n", strerror(errno));
run = 0;
continue;
}
if (optval) {
fprintf(stderr, "SO_ERROR: %s (%d)\n", strerror(optval), optval);
run = 0;
continue;
}


if (FD_ISSET(fd, &write_fd))
{
if ((fdmask = fcntl(fd, F_GETFL, 0)) < 0)
{
fprintf(stderr, "fcntl[F_GETFL]: %s\n", strerror(errno));
run = 0;
continue;
}

fdmask &= (~O_NONBLOCK);

fprintf(stderr, "set blocking mode ...\n");
if (fcntl(fd, F_SETFL, fdmask) < 0)
{
fprintf(stderr, "fcntl[F_SETFL]: %s\n", strerror(errno));
run = 0;
}
continue;
}

if (cnt > 3)
{
run--;
continue;
}

if (FD_ISSET(fd, &read_fd))
{
if ((len = read(fd, &buffer, sizeof(int))) < 0)
{
fprintf(stderr, "read: %s\n", strerror(errno));
run = 0;
continue;
}

fprintf(stdout, ": size %d; buffer %d\n", len, buffer);
cnt++;
}
}

close(fd);

return 0;
}

tanis
04-11-2009, 08:32
Die haben ein Problem mit ihrem Server und aktuell läuft nur die Seite auf Port 80. Alle anderen Dienste sind nicht aktiv, daher der Timeout. :p :p :D