+int write_packet(vpn_packet_t *packet)
+{
+ u_int32_t type;
+ struct iovec vector[2];
+ int af;
+
+ cp();
+
+ if(debug_lvl >= DEBUG_TRAFFIC)
+ syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
+ packet->len, device_info);
+
+ af = (packet->data[12] << 8) + packet->data[13];
+
+ switch (af) {
+ case 0x800:
+ type = htonl(AF_INET);
+ break;
+ case 0x86DD:
+ type = htonl(AF_INET6);
+ break;
+ default:
+ if(debug_lvl >= DEBUG_TRAFFIC)
+ syslog(LOG_ERR,
+ _("Unknown address family %d while writing packet to %s %s"),
+ af, device_info, device);
+ return -1;
+ }
+
+ vector[0].iov_base = &type;
+ vector[0].iov_len = sizeof(type);
+ vector[1].iov_base = packet->data + 14;
+ vector[1].iov_len = packet->len - 14;
+
+ if(writev(device_fd, vector, 2) < 0) {
+ syslog(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
+ strerror(errno));
+ return -1;
+ }
+
+ device_total_out += packet->len;
+}