Subversion Repositories Mokou

Rev

Rev 5 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 nishi 1
/* $Id: server.c 7 2024-09-07 09:14:35Z nishi $ */
2
 
3
#include "mk_server.h"
7 nishi 4
 
5
#include "mk_version.h"
6
#include "mk_util.h"
7
#include "mk_log.h"
8
 
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
 
13
#include <sys/types.h>
14
#include <sys/socket.h>
15
#include <sys/un.h>
16
 
17
struct sockaddr_un sun;
18
int server;
19
 
20
int mk_server_init(void){
21
	remove("/tmp/mokou.sock");
22
	memset(&sun, 0, sizeof(sun));
23
	server = socket(AF_LOCAL, SOCK_STREAM, 0);
24
	if(server == -1){
25
		mk_log("Socket creation failure");
26
		return 1;
27
	}
28
	sun.sun_family = AF_LOCAL;
29
	strcpy(sun.sun_path, "/tmp/mokou.sock");
30
	if(bind(server, (struct sockaddr*)&sun, sizeof(sun)) == -1){
31
		mk_log("Bind failure");
32
		close(server);
33
		return 1;
34
	}
35
	if(listen(server, 16) == -1){
36
		mk_log("Listen failure");
37
		close(server);
38
		return 1;
39
	}
40
	return 0;
41
}
42
 
43
void mk_server_loop(void){
44
	struct sockaddr_un cun;
45
	socklen_t socklen = sizeof(cun);
46
	char* ver = mk_strcat3("V", mk_get_version(), "\n");
47
	while(1){
48
		mk_log("Waiting for the connection");
49
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
50
		send(cli, ver, strlen(ver), 0);
51
		close(cli);
52
	}
53
	free(ver);
54
}