Modify the subnet mask of a network interface programmatically - networking

I want to modify the subnet mask of a network interface. I am able to get read the default mask using getifaddrs(). However, is there a way to write (and modify) ifa_netmask parameter? Does a socket function exist that can do the work? or maybe ioctl()?

Related

How to send data between two ESP32's over same LAN?

I want to send data(I think I can achieve it sending http requests) between two ESP's that are connected to the same LAN. I don't want to use one as an access point! I have one esp that is acting as a homekit device, and I want to send data(for example, sensor readings) to it from another esp. I've seen many tutorials where one device is acting as an access point, but that's not what I need. I also don't really need to access the esp data directly from my laptop, so I believe I don't need the html page. I would appreciate any help, code, or any existing tutorials, because I didn't find a single one.
Configuring one ESP as an AP is only done for convenience. You can configure both of them in STA (station) mode and they will connect to, e.g. your phone hotspot or your wifi router.
Connected to the same hotspot, they are on the same network, and they will be able to communicate with each other (unless the hotspot is configured to not allow this ... ). The tricky part will most likely be for the "client" to determine the IP address of the "server" in order to create the connection.
If you have admin access to the hotspot, you can cheat and hard code the IP addresses (assuming that the hotspot will assign the same IP address to the device the next time it sees it.)
It's also not difficult to use mdns (aka bonjour), which allows a network device to assign itself a local hostname. So you can name one ESP ("myserver") which allows the ESP running the client code to connect to "http://myserver.local"

Scan a network and look for device

I would like to be able to scan my local network, using either an existing command line tool or writing my own, that will look for a certain type of embedded device and then print a list over all devices found.
The embedded device I'm looking for has the lwIP TCP/IP stack implemented, and we are free to add functionality to it that will help us locate it.
Using the MAC address of the device is not an option because the user is free to configure this and it may not have been properly set.
How can I make my devices "scanable" on the LAN and how can I perform the scan with my PC?
let me try to answer this: your device will be added on a LAN, so will get an IP address. That's really what's critical to locate it. The IP address can be:
fixed, like defined by your device (something like 192.168.0.1), but it's a risky option because it might collide. In that case, you can just tell the user to reach this IP
dynamic, defined by DHCP. So your device will get an IP on your LAN like 192.168.3.178.
In case on DHCP, the issue is finding your device. You will find detailed answers there How to get a list of all valid IP addresses in a local network?, but there is basically 2 options:
nmap, a real scanner. This may be long, but super powerful. This also could be viewed as agressive, so if you're in a controlled environment (like the LAN of a entreprise), please ask your sys admin before doing that
arp, which is not as exhaustive

two interfaces on the same device on different subnets

I've been told that it is bad practice to have two interfaces on the same device on the same subnet. i.e. two Ethernet ports on a switch should be on different subnets. Could somebody explain why this is the case? (preferably simply as possible as I'm new to networking)
Because routing in your OS normally sets one of Ethernet card as out gate to specified subnet and all traffic to this subnet will have only 1 output. Second route to same subnet will have bigger Metrik value and will use to send some data if first interface is down. Even if somebody will send request to second interface answer can have first Ip as sender.
If you try to increase throughput to subnet you must use aggregation of Ethernet link. you`ll have 2 physically link and 1 IP.
subnet is the logical division of the IP network based on the subnet-mask/netmask. So unless you plan to have two different separate networks, you need not to have two different subnets. This link explains most of the possible cases to explain what it means by subnetworks on a switch.
Whether two interfaces on the same subnet is good or bad depends entirely of what you're trying to accomplish.
If you need link redundancy or a simple way of load sharing (L2 or L3) it may the right way to go.
If you need network/uplink redundancy or a more complex way of load sharing (L3 only) you connect to two different networks (multi-homing). This is also the setup for a router connecting the two networks.

How can I guess the name of the network interface recently started?

I start my USB 3G Modem by:
# wvdial
And then I must do, in order to acquire internet access:
# route add default ppp0
Note that ppp0 is the new virtual interface name.
I would like to automate this:
#!/bin/bash
wvdial
route add default ppp0
But sometimes there is some previous ppp connection established (i.e: VPN), so the correct adapter must have another number (it is created as ppp1, ppp2... etc).
How could I guess/specify the name of the newly created ppp interface?
P.S: I am sure that there should be another more practical method to establish such PPP connection. So I opened this other question.
You can force the system to use a specific interface name with option unit.
Just add unit 1 to your pppd-options file (usually /etc/ppp/options).
That works if you always have only one ppp-device.

Confusion in bind call in socket programming

i was learning socket programming in unix using c/c++.
I am confused with one function call bind(params..).
Actually it takes the adreess structure "sockaddr_in" and we can create the structure in the following way
sockaddr_in.*** = somthing..
sockaddr_in..s_addr htonl(INADDR_ANY)
**Passing INADDR_ANY will alow to bind all local addresses**
My question is , why do we need to use "INADDR_ANY" ?
In my knowledge every machine can has only one unique IP Address. In this way there is only one address associated with the machien. Thye bind call should directly bind the socket to the single available address.
Please explain what are the different scenarios and why is it so?
A machine usually has an IP address for each connected network interface plus 127.0.0.1 for localhost (loopback). For example, a laptop might be connected to a wireless network as 10.0.1.25 and also to a wired network as 10.0.2.4. Servers often have an interface to an internal network as well as a public network.
It's possible for a machine to have more than one IP address, either because it has multiple network cards or through software. INADDR_ANY lets you bind a socket to all of the machine's IP addresses.
Not true. Most machines are Multi-Homed, that means, they have more than one IP address.
For example, the network address and 127.0.0.1.

Resources