Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

sockopts.c 3.27 KB
Newer Older
1
/* sockopts.c */
2
// vi: tabstop=4
3

4
/* Set socket options based on contents of ctrl/sockopts.ini */
5

6
/* $Id: sockopts.c,v 1.26 2019/01/12 22:44:08 rswindell Exp $ */
7 8 9 10 11

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
12
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
40
#include "ini_file.h"	/* ini file API */
41

Rob Swindell's avatar
Rob Swindell committed
42
int set_socket_options(scfg_t* cfg, SOCKET sock, const char* protocol, char* error, size_t errlen)
43 44 45
{
	char		cfgfile[MAX_PATH+1];
	FILE*		fp;
deuce's avatar
deuce committed
46
	int			type=0;		// Assignment is to silence Valgrind
47 48 49 50 51 52 53
	int			result=0;
	str_list_t	list;
	socklen_t	len;

	len = sizeof(type);
	result=getsockopt(sock,SOL_SOCKET,SO_TYPE,(void*)&type,&len);
	if(result) {
54
		safe_snprintf(error,errlen,"%d getting socket option type (%d)"
55 56 57 58
			,ERROR_VALUE, SO_TYPE);
		return(result);
	}

59 60
	/* Set user defined socket options */
	iniFileName(cfgfile,sizeof(cfgfile),cfg->ctrl_dir,"sockopts.ini");
Deucе's avatar
Deucе committed
61 62
	if((fp=iniOpenFile(cfgfile,FALSE))==NULL) {
		int optval = 1;
Deucе's avatar
Deucе committed
63
#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
Deucе's avatar
Deucе committed
64 65
		// Set the only sane choice...
		setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&optval, sizeof(optval));
Deucе's avatar
Deucе committed
66
#endif
67
		return(0);
Deucе's avatar
Deucе committed
68
	}
69 70 71
	list=iniReadFile(fp);
	fclose(fp);

72
	result=iniGetSocketOptions(list,ROOT_SECTION,sock,error,errlen);
73

74 75 76 77 78 79 80 81
	if(result==0) {
		const char* section = (type==SOCK_STREAM) ? "tcp":"udp";
		struct sockaddr sockaddr;
		socklen_t len = sizeof(sockaddr);
		if(getsockname(sock, &sockaddr, &len) == 0 && sockaddr.sa_family == PF_UNIX)
			section = "unix";
		result=iniGetSocketOptions(list,section,sock,error,errlen);
	}
82 83
	if(result==0 && protocol!=NULL && *protocol!=0)
		result=iniGetSocketOptions(list,protocol,sock,error,errlen);
84 85 86

	iniFreeStringList(list);

87 88
	return(result);
}