September 13, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

setting ip address using qt

Page  
1

Hi,
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.

Thanks

16 replies

September 13, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

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.

September 13, 2011

koahnig koahnig
Gene Splicer
2901 posts

If you like to input the ip address and validate it only, this might help [developer.qt.nokia.com]

September 16, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

Hi,
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 :

  1. {
  2.       QString setbuff;
  3.       char* ip_buff = "192.168.2.238";
  4.       setbuff.sprintf("ifconfig eth0 %s netmask 255.255.255.0 up",ip_buff);
  5.       qDebug() << setbuff;
  6.  
  7.  
  8.     QProcess *qProc = new QProcess(this);
  9.    qProc->start(setbuff);
  10.    qDebug() << setbuff;
  11.    qProc->waitForFinished();
  12.    QString tmp = qProc->readAll();
  13. }

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

September 16, 2011

Volker Volker
Ant Farmer
5428 posts

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?

  1. QString ip = "192.168.2.238";
  2. QString cmd = QString("ifconfig eth0 %1 netmask 255.255.255.0 up").arg(ip);

September 16, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

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..

September 16, 2011

Volker Volker
Ant Farmer
5428 posts

Does your code work from the command line?

September 17, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

Hi volker..
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.

Thanks

September 17, 2011

Volker Volker
Ant Farmer
5428 posts

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.

September 17, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

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.

Thanks

September 17, 2011

Volker Volker
Ant Farmer
5428 posts

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.

September 17, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

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?

September 17, 2011

Volker Volker
Ant Farmer
5428 posts

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.

September 20, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

Hi,
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?

Thanks

September 20, 2011

prabhuraj prabhuraj
Lab Rat
20 posts

Hi,
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.

September 20, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts
prabhuraj wrote:
BTW the target device is executing the code in root mode.
prabhuraj wrote:
The code executing and running on the linux embedded device is in root mode but still the code is not working fine.
prabhuraj wrote:
Al of this stuff I did it in root mode on PC.
prabhuraj wrote:
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.

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).

Page  
1

  ‹‹ N950 RNDIS/Ethernet driver is not installing on WindowsXp:      QTextCodec::codecForLocale does not work in N950 for codec GB2312 encoding text ››

You must log in to post a reply. Not a member yet? Register here!