Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Changing program from AF_UNIX to AF_INET

Options
  • 21-06-2005 10:54am
    #1
    Moderators, Education Moderators Posts: 1,863 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