Author Topic: [Noob Fetish]Creating Wifi hotspot in C and system calls  (Read 811 times)

0 Members and 1 Guest are viewing this topic.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
[Noob Fetish]Creating Wifi hotspot in C and system calls
« on: January 22, 2015, 12:53:15 am »
hey this is a simple program to create and start a wifi hotspot through basic system calls
note that it requires admin privileges to run!
Code: (C) [Select]



# include<Windows.h>
#include <Stdio.h>
# include<string.h>
main()
{
char choice;
char ssidName[100],SSID[100],command[]="netsh wlan set hostednetwork mode=allow ssid=";
 char Password[100],passwd[]=" key=";
    printf("--------------------------------------------------------\n");
    printf("--------------------------------------------------------\n");
    printf("C program to create a Wifi hotspot based on sys calls\n");
    printf("--------------------------------------------------------\n");
    printf("--------------------------------------------------------\n");


   printf("Enter Network SSID:");
   fgets(SSID,100,stdin);
   strcpy(ssidName,SSID);
   printf("--------------------------------------------------------\n");
   printf("Enter Network Password:");
   fgets(Password,100,stdin);
   printf("--------------------------------------------------------\n");
   strcat(command,SSID);
   strcat(passwd,Password);
   strcat(command,passwd);
   printf("---------------------------------------------------------\n");
   printf("Output:\n");
   printf("---------------------------------------------------------\n");
   if(system(command)==0)
   { printf("---------------------------------------------------------\n");
       printf("Hotspot successfully created!\n");
   printf("Do you want to start connections to %s:",ssidName);
   scanf("%c",&choice);
   switch(choice)
   { case 'y':
   system("netsh wlan start hostednetwork\n");
   break;
   case 'n':
      //should not be there use Wlan fucntions...maybe next time
   system("netsh wlan stop hostednetwork\n");
   break;
   default:
      printf("Please choose choice y or n\n");
      break;
   }
   }
   else
      printf("Error creating Wifi hotspot\n");
printf("--------------------------------------------------------\n");
printf("--------------------------------------------------------\n");
printf("End");
getchar();


   return 0;
}




Nothing much just basics. :P
« Last Edit: January 22, 2015, 12:54:08 am by $Clone »

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #1 on: January 22, 2015, 01:55:39 am »
I'm not going to -1 cookie you because this is the noob programming forum  :D but I almost did on accident.

Now, let me first say that your main function should return an integer.  I don't care what tutorial site said what or why they said that, but this is convention.  Your main function should return 0 if it was sucessful and if something messes up (like your program isn't given enough command line arguments or somthing) then you return a nonzero integer.  There are "better" ways to do this with functions like exit, perror, etc.. but returning a nonzero integer will be fine for now.

Also you should look into what a proper coding style is, pick one you like, and follow it.  I'd recommend looking at this wiki category on general rules of thumb and this wiki article on code style. (Although you might find more information on coding styles by googling them yourselves.)

Also, excessive printf's aren't the way to go all the time.  Like I wrote above, if you'd like to impliment different "options" it's best to do them through command line switches.  I'll write some basic command line argument code below just so you can kind of see what's going on.

Code: (c) [Select]

#include <stdio.h>

/* Our example program is going to take 2 arguments "ip" and "port" */
int main(int argc, char **argv)
{
    char *ip;
    char *port;

    if (argc != 3) {

        /* Notice I'm writing to stderr instead of stdout (if you don't know what these are,
         * you can google them very easily) this is to distinguish between regular and
         * error messages. */

        fprintf(stderr, "Usage: ./program <ip> <port>\n");
        return 1;
    }

    /* argv is a list of the command line arguments.  argc (as shown above)
     * is the argument "count" and is useful for checking to see if the user has
     * provided enough arguments.  REMEMBER: you must verify the format of
     * the arguments are correct.  I haven't included this in this snip of code,
     * but it'd definitely important if you plan on passing user input through system!
     * (passing user defined data through system is a terrible idea, i'd recommend
     * staying faaar away from system() unless you're just messing around with it
     */

    ip = argv[1];
    port = argv[2];

    printf("Your two arguments are `%s` and `%s`\n", ip, port);
   
    /* We've made it to the end of the program and we missed the argc error!  We can safely
     * return 0 and go on our way.  */
    return 0;
}


If you have any other questions about this sort of coding style (using command line arguments instead of an interactive UI) please ask.  As far as the posted code goes, it's cool you're getting into syscalls, check out <unistd.h> for other syscall wrappers or alternatively <sys/types.h> and <sys/syscall.h> if you want raw syscall action. :P

I've also been looking into different linux syscalls and i noticed you included <Windows.h> so i can tell you're probably on windows.  In that case I feel like everything I've typed might be incorrect (I really don't know how to program on windows) but I'd suggest getting a debian VM or something if you aren't dual booting at the moment just to begin using things like man pages and Linux syscalls.

I feel like I've rambled a bit but if I've been unclear on anything please ask. :)
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline L0aD1nG

  • Peasant
  • *
  • Posts: 83
  • Cookies: 6
  • NeverFear1isHere
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #2 on: January 22, 2015, 02:33:44 am »
And even if you don't want to follow the way Polyphony noted above, with the argv, argc usage... which is the best way for the program you were coding. You could also make it more appropriate like this:

Code: (c) [Select]
int main(void)
{
// Code Code
return 0;
}



The thing is that now your main function is somewhat taken as a void type function if I am not mistaken when it actually returns an int at last. This is very wrong.
« Last Edit: January 22, 2015, 02:35:08 am by L0aD1nG »

