2 * collectd - src/common.c
3 * Copyright (C) 2005-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
20 * Niki W. Waibel <niki.waibel@gmx.net>
21 * Sebastian Harl <sh at tokkee.org>
22 * Michał Mirosław <mirq-linux at rere.qmqm.pl>
32 #include "utils_cache.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
48 # include <netinet/in.h>
51 /* for ntohl and htonl */
53 # include <arpa/inet.h>
57 extern kstat_ctl_t *kc;
61 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER;
68 char *sstrncpy (char *dest, const char *src, size_t n)
70 strncpy (dest, src, n);
74 } /* char *sstrncpy */
76 int ssnprintf (char *dest, size_t n, const char *format, ...)
81 va_start (ap, format);
82 ret = vsnprintf (dest, n, format, ap);
89 char *sstrdup (const char *s)
97 /* Do not use `strdup' here, because it's not specified in POSIX. It's
98 * ``only'' an XSI extension. */
100 r = (char *) malloc (sizeof (char) * sz);
103 ERROR ("sstrdup: Out of memory.");
106 memcpy (r, s, sizeof (char) * sz);
109 } /* char *sstrdup */
111 /* Even though Posix requires "strerror_r" to return an "int",
112 * some systems (e.g. the GNU libc) return a "char *" _and_
113 * ignore the second argument ... -tokkee */
114 char *sstrerror (int errnum, char *buf, size_t buflen)
122 pthread_mutex_lock (&strerror_r_lock);
124 temp = strerror (errnum);
125 sstrncpy (buf, temp, buflen);
127 pthread_mutex_unlock (&strerror_r_lock);
129 /* #endif !HAVE_STRERROR_R */
131 #elif STRERROR_R_CHAR_P
134 temp = strerror_r (errnum, buf, buflen);
137 if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
138 sstrncpy (buf, temp, buflen);
140 sstrncpy (buf, "strerror_r did not return "
141 "an error message", buflen);
144 /* #endif STRERROR_R_CHAR_P */
147 if (strerror_r (errnum, buf, buflen) != 0)
149 ssnprintf (buf, buflen, "Error #%i; "
150 "Additionally, strerror_r failed.",
153 #endif /* STRERROR_R_CHAR_P */
156 } /* char *sstrerror */
158 void *smalloc (size_t size)
162 if ((r = malloc (size)) == NULL)
164 ERROR ("Not enough memory.");
169 } /* void *smalloc */
172 void sfree (void **ptr)
184 ssize_t sread (int fd, void *buf, size_t count)
195 status = read (fd, (void *) ptr, nleft);
197 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
205 DEBUG ("Received EOF from fd %i. "
206 "Closing fd and returning error.",
212 assert ((0 > status) || (nleft >= (size_t)status));
214 nleft = nleft - status;
222 ssize_t swrite (int fd, const void *buf, size_t count)
228 ptr = (const char *) buf;
233 status = write (fd, (const void *) ptr, nleft);
235 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
241 nleft = nleft - status;
248 int strsplit (char *string, char **fields, size_t size)
257 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
269 int strjoin (char *dst, size_t dst_len,
270 char **fields, size_t fields_num,
277 memset (dst, '\0', dst_len);
284 sep_len = strlen (sep);
286 for (i = 0; i < (int)fields_num; i++)
288 if ((i > 0) && (sep_len > 0))
290 if (dst_len <= sep_len)
293 strncat (dst, sep, dst_len);
297 field_len = strlen (fields[i]);
299 if (dst_len <= field_len)
302 strncat (dst, fields[i], dst_len);
303 dst_len -= field_len;
306 return (strlen (dst));
309 int strsubstitute (char *str, char c_from, char c_to)
328 } /* int strsubstitute */
330 int strunescape (char *buf, size_t buf_len)
334 for (i = 0; (i < buf_len) && (buf[i] != '\0'); ++i)
339 if ((i >= buf_len) || (buf[i + 1] == '\0')) {
340 ERROR ("string unescape: backslash found at end of string.");
344 switch (buf[i + 1]) {
359 memmove (buf + i + 1, buf + i + 2, buf_len - i - 2);
362 } /* int strunescape */
364 int escape_slashes (char *buf, int buf_len)
368 if (strcmp (buf, "/") == 0)
373 strncpy (buf, "root", buf_len);
380 /* Move one to the left */
382 memmove (buf, buf + 1, buf_len - 1);
384 for (i = 0; i < buf_len - 1; i++)
388 else if (buf[i] == '/')
394 } /* int escape_slashes */
396 void replace_special (char *buffer, size_t buffer_size)
400 for (i = 0; i < buffer_size; i++)
404 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
407 } /* void replace_special */
409 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
411 struct timeval *larger;
412 struct timeval *smaller;
416 NORMALIZE_TIMEVAL (tv0);
417 NORMALIZE_TIMEVAL (tv1);
419 if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
428 if ((tv0.tv_sec < tv1.tv_sec)
429 || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
443 delta->tv_sec = larger->tv_sec - smaller->tv_sec;
445 if (smaller->tv_usec <= larger->tv_usec)
446 delta->tv_usec = larger->tv_usec - smaller->tv_usec;
450 delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
454 assert ((delta == NULL)
455 || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
458 } /* int timeval_cmp */
460 int check_create_dir (const char *file_orig)
471 int last_is_file = 1;
472 int path_is_absolute = 0;
477 * Sanity checks first
479 if (file_orig == NULL)
482 if ((len = strlen (file_orig)) < 1)
484 else if (len >= sizeof (file_copy))
488 * If `file_orig' ends in a slash the last component is a directory,
489 * otherwise it's a file. Act accordingly..
491 if (file_orig[len - 1] == '/')
493 if (file_orig[0] == '/')
494 path_is_absolute = 1;
497 * Create a copy for `strtok_r' to destroy
499 sstrncpy (file_copy, file_orig, sizeof (file_copy));
502 * Break into components. This will eat up several slashes in a row and
503 * remove leading and trailing slashes..
508 while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
513 if (fields_num >= 16)
518 * For each component, do..
520 for (i = 0; i < (fields_num - last_is_file); i++)
523 * Do not create directories that start with a dot. This
524 * prevents `../../' attacks and other likely malicious
527 if (fields[i][0] == '.')
529 ERROR ("Cowardly refusing to create a directory that "
530 "begins with a `.' (dot): `%s'", file_orig);
535 * Join the components together again
538 if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
539 fields, i + 1, "/") < 0)
541 ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
546 if ((stat (dir, &statbuf) == -1)
547 && (lstat (dir, &statbuf) == -1))
551 if (mkdir (dir, 0755) == 0)
554 /* this might happen, if a different thread created
555 * the directory in the meantime
556 * => call stat() again to check for S_ISDIR() */
561 ERROR ("check_create_dir: mkdir (%s): %s", dir,
563 errbuf, sizeof (errbuf)));
569 ERROR ("check_create_dir: stat (%s): %s", dir,
570 sstrerror (errno, errbuf,
575 else if (!S_ISDIR (statbuf.st_mode))
577 ERROR ("check_create_dir: `%s' exists but is not "
578 "a directory!", dir);
586 } /* check_create_dir */
589 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
598 ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
600 *ksp_ptr = kstat_lookup (kc, module, instance, name);
601 if (*ksp_ptr == NULL)
603 ERROR ("get_kstat: Cound not find kstat %s", ident);
607 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
609 ERROR ("get_kstat: kstat %s has wrong type", ident);
615 assert (*ksp_ptr != NULL);
616 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
619 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
621 ERROR ("get_kstat: kstat %s could not be read", ident);
625 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
627 ERROR ("get_kstat: kstat %s has wrong type", ident);
634 long long get_kstat_value (kstat_t *ksp, char *name)
637 long long retval = -1LL;
640 assert (ksp != NULL);
641 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
645 ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
648 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
650 ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
655 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
658 if (kn->data_type == KSTAT_DATA_INT32)
659 retval = (long long) kn->value.i32;
660 else if (kn->data_type == KSTAT_DATA_UINT32)
661 retval = (long long) kn->value.ui32;
662 else if (kn->data_type == KSTAT_DATA_INT64)
663 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
664 else if (kn->data_type == KSTAT_DATA_UINT64)
665 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
667 WARNING ("get_kstat_value: Not a numeric value: %s", name);
671 #endif /* HAVE_LIBKSTAT */
674 unsigned long long ntohll (unsigned long long n)
676 #if BYTE_ORDER == BIG_ENDIAN
679 return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
681 } /* unsigned long long ntohll */
683 unsigned long long htonll (unsigned long long n)
685 #if BYTE_ORDER == BIG_ENDIAN
688 return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
690 } /* unsigned long long htonll */
691 #endif /* HAVE_HTONLL */
693 #if FP_LAYOUT_NEED_NOTHING
694 /* Well, we need nothing.. */
695 /* #endif FP_LAYOUT_NEED_NOTHING */
697 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
698 # if FP_LAYOUT_NEED_ENDIANFLIP
699 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
700 (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
701 (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
702 (((uint64_t)(A) & 0x000000ff00000000LL) >> 8) | \
703 (((uint64_t)(A) & 0x00000000ff000000LL) << 8) | \
704 (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
705 (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
706 (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
708 # define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
709 (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
712 double ntohd (double d)
723 /* NAN in x86 byte order */
724 if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
725 && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
726 && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
727 && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
736 ret.integer = FP_CONVERT (tmp);
737 return (ret.floating);
741 double htond (double d)
752 ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
753 ret.byte[4] = ret.byte[5] = 0x00;
756 return (ret.floating);
763 tmp = FP_CONVERT (ret.integer);
765 return (ret.floating);
768 #endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
770 int format_name (char *ret, int ret_len,
771 const char *hostname,
772 const char *plugin, const char *plugin_instance,
773 const char *type, const char *type_instance)
777 assert (plugin != NULL);
778 assert (type != NULL);
780 if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
782 if ((type_instance == NULL) || (strlen (type_instance) == 0))
783 status = ssnprintf (ret, ret_len, "%s/%s/%s",
784 hostname, plugin, type);
786 status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
787 hostname, plugin, type,
792 if ((type_instance == NULL) || (strlen (type_instance) == 0))
793 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
794 hostname, plugin, plugin_instance,
797 status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
798 hostname, plugin, plugin_instance,
799 type, type_instance);
802 if ((status < 1) || (status >= ret_len))
805 } /* int format_name */
807 int format_values (char *ret, size_t ret_len, /* {{{ */
808 const data_set_t *ds, const value_list_t *vl,
814 gauge_t *rates = NULL;
816 assert (0 == strcmp (ds->type, vl->type));
818 memset (ret, 0, ret_len);
820 #define BUFFER_ADD(...) do { \
821 status = ssnprintf (ret + offset, ret_len - offset, \
828 else if (((size_t) status) >= (ret_len - offset)) \
834 offset += ((size_t) status); \
837 BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time));
839 for (i = 0; i < ds->ds_num; i++)
841 if (ds->ds[i].type == DS_TYPE_GAUGE)
842 BUFFER_ADD (":%f", vl->values[i].gauge);
843 else if (store_rates)
846 rates = uc_get_rate (ds, vl);
849 WARNING ("format_values: "
850 "uc_get_rate failed.");
853 BUFFER_ADD (":%g", rates[i]);
855 else if (ds->ds[i].type == DS_TYPE_COUNTER)
856 BUFFER_ADD (":%llu", vl->values[i].counter);
857 else if (ds->ds[i].type == DS_TYPE_DERIVE)
858 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
859 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
860 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
863 ERROR ("format_values plugin: Unknown data source type: %i",
868 } /* for ds->ds_num */
874 } /* }}} int format_values */
876 int parse_identifier (char *str, char **ret_host,
877 char **ret_plugin, char **ret_plugin_instance,
878 char **ret_type, char **ret_type_instance)
880 char *hostname = NULL;
882 char *plugin_instance = NULL;
884 char *type_instance = NULL;
887 if (hostname == NULL)
890 plugin = strchr (hostname, '/');
893 *plugin = '\0'; plugin++;
895 type = strchr (plugin, '/');
898 *type = '\0'; type++;
900 plugin_instance = strchr (plugin, '-');
901 if (plugin_instance != NULL)
903 *plugin_instance = '\0';
907 type_instance = strchr (type, '-');
908 if (type_instance != NULL)
910 *type_instance = '\0';
914 *ret_host = hostname;
915 *ret_plugin = plugin;
916 *ret_plugin_instance = plugin_instance;
918 *ret_type_instance = type_instance;
920 } /* int parse_identifier */
922 int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
924 char str_copy[6 * DATA_MAX_NAME_LEN];
927 char *plugin_instance = NULL;
929 char *type_instance = NULL;
932 if ((str == NULL) || (vl == NULL))
935 sstrncpy (str_copy, str, sizeof (str_copy));
937 status = parse_identifier (str_copy, &host,
938 &plugin, &plugin_instance,
939 &type, &type_instance);
943 sstrncpy (vl->host, host, sizeof (vl->host));
944 sstrncpy (vl->plugin, plugin, sizeof (vl->plugin));
945 sstrncpy (vl->plugin_instance,
946 (plugin_instance != NULL) ? plugin_instance : "",
947 sizeof (vl->plugin_instance));
948 sstrncpy (vl->type, type, sizeof (vl->type));
949 sstrncpy (vl->type_instance,
950 (type_instance != NULL) ? type_instance : "",
951 sizeof (vl->type_instance));
954 } /* }}} int parse_identifier_vl */
956 int parse_value (const char *value, value_t *ret_value, int ds_type)
962 case DS_TYPE_COUNTER:
963 ret_value->counter = (counter_t) strtoull (value, &endptr, 0);
967 ret_value->gauge = (gauge_t) strtod (value, &endptr);
971 ret_value->derive = (derive_t) strtoll (value, &endptr, 0);
974 case DS_TYPE_ABSOLUTE:
975 ret_value->absolute = (absolute_t) strtoull (value, &endptr, 0);
979 ERROR ("parse_value: Invalid data source type: %i.", ds_type);
983 if (value == endptr) {
984 ERROR ("parse_value: Failed to parse string as %s: %s.",
985 DS_TYPE_TO_STRING (ds_type), value);
988 else if ((NULL != endptr) && ('\0' != *endptr))
989 INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. "
990 "Input string was \"%s\".",
991 endptr, DS_TYPE_TO_STRING (ds_type), value);
994 } /* int parse_value */
996 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
1006 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
1010 if (i >= vl->values_len)
1012 /* Make sure i is invalid. */
1013 i = vl->values_len + 1;
1019 if (strcmp ("N", ptr) == 0)
1020 vl->time = cdtime ();
1023 char *endptr = NULL;
1027 tmp = strtod (ptr, &endptr);
1028 if ((errno != 0) /* Overflow */
1029 || (endptr == ptr) /* Invalid string */
1030 || (endptr == NULL) /* This should not happen */
1031 || (*endptr != 0)) /* Trailing chars */
1034 vl->time = DOUBLE_TO_CDTIME_T (tmp);
1039 if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
1040 vl->values[i].gauge = NAN;
1041 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
1046 } /* while (strtok_r) */
1048 if ((ptr != NULL) || (i != vl->values_len))
1051 } /* int parse_values */
1053 #if !HAVE_GETPWNAM_R
1054 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
1055 size_t buflen, struct passwd **pwbufp)
1060 memset (pwbuf, '\0', sizeof (struct passwd));
1062 pthread_mutex_lock (&getpwnam_r_lock);
1066 pw = getpwnam (name);
1069 status = (errno != 0) ? errno : ENOENT;
1073 #define GETPWNAM_COPY_MEMBER(member) \
1074 if (pw->member != NULL) \
1076 int len = strlen (pw->member); \
1077 if (len >= buflen) \
1082 sstrncpy (buf, pw->member, buflen); \
1083 pwbuf->member = buf; \
1085 buflen -= (len + 1); \
1087 GETPWNAM_COPY_MEMBER(pw_name);
1088 GETPWNAM_COPY_MEMBER(pw_passwd);
1089 GETPWNAM_COPY_MEMBER(pw_gecos);
1090 GETPWNAM_COPY_MEMBER(pw_dir);
1091 GETPWNAM_COPY_MEMBER(pw_shell);
1093 pwbuf->pw_uid = pw->pw_uid;
1094 pwbuf->pw_gid = pw->pw_gid;
1100 pthread_mutex_unlock (&getpwnam_r_lock);
1103 } /* int getpwnam_r */
1104 #endif /* !HAVE_GETPWNAM_R */
1106 int notification_init (notification_t *n, int severity, const char *message,
1108 const char *plugin, const char *plugin_instance,
1109 const char *type, const char *type_instance)
1111 memset (n, '\0', sizeof (notification_t));
1113 n->severity = severity;
1115 if (message != NULL)
1116 sstrncpy (n->message, message, sizeof (n->message));
1118 sstrncpy (n->host, host, sizeof (n->host));
1120 sstrncpy (n->plugin, plugin, sizeof (n->plugin));
1121 if (plugin_instance != NULL)
1122 sstrncpy (n->plugin_instance, plugin_instance,
1123 sizeof (n->plugin_instance));
1125 sstrncpy (n->type, type, sizeof (n->type));
1126 if (type_instance != NULL)
1127 sstrncpy (n->type_instance, type_instance,
1128 sizeof (n->type_instance));
1131 } /* int notification_init */
1133 int walk_directory (const char *dir, dirwalk_callback_f callback,
1134 void *user_data, int include_hidden)
1144 if ((dh = opendir (dir)) == NULL)
1147 ERROR ("walk_directory: Cannot open '%s': %s", dir,
1148 sstrerror (errno, errbuf, sizeof (errbuf)));
1152 while ((ent = readdir (dh)) != NULL)
1158 if ((strcmp (".", ent->d_name) == 0)
1159 || (strcmp ("..", ent->d_name) == 0))
1162 else /* if (!include_hidden) */
1164 if (ent->d_name[0]=='.')
1168 status = (*callback) (dir, ent->d_name, user_data);
1177 if ((success == 0) && (failure > 0))
1182 int read_file_contents (const char *filename, char *buf, int bufsize)
1187 if ((fh = fopen (filename, "r")) == NULL)
1190 n = fread(buf, 1, bufsize, fh);
1196 counter_t counter_diff (counter_t old_value, counter_t new_value)
1200 if (old_value > new_value)
1202 if (old_value <= 4294967295U)
1203 diff = (4294967295U - old_value) + new_value;
1205 diff = (18446744073709551615ULL - old_value)
1210 diff = new_value - old_value;
1214 } /* counter_t counter_to_gauge */
1216 int service_name_to_port_number (const char *service_name)
1218 struct addrinfo *ai_list;
1219 struct addrinfo *ai_ptr;
1220 struct addrinfo ai_hints;
1224 if (service_name == NULL)
1228 memset (&ai_hints, 0, sizeof (ai_hints));
1229 ai_hints.ai_family = AF_UNSPEC;
1231 status = getaddrinfo (/* node = */ NULL, service_name,
1232 &ai_hints, &ai_list);
1235 ERROR ("service_name_to_port_number: getaddrinfo failed: %s",
1236 gai_strerror (status));
1240 service_number = -1;
1241 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1243 if (ai_ptr->ai_family == AF_INET)
1245 struct sockaddr_in *sa;
1247 sa = (void *) ai_ptr->ai_addr;
1248 service_number = (int) ntohs (sa->sin_port);
1250 else if (ai_ptr->ai_family == AF_INET6)
1252 struct sockaddr_in6 *sa;
1254 sa = (void *) ai_ptr->ai_addr;
1255 service_number = (int) ntohs (sa->sin6_port);
1258 if ((service_number > 0) && (service_number <= 65535))
1262 freeaddrinfo (ai_list);
1264 if ((service_number > 0) && (service_number <= 65535))
1265 return (service_number);
1267 } /* int service_name_to_port_number */
1269 int strtoderive (const char *string, derive_t *ret_value) /* {{{ */
1274 if ((string == NULL) || (ret_value == NULL))
1279 tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0);
1280 if ((endptr == string) || (errno != 0))
1285 } /* }}} int strtoderive */