File: util.c

package info (click to toggle)
l2tpns 2.1.21-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 820 kB
  • ctags: 1,621
  • sloc: ansic: 16,737; makefile: 160; sh: 142; perl: 139
file content (175 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Misc util functions */

char const *cvs_id_util = "$Id: util.c,v 1.14 2006/04/05 01:45:57 bodea Exp $";

#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/mman.h>

#include "l2tpns.h"
#ifdef BGP
#include "bgp.h"
#endif

// format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
// to use
char *fmtaddr(in_addr_t addr, int n)
{
    static char addrs[4][16];
    struct in_addr in;

    if (n < 0 || n >= 4)
	return "";

    in.s_addr = addr;
    return strcpy(addrs[n], inet_ntoa(in));
}

void *shared_malloc(unsigned int size)
{
    void * p;
    p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);

    if (p == MAP_FAILED)
	p = NULL;

    return p;
}

extern int forked;
extern int cluster_sockfd, tunfd, udpfd, controlfd, daefd, snoopfd, ifrfd, ifr6fd, rand_fd;
extern int *radfds;

pid_t fork_and_close()
{
    pid_t pid = fork();
    int i;

    if (pid)
	return pid;

    forked++;
    if (config->scheduler_fifo)
    {
	struct sched_param params = {0};
	params.sched_priority = 0;
	if (sched_setscheduler(0, SCHED_OTHER, &params))
	{
	    LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno));
	    LOG(0, 0, 0, "This is probably really really bad.\n");
	}
    }

    signal(SIGPIPE, SIG_DFL);
    signal(SIGCHLD, SIG_DFL);
    signal(SIGHUP,  SIG_DFL);
    signal(SIGUSR1, SIG_DFL);
    signal(SIGQUIT, SIG_DFL);
    signal(SIGKILL, SIG_DFL);
    signal(SIGTERM, SIG_DFL);

    // Close sockets
    if (clifd != -1)          close(clifd);
    if (cluster_sockfd != -1) close(cluster_sockfd);
    if (tunfd != -1)          close(tunfd);
    if (udpfd != -1)          close(udpfd);
    if (controlfd != -1)      close(controlfd);
    if (daefd != -1)          close(daefd);
    if (snoopfd != -1)        close(snoopfd);
    if (ifrfd != -1)          close(ifrfd);
    if (ifr6fd != -1)         close(ifr6fd);
    if (rand_fd != -1)        close(rand_fd);
    if (epollfd != -1)        close(epollfd);

    for (i = 0; radfds && i < RADIUS_FDS; i++)
	close(radfds[i]);

#ifdef BGP
    for (i = 0; i < BGP_NUM_PEERS; i++)
	if (bgp_peers[i].sock != -1)
	    close(bgp_peers[i].sock);
#endif /* BGP */

    return pid;
}

ssize_t recvfromto(int s, void *buf, size_t len, int flags,
    struct sockaddr *from, socklen_t *fromlen, struct in_addr *toaddr)
{
    ssize_t r;
    struct msghdr msg;
    struct cmsghdr *cmsg;
    struct iovec vec;
    char cbuf[128];

    memset(&msg, 0, sizeof(msg));
    msg.msg_name = from;
    msg.msg_namelen = *fromlen;

    vec.iov_base = buf;
    vec.iov_len = len;
    msg.msg_iov = &vec;
    msg.msg_iovlen = 1;
    msg.msg_flags = 0;

    msg.msg_control = cbuf;
    msg.msg_controllen = sizeof(cbuf);

    if ((r = recvmsg(s, &msg, flags)) < 0)
    	return r;

    if (fromlen)
	*fromlen = msg.msg_namelen;

    memset(toaddr, 0, sizeof(*toaddr));
    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
    {
	if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO)
	{
	    struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
	    memcpy(toaddr, &i->ipi_addr, sizeof(*toaddr));
	    break;
	}
    }

    return r;
}

ssize_t sendtofrom(int s, void const *buf, size_t len, int flags,
    struct sockaddr const *to, socklen_t tolen, struct in_addr const *from)
{
    struct msghdr msg;
    struct cmsghdr *cmsg;
    struct iovec vec;
    struct in_pktinfo pktinfo;
    char cbuf[CMSG_SPACE(sizeof(pktinfo))];

    memset(&msg, 0, sizeof(msg));
    msg.msg_name = (struct sockaddr *) to;
    msg.msg_namelen = tolen;

    vec.iov_base = (void *) buf;
    vec.iov_len = len;
    msg.msg_iov = &vec;
    msg.msg_iovlen = 1;
    msg.msg_flags = 0;

    msg.msg_control = cbuf;
    msg.msg_controllen = sizeof(cbuf);

    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_IP;
    cmsg->cmsg_type = IP_PKTINFO;
    cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));

    memset(&pktinfo, 0, sizeof(pktinfo));
    memcpy(&pktinfo.ipi_spec_dst, from, sizeof(*from));
    memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));

    return sendmsg(s, &msg, flags);
}