summaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 8da909cfd870d021fa606ed5fa6008a8a4738e34 (plain)
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
176
177
178
179
#include "astmanproxy.h"

/* This routine based on get_input from Asterisk manager.c */
/* Good generic line-based input routine for \r\n\r\n terminated input */
/* Used by standard.c and other input handlers */
int get_input(struct mansession *s, char *output)
{
        /* output must have at least sizeof(s->inbuf) space */
        int res;
        int x;
        struct pollfd fds[1];
        char iabuf[INET_ADDRSTRLEN];

        debugmsg("in get_input");

        /* Look for \r\n from the front, our preferred end of line */
        for (x=0;x<s->inlen;x++) {
               int xtra = 0;
                if (s->inbuf[x] == '\n') {
                       if (x && s->inbuf[x-1] == '\r') {
                               xtra = 1;
                       }
                        /* Copy output data not including \r\n */
                        memcpy(output, s->inbuf, x - xtra);
                        /* Add trailing \0 */
                        output[x-xtra] = '\0';
                        /* Move remaining data back to the front */
                        memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
                        s->inlen -= (x + 1);
                        return 1;
                }
        }

        if (s->inlen >= sizeof(s->inbuf) - 1) {
            if (debug)
                debugmsg("Warning: Got long line with no end from %s: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->inbuf);
            s->inlen = 0;
        }
        debugmsg("attempting poll operation");
        fds[0].fd = s->fd;
        fds[0].events = POLLIN;
        res = poll(fds, 1, -1);
        debugmsg("returned from poll op");
        if (res < 0 && debug) {
                debugmsg("Select returned error");
        } else if (res > 0) {
                pthread_mutex_lock(&s->lock);
                debugmsg("attempting socket read in get_input...");
                res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
                pthread_mutex_unlock(&s->lock);
                if (res < 1)
                        return -1;
        }
        s->inlen += res;
        s->inbuf[s->inlen] = '\0';
        return 0;
        /* We have some input, but it's not ready for processing */
}

char *astman_get_header(struct message *m, char *var)
{
        char cmp[80];
        int x;
        snprintf(cmp, sizeof(cmp), "%s: ", var);
        for (x=0;x<m->hdrcount;x++)
                if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
                        return m->headers[x] + strlen(cmp);
        return "";
}

int AddHeader(struct message *m, const char *fmt, ...) {
    va_list ap;

    int res;

    if (m->hdrcount < MAX_HEADERS - 1) {
        va_start(ap, fmt);
        vsprintf(m->headers[m->hdrcount], fmt, ap);
        va_end(ap);
        m->hdrcount++;
        res = 0;
    } else
        res = 1;

    return res;
}

/* Recursive thread safe replacement of inet_ntoa */
const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia)
{
        return inet_ntop(AF_INET, &ia, buf, bufsiz);
}


int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, int nsec)
{
	int				flags, n, error;
	socklen_t		len;
	fd_set			rset, wset;
	struct timeval	tval;

	flags = fcntl(sockfd, F_GETFL, 0);
	fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

	error = 0;
	if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0)
		if (errno != EINPROGRESS)
			return(-1);

	/* Do whatever we want while the connect is taking place. */

	if (n == 0)
		goto done;	/* connect completed immediately */

	FD_ZERO(&rset);
	FD_SET(sockfd, &rset);
	wset = rset;
	tval.tv_sec = nsec;
	tval.tv_usec = 0;

	if ( (n = select(sockfd+1, &rset, &wset, NULL,
					 nsec ? &tval : NULL)) == 0) {
		/*close(sockfd);*/		/* we want to retry */
		errno = ETIMEDOUT;
		return(-1);
	}

	if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
		len = sizeof(error);
		if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
			return(-1);			/* Solaris pending error */
	} else {
		/*err_quit("select error: sockfd not set");*/
		logmsg("select error: sockfd not set");
                return(-1);
        }

done:
	fcntl(sockfd, F_SETFL, flags);	/* restore file status flags */

	if (error) {
		/* close(sockfd); */	/* disable for now, we want to retry... */
		errno = error;
		return(-1);
	}
	return(0);
}

/*! If you are calling ast_carefulwrite, it is assumed that you are calling
   it on a file descriptor that _DOES_ have NONBLOCK set.  This way,
   there is only one system call made to do a write, unless we actually
   have a need to wait.  This way, we get better performance. */
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
{
        /* Try to write string, but wait no more than ms milliseconds
           before timing out */
        int res=0;
        struct pollfd fds[1];
        while(len) {
                res = m_send(fd, s, len);
                if ((res < 0) && (errno != EAGAIN)) {
                        return -1;
                }
                if (res < 0) res = 0;
                len -= res;
                s += res;
                res = 0;
                if (len) {
                        fds[0].fd = get_real_fd(fd);
                        fds[0].events = POLLOUT;
                        /* Wait until writable again */
                        res = poll(fds, 1, timeoutms);
                        if (res < 1)
                                return -1;
                }
        }
        return res;
}