1

Run in adb shell, and compiled by NDK.

My code:

#include <sys/socket.h>
#include <errno.h>
#include <linux/netlink.h>
#include <linux/in.h>
#include <string.h>
#include <stdio.h>
int main()
{
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
    printf("sock = %d and errno = %d\n", sock, errno);
    return 0;
}

Output:

shell@android:/data/local/tmp $ ./poc
sock = -1 and errno = 13

Does anybody know why?

jptang
  • 11
  • 3
  • Do you have your SELinux in `enforced` or `permissive` mode? Also, what is kernel output after you run your program (`dmesg`)? – Sam Protsenko Mar 16 '17 at 11:48
  • vbox86p:/data/local/tmp # getenforce Disabled – jptang Mar 17 '17 at 05:30
  • dmesg: <6> [ 8.208501] qtaguid: ctrl_counterset(s 1 10023): insufficient priv from pid=466 tgid=279 uid=1000 <3>[ 9.362826] init: sys_prop: permission denied uid:1003 name:service.bootanim.exit <7>[ 10.959880] eth0: no IPv6 routers present <7>[ 20.643991] eth1: no IPv6 routers present – jptang Mar 17 '17 at 05:32

1 Answers1

0

See https://developer.android.com/guide/topics/manifest/uses-permission-element.html

It looks like you need to add: -

<uses-permission android:name="android.permission.INTERNET" /> 

Possible duplicate of Using Socket() in Android NDK

If you're targeting android 7.0, it may be worth looking at the permission changes at https://developer.android.com/about/versions/nougat/android-7.0-changes.html but this will depend on whether your targetSdkVersion is greater than 23

Community
  • 1
  • 1
ed_me
  • 3,338
  • 2
  • 24
  • 34