LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 01-02-2020, 07:53 PM   #1
smaclennan
Member
 
Registered: May 2010
Location: Ottawa, Canada
Distribution: slackware
Posts: 37
Blog Entries: 2

Rep: Reputation: 13
Question freebsd: SIOCSIFADDR returns EINVAL


I am trying to set the IP address of an interface. The interface is virtual (vtnet0) if that matters.

The SIOCGIFADDR ioctl works, but I can't get the set to work. Here is the code fragment:

Code:
static int set_ip(const char *ifname, const char *ip, unsigned mask, int down)
{
	int s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s == -1) {
		perror("set_ip socket");
		return -1;
	}

	struct ifreq req;
	memset(&req, 0, sizeof(req));

	strlcpy(req.ifr_name, ifname, IF_NAMESIZE);

	struct sockaddr_in *in = (struct sockaddr_in *)&req.ifr_addr;
#ifndef __linux__
	in->sin_len = sizeof(struct sockaddr_in);
#endif
	in->sin_family = AF_INET;
	if (ip)
		in->sin_addr.s_addr = inet_addr(ip);

	errno = 0;
	if (ioctl(s, SIOCSIFADDR, &req)) {
		perror("SIOCSIFADDR");
		goto failed;
	}
Is this not the correct way to set the IP on FreeBSD?
 
Old 01-03-2020, 02:47 AM   #2
cynwulf
Senior Member
 
Registered: Apr 2005
Posts: 2,727

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Refer to ifconfig(8).

You need a ifconfig_<interface> stanza in rc.conf(8) to bring up the interface on boot.
 
Old 01-03-2020, 07:09 PM   #3
smaclennan
Member
 
Registered: May 2010
Location: Ottawa, Canada
Distribution: slackware
Posts: 37

Original Poster
Blog Entries: 2

Rep: Reputation: 13
Ok, I solved it. You can't use SIOCSIFADDR, or SIOCIFNETMASK, you must use SIOCAIFADDR (note the A) which takes an ifaliasreq struct. You still use SIOCSIFFLAGS though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inotify_rm_watch returns EINVAL on legitimate call intcwrtr Programming 7 08-02-2011 06:37 PM
[SOLVED] bind() returns EINVAL with PF_INET socket hda7 Linux - Networking 2 04-12-2010 12:09 PM
simple mmap() call failed with EINVAL on Redhat EL 3 update 9 modemer Programming 3 03-13-2009 01:37 PM
sem_wait() fails with EINVAL after working OK - can semaphores become invalid? hippopotamist Programming 2 08-04-2008 08:16 AM
poll returns errno 22, EINVAL mallport Programming 5 08-21-2006 03:24 PM

LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration