alejolp / extmodem

Sound card modem for Amateur Radio AX25
43 stars 11 forks source link

Add pty support for KISS #15

Open sparques opened 7 years ago

sparques commented 7 years ago

Can support for creating a pseudo terminal be added for KISS? That way the kernel's MKISS module can be used and you get a real kernel network interface.

This should be pretty minimal work of adding a call to openpty(3) and reading and writing to the master side, rather than to a TCP socket.

I tested out the kernel's MKISS module and extmodem by using socat to wrap the tcp socket as a pty and it seemed to work.

alejolp commented 7 years ago

This would be great! A pull request with an implementation would be amazing for me :) Otherwise, do you have a minimum working example? Thanks

sparques commented 7 years ago

My C skills are mediocre and my C++ are practically non-existent. I can sketch out how it would work in C, but you'd have to figure out how to integrate it into extmodem. Would that be good enough?

Hmm... I might be able to naively adapt tcpserver_kiss.cpp. I'll give it a shot.

sparques commented 7 years ago

Yeah... the C++ and boost stuff is kicking my ass...

I understand the high level stuff though, so maybe I can describe it well enough for you to implement?

/* must link with -lutil */                      
#include <pty.h>                                 
#include <unistd.h>                              

#include <stdout.h>                              

/* open a pty */                                 
int pty_init (int *amaster)                      
{                                                
    int aslave;                                  
    char name[16]; // reasonable amount of space for /dev/pts/12345 ?                              

    if (-1 == openpty(amaster, &aslave, name, NULL, NULL) )                                        
    {                                            
        perror("Failed to open pty");            
        return -1;                               
    }                                            

    printf("pty: %s\n", name);                     

    return 0;                                    
}

Once open, you have the master file descriptor. Reading and writing to the pty is done with standard read(2) and write(2) syscalls. boost asio has support for creating io objects from fds. So after you open the pty, you create boost asio objects and then you should be able to treat them exactly the same as the tcp connections, except you don't need to handle connects and disconnects; the pty is always there after a successful open and won't go away until you close the master side.

posix::stream_descriptor pty_obj ( io_service_obj, ::dup(amaster) );