Showing posts with label Solaris. Show all posts
Showing posts with label Solaris. Show all posts

28 July 2009

25 April 2008

Get the list of gcc defined macro after preprocessing

It various situation, it is very useful to get the list of all defined maco after gcc preprocessing.
Particularly when porting C/C++ source code to another OS/Architecture to know what macro to use to identify it.

A simple way is to use gcc with the following options:
-E to end after preprocessing step,
-dM to print all defined macros.

For instance to known the "default" macros, use those options with a simple source code file "test.c" defining a main function:
gcc -E -dM test.c

11 February 2008

Manage services under Solaris

A simple refresh of what can be done with svcs and svcadm tools.

Get list of all services:
svcs

Add a service:
To begin, a manifest file is needed to define the service (some information there).
Then, it is needed to ensure it is valid:
xmllint --valid /path/to/my/service/manifest/file

Finally, the service can be add:
svccfg import /path/to/my/service/manifest/file

Activate a service (which has been priorly added):
svcadm enable * serviceName
The -s option can be used to get focus only after the service is fully activated (or failed).

Disable a service:
svcadm disable * serviceName
The -s option can be used to get focus only after the service has been disabled.

Put a service under maintenance:
svcadm mark * serviceName

Remove the "maintenance" status of a service:
svcadm clear * serviceName

Update the configuration of a service:
svcadm refresh * serviceName

Get the current status of a service:
svcs -x serviceName stop

23 September 2007

Remove network interface under Solaris

Under Solaris, it is very more easy to remove than to add. The network interface can be found thanks to the couple (name, inet address):
N.B.: All checks have been removed for better legibility, but it is very important to check returned code after each request.

// We consider the existence of the variables (unsigned char *) "networkInterfaceName", "inetAddress" which could be respectively "eth0" and "192.168.80.1" for instance.

int socketDescriptor;
struct sockaddr_in *addr;
struct lifreq lifr;

sd = socket(AF_INET, SOCK_DGRAM, 0);

memset(&lifr, 0, sizeof(lifr));
addr = (struct sockaddr_in *) &(lifr.lifr_addr);
strncpy(lifr.lifr_name, networkInterfaceName, sizeof(lifr.lifr_name));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(inetAddress);
ioctl(socketDescriptor, SIOCLIFREMOVEIF, (caddr_t) &lifr)
close(socketDescriptor);

Add network interface under Solaris

Under Solairs, it is not trivial to add a network interface, there is no alias, and it seems to not be possible to specify the additional network interface name (so it must be got and kept).
N.B.: All checks have been removed for better legibility, but it is very important to check returned code after each request.

// We consider the existence of the variables (unsigned char *) "networkInterfaceName", "inetAddress", "broadcastAddress" and "netmaskAddress" which could be respectively "eth0", "192.168.80.1", "192.168.80.255" and "255.255.255.0" for instance.

int sd;
struct sockaddr_in *addr;
struct lifreq lifr, lifrBroadcast, lifrNetmask, lifrFlags;
const char *aliasName;

// Resets structure.
memset(&lifr, 0, sizeof(lifr));
strncpy(lifr.lifr_name, networkInterfaceName, sizeof(lifr.lifr_name));

sd = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(sd, SIOCLIFADDIF, (caddr_t) &lifr);

// Very important to get the alias name to update network interface parameters.
aliasName = lifr.lifr_name;

// Manages broadcast.
memset(&lifrBroadcast, 0, sizeof(lifrBroadcast));
addr = (struct sockaddr_in *) &(lifrBroadcast.lifr_broadaddr);
strncpy(lifrBroadcast.lifr_name, aliasName, sizeof(lifrBroadcast.lifr_name));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(broadcastAddress);
ioctl(sd, SIOCSLIFBRDADDR, (caddr_t) &lifrBroadcast);

// Manages netmask.
memset(&lifrNetmask, 0, sizeof(lifrNetmask));
addr = (struct sockaddr_in *) &(lifrNetmask.lifr_addr);
strncpy(lifrNetmask.lifr_name, aliasName, sizeof(lifrNetmask.lifr_name));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(netmaskAddress);
ioctl(sd, SIOCSLIFNETMASK, (caddr_t) &lifrNetmask);

// Defines the address of the new interface.
addr = (struct sockaddr_in *) &(lifr.lifr_addr);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(inetAddress);
ioctl(sd, SIOCSLIFADDR, (caddr_t) &lifr);

// Sets the new interface UP.
memset(&lifrFlags, 0, sizeof(lifrFlags));
strncpy(lifrFlags.lifr_name, aliasName, sizeof(lifrFlags.lifr_name));
ioctl(sd, SIOCGLIFFLAGS, (caddr_t) &lifrFlags)
lifrFlags.lifr_flags |= IFF_UP;
ioctl(sd, SIOCSLIFFLAGS, (caddr_t) &lifrFlags);
close(sd);

Get MAC address under GNU/Linux or Solaris

This is a way to get MAC address of a network interface in C language.
Under GNU/Linux it can be done thanks to a RAW socket:
N.B.: All checks have been removed for better legibility, but it is very important to check returned code after each request.

// Consider a variable "networkInterfaceName" of type char * is defined ("eth0" for instance).
int sd;
struct ifreq ifr;
const unsigned char *hardwareAddress;

sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
memset(&ifr, 0, sizeof(ifr));
setNameInIfreq(&ifr, networkInterfaceName);
hardwareAddress = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
close(sd);

Under Solaris, it begins to be less easy, we need to use DLPI code:
// Consider a variable "devicePath" of type char * is defined ("/dev/eth0" for instance).
int sd;
dl_phys_addr_req_t dlpareq;
dl_phys_addr_ack_t *dlpaack;
struct strbuf msg;
char buf[128];
int flags = 0;
const unsigned char *hardwareAddress;

sd = open(devicePath, O_RDWR));
dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
msg.buf = (char *)&dlpareq;
msg.len = DL_PHYS_ADDR_REQ_SIZE;
putmsg(sd, &msg, NULL, 0);
dlpaack = (dl_phys_addr_ack_t *)buf;
msg.buf = (char *)buf;
msg.len = 0;
msg.maxlen = sizeof (buf);
getmsg(sd, &msg, NULL, &flags);
memcpy(hardwareAddress, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
close(sd);

4 September 2007

Compile OpenVPN and TUN under Solaris

I've recently worked on Solaris and need to compile the source code of openVPN and TUN.
At first, it seemed easy but because of some compatibility issues, it was not.
You can follow those steps to get openvpn executable under Solaris:
- download the openVPN source code and uncompress it under [OPENVPN_DIRECTORY],
- download the TUN source code and uncompress it under [TUNE_DIRECTORY],
- copy the [TUNE_DIRECTORY]/solaris/if_tun.h file to [OPENVPN_DIRECTORY],
- edit the [OPENVPN_DIRECTORY]/tun.c file and add the #include "if_tun.h" line after the #include "tun.h" one,
- under [OPENVPN_DIRECTORY], execute ./configure --disable-lzo,
- ensure there is a make executable into your path, for instance create a symbolic link executing ln -s /usr/sfw/bin/gmake /usr/bin/make,
- then execute make under [OPENVPN_DIRECTORY],
- finally the openvpn executable is ready for Solaris.

Then, you can follow those steps to get the tun functionality under Solaris:
- create the subdirectory [TUNE_DIRECTORY] /solaris/sys,
- download the dditypes.h file and put it under [TUNE_DIRECTORY] /solaris/sys (it fixes an incompatibility issue),
- ensure there is a ld executable into your path, for instance create a symbolic link executing ln -s /usr/sfw/bin/gld /usr/bin/ld,
- execute make install under [TUNE_DIRECTORY],
- finally the tun functionality is ready for Solaris.

It is now possible to use openVPN and TUN under Solaris.