Line | |
---|
1 | //network.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int net_check_listen (void) { |
---|
6 | int r; |
---|
7 | fd_set sl; |
---|
8 | struct timeval tv; |
---|
9 | |
---|
10 | FD_ZERO(&sl); |
---|
11 | FD_SET(g_listen_socket, &sl); |
---|
12 | |
---|
13 | tv.tv_sec = 0; |
---|
14 | tv.tv_usec = 1; |
---|
15 | |
---|
16 | if ((r = select(g_listen_socket + 1, &sl, NULL, NULL, &tv)) > 0) { |
---|
17 | ROAR_DBG("net_check_listen(void): We have a connection!"); |
---|
18 | return net_get_new_client(); |
---|
19 | } |
---|
20 | |
---|
21 | return r; |
---|
22 | } |
---|
23 | |
---|
24 | int net_get_new_client (void) { |
---|
25 | int fh; |
---|
26 | int client; |
---|
27 | #ifdef SO_PEERCRED |
---|
28 | struct roar_client * c; |
---|
29 | struct ucred cred; |
---|
30 | socklen_t cred_len = sizeof(cred); |
---|
31 | #endif |
---|
32 | |
---|
33 | fh = accept(g_listen_socket, NULL, NULL); |
---|
34 | |
---|
35 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
36 | |
---|
37 | client = clients_new(); |
---|
38 | |
---|
39 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
40 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
41 | |
---|
42 | clients_delete(client); |
---|
43 | close(fh); |
---|
44 | |
---|
45 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
46 | return -1; |
---|
47 | } |
---|
48 | |
---|
49 | #ifdef SO_PEERCRED |
---|
50 | if ( clients_get(client, &c) != -1 ) { |
---|
51 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
52 | if ( cred.pid != 0 ) { |
---|
53 | c->pid = cred.pid; |
---|
54 | c->uid = cred.uid; |
---|
55 | c->gid = cred.gid; |
---|
56 | } |
---|
57 | } else { |
---|
58 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
59 | } |
---|
60 | } |
---|
61 | #endif |
---|
62 | |
---|
63 | // close(fh); |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | //ll |
---|
Note: See
TracBrowser
for help on using the repository browser.