Setting ip address using qt
I am working on a project(Embedded device), in which I have to change the write an application for changing the IP address using Qt.
Typically the application should ask for the option for changing the IP address and if user enters a valid IP address then that IP address should be set. I know I have to make use of QHostaddress in Qt but I want some more info and a link for example code will be very helpful.
I appreciate any suggestion regarding this.
QHostAddress just allows you to hold an IPv4 or IPv6 address in a platform- and protocol-independent manner. It doesn't allow for changing the address of a network interface.
As far as I know you will need platform-specific code, for example invoking ifconfig or netsh using QProcess or altering the interface using DBus.
If you like to input the ip address and validate it only, "this might help":http://developer.qt.nokia.com/forums/viewthread/5736
I am working on an embedded project (Linux os running on ARM 9 processor). Qt is used for GUI.
currently I have to develop an application which can set/reset the Ip address of the device using Qt application. Till now I am able to get the IP address of the device through Qt by using Qprocess and ifconfig etc but I am not been able to change the Ip address.
Here is the snippet :
char* ip_buff = "192.168.2.238";
setbuff.sprintf("ifconfig eth0 %s netmask 255.255.255.0 up",ip_buff);
qDebug() << setbuff;
QProcess *qProc = new QProcess(this);
qDebug() << setbuff;
QString tmp = qProc->readAll();
The set buff content is showing correctly as : “ifconfig eth0 192.168.2.238 netmask 255.255.255.0 up”
But when the code is cross compiled and run on the target embedded device, the Ip address is not setting as 192.168.2.238.
BTW the target device is executing the code in root mode.
Please help me solve this problem
Edit: please format code sections using @ tags
You must be root to change the IP address. I don't know for embedded systems, but I would guess that your application does not have root access.
You have strange way of constructing your command string. Any reasons why do do not this?
QString ip = "192.168.2.238";
QString cmd = QString("ifconfig eth0 %1 netmask 255.255.255.0 up").arg(ip);
Hi Volker..Thanks for your reply.
I tried your method mentioned above but with no success. The code executing and running on the linux embedded device is in root mode but still the code is not working fine.
Please suggest me if you have any idea.
Thanks once again..
Does your code work from the command line?
For changing the ip adress, first I developed a small c application using linux system call and also successfully changed it. eg I used "ifconfig eth0 192.168.2.200 netmask 255.255.255.0 up". Al of this stuff I did it in root mode on PC.
I did a few modifications for Qt as mentioned above and tried to run but not been able to change the IP address.I agree to what you said that I should have root access to change the IP address but the code is running the code on the embedded device in the root mode. Do I have to provide any root permission through Qt?
Please suggest any idea if you have.
You can include the C code in your Qt application. C++ is downwards compatible to C, so your system calls can be used in your Qt code too.
I truly appreciate your reply volker..
Do you mean I can write an application in c and include it in Qt directly? If you have any idea about it please elaborate it. Any links of how to do it and run it will also be very helpful.I again want to mention that this code will be used on an embedded device.
Please help me regarding this.
Waiting for your reply.
You never write an application in language X and "include" it into another application.
You have your C application to set the IP address. That contains some code (snippet), hopefully in a function or method. You can integrate that snippet in any other C or C++ application and call that method. This has nothing to do with embedded or desktop.
I developed a c application for changing the Ip address and it worked perfectly fine.I made the changes as mentioned in the above code snippet.I also made the changes and used arg() instead of sprintf() as per your suggestion but still I am not been able to change the Ip address. Can you please go through the above snippet of code and let me know if I have done any mistakes?
So why don't you use the means of the C application within your C++ application? Just copy 'n paste that damn code into your Qt app, it is that simple.
I tried various things but its not working.The thing is,the Qt application needs root permission to run the code.I searched as to how to give root permission to Qt app but with no success.Can anyone tell me how to give root permission to Qt app?
When I run the code on Qt I get following errors.
SIOCSIFADDR: Permission denied
SIOCSIFFLAGS: Permission denied
SIOCSIFNETMASK: Permission denied
SIOCSIFFLAGS: Permission denied
I know these are because of root permission but not getting how to solve.
[quote author="prabhuraj" date="1316169665"]BTW the target device is executing the code in root mode.[/quote]
[quote author="prabhuraj" date="1316176111"]The code executing and running on the linux embedded device is in root mode but still the code is not working fine.[/quote]
[quote author="prabhuraj" date="1316234927"]Al of this stuff I did it in root mode on PC.[/quote]
[quote author="prabhuraj" date="1316234927"]I agree to what you said that I should have root access to change the IP address but the code is running the code on the embedded device in the root mode.[/quote]
Is your application now executed as root or isn't it?
If not: you cannot elevate privileges from an unprivileged process. You will have to use an external application having the setuid bit set, like sudo, or you will have to set the setuid bit on your application itself (don't do this unless you know what you are doing).
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html you can get idea how to integrate c code in c++ then may be you can solve your probably i hope