-cp
- if(debug_lvl >= DEBUG_TRAFFIC)
- syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
- packet->len, device_info);
-
- if(device_type == DEVICE_TYPE_TUNTAP)
- {
- if(write(device_fd, packet->data, packet->len) < 0)
- {
- syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
- return -1;
- }
- }
- else/* ethertap */
- {
- struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
-
- if(writev(device_fd, vector, 2) < 0)
- {
- syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
- return -1;
- }
- }
-
- device_total_out += packet->len;
-cp
- return 0;
+ int lenin;
+
+ cp();
+
+ switch(device_type) {
+ case DEVICE_TYPE_TUN:
+ lenin = read(device_fd, packet->data + 10, MTU - 10);
+
+ if(lenin <= 0) {
+ logger(LOG_ERR, _("Error while reading from %s %s: %s"),
+ device_info, device, strerror(errno));
+ return false;
+ }
+
+ packet->len = lenin + 10;
+ break;
+ case DEVICE_TYPE_TAP:
+ lenin = read(device_fd, packet->data, MTU);
+
+ if(lenin <= 0) {
+ logger(LOG_ERR, _("Error while reading from %s %s: %s"),
+ device_info, device, strerror(errno));
+ return false;
+ }
+
+ packet->len = lenin;
+ break;
+ case DEVICE_TYPE_ETHERTAP:
+ lenin = read(device_fd, packet->data - 2, MTU + 2);
+
+ if(lenin <= 0) {
+ logger(LOG_ERR, _("Error while reading from %s %s: %s"),
+ device_info, device, strerror(errno));
+ return false;
+ }
+
+ packet->len = lenin - 2;
+ break;
+ }
+
+ device_total_in += packet->len;
+
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
+ device_info);
+
+ return true;
+}
+
+bool write_packet(vpn_packet_t *packet)
+{
+ cp();
+
+ ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
+ packet->len, device_info);
+
+ switch(device_type) {
+ case DEVICE_TYPE_TUN:
+ packet->data[10] = packet->data[11] = 0;
+ if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
+ logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
+ strerror(errno));
+ return false;
+ }
+ break;
+ case DEVICE_TYPE_TAP:
+ if(write(device_fd, packet->data, packet->len) < 0) {
+ logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
+ strerror(errno));
+ return false;
+ }
+ break;
+ case DEVICE_TYPE_ETHERTAP:
+ *(short int *)(packet->data - 2) = packet->len;
+
+ if(write(device_fd, packet->data - 2, packet->len + 2) < 0) {
+ logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
+ strerror(errno));
+ return false;
+ }
+ break;
+ }
+
+ device_total_out += packet->len;
+
+ return true;
+}
+
+void dump_device_stats(void)
+{
+ cp();
+
+ logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
+ logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
+ logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);