File: util.c

package info (click to toggle)
linux-igd 1.0%2Bcvs20070630-5%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 888 kB
  • sloc: ansic: 2,066; xml: 593; sh: 136; makefile: 50
file content (75 lines) | stat: -rw-r--r-- 1,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include "globals.h"

#include <ithread.h>
#include <upnpdebug.h>

int GetIpAddressStr(char *address, char *ifname)
{
   struct ifreq ifr;
   struct sockaddr_in *saddr;
   int fd;
   int succeeded = 0;

   fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
   if (fd >= 0 )
   {
      strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
      ifr.ifr_addr.sa_family = AF_INET;
      if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
      {
         saddr = (struct sockaddr_in *)&ifr.ifr_addr;
         strcpy(address,inet_ntoa(saddr->sin_addr));
         succeeded = 1;
      }
      else
      {
         syslog(LOG_ERR, "Failure obtaining ip address of interface %s", ifname);
         succeeded = 0;
      }
      close(fd);
   }
   else
   {
         perror("user: socket creating failed");
   }
   return succeeded;
}

void trace(int debuglevel, const char *format, ...)
{
  va_list ap;
  char *thr_format;

  va_start(ap, format);

  if( -1 == asprintf(&thr_format, "0x%lx %s", (unsigned long int)ithread_self(), format) )
    thr_format = (char *)format;

  if (g_vars.debug >= debuglevel) {
    FILE * upnp_fd = UpnpGetDebugFile(UPNP_ALL, 0);
    vsyslog(LOG_DEBUG, thr_format,  ap);
    va_end(ap);
    va_start(ap, format);
    if( NULL != upnp_fd )
    {
      vfprintf(upnp_fd, thr_format, ap);
      fprintf(upnp_fd, "\n");
    }
  }

  if( thr_format != (char *)format )
    free(thr_format);

  va_end(ap);
}