Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Changing program from AF_UNIX to AF_INET

  • 21-06-2005 10:54AM
    #1
    Moderators, Education Moderators Posts: 1,699 Mod ✭✭✭✭


    Hey,

    I am trying to change a C program from AF_UNIX to AF_INET, unfortunately I am not a C programmer and don't know how to go about this.

    I understand the basics and have examples of INET server based programs.

    Here is the main section of code that I think needs to be changed, any help/pointers would be appreciated.
    int main(int argc, char *argv[], char *envp[])
    {
    	struct sched_param sched;
    	struct sockaddr_un sun;
    	struct passwd *pwd;
    	struct passwd user;
    	const char *eqname;
    	const char *dname;
    	int out, flg, rtm;
    	char *ptr;
    	pid_t pid;
    
    	proc_title_init(argv, envp);
    
    	strncpy(sun.sun_path, DEF_SOCKET, sizeof(sun.sun_path));
    
    	dname = DEF_DEVICE;
    	user.pw_name = NULL;
    	eqname = NULL;
    	channels = 2;
    	opterr = 0;
    	rtm = 0;
    
    	//// Ommitted Code Here
    
    	/* ensure socket path is 0 terminated */
    
    	sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
    
            // Ommitted code here too
    
    	close(1);
    	dup(out);
    	close(out);
    
            // More code ommitted
    	/* drop privileges */
    
    	if(socket(PF_UNIX, SOCK_DGRAM, 0) != 0) {
    
    		dprintf(2, "failed to allocate socket, %s\n", strerror(errno));
    		return 1;
    	}
    
    	if((flg = fcntl(0, F_GETFL)) == -1 || fcntl(0, F_SETFL, flg | O_NONBLOCK)) {
    
    		dprintf(2, "failed to set socket for non-blocking, %s\n", strerror(errno));
    		return 1;
    	}
    
    	sun.sun_family = AF_UNIX;
    
    	umask(077);
    
    	if(bind(0, &sun, SUN_LEN(&sun))) {
    
    		dprintf(2, "failed to bind socket %s, %s\n", sun.sun_path, strerror(errno));
    		return 1;
    	}
    
    	/* ensure SIGTERM handler can unlink socket */
    
    	sname = sun.sun_path;
    
    	switch(pid = fork()) {
    
    		case -1:
    			dprintf(2, "failed to fork child, %s\n", strerror(errno));
    			break;
    
    		case 0:
    			setsid();
    
    			openlog(APP_NAME "(decoder)", LOG_PID, LOG_LOCAL0);
    
    			signal(SIGTERM, sig_term);
    			signal(SIGPIPE, SIG_IGN);
    
    			proc_title_set(APP_NAME " <-");
    
    			decoder();
    			break;
    
    		default:
    			return 0;
    	}
    
    	unlink(sname);
    
    	return 1;
    }
    

    Thanks,

    Slaan.


Advertisement