-/*
- read, encrypt and send data that is
- available through the ethertap device
-*/
-int read_packet(vpn_packet_t *packet)
-{
- int lenin;
-cp
- if(device_type == DEVICE_TYPE_TUNTAP)
- {
- if((lenin = read(device_fd, packet->data, MTU)) <= 0)
- {
- syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
- return -1;
- }
-
- packet->len = lenin;
- }
- else /* ethertap */
- {
- struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
-
- if((lenin = readv(device_fd, vector, 2)) <= 0)
- {
- syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
- return -1;
- }
-
- packet->len = lenin - 2;
- }
-
- device_total_in += packet->len;
-
- if(debug_lvl >= DEBUG_TRAFFIC)
- {
- syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info);
- }
-
- return 0;
-cp
+void close_device(void) {
+ close(device_fd);
+
+ free(device);
+ free(iface);
+}
+
+bool read_packet(vpn_packet_t *packet) {
+ int lenin;
+
+ 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) {
+ 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;