#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include <unistd.h>
#include "client.h"
#include "proto.h"
#include "repo.h"
static int proc_request(struct client *c);
+static void print_invalid(const char *s);
static int proc_flist(struct client *c, char *data, int datasz);
static int proc_push(struct client *c, char *data, int datasz);
static int proc_pull(struct client *c, char *data, int datasz);
if(proc_request(c) == -1) {
return -1;
}
+
+ if(sz == 0) return -1;
return 0;
}
int reqdata_len;
while(endp < c->buf + c->bsz) {
+ if(*endp == '\r' && endp[1] == '\n') {
+ *endp++ = 0;
+ break;
+ }
if(*endp == '\n') break;
+ endp++;
}
if(endp >= c->buf + c->bsz) {
return 0;
return 0;
}
+ print_invalid(c->buf);
send_err(c);
return 0;
}
+static void print_invalid(const char *s)
+{
+ int i;
+ printf("Recv invalid request: \"");
+ for(i=0; i<60; i++) {
+ if(!*s) break;
+ if(isprint(*s)) {
+ putchar(*s);
+ } else {
+ printf("\\x%x", (unsigned int)*s);
+ }
+ s++;
+ }
+ puts("\"");
+}
+
void send_string(struct client *c, const char *s)
{
write(c->s, s, strlen(s));
static struct flist *gen_flist(int contents)
{
- int i, count;
+ int i, count, len;
struct flist *flist;
+ char *pathbuf = 0;
+ int pathbuf_sz = 0;
if(!(flist = flist_create())) {
return 0;
count = repo_num_files();
for(i=0; i<count; i++) {
- flist_add(flist, repo_file(i)->path, contents);
+ len = strlen(repo_path) + strlen(repo_file(i)->path) + 1;
+ if(len > pathbuf_sz) {
+ free(pathbuf);
+ if(!(pathbuf = malloc(len + 1))) {
+ fprintf(stderr, "gen_flist: failed to allocate path buffer (%d bytes)\n", len + 1);
+ repo_cleanup();
+ flist_destroy(flist);
+ return 0;
+ }
+ }
+ sprintf(pathbuf, "%s/%s", repo_path, repo_file(i)->path);
+ flist_add(flist, pathbuf, contents);
}
repo_cleanup();
+ free(pathbuf);
return flist;
}
#include <errno.h>
#include <limits.h>
#include <time.h>
+#include <signal.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
+#include <arpa/inet.h>
#include "repo.h"
#include "client.h"
static const char *guess_repo_name(const char *path);
+static void sighandler(int s);
static int parse_args(int argc, char **argv);
static const char *repo_name;
static int lis = -1;
static int port = 64357;
+static int quit;
static struct client *clients;
return 1;
}
+ signal(SIGINT, sighandler);
+ signal(SIGQUIT, sighandler);
+ signal(SIGILL, sighandler);
+ signal(SIGSEGV, sighandler);
+ signal(SIGTERM, sighandler);
+
if(repo_init(repo_path) == -1) {
return 1;
}
fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
return 1;
}
+ fcntl(lis, F_SETFL, fcntl(lis, F_GETFL) | O_NONBLOCK);
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
perror("select failed");
break;
}
-
+ if(quit) break;
if(res <= 0) continue;
dummy.next = clients;
if(handle_client(c->next) == -1) {
struct client *tmp = c->next;
c->next = tmp->next;
+
+ printf("Client %s disconnected\n", tmp->addr ? tmp->addr : "unknown");
close(tmp->s);
+ free(tmp->addr);
free(tmp);
continue;
}
clients = dummy.next;
if(FD_ISSET(lis, &rdset)) {
- if((s = accept(lis, 0, 0)) == -1) {
+ struct sockaddr_in addr;
+ unsigned int addrsz = sizeof addr;
+
+ if((s = accept(lis, (struct sockaddr*)&addr, &addrsz)) == -1) {
perror("failed to accept connection");
continue;
}
+ fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
+
if(!(c = calloc(1, sizeof *c))) {
perror("failed to allocate memory for client structure");
close(s);
}
c->s = s;
c->next = clients;
+ c->addr = strdup(inet_ntoa(addr.sin_addr));
clients = c;
send_string(c, "reposerve-0.1 protocol:0\n");
+
+ printf("Connection from: %s\n", c->addr ? c->addr : "unknown");
}
}
+ printf("Shutting down\n");
return 0;
}
return 0;
}
+static void sighandler(int s)
+{
+ quit = 1;
+}
+
static void print_usage(const char *argv0)
{
printf("Usage: %s [options] [repo path]\n", argv0);