Spacecow

  • Guest
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #3 on: January 22, 2015, 04:50:36 am »
I like your strcpy()s $Clone-chan :3 I promise to be gentle...

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #4 on: January 22, 2015, 09:21:21 am »
You guys are really making a big deal out of this, it was written "noob fetish"  ::) .....anyway points taken!
------------
I'm not going to -1 cookie you because this is the noob programming forum  :D  but I almost did on accident.Now, let me first say that your main function should return an integer.  I don't care what tutorial site said what or why they said that, but this is convention.  Your main function should return 0 if it was sucessful and if something messes up (like your program isn't given enough command line arguments or somthing) then you return a nonzero integer.  There are "better" ways to do this with functions like exit, perror, etc.. but returning a nonzero integer will be fine for now.Also you should look into what a proper coding style is, pick one you like, and follow it.  I'd recommend looking at this wiki category on general rules of thumb and this wiki article on code style. (Although you might find more information on coding styles by googling them yourselves.)Also, excessive printf's aren't the way to go all the time.  Like I wrote above, if you'd like to impliment different "options" it's best to do them through command line switches.  I'll write some basic command line argument code below just so you can kind of see what's going on.
Code: (c) [Select]
#include <stdio.h>/* Our example program is going to take 2 arguments "ip" and "port" */int main(int argc, char **argv){    char *ip;    char *port;    if (argc != 3) {        /* Notice I'm writing to stderr instead of stdout (if you don't know what these are,          * you can google them very easily) this is to distinguish between regular and          * error messages. */        fprintf(stderr, "Usage: ./program <ip> <port>\n");        return 1;    }    /* argv is a list of the command line arguments.  argc (as shown above)      * is the argument "count" and is useful for checking to see if the user has      * provided enough arguments.  REMEMBER: you must verify the format of      * the arguments are correct.  I haven't included this in this snip of code,      * but it'd definitely important if you plan on passing user input through system!      * (passing user defined data through system is a terrible idea, i'd recommend      * staying faaar away from system() unless you're just messing around with it     */    ip = argv[1];    port = argv[2];    printf("Your two arguments are `%s` and `%s`\n", ip, port);        /* We've made it to the end of the program and we missed the argc error!  We can safely      * return 0 and go on our way.  */    return 0;}If you have any other questions about this sort of coding style (using command line arguments instead of an interactive UI) please ask.  As far as the posted code goes, it's cool you're getting into syscalls, check out <unistd.h> for other syscall wrappers or alternatively <sys/types.h> and <sys/syscall.h> if you want raw syscall action. :P  I've also been looking into different linux syscalls and i noticed you included <Windows.h> so i can tell you're probably on windows.  In that case I feel like everything I've typed might be incorrect (I really don't know how to program on windows) but I'd suggest getting a debian VM or something if you aren't dual booting at the moment just to begin using things like man pages and Linux syscalls.I feel like I've rambled a bit but if I've been unclear on anything please ask. :) 
PolyPhony do you really code like this http://en.wikipedia.org/wiki/Programming_style ??? ??

NO DOUBLE POSTING
« Last Edit: January 22, 2015, 09:28:18 am by HTH »

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #5 on: January 22, 2015, 09:37:36 am »
All of Poly and SpaceCows points are valid, however a note for anyone who cares to leave it out, you can TECHNICALLY write main() without a return type, and for MOST compilers it will be implicitly set to int. (especially if you have a return int; statement anywhere)


HOWEVER. That is bad practice, for the same reason as not  defining signedness of variables andnot returning anything from a function(even voids), plus a million other things people do that compilers fix for you.


And yes, if someone goes to compile your code on some other random backwoods compiler (assuming it is compliant with standards) then YOUR code is shitty/ the problem. Because YOUR code doesn't meet the standards.


Not saying this will happen here, and I actually +1'd OP for going outside his norm/expected coding level (I assume), but definitely something to make note of. The only way to get better at coding is to have your code critiqued and if you aren't at the point where you can look at it and say "fuck i did that in a shitty way" or "I could have optimized that loop a bit more" then you gotta accept it from others :)


So keep coding OP.
« Last Edit: January 22, 2015, 09:38:22 am by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #6 on: January 22, 2015, 04:00:42 pm »
@HTH  and everybody else.....point noted....thnx.
« Last Edit: January 22, 2015, 04:01:19 pm by $Clone »

Offline 0E 800

  • Not a VIP
  • VIP
  • Baron
  • *
  • Posts: 895
  • Cookies: 131
  • • тнε ιηтεяηεт ιs мү яεcүcℓε-вιη •
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #7 on: January 22, 2015, 07:54:35 pm »
So can you compile and post an exe so I can try it out? I am in need of a Connectify alternative.

Also please add ability to stop hotspot and/or completely remove.
« Last Edit: January 23, 2015, 06:02:54 pm by 0E 800 »
The invariable mark of wisdom is to see the miraculous in the common.

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [Noob Fetish]Creating Wifi hotspot in C and system calls
« Reply #8 on: January 22, 2015, 08:17:48 pm »
$Clone:  Hey I understand it's noob code and like HTH said, as long as you're trying new things that's great.  Also, if I sound harsh I really don't mean it, I think I'm just kind of bad at writing. :D

As far as coding style goes I don't think I've ever followed one too strictly, it kind of changes on little things day by day.  I find that I agree most with the coding style described in the Geany coding style guide but like I said before, it's really a personal preference unless you want to write code for another project like geany, the kernel, etc, as they all have their own style guides. 
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple