How do I find the MAC address of a network card on IRIX? I'd rather not shell out to something that displays it and parse the output.
I'm coding C.
Methods that require root access are acceptable.
#include <net/raw.h>
#include <net/if.h>
#include <net/soioctl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
...
struct ifreq ifdat;
int s;
s = socket (PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
strcpy (ifdat.ifr_name, "en0");
ioctl (s, SIOCGIFADDR, &ifdat);
...
Clean it up a little, and ifdat should contain your MAC address.
I don't know about programmatically, but you could try /etc/nvram eaddr, I suppose you could exec() that.
On some platforms (Linux, for example) ioctl() allows to obtain MAC address. You need to check on IRIX as ioctl() is platform-dependent.
Related
I'd noticed that arpa/inet.h lib is not available for Windows platform. Also read that winsock2.h implements an analog function InetPton to converts an IPv4 or IPv6 Internet network address. Then I tried to build some instructions like:
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct sockaddr_in sa;
InetPton(AF_INET, "10.20.30.40", &(sa.sin_addr));
}
And get below compilation error:
undefined reference to `InetPton'
What I need to do/include to use this function on Windows (10)?
You need to include the ws2_32.lib library. That contains the Winsock functions.
See the MSDN page for InetNtop for more details.
I have to get name server IP of my system using C language.I am using Linux machine.
I have tried.
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cdefs.h>
int main()
{
int res_init(void);
printf("_res.nscount %d\n",_res.nscount);
//printf("_res.nsaddr_list[0] %s\n",_res.nsaddr_list[0]);
return 0;
}
But I am getting _res.nscount as 0.Am I doing anything wrong?
You declared res_init() instead of calling it. Try:
Int main()
{
res_init();
/* ... */
However, nsaddr_list[0] isn't a string, so you won't be able to print it with printf("%s"). You'll have to use inet_ntoa() or similar to convert its sin_addr.s_addr value to a printable string.
I have a piece of code that used to work in some environment a long time ago. I'm pretty sure it was a FreeBSD machine so I got FreeBSD 8.3 and I'm trying to make this file but it's not working.
When I try to compile it it complains with:
f.c: In function 'tcp'>
f.c:24: error: storage size of 'socket_stru' isn't known
f.c:29: error: 'IPPROTO_TCP' undeclared (first use in this function)
...
I've been looking around and I see these are all specified in the sys/socket.h file. This is my actual file:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include "f.h"
int tcp4 (in_addr_t ip, int port, int qsize )
{
struct sockaddr_in socket_stru; // line 24
socket_stru.sin_family = AF_INET;
socket_stru.sin_port = htons(port);
socket_stru.sin_addr.s_addr = ip;
int actual_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // line 29
...
I feel like my code somehow doesn't "read" the sys/socket.h file so it doesn't know about socket_stru and IPPROTO_TCP, but I'm just really lost.
Any ideas?
None of the other answers worked for me. After taking a look inside the sys/socket.h file, I didn't even see a definition for struct sockaddr_in.
What worked for me was to #include one of the following files when using the corresponding struct sockaddr_* type:
if you're using struct sockaddr_in, #include <netinet/in.h>
if you're using struct sockaddr_un, #include <sys/un.h>
if you're using struct sockaddr_ns, #include <netns/ns.h>
if you're using struct sockaddr_ndd, #include <sys/ndd_var.h>
More information on the header files for socket programming can be found here.
I cut and paste your code into a file (removing only the #include f.h and closed off the function call.) It compiles just fine on Linux.
I suspect there may be header files differences on BSD. For socket programming, I typically include ALL these header files. And I know my socket code compiles on BSD as well. I suspect one of these header files brings in the definition for sockaddr_in. I recall when I ported by socket code to BSD, I had to explicitly add a few of these.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
/* the next two includes probably aren't relevant for you, but I typically use them all anyway */
#include <math.h>
#include <sys/termios.h>
Hope this helps
I had the same problem, but the following include fixed the issue for me
#include <arpa/inet.h>
Just add #include <resolv.h> to your source and you are good to go.
According to freebsd developer's handbook you need
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
I'm learning how to work with raw sockets in Linux. I'm trying to create a socket like that:
if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
perror("socket() failed");
exit(-1);
}
But all I got after launch is:
socket() failed: Operation not permitted
I know that only root can create raw sockets, but if I run it with SUID bit or sudo - the
problem is the same. What's wrong? The system is Ubuntu 11.04.
Maybe I'm including needless headers?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
And I'm wondering - why SUID is useless?
My money on you not running your code correctly.
I've copied and pasted your exact code into an empty main(). I get the same error if I run it as myself, but it runs correctly under sudo. This is on Ubuntu.
The code:
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int sd;
if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
perror("socket() failed");
return -1;
}
return 0;
}
Run as myself:
aix#aix:~$ ./a.out
socket() failed: Operation not permitted
aix#aix:~$
Run as root:
aix#aix:~$ sudo ./a.out
aix#aix:~$
according to man: Only processes with an effective user ID of 0 or the CAP_NET_RAW capability are allowed to open raw sockets
So you could run you application with sudo as was suggested below or set CAP_NET_RAW capability to it (actually you'll need CAP_NET_ADMIN too):
# setcap cap_net_raw,cap_net_admin=eip PATH_TO_YOUR_APPLICATION
Details could be found at http://ftp.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.4/capfaq-0.2.txt
Header will not affect it in anyway.
Even if you would be adding some more unnecessary files it will not affect the working of the program.
I'm new to C89, and trying to do some socket programming:
void get(char *url) {
struct addrinfo *result;
char *hostname;
int error;
hostname = getHostname(url);
error = getaddrinfo(hostname, NULL, NULL, &result);
}
I am developing on Windows. Visual Studio complains that there is no such file if I use these include statements:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
What should I do? Does this mean that I won't have portability to Linux?
On Windows, instead of the includes you have mentioned, the following should suffice:
#include <winsock2.h>
#include <windows.h>
You'll also have to link to ws2_32.lib. It's kind of ugly to do it this way, but for VC++ you can do this via: #pragma comment(lib, "ws2_32.lib")
Some other differences between Winsock and POSIX include:
You will have to call WSAStartup() before using any socket functions.
close() is now called closesocket().
Instead of passing sockets as int, there is a typedef SOCKET equal to the size of a pointer. You can still use comparisons with -1 for error, though Microsoft has a macro called INVALID_SOCKET to hide this.
For things like setting non-blocking flags, you'll use ioctlsocket() instead of fcntl().
You'll have to use send() and recv() instead of write() and read().
As for whether or not you will lose portability with Linux code if you start coding for Winsock... If you are not careful, then yes. But you can write code that tries to bridge the gaps using #ifdefs..
For example:
#ifdef _WINDOWS
/* Headers for Windows */
#include <winsock2.h>
#include <windows.h>
#else
/* Headers for POSIX */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/* Mimic some of the Windows functions and types with the
* POSIX ones. This is just an illustrative example; maybe
* it'd be more elegant to do it some other way, like with
* a proper abstraction for the non-portable parts. */
typedef int SOCKET;
#define INVALID_SOCKET ((SOCKET)-1)
/* OK, "inline" is a C99 feature, not C89, but you get the idea... */
static inline int closesocket(int fd) { return close(fd); }
#endif
Then once you do something like this, you can code against the functions which appear in both OS's, using these wrappers where appropriate.