rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | LICENSE

net.c (6193B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <unistd.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <stdarg.h>
     10 #include <string.h>
     11 #include <strings.h>
     12 #include <poll.h>
     13 #include <arpa/inet.h>
     14 #include <netinet/in.h>
     15 #include <sys/types.h>
     16 #include <sys/socket.h>
     17 #include <netdb.h>
     18 
     19 #include <tls.h>
     20 
     21 #include "ind.h"
     22 #include "net.h"
     23 
     24 /*
     25  * net format:
     26  *	net!addr!service
     27  */
     28 enum {
     29 	NET_NET = 0x00,
     30 	NET_TCP,
     31 	NET_TCPS
     32 };
     33 static char *netnames[] = {
     34 	[NET_NET] = "net",
     35 	[NET_TCP] = "tcp",
     36 	[NET_TCPS] = "tcps"
     37 };
     38 
     39 int
     40 net_gettype(char *str)
     41 {
     42 	int i;
     43 
     44 	for (i = 0; i < nelem(netnames); i++)
     45 		if (!strcmp(netnames[i], str))
     46 			return i;
     47 	return -1;
     48 }
     49 
     50 net_t *
     51 net_new(char *desc)
     52 {
     53 	net_t *ret;
     54 	int state;
     55 	char *tok, *buf, *toks;
     56 
     57 	state = -1;
     58 	toks = NULL;
     59 	ret = NULL;
     60 	buf = memdup(desc, strlen(desc)+1);
     61 
     62 	tok = strtok_r(buf, "!", &toks);
     63 	if (tok == NULL)
     64 		goto netnewerror;
     65 
     66 	ret = mallocz(sizeof(net_t), 2);
     67 	do {
     68 		state++;
     69 		switch(state) {
     70 		case 0:
     71 			ret->net = memdup(tok, strlen(tok)+1);
     72 			ret->type = net_gettype(ret->net);
     73 			break;
     74 		case 1:
     75 			ret->addr = memdup(tok, strlen(tok)+1);
     76 			break;
     77 		case 2:
     78 			ret->service = memdup(tok, strlen(tok)+1);
     79 			break;
     80 		case 3:
     81 			ret->options = memdup(tok, strlen(tok)+1);
     82 			break;
     83 		default:
     84 			break;
     85 		}
     86 	} while ((tok = strtok_r(NULL, "!", &toks)) != NULL);
     87 	if (state < 2)
     88 		goto netnewerror;
     89 	free(buf);
     90 
     91 	return ret;
     92 netnewerror:
     93 	if (buf != NULL)
     94 		free(buf);
     95 
     96 	switch (state) {
     97 	case 1:
     98 		free(ret->addr);
     99 	case 0:
    100 		free(ret->net);
    101 	default:
    102 		break;
    103 	}
    104 	if (ret != NULL)
    105 		free(ret);
    106 
    107 	return NULL;
    108 }
    109 
    110 void
    111 net_free(net_t *net)
    112 {
    113 	switch (net->type) {
    114 	case NET_TCPS:
    115 		tls_free((struct tls *)net->data[0]);
    116 		tls_config_free((struct tls_config *)net->data[1]);
    117 	}
    118 
    119 	if (net->net != NULL)
    120 		free(net->net);
    121 	if (net->addr != NULL)
    122 		free(net->addr);
    123 	if (net->service != NULL)
    124 		free(net->service);
    125 	free(net);
    126 }
    127 
    128 int
    129 net_connecttcp(net_t *net)
    130 {
    131 	struct addrinfo hints, *ai, *rp;
    132 
    133 	bzero(&hints, sizeof(hints));
    134 	/*
    135 	 * Anyone knowing a real check for IPv6, instead of failing
    136 	 * everything, should contact me.
    137 	 */
    138 	hints.ai_family = AF_UNSPEC;
    139 	hints.ai_socktype = SOCK_STREAM;
    140 	hints.ai_protocol = IPPROTO_TCP;
    141 	if (getaddrinfo(net->addr, net->service, &hints, &ai))
    142 		return 1;
    143 	if (ai == NULL)
    144 		return 1;
    145 
    146 	for (rp = ai; rp != NULL; rp = rp->ai_next) {
    147 		net->fd = socket(rp->ai_family, rp->ai_socktype,
    148 				rp->ai_protocol);
    149 		if (net->fd < 0)
    150 			continue;
    151 		if (!connect(net->fd, rp->ai_addr, rp->ai_addrlen))
    152 			break;
    153 		close(net->fd);
    154 	}
    155 	if (rp == NULL)
    156 		return 1;
    157 	freeaddrinfo(ai);
    158 
    159 	return 0;
    160 }
    161 
    162 int
    163 net_addssl(net_t *net)
    164 {
    165 	struct tls *tls = NULL;
    166 	struct tls_config *config = NULL;
    167 
    168 	if (tls_init() < 0)
    169 		return 1;
    170 	if ((tls = tls_client()) == NULL)
    171 		return 1;
    172 	if ((config = tls_config_new()) == NULL) {
    173 		tls_free(tls);
    174 		return 1;
    175 	}
    176 
    177 	if (net->options != NULL) {
    178 		if (strstr(net->options, "tlscertverify=off"))
    179 			tls_config_insecure_noverifycert(config);
    180 		if (strstr(net->options, "tlsnameverify=off"))
    181 			tls_config_insecure_noverifyname(config);
    182 	}
    183 
    184 	if (tls_configure(tls, config) < 0) {
    185 		fprintf(stderr, "tls_configure: %s\n", tls_error(tls));
    186 		tls_free(tls);
    187 		tls_config_free(config);
    188 		return 1;
    189 	}
    190 
    191 	if (tls_connect_socket(tls, net->fd, net->addr) < 0) {
    192 		fprintf(stderr, "tls_connect_socket: %s\n", tls_error(tls));
    193 		tls_free(tls);
    194 		tls_config_free(config);
    195 		return 1;
    196 	}
    197 
    198 	switch (net->type) {
    199 	case NET_NET:
    200 	case NET_TCP:
    201 		free(net->net);
    202 		net->net = memdup("tcps", 5);
    203 		net->type = net_gettype(net->net);
    204 	case NET_TCPS:
    205 	default:
    206 		break;
    207 	}
    208 
    209 	net->data[0] = tls;
    210 	net->data[1] = config;
    211 
    212 	return 0;
    213 }
    214 
    215 int
    216 net_connecttcps(net_t *net)
    217 {
    218 	if (net_connecttcp(net))
    219 		return 1;
    220 	if (net_addssl(net))
    221 		return 1;
    222 
    223 	return 0;
    224 }
    225 
    226 int
    227 net_connect(net_t *net)
    228 {
    229 	switch (net->type) {
    230 	case NET_NET:
    231 	case NET_TCP:
    232 		return net_connecttcp(net);
    233 	case NET_TCPS:
    234 		return net_connecttcps(net);
    235 	default:
    236 		return 1;
    237 	}
    238 	return 0;
    239 }
    240 
    241 void
    242 net_closetcp(net_t *net)
    243 {
    244 	shutdown(net->fd, SHUT_RDWR);
    245 	close(net->fd);
    246 }
    247 
    248 void
    249 net_closetcps(net_t *net)
    250 {
    251 	tls_close((struct tls *)net->data[0]);
    252 }
    253 
    254 void
    255 net_close(net_t *net)
    256 {
    257 	switch (net->type) {
    258 	case NET_NET:
    259 	case NET_TCP:
    260 		net_closetcp(net);
    261 	case NET_TCPS:
    262 		net_closetcps(net);
    263 	default:
    264 		break;
    265 	}
    266 }
    267 
    268 int
    269 net_writetcp(net_t *net, char *buf, int len)
    270 {
    271 	return send(net->fd, buf, len, MSG_NOSIGNAL);
    272 }
    273 
    274 int
    275 net_writetcps(net_t *net, char *buf, int len)
    276 {
    277 	return tls_write((struct tls *)net->data[0], buf, len);
    278 }
    279 
    280 int
    281 net_write(net_t *net, char *buf, int len)
    282 {
    283 	switch (net->type) {
    284 	case NET_NET:
    285 	case NET_TCP:
    286 		return net_writetcp(net, buf, len);
    287 	case NET_TCPS:
    288 		return net_writetcps(net, buf, len);
    289 	default:
    290 		return -1;
    291 	}
    292 	return -1;
    293 }
    294 
    295 int
    296 net_writeall(net_t *net, char *buf, int len)
    297 {
    298 	int olen, nlen;
    299 
    300 	for (olen = 0;
    301 			(nlen = net_write(net, &buf[olen], len-olen))
    302 			&& olen < len;
    303 			olen += nlen);
    304 
    305 	return 0;
    306 }
    307 
    308 int
    309 net_printf(net_t *net, char *fmt, ...)
    310 {
    311 	va_list fmtargs;
    312 	char *buf;
    313 	int len;
    314 
    315 	va_start(fmtargs, fmt);
    316 	len = vsnprintf(NULL, 0, fmt, fmtargs);
    317 	va_end(fmtargs);
    318 
    319 	va_start(fmtargs, fmt);
    320 	buf = vsmprintf(fmt, fmtargs, len);
    321 	va_end(fmtargs);
    322 
    323 	len = net_writeall(net, buf, strlen(buf));
    324 	free(buf);
    325 	return len;
    326 }
    327 
    328 int
    329 net_readtcp(net_t *net, char *buf, int len)
    330 {
    331 	return recv(net->fd, buf, len, 0);
    332 }
    333 
    334 int
    335 net_readtcps(net_t *net, char *buf, int len)
    336 {
    337 	return tls_read((struct tls *)net->data[0], buf, len);
    338 }
    339 
    340 int
    341 net_read(net_t *net, char *buf, int len)
    342 {
    343 	switch (net->type) {
    344 	case NET_NET:
    345 	case NET_TCP:
    346 		return net_readtcp(net, buf, len);
    347 	case NET_TCPS:
    348 		return net_readtcps(net, buf, len);
    349 	default:
    350 		return -1;
    351 	}
    352 	return -1;
    353 }
    354 
    355 int
    356 net_readall(net_t *net, char *buf, int len)
    357 {
    358 	int olen, nlen;
    359 
    360 	for (olen = 0;
    361 			(nlen = net_read(net, &buf[olen], len-olen))
    362 			&& olen < len;
    363 			olen += nlen);
    364 
    365 	return 0;
    366 }
    367 
    368 char *
    369 net_gets(net_t *net)
    370 {
    371 	char *ret;
    372 	int len;
    373 
    374 	len = 1;
    375 	ret = mallocz(len, 2);
    376 	while (net_read(net, &ret[len-1], 1) > 0 && ret[len-1] != '\n'
    377 			&& len < MAXLINESIZE)
    378 		ret = reallocz(ret, ++len + 1, 0);
    379 	/*
    380 	if (ret[len-1] != '\n') {
    381 		free(ret);
    382 		return NULL;
    383 	}
    384 	ret[len-1] = '\0';
    385 	*/
    386 	ret[len] = '\0';
    387 
    388 	return ret;
    389 }
    390