author | viric@llimona |
Thu, 06 Apr 2006 01:47:35 +0200 | |
changeset 18 | 84fa30ea0b0d |
parent 15 | fea6e87812f0 |
child 26 | 7227789ca718 |
permissions | -rw-r--r-- |
9 | 1 |
#include <unistd.h> // Per les crides a fitxer |
2 |
#include <stdio.h> // Per l'I/O de stdin/stdout |
|
3 |
#include <errno.h> // Pels errors de les crides a fitxer |
|
4 |
#include <string.h> // Per strerror() |
|
5 |
#include <stdlib.h> // Per abort() |
|
6 |
#include <sys/types.h> // Per crides de fitxer |
|
7 |
#include <sys/socket.h> // Per les macros bool,true,false |
|
8 |
#include <sys/un.h> // Per les estructures dels unix sockets |
|
9 |
#include <assert.h> // Per assert() |
|
10 |
||
11 |
#include "rfc3164.h" |
|
12 |
||
13 |
void show_help(const char * restrict program) |
|
14 |
{ |
|
15 |
printf("Usage: %s <unix_socket>\n", program); |
|
16 |
} |
|
17 |
||
18 |
||
19 |
int listen_unix(const char * restrict socketname) |
|
20 |
{ |
|
21 |
int socket_unix; |
|
22 |
struct sockaddr_un source_unix; |
|
23 |
int result; |
|
15
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
24 |
int sockopt; |
9 | 25 |
|
26 |
||
27 |
socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0 ); |
|
28 |
if (socket_unix == -1) |
|
29 |
{ |
|
30 |
printf("Unix socket creation error: %s\n", strerror(errno)); |
|
31 |
abort(); |
|
32 |
} |
|
33 |
||
15
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
34 |
/* Allow socket reuse */ |
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
35 |
sockopt = 1; |
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
36 |
result = setsockopt(socket_unix, SOL_SOCKET, SO_REUSEADDR, &sockopt, |
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
37 |
sizeof(sockopt)); |
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
38 |
assert(result == 0); |
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
39 |
|
9 | 40 |
/* Unix socket listen address */ |
41 |
memset(&source_unix, 0, sizeof(source_unix)); |
|
42 |
source_unix.sun_family = AF_UNIX; |
|
43 |
strcpy(source_unix.sun_path, socketname); |
|
44 |
result = bind(socket_unix, (struct sockaddr *) &source_unix, |
|
45 |
sizeof(source_unix)); |
|
46 |
assert(result == 0); |
|
47 |
||
48 |
/* Here we should take care of the socket permissions */ |
|
49 |
||
50 |
return socket_unix; |
|
51 |
} |
|
52 |
||
15
fea6e87812f0
Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents:
11
diff
changeset
|
53 |
void server_loop(const char * restrict socketname) |
9 | 54 |
{ |
55 |
int socket_unix; |
|
56 |
int result; |
|
57 |
char message[MESSAGE_LENGTH+1]; |
|
58 |
||
59 |
||
60 |
socket_unix = listen_unix(socketname); |
|
61 |
||
62 |
result = 0; |
|
63 |
while ((result = recv(socket_unix, message, MESSAGE_LENGTH, 0)) >=0 ) |
|
64 |
{ |
|
65 |
process_message(message); |
|
66 |
/* Debug */ |
|
67 |
if(!strcmp(message,"close")) |
|
68 |
break; |
|
69 |
} |
|
70 |
result = close(socket_unix); |
|
71 |
if (result == -1) |
|
72 |
{ |
|
73 |
printf("Unix socket close error: %s\n", strerror(errno)); |
|
74 |
abort(); |
|
75 |
} |
|
76 |
result = unlink(socketname); |
|
77 |
if (result == -1) |
|
78 |
{ |
|
79 |
printf("Unix socket unlink error: %s\n", strerror(errno)); |
|
80 |
abort(); |
|
81 |
} |
|
82 |
||
83 |
} |
|
84 |
||
85 |
||
86 |
int main(int argn, char **argv) |
|
87 |
{ |
|
88 |
/* Processem els paràmetres d'entrada */ |
|
89 |
if (argn != 2) { |
|
90 |
show_help(argv[0]); |
|
91 |
return 2; |
|
92 |
} |
|
93 |
||
94 |
server_loop(argv[1]); |
|
95 |
||
96 |
return 0; |
|
97 |
} |