+ if (bind (fd, obj->srcaddr, obj->srcaddrlen) == -1)
+ {
+ ping_set_errno (obj, errno);
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("bind: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ close (fd);
+ return -1;
+ }
+ }
+
+#ifdef SO_BINDTODEVICE
+ if (obj->device != NULL)
+ {
+ if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
+ obj->device, strlen (obj->device) + 1) != 0)
+ {
+ ping_set_errno (obj, errno);
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ close (fd);
+ return -1;
+ }
+ }
+#endif /* SO_BINDTODEVICE */
+#ifdef SO_MARK
+ if (obj->set_mark)
+ {
+ if (setsockopt(fd, SOL_SOCKET, SO_MARK,
+ &obj->mark, sizeof(obj->mark)) != 0)
+ {
+ ping_set_errno (obj, errno);
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_MARK): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ close (fd);
+ return -1;
+ }
+ }
+#endif
+#ifdef SO_TIMESTAMP
+ if (1) /* {{{ */
+ {
+ int status;
+ int opt = 1;
+
+ status = setsockopt (fd,
+ SOL_SOCKET, SO_TIMESTAMP,
+ &opt, sizeof (opt));
+ if (status != 0)
+ {
+ ping_set_errno (obj, errno);
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ close (fd);
+ return -1;
+ }
+ } /* }}} if (1) */
+#endif /* SO_TIMESTAMP */
+
+ if (addrfam == AF_INET)
+ {
+ int opt;
+
+#ifdef IP_RECVTOS
+ /* Enable receiving the TOS field */
+ opt = 1;
+ setsockopt (fd, IPPROTO_IP, IP_RECVTOS,
+ &opt, sizeof (opt));
+#endif /* IP_RECVTOS */
+
+ /* Enable receiving the TTL field */
+ opt = 1;
+ setsockopt (fd, IPPROTO_IP, IP_RECVTTL,
+ &opt, sizeof (opt));
+ }
+#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
+ else if (addrfam == AF_INET6)
+ {
+ int opt;
+
+# if defined(IPV6_RECVHOPLIMIT)
+ /* For details see RFC 3542, section 6.3. */
+ opt = 1;
+ setsockopt (fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
+ &opt, sizeof (opt));
+# endif /* IPV6_RECVHOPLIMIT */
+
+# if defined(IPV6_RECVTCLASS)
+ /* For details see RFC 3542, section 6.5. */
+ opt = 1;
+ setsockopt (fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
+ &opt, sizeof (opt));
+# endif /* IPV6_RECVTCLASS */
+ }
+#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
+
+ return fd;
+}
+
+/*
+ * public methods
+ */
+const char *ping_get_error (pingobj_t *obj)
+{
+ if (obj == NULL)
+ return (NULL);
+ return (obj->errmsg);
+}
+
+pingobj_t *ping_construct (void)
+{
+ pingobj_t *obj;
+
+ if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
+ return (NULL);
+ memset (obj, 0, sizeof (pingobj_t));
+
+ obj->timeout = PING_DEF_TIMEOUT;
+ obj->ttl = PING_DEF_TTL;
+ obj->addrfamily = PING_DEF_AF;