Files
@ 1a829a8ad7e6
Branch filter:
Location: DistRen/src/server/listen.c
1a829a8ad7e6
3.3 KiB
text/plain
reorganization of structs, partial implementation of distrend_accept()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /*
Copyright 2009 Nathan Phillip Brink
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "listen.h"
#include <list.h>
#include <malloc.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
/* local */
struct distrend_clientset
{
LIST *clients;
/*
for select()
*/
fd_set readfds;
fd_set writefds;
int nfds;
};
int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients)
{
int tmp;
struct sockaddr_in6 sockaddr =
{
.sin6_family = AF_INET6,
.sin6_port = 0,
.sin6_flowinfo = 0,
.sin6_addr = IN6ADDR_ANY_INIT,
.sin6_scope_id = 0
};
*clients = malloc(sizeof(struct distrend_clientset));
(*clients)->clients = list_init();
sockaddr.sin6_port = htonl(4050);
config->listens->sock = socket(AF_INET6, SOCK_STREAM, 0);
tmp = bind(config->listens->sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if(tmp == -1)
{
perror("bind");
free(*clients);
return 1;
}
tmp = listen(config->listens->sock, 1);
FD_ZERO(&(*clients)->readfds);
FD_ZERO(&(*clients)->writefds);
return 0;
}
int distrend_accept_find_client(fd_set *fdset, struct distrend_client *client)
{
if(FD_ISSET(client->sock, fdset))
return FALSE;
return TRUE;
}
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action)
{
int tmp;
fd_set readfds;
fd_set writefds;
struct distrend_client *cli;
memcpy(&readfds, &clients->readfds, sizeof(fd_set));
memcpy(&writefds, &clients->writefds, sizeof(fd_set));
tmp = select(clients->nfds, &readfds, &writefds, NULL, (struct timeval *)NULL);
if(tmp == -1)
{
perror("select");
return 1;
}
/**
check if our traversal function found a bit that was set
*/
if(list_traverse(clients->clients, &readfds, &distrend_accept_find_client, LIST_FRNT | LIST_ALTR) == LIST_OK)
{
cli = (struct distrend_client *)list_curr(clients->clients);
fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting, which is %s\n",
__FILE__, __LINE__,
cli->sock, FD_ISSET(cli->sock, &readfds) ? "true" : "false");
fprintf(stderr, "stub\n");
return 1;
}
return 0;
}
int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients)
{
fprintf(stderr, "%s:%d: I am a stub that needn't be implemented 'til later\n", __FILE__, __LINE__);
return 1;
}
/**
This is probably just NOT a placeholder for remotio
*/
void remotio_send_to_client()
{
fprintf(stderr, "%s:%d: I am futile! And I'm happy because of it :-D\n", __FILE__, __LINE__);
}
|