summaryrefslogtreecommitdiffstats
path: root/hw1/socktool.c
blob: 109a02c776bc6815a45efff481c7c871beba6784 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "socktool.h"
#include "xwrap.h"

#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

typedef int (*SockNameGetter) (int, struct sockaddr*, socklen_t*);
static char* ras_socktool_get_name (int sockfd, SockNameGetter getter) {
    struct sockaddr_storage sock;
    socklen_t socklen = sizeof (sock);

    memset (&sock, 0, socklen);
    if ((*getter)(sockfd, SOCKADDR (&sock), &socklen) < 0) {
        return xstrdup ("invalid socket");
    }

    int domain = sock.ss_family;
    if (domain == AF_UNIX) {
        return xstrdup ("local process");
    }

    socklen_t ipstrlen;
    void* ipnet;
    char* ipstr;
    if (domain == AF_INET) {
        ipstrlen = INET_ADDRSTRLEN;
        ipnet = &(SOCKADDR_IN (&sock)->sin_addr);
    } else {
        ipstrlen = INET6_ADDRSTRLEN;
        ipnet = &(SOCKADDR_IN6 (&sock)->sin6_addr);
    }

    ipstr = xmalloc (ipstrlen);
    if (inet_ntop (domain, ipnet, ipstr, ipstrlen) == NULL) {
        free (ipstr);
        return xstrdup ("unknown address");
    }

    return ipstr;
}

char* ras_socktool_get_sockname (int sockfd) {
    return ras_socktool_get_name (sockfd, getsockname);
}

char* ras_socktool_get_peername (int sockfd) {
    return ras_socktool_get_name (sockfd, getpeername);
}

void ras_socktool_buffer_clear (RasBuffer* buf, bool initial) {
    buf->buf_start = 0;
    buf->buf_len = 0;

    if (!initial) {
        free (buf->buf_line);
    }
    buf->buf_line = NULL;
    buf->buf_line_len = 0;
    buf->buf_error = false;
    buf->buf_eof = false;
}

void ras_socktool_buffer_free_line (RasBuffer* buf) {
    free (buf->buf_line);
    buf->buf_line = NULL;
    buf->buf_line_len = 0;
}

char* ras_socktool_getline (int sockfd, RasBuffer* buf, int delim, int* len) {
    if (buf->buf_error || buf->buf_eof) {
        return NULL;
    }

    if (buf->buf_len == 0) {
        int rval = read (sockfd, buf->buf, RAS_BUFFER_SIZE);
        if (rval < 0) {
            if (errno != EAGAIN && errno != EINTR) {
                buf->buf_error = true;
            }
            return NULL;
        }
        if (rval == 0) {
            buf->buf_eof = true;
            return NULL;
        }
        buf->buf_start = 0;
        buf->buf_len = rval;
    }

    int i;
    for (i = 0; i < buf->buf_len; i++) {
        if (buf->buf[buf->buf_start + i] == delim) {
            break;
        }
    }

    int buf_line_start = buf->buf_line_len;
    buf->buf_line_len += i;
    buf->buf_line = xrealloc (buf->buf_line, buf->buf_line_len + 1);
    memcpy (buf->buf_line + buf_line_start, buf->buf + buf->buf_start, i);
    buf->buf_line[buf->buf_line_len] = '\0';

    /* remove CR if delim is LF and delim is found */
    if (i < buf->buf_len && delim == '\n' && buf->buf_line_len - 1 >= 0 &&
        buf->buf_line[buf->buf_line_len - 1] == '\r') {
        buf->buf_line[buf->buf_line_len - 1] = '\0';
        buf->buf_line_len--;
    }

    int buf_len_saved = buf->buf_len;
    buf->buf_start += i + 1;
    buf->buf_len -= i + 1;
    if (buf->buf_len <= 0) {
        buf->buf_start = 0;
        buf->buf_len = 0;
    }

    if (i < buf_len_saved) {
        /* delim is found */
        char* newstr = buf->buf_line;
        if (len != NULL) {
            *len = buf->buf_line_len;
        }
        buf->buf_line = NULL;
        buf->buf_line_len = 0;
        return newstr;
    }

    return NULL;
}

int ras_socktool_write_string (int sockfd, const char* str, size_t size) {
    size_t wtn = 0;
    size_t rem = size;
    if (size <= 0) {
        size = strlen (str);
    }
    while (rem > 0) {
        wtn = write (sockfd, str, rem);
        if (wtn < 0) {
            if (errno != EINTR && errno != EAGAIN) {
                break;
            }
            continue;
        }
        str += wtn;
        rem -= wtn;
    }

    rem = rem > 0 ? rem : 0;
    return size - rem;
}

void ras_socktool_reset_signals (void) {
    const int must_reset[] = { SIGHUP, SIGINT, SIGQUIT, SIGABRT,
        SIGFPE, SIGTERM, SIGCHLD, SIGTSTP, SIGTTIN, SIGTTOU };
    struct sigaction action = {
        .sa_handler = SIG_DFL,
        .sa_flags = 0
    };
    sigemptyset (&action.sa_mask);
#if defined(SIGRTMIN) && defined(SIGRTMAX)
    int signal_max = xmax (SIGRTMIN, SIGRTMAX);
    for (int i = 1; i < signal_max; i++) {
        sigaction (i, &action, NULL);
    }
#endif
    for (int i = 0; i < sizeof (must_reset) / sizeof (int); i++) {
        sigaction (must_reset[i], &action, NULL);
    }
}

int ras_socktool_getdtablesize (void) {
#ifdef HAVE_GETDTABLESIZE
    return getdtablesize ();
#else
    long rval = sysconf (_SC_OPEN_MAX);
    if (rval < 0) {
# ifdef _POSIX_OPEN_MAX
        rval = _POSIX_OPEN_MAX
# else
        rval = 20;
# endif
    }
    return rval;
#endif
}

void ras_socktool_close_except (int dontclose) {
    int dtsize = ras_socktool_getdtablesize ();
    for (int i = 0; i < dtsize; i++) {
        if (i != dontclose) {
            close (i);
        }
    }
}