PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : socket programmierung...



Takt
23-10-2003, 03:37
/************************************************** *************************
main.cpp - description
-------------------
begin : Don Okt 23 01:32:08 CEST 2003
copyright : (C) 2003 by Oliver Herms
email : takt@excluded.org
************************************************** *************************/

/************************************************** *************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************** *************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <arpa/inet.h>
#include <time.h>

#define CONFIG "/etc/expopper.conf"

void sig_chld(int signo);

int main(int argc, char *argv[])
{
pid_t pid;
time_t istime;
ssize_t len;
int sockfd;
int connfd;
int maxclients;
int port;
int loglevel;
char logfile[1024];
struct sockaddr_in servaddr, cliaddr;

if(getuid() != 0 || getgid() != 0)
{
cout << "You need to be root...\n";
return -1;
}

ifstream fin(CONFIG);
if(!fin)
{
cout << "No Config file found! Get sure there is a expopper.conf in /etc!\n";
return -1;
}
while(!fin.eof())
{
fin >> port;
fin >> maxclients;
fin >> logfile;
fin >> loglevel;
}
fin.close();

if(loglevel != 0)
{
ofstream fout(logfile, ios::app);
if(!fout)
{
cout << "Couldn't write to log file! Do you need to chmod? \n";
return -1;
}
else
{
if(loglevel == 2)
{
istime = time(NULL);
fout << ctime(&istime) << "config file exists...\n";
fout << ctime(&istime) << "logfile access granetd...\n";
}
}
fout.close();
}

memset(&servaddr, '\0', sizeof(servaddr));
servaddr.sin_family = AF_INET; /* IP v4 */
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
if(loglevel != 0)
{
ofstream fout(logfile, ios::app);
istime = time(NULL);
fout << ctime(&istime) << "Couldn't create socket!\n";
fout << ctime(&istime) << "expopper died!!\n";
fout.close();
}
return -1;
}
else
{
if(loglevel == 2)
{
ofstream fout(logfile, ios::app);
istime = time(NULL);
fout << ctime(&istime) << "Socket creation success!\n";
fout.close();
}
}

if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 && loglevel != 0)
{
istime = time(NULL);
ofstream fout(logfile, ios::app);
fout << ctime(&istime) << "Binding port " << port << " failed! maybe port " << port << " in use or not root?\n";
fout << ctime(&istime) << "expopper died!\n";
fout.close();
return -1;
}
else
{
if(loglevel == 2)
{
istime = time(NULL);
ofstream fout(logfile, ios::app);
fout << ctime(&istime) << "Binding port " << port << " success!\n";
fout.close();
}
}
listen(sockfd, maxclients);
len = sizeof(cliaddr);
signal(SIGCHLD, sig_chld);
while(1>0)
{
connfd = accept(sockfd, (struct sockaddr *)&cliaddr,&len);
if( (pid = fork()) == 0) /* ****EN */
{
close(sockfd);

/* pop3 routine... */
char banner[] = "This is eXcluded.org popper (pop3d) from www.excluded.org\n";
write(connfd, banner, strlen(banner));
if(loglevel != 0)
{
istime = time(NULL);
ofstream fout(logfile, ios::app);
fout << ctime(&istime) << inet_ntoa(cliaddr.sin_addr.s_addr) << " connected\n";
fout.close();
}



/* end of pop3 routine... */
exit(0);
}
}
close(sockfd);
return 0;
}

void sig_chld(int signo)
{
pid_t pid;
int stat;
pid=wait(&stat);
return;
}

Also, ich bin grade dabei mit nen pop3 server zu basteln. Soweit so gut, nur, jetzt bin ich beim loggen des conenctens angelangt. Will ntürlich die cliaddr "lesbar" machen, das macht man ja normal mit inet_ntoa(). So wie ich das da in meinem source habe.
Nur bekomme ich beim Compilieren folgende fehlerausgabe:


In file included from /usr/include/g++/backward/iostream.h:31,
from main.cpp:21:
/usr/include/g++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
main.cpp: In function `int main(int, char**)':
main.cpp:143: warning: invalid conversion from `ssize_t*' to `socklen_t*'
main.cpp:155: error: conversion from `in_addr_t' to non-scalar type `in_addr'
requested
main.cpp:37: warning: unused parameter `int argc'
main.cpp:37: warning: unused parameter `char**argv'
main.cpp: In function `void sig_chld(int)':
main.cpp:170: warning: unused parameter `int signo'
gmake[2]: *** [main.o] Fehler 1
gmake[2]: Leaving directory `/home/taktloss/expopper/expopper'
gmake[1]: *** [all-recursive] Fehler 1
gmake[1]: Leaving directory `/home/taktloss/expopper'
gmake: *** [all] Fehler 2
*** failed ***


main.cpp:155: error: conversion from `in_addr_t' to non-scalar type `in_addr'
requested

Wie ist das jetzt zu verstehen und wie kann ich das lösen?
Ich bin am Ende meines Latein.
MfG. Taktloss

anda_skoa
23-10-2003, 10:45
Wenn du so langen Code hast, wäre es hilfreich bei nach den Fehlermeludng die relevanten Zeilen nochmal extra zu haben.

Lösung zur ersten Warning


#include <iostream>
#include <fstream>
#include <cstdio>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <arpa/inet.h>
#include <time.h>



while(1>0)

while(true) ?




connfd = accept(sockfd, (struct sockaddr *)&cliaddr,&len);

Hier passt der Typ von &len nicht ganz. Wahrscheinlich kann man casten, aber sauberer ist sicher, wenn man eine Variable vom richtigen Typ nimmt



fout << ctime(&istime) << inet_ntoa(cliaddr.sin_addr.s_addr) << " connected\n";


Meines wissens ist s_addr ein "unsigned long".
inet_ntoa will aber ein "struct in_addr"

Ich schätze ".s_addr" ist zu viel

Ciao,
_

Takt
23-10-2003, 18:12
danke, das prog funtzt jetzt soweit... nur jetzt habe ich ein wieder ein problem.
Oder eher 2!
1. Wie greife ich auf die linux/unix benutzer datenbank zu, bzw. wie decrypte ich die pw's zum vergleichen?
2. Ich habe 2 arrays, einmal "user <username>" und einmal "pass <pw>". Wie kann ich die jetzt nur zu <pw> und <username> auseinander nehmen?
MfG. Takt

Takt
24-10-2003, 00:42
Also, das aus meinem 2. post hat sich sogut wie erledigt. Jetzt habe ich noch 2 Fragen:
1. Wie kann ich einen String md5 verschlüsseln?
2. Wie kann ich das \n aus einem array entfernen?
Das brauche ich, um meinen "shadow" string zu bilden mit snprintf... also user:pw, geht soweit auch, aber hinter user ist nen zeilenumbruch, und der kommt halt durch die Eingabe über den describtor. Hat da jemand ne Idee?
MfG. Takt

anda_skoa
24-10-2003, 09:51
Original geschrieben von Takt
Also, das aus meinem 2. post hat sich sogut wie erledigt. Jetzt habe ich noch 2 Fragen:
1. Wie kann ich einen String md5 verschlüsseln?

Hmm, hilft dir crypt?
man crypt



2. Wie kann ich das \n aus einem array entfernen?
Das brauche ich, um meinen "shadow" string zu bilden mit snprintf... also user:pw, geht soweit auch, aber hinter user ist nen zeilenumbruch, und der kommt halt durch die Eingabe über den describtor. Hat da jemand ne Idee?


Wenn es das letze Zeichen im String ist, geht das einfach


user[strlen(user)-1] = '\0';


Ciao,
_

Takt
24-10-2003, 15:35
user[strlen(user)-1] = '\0';
Mh das hat nicht geholfen, wenn ich das angewendet habe, war das array auf einmal "leer" also der nullzeichen fhelte ode ros ka, hab eus -1 mal -2 gemacht und alles funtzt wie es soll :D crypt werd ich mir gleich mal ansehen!
MfG. Takt