/* IRCfs - IRC FileServ for *nix. * Copyright (C) 2002 Nick 'Zaf' Clifford * For licensing details, refer to the LICENSE file in the source * code directory. */ #include "archdep.h" #include "runtime.h" #include #include #include #include #include #include #include #include #include #include #include static char my_hostname[512]; unsigned long my_hostname_ip; unsigned long linux_get_iface_addr(const char *iface) { struct sockaddr *sa; struct sockaddr_in *sin; struct ifreq ifr; int fd; fd = socket(PF_INET, SOCK_STREAM,0); if (fd == -1) return 0; /* Copy the interface name into the buffer */ strncpy(ifr.ifr_name,iface,IFNAMSIZ); if (ioctl(fd,SIOCGIFADDR,&ifr) == -1) { return 0; } /* Now the buffer will contain the information we requested */ //printf(ifr.ifr_name); sa = (struct sockaddr *)&(ifr.ifr_addr); if (sa->sa_family == AF_INET) { sin = (struct sockaddr_in*) sa; return sin->sin_addr.s_addr; } else { /* Unknown family */ return 0; } } unsigned long linux_get_default_route_addr() { static char tmpbuf[512]; char *iface,*dest,*cp; FILE *fp; int found = 0; fp = fopen("/proc/net/route","r"); if (fp == NULL) { return 0; } while(fgets(tmpbuf,512,fp) != NULL) { if (strncasecmp(tmpbuf,"Iface",5) == 0) continue; iface = cp = tmpbuf; while(*cp && !isspace(*cp)) cp++; if (*cp) { *cp = 0; cp++; while(isspace(*cp)) cp++; } /* Destination */ dest = cp; while(*cp && !isspace(*cp)) cp++; if (*cp) { *cp = 0; } if (strcasecmp(dest,"00000000") != 0) continue; /* Found default route */ found = 1; break; } fclose(fp); if (found) return linux_get_iface_addr(iface); return 0; } unsigned long linux_get_hostname_addr() { static char tmpbuf[512]; struct hostent *he; struct in_addr ip; /* Use gethostname */ if (gethostname(tmpbuf,512) == -1) return 0; if (strcasecmp(tmpbuf,my_hostname) == 0) { return my_hostname_ip; } he = gethostbyname(tmpbuf); snprintf(tmpbuf,512,"%u.%u.%u.%u", (unsigned char)he->h_addr_list[0][0], (unsigned char)he->h_addr_list[0][1], (unsigned char)he->h_addr_list[0][2], (unsigned char)he->h_addr_list[0][3]); inet_aton(tmpbuf,&ip); my_hostname_ip = ip.s_addr; return my_hostname_ip; } int linux_file_exists(const char *f) { static struct stat st; if (stat(f,&st) == -1) return 0; return S_ISREG(st.st_mode) ? 1 : 0; }