+ return 1;
+}
+
+static int socket_server(int port, int daemon)
+{
+ struct sigaction sact;
+ struct sockaddr_in sin;
+ int sock;
+ int n;
+ pid_t pid;
+
+ if (daemon) {
+ pid = fork();
+ if (pid > 0) {
+ printf("Daemon with PID %u started!\n", pid);
+ exit(EXIT_SUCCESS);
+ } else if (pid < 0) {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ memset(&sact, 0, sizeof(sact));
+ sact.sa_handler = SIG_IGN;
+
+ if (sigaction(SIGPIPE, &sact, NULL) == -1) {
+ perror("sigaction");
+ exit(EXIT_FAILURE);
+ }
+
+ impersonate_hmlanif = 1;
+
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sock == -1) {
+ perror("Can't open socket");
+ return EXIT_FAILURE;
+ }
+
+ n = 1;
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
+ perror("Can't set socket options");
+ return EXIT_FAILURE;
+ }
+
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(port);
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
+ perror("Can't bind socket");
+ return EXIT_FAILURE;
+ }
+
+ if (listen(sock, 1) == -1) {
+ perror("Can't listen on socket");
+ return EXIT_FAILURE;
+ }
+
+ while(1) {
+ struct sockaddr_in csin;
+ socklen_t csinlen;
+ int client;
+ in_addr_t client_addr;
+
+ memset(&csin, 0, sizeof(csin));
+ csinlen = sizeof(csin);
+ client = accept(sock, (struct sockaddr*)&csin, &csinlen);
+ if (client == -1) {
+ perror("Couldn't accept client");
+ continue;
+ }
+
+ /* FIXME: getnameinfo... */
+ client_addr = ntohl(csin.sin_addr.s_addr);
+
+ if (verbose) {
+ printf("Client %d.%d.%d.%d connected!\n",
+ (client_addr & 0xff000000) >> 24,
+ (client_addr & 0x00ff0000) >> 16,
+ (client_addr & 0x0000ff00) >> 8,
+ (client_addr & 0x000000ff));
+ }
+
+ comm(client, client, sock);
+
+ shutdown(client, SHUT_RDWR);
+ close(client);
+
+ if (verbose) {
+ printf("Connection to %d.%d.%d.%d closed!\n",
+ (client_addr & 0xff000000) >> 24,
+ (client_addr & 0x00ff0000) >> 16,
+ (client_addr & 0x0000ff00) >> 8,
+ (client_addr & 0x000000ff));
+ }
+
+ }
+
+ return EXIT_SUCCESS;
+}
+
+static int interactive_server(void)
+{
+ if (!comm(STDIN_FILENO, STDOUT_FILENO, -1))
+ return EXIT_FAILURE